0

any helper.

I've a problem that when i use addPoint method for charts.series and it doesn't work.

I got some data from database and assign it to the options.series.data for the first time, it works.

like this: options.series.data = data_from_database

       charts = new Highcharts.Chart(options);

but when i used the addPoint method on charts.series, it failed and the chart doesn't redraw.

like this: var arr_time_status = [get_time(), cpu_status];

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

i just want to use the addPoint method to drive the chart to redraw.

can anybody help? thank you so much.

`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);
});`
leppie
  • 115,091
  • 17
  • 196
  • 297
Danielsss
  • 11
  • 1
  • 4

0 Answers0