如何在 Flot Charts 的 X 轴上绘制日期范围? [英] How to plot a date range on X-axis in Flot Charts?

查看:30
本文介绍了如何在 Flot Charts 的 X 轴上绘制日期范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flot 图表来显示特定时期的数据(由用户选择,例如过去 30 天、过去 7 天、2013 年 1 月 1 日至 2013 年 3 月 3 日等)

所以我想显示一个以 x 轴为日期的折线图.

例如如果我有两天的 startDate 和 endDate 如何使 X 轴显示如下:

2013 年 1 月 1 日 |2013 年 1 月 2 日......................2013 年 3 月 3 日

我的代码如下:数据(目前是静态的).

var mydata = [[1, 2.4],[2, 3.4 ],[3, 4.5 ],[4, 5],[5, 5],[6, 5],[7, 2],[8, 1],[9, 1.5 ],[10, 2.5 ],[11, 3.5],[12, 4],[13, 4],[14, 2.4],[15, 3.4 ],[16, 4.5 ],[17, 5],[18, 5],[19, 5],[20, 2],[21, 1],[22, 1.5 ],[23, 2.5 ],[24, 3.5],[25, 4],[26, 4],[27, 2.5 ],[28, 3.5],[29, 4],[30, 4],];var plot = $.plot($("#mychart"), [{数据:我的数据,标签:Y 轴标签"}], {系列: {行:{显示:真实},点:{显示:真实},阴影大小:2},网格: {可悬停:真实,可点击:真实},颜色:[#37b7f3"、#d12610"、#52e136"]、x轴:{模式:时间",时间格式:%d/%m/%y",minTickSize:[1,天"]},y轴:{滴答声:11,滴答小数:0,最小:0,最大:5}});

我意识到我需要让 mydata 看起来像 [date, value].那行得通吗?我在

中有服务器在 JSON 中动态生成的数据<块引用>

[{date, value}, {date, value}...]

格式.请指导.

解决方案

您需要将数字更改为 UNIX 时间戳乘以 1000.如果您搜索时间序列数据,这是来自 API:

Flot 中的时间序列支持基于 Javascript 时间戳,即在任何预期或传递时间值的地方,一个 Javascript使用时间戳编号.这是一个数字,而不是 Date 对象.一个Javascript 时间戳是自 1 月 1 日以来的毫秒数,1970 00:00:00 UTC.这几乎与 Unix 时间戳相同,除了它是以毫秒为单位,所以记得乘以 1000!

API 中有一个 .Net 示例:

public static int GetJavascriptTimestamp(System.DateTime input){System.TimeSpan span = new System.TimeSpan(System.DateTime.Parse("1/1/1970").Ticks);System.DateTime 时间 = input.Subtract(span);return (long)(time.Ticks/10000);}

这是一个例子 - http://jsfiddle.net/zxtFc/4/>

I'm using Flot charts to display data for a certain period (to be selected by the user, e.g. last 30 days, last 7 days, from 1st Jan 2013 to 3rd Mar 2013 etc)

So I want to display a line chart with x-axis as the date.

E.g. if I've two days, startDate and endDate how do I make the X-axis display something like:

1 Jan 2013 | 2 Jan 2013........................3 Mar 2013

My code is as follows: The data (currently it's static).

var mydata = [
                [1, 2.4],
                [2, 3.4 ],
                [3, 4.5 ],
                [4, 5 ],
                [5,  5],
                [6, 5],
                [7, 2 ],
                [8, 1 ],
                [9, 1.5 ],
                [10, 2.5 ],
                [11,  3.5],
                [12, 4 ],
                [13, 4 ],
                [14, 2.4],
                [15, 3.4 ],
                [16, 4.5 ],
                [17, 5 ],
                [18,  5],
                [19, 5],
                [20, 2 ],
                [21, 1 ],
                [22, 1.5 ],
                [23,  2.5 ],
                [24,   3.5],
                [25,  4 ],
                [26,  4 ],
                [27,  2.5 ],
                [28,   3.5],
                [29,  4 ],
                [30,  4 ],
            ];

var plot = $.plot($("#mychart"), [{
                data: mydata,
                label: "Y-axis label"
            }], {
                series: {
                    lines: {
                        show: true
                    },
                    points: {
                        show: true
                    },
                    shadowSize: 2
                },
                grid: {
                    hoverable: true,
                    clickable: true
                },
                colors: ["#37b7f3", "#d12610", "#52e136"],
                xaxis: {
                     mode: "time", timeformat: "%d/%m/%y", minTickSize: [1, "day"]
                },
                yaxis: {
                    ticks: 11,
                    tickDecimals: 0,
            min:0, max: 5
                }
            });

I realize that I need to make mydata look like [date, value]. Will that work? I've the data dynamically generated by the server in JSON in

[{date, value}, {date, value}...]

format. Please guide.

解决方案

You will need to change the numbers to UNIX time stamps multiplied by 1000. This is from the API if you search Time Series Data:

The time series support in Flot is based on Javascript timestamps, i.e. everywhere a time value is expected or handed over, a Javascript timestamp number is used. This is a number, not a Date object. A Javascript timestamp is the number of milliseconds since January 1, 1970 00:00:00 UTC. This is almost the same as Unix timestamps, except it's in milliseconds, so remember to multiply by 1000!

There is a .Net example in the API:

public static int GetJavascriptTimestamp(System.DateTime input)
{
System.TimeSpan span = new System.TimeSpan(System.DateTime.Parse("1/1/1970").Ticks);
System.DateTime time = input.Subtract(span);
return (long)(time.Ticks / 10000);
}

Here is an example - http://jsfiddle.net/zxtFc/4/

这篇关于如何在 Flot Charts 的 X 轴上绘制日期范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆