score:1

change your function to this:

    function() {
            var Highcharts = this;
            var series = Highcharts.series[0];
            var data = [];
            var period = 2;
            var sumForAverage = 0;
            var i;
            for(i=0;i<series.data.length;i++) {
                sumForAverage += series.data[i].y;
                if(i<period) {
                    data.push({x:series.data[i].x,y: null});
                } else {
                    sumForAverage -= series.data[i-period].y;
                    data.push({x:series.data[i].x,y: sumForAverage/period});
                }
            }
    
            Highcharts.addSeries({
                name: 'Moving Average',
                data: data
            });
        });

I just changed the format of the corresponding series.

output: enter image description here


Related Articles