如何在X轴绘制日期范围在Flot图表? [英] How to plot a date range on X-axis in Flot Charts?
问题描述
我使用浮动图表显示特定时段(由用户选择,例如最近30天,过去7天,从2013年1月1日至2013年3月3日等)的数据
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)
所以我想显示一个以x轴为日期的折线图。
So I want to display a line chart with x-axis as the date.
如果我有两天,startDate和endDate如何使X轴显示类似:
E.g. if I've two days, startDate and endDate how do I make the X-axis display something like:
1 Jan 2013 | 2013年1月2日........................ 2013年3月3日
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
}
});
我意识到我需要使mydata看起来像[date,value]。会工作吗?
我有服务器在JSON中动态生成的数据
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 } ...]
[{date, value}, {date, value}...]
格式。
请指导。
format. Please guide.
推荐答案
您需要将数字更改为UNIX时间戳乘以1000。如果您搜索时间序列数据:API:
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:
Flot中的时间序列支持基于Javascript时间戳,
或移交,使用Javascript
时间戳记号。这是一个数字,而不是一个Date对象。 A
Javascript时间戳是自1月1日以来的毫秒数,
1970 00:00:00 UTC。这几乎与Unix时间戳相同,除了它的
(以毫秒为单位),所以请记住乘以1000!
在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);
}
下面是一个示例 - http://jsfiddle.net/zxtFc/4/
Here is an example - http://jsfiddle.net/zxtFc/4/
这篇关于如何在X轴绘制日期范围在Flot图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!