Javascript 如何在highcharts上动态设置最大最小值
我试图在highcharts中设置xAxis,使最大值等于今天的日期,最小值等于7天前的日期。因此,即使给定日期没有数据点,它也将始终显示7个日期 数据显示正确,只有最小值和最大值会将其打破 下面是脚本--注意,第3行被注释掉了,当未注释时,它将打破图表。不知道为什么Javascript 如何在highcharts上动态设置最大最小值,javascript,highcharts,Javascript,Highcharts,我试图在highcharts中设置xAxis,使最大值等于今天的日期,最小值等于7天前的日期。因此,即使给定日期没有数据点,它也将始终显示7个日期 数据显示正确,只有最小值和最大值会将其打破 下面是脚本--注意,第3行被注释掉了,当未注释时,它将打破图表。不知道为什么 var max = new Date().getTime(); var min = new Date().getTime(); // min.setDate(min.getDate()-7); $(function () {
var max = new Date().getTime();
var min = new Date().getTime();
// min.setDate(min.getDate()-7);
$(function () {
$('#container').highcharts({
chart: {
type: 'areaspline'
},
title: {
text: 'Weight History'
},
credits: {
enabled: false
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
day: '%e %b'
},
min: min,
max: max
},
yAxis: {
title: {
text: 'Weight'
},
min: 20,
max: 400
},
series: [{
name: 'Daily Weigh In',
color: '#7ec152',
data: [[Date.UTC(2014, 3, 13), 400],[Date.UTC(2014, 3, 14), 300],[Date.UTC(2014, 3, 15), 300],[Date.UTC(2014, 3, 16), 300],[Date.UTC(2014, 3, 18), 300],[Date.UTC(2014, 3, 19), 300],[Date.UTC(2014, 3, 20), 300] ]
}]
});
});
Date.getTime()
返回时间戳,而不是日期对象。然后,您正在为min
切换到setDate
,但它在JS控制台中返回错误:
Uncaught TypeError: undefined is not a function
以下是如何实现这一目标的示例:
var today = new Date().getTime();
...
// axis options:
min: today - 7 * 24 * 3600 * 1000,
max: today