新的Highcharts.Chart(选项)后,highcharts不能使用addPoint()

时间:2014-05-19 08:57:29

标签: highcharts redraw

任何助手。

我遇到的问题是,当我对chart.series使用addPoint方法时,它不起作用。

我从数据库获得了一些数据,并且第一次将它分配给options.series.data,它可以工作。

像这样:options.series.data = data_from_database

       charts = new Highcharts.Chart(options);

但是当我在charts.series上使用addPoint方法时,它失败了并且图表没有重绘。

像这样:var arr_time_status = [get_time(),cpu_status];

       charts.series[i].addPoint(arr_time_status, true, true);

我只是想使用addPoint方法来驱动图表重绘。

任何人都可以帮忙吗?非常感谢你。

`var charts;
$(function () {
    var options = {
        chart:{
            renderTo: 'new'
        },
        title: {
            text: 'CPU for PM2'
            //x: -20 //center
        },
        subtitle: {
            text: 'MIX-CPU'
            //x: -20
        },
        xAxis: {
            type: 'datetime',
            maxZoom: 1000 * 60 * 10,
            dateTimeLabelFormats: {
                minute: '%H:%M',
                hour:   '%H:%M',
                day:    '%H:%M',
                week:   '%H:%M',
                month:  '%H:%M',
                year:   '%H:%M'
            }
        },
        yAxis: {
            title: {
                text: 'CPU Load Average'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        plotOption: {
            series: {
                animation: {
                    duration: 1000,
                    easing: 'swing'
                },
                marker :{
                    radius: 1
                }
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x) +': '+ this.y;
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [{
            name: '1/Mins',
            color: '#2EFF74',
            data:[]
        }, {
            name: '5/Mins',
            color: '#C361ED',
            data: []
        },{
            name: '15/Mins',
            color: '#BF0B23',
            data: []
        }]
    };

    Highcharts.setOptions({
        global: {
            useUTC: true
        }
    });

    $.getJSON('http://localhost:8080/history', function(results){
        var a = [];
        var b = [];
        var c = [];
        for (var i = 0; i < results.length; i++){
            a[i] = [Number(results[i].milliSeconds), results[i].oneMin];
            b[i] = [Number(results[i].milliSeconds), results[i].fiveMins];
            c[i] = [Number(results[i].milliSeconds), results[i].fifteenMins];
        };
        options.series[0].data = a;
        options.series[1].data = b;
        options.series[2].data = c;
        charts = new Highcharts.Chart(options);
    });
    setInterval(function(){
        $.getJSON('http://localhost:8080/keys?name=MIX', function(dataUrl){
            $.getJSON(dataUrl[0].v,function(dataBody){
                var time = new Date();
                for (var i = 0; i < 3; i++) {
                    function get_time() {
                        var t = Date.UTC(
                            time.getFullYear(),
                            time.getMonth(),
                            time.getDate(),
                            time.getHours(),
                            time.getMinutes(),
                            time.getSeconds());
                        return t;
                    };

                    //Creating an array for the addPoint's method
                    var cpu_status = Math.round(dataBody.monit.loadavg[i] * 100) / 100;
                    var arr_time_status = [get_time(), cpu_status];

                    //Executing the addPoint's method
                    if (charts.series[i].name === '1/Mins'){
                        charts.series[i].addPoint(arr_time_status, true, true);

                    } else if (charts.series[i].name === '5/Mins'){
                        charts.series[i].addPoint(arr_time_status, true, true);

                    } else if (charts.series[i].name === '15/Mins'){
                        charts.series[i].addPoint(arr_time_status, true, true);
                    };
                };
            });
        });
    }, 1000 * 10);
});`

0 个答案:

没有答案