简体   繁体   English

如何动态设置高图上的最大最小值

[英]how to set max min on highcharts dynamically

I'm trying to set the xAxis in highcharts to have the max equal today's date and the min equal to 7 days prior. 我试图在高图中将xAxis设置为最大等于今天的日期,最小等于7天。 So it will always show 7 dates even if there isn't a data point for a given date. 因此,即使没有给定日期的数据点,它也将始终显示7个日期。

The data is displaying correctly, just the min and max break it. 数据显示正确,只是最小和最大中断了它。

here's the script - http://jsfiddle.net/Jv82q/1/ - notice line 3 is commented out, when uncommented it breaks the chart. 这是脚本-http://jsfiddle.net/Jv82q/1/-注意第3行已注释掉,如果未注释,则会破坏图表。 Not sure why. 不知道为什么。

 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() returns timestamp, not Date object. Date.getTime()返回时间戳,而不是Date对象。 Then you are truing to setDate for min , but it return error in your JS console: 然后,您将setDatemin ,但它在JS控制台中返回错误:

Uncaught TypeError: undefined is not a function 

Here is example how to achieve that: http://jsfiddle.net/Jv82q/4/ 这是实现该目标的示例: http : //jsfiddle.net/Jv82q/4/

var today = new Date().getTime();

...

// axis options: 
min: today - 7 * 24 * 3600 * 1000,
max: today

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM