[英]Highcharts (highstock) plotting incorrect date on x-axis line chart
[英]How to display the date on the x-axis in highstock?
我需要在x軸上顯示日期/時間。 現在在x軸上有一些奇怪的數據00 00 00 001、00 00 00002。我想查看1.08.2014 22.01.01(day.month.year hour.minute.second)。 我的JSON數據:
[
["30 June 2014 19:14",24],
["30 June 2014 19:16",41],
["30 June 2014 19:16",12],
["30 June 2014 19:16",8]
]
等等(時間,價值)。
我的Javascript代碼-可以正常工作,但無法在x軸上顯示日期/時間:
var seriesOptions = [],
seriesCounter = 0,
//My data
names = ['data_input_1', 'data_input_2'],colors = Highcharts.getOptions().colors;
//My graph captions
captions = ['Датчик 1', 'Датчик 2'];
//Type of my two graph - 2 lines
type = ['line', 'line'];
//Configure graph (now 2 names so 2 graph)
$.each(names, function(i, name)
{
//get data
$.getJSON('../graph/select_data.php?'+ name.toLowerCase() +'',
function(data)
{
seriesOptions[i] =
{
name: captions[i],
data: data,
type:type[i],
//I think- this is the place for code, that i want
// it doesnot work here categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
// all other code here - work well
};
seriesCounter++;
if (seriesCounter == names.length){createChart();}
});
});
結果:
如何在我的兩行中在highstock的x軸上顯示日期?
問題是您的JSON數據。 Highstock不能采用這種格式立即將其顯示為x軸上的datetime
時間。 問題在於您的字符串(例如“ 30 June 2014 19:14”)不是時間戳記 。
x軸需要時間戳(以毫秒為單位)(自1970年1月1日起)。 請注意,從其他來源收到的某些時間戳記可能以秒為單位,而不是毫秒。 如果那是問題,則必須將它們乘以1000。
當使用字符串時,這對Highstock而言並沒有什么意義,因此Highstock只是假裝數據的時間戳為0、1、2 ...,其轉換為00:00:00.000(0毫秒),00:00:00.001( 1毫秒),00:00:00.002(2毫秒)...
您需要將日期的字符串表示形式轉換為時間戳。 我不確定是否可以操縱接收JSON的格式,但是如果不能,則可以對其進行后處理以轉換數據,就像這樣( JSFiddle示例 ):
var data = [
["30 June 2014 19:15",24],
["30 June 2014 19:16",41],
["30 June 2014 19:17",12],
["30 June 2014 19:18",8]
];
var timestampData = [];
for(i in data) {
timestampData.push([new Date(data[i][0]).getTime(), data[i][3]]);
}
$('#container').highcharts({
...
series: [{
data: timestampData
}]
});
這里的本質是, new Date(data[i][0])
將您的字符串解析為具有年,月日值的Date對象...然后,您可以使用該對象的getTime()
函數獲取時間戳記。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.