Highchart UTC 日期始终设置为 1970 年 1 月 1 日
Highchart UTC date always set to January, 1 1970
我从外部服务收到一个已转换为 UTC 的日期。 Highcharts 工具提示显示 Jan 01, 1970
我不确定为什么它不能正确解释日期。如果我手动将 UTC 时间转换为字符串,然后使用 Date.UTC JavaScript 方法,它工作正常。我不确定为什么 UTC 格式的日期不起作用。
var weightChart;
var weightData = [];
var minWeight;
var maxWeight;
$(function () {
var json = {"ibw":175,"max_weight":300,
"min_weight":175,
"weights":[{"date":1232521200,"weight":300},
{"date":1245218400,"weight":300},
{"date":1313042400,"weight":290},
{"date":1319522400,"weight":270},
{"date":1330498800,"weight":200},
{"date":1342591200,"weight":250},
{"date":1365141600,"weight":235}]};
minWeight = json.min_weight;
maxWeight = json.max_weight;
$.each(json.weights, function(i, item) {
weightData.push([item.date, item.weight]);
});
displayGraph();
});
function displayGraph() {
//graph
weightChart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'spline',
zoomType: 'xy',
height: 113
},
credits: {
enabled: false
},
title: {
text: ''
},
tooltip: {
xDateFormat: '%b %d, %Y',
headerFormat: 'Date: <b>{point.key}</b><br />',
pointFormat: 'Weight: <b>{point.y}</b>'
},
xAxis: {
type: 'datetime',
labels: {
enabled: false
}
},
yAxis: {
title: {
text: ''
},
plotLines: [{
color: '#FF0000',
width: 2,
value: 125
}],
min: minWeight,
max: maxWeight
},
series: [{
name: ['Weight'],
data: weightData
}],
exporting: {
enabled: false
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
colorByPoint: false,
pointWidth: 12,
shadow: true
}
}
});
}
您的数据似乎来自后端,采用 UNIX 时间戳。 Highcharts 期望 javascript 时间,这是以毫秒为单位的 UNIX 时间。它显示 1970 年 1 月 1 日,因为“1232521200”是日期。将您的日期戳乘以 1000,您将得到合适的时间。直播demo.
var json = {
"ibw": 175,
"max_weight": 300,
"min_weight": 175,
"weights": [{
"date": 1232521200000,
"weight": 300
}, {
"date": 1245218400000,
"weight": 300
}, {
"date": 1313042400000,
"weight": 290
}, {
"date": 1319522400000,
"weight": 270
}, {
"date": 1330498800000,
"weight": 200
}, {
"date": 1342591200000,
"weight": 250
}, {
"date": 1365141600000,
"weight": 235
}]
};
我建议您将服务器端的日期转换为字符串并提供正确的日期格式,然后 return 将数据转换为字符串,highchart 将以正确的格式显示它。
例如在服务器端我有 DateTime 对象
日期时间日期=DateTime.Now
我将日期转换为正确的格式 return 将其转换为字符串 date.ToString("dd.MM.yyyy")
我从外部服务收到一个已转换为 UTC 的日期。 Highcharts 工具提示显示 Jan 01, 1970
我不确定为什么它不能正确解释日期。如果我手动将 UTC 时间转换为字符串,然后使用 Date.UTC JavaScript 方法,它工作正常。我不确定为什么 UTC 格式的日期不起作用。
var weightChart;
var weightData = [];
var minWeight;
var maxWeight;
$(function () {
var json = {"ibw":175,"max_weight":300,
"min_weight":175,
"weights":[{"date":1232521200,"weight":300},
{"date":1245218400,"weight":300},
{"date":1313042400,"weight":290},
{"date":1319522400,"weight":270},
{"date":1330498800,"weight":200},
{"date":1342591200,"weight":250},
{"date":1365141600,"weight":235}]};
minWeight = json.min_weight;
maxWeight = json.max_weight;
$.each(json.weights, function(i, item) {
weightData.push([item.date, item.weight]);
});
displayGraph();
});
function displayGraph() {
//graph
weightChart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'spline',
zoomType: 'xy',
height: 113
},
credits: {
enabled: false
},
title: {
text: ''
},
tooltip: {
xDateFormat: '%b %d, %Y',
headerFormat: 'Date: <b>{point.key}</b><br />',
pointFormat: 'Weight: <b>{point.y}</b>'
},
xAxis: {
type: 'datetime',
labels: {
enabled: false
}
},
yAxis: {
title: {
text: ''
},
plotLines: [{
color: '#FF0000',
width: 2,
value: 125
}],
min: minWeight,
max: maxWeight
},
series: [{
name: ['Weight'],
data: weightData
}],
exporting: {
enabled: false
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
colorByPoint: false,
pointWidth: 12,
shadow: true
}
}
});
}
您的数据似乎来自后端,采用 UNIX 时间戳。 Highcharts 期望 javascript 时间,这是以毫秒为单位的 UNIX 时间。它显示 1970 年 1 月 1 日,因为“1232521200”是日期。将您的日期戳乘以 1000,您将得到合适的时间。直播demo.
var json = {
"ibw": 175,
"max_weight": 300,
"min_weight": 175,
"weights": [{
"date": 1232521200000,
"weight": 300
}, {
"date": 1245218400000,
"weight": 300
}, {
"date": 1313042400000,
"weight": 290
}, {
"date": 1319522400000,
"weight": 270
}, {
"date": 1330498800000,
"weight": 200
}, {
"date": 1342591200000,
"weight": 250
}, {
"date": 1365141600000,
"weight": 235
}]
};
我建议您将服务器端的日期转换为字符串并提供正确的日期格式,然后 return 将数据转换为字符串,highchart 将以正确的格式显示它。
例如在服务器端我有 DateTime 对象
日期时间日期=DateTime.Now
我将日期转换为正确的格式 return 将其转换为字符串 date.ToString("dd.MM.yyyy")