How to implement a moving average in highchart

I want to implement a moving average in hightchart. Is there any option in highchart for this.

Like: I have a series of 10, 20, 30, 40, 50, 60, 70

and want a moving average of 2

then the second series will generate with average values โ€‹โ€‹of series 1

as: 15, 35, 105 (average value of each of two data points)

And we are introducing this moving average series of series 1 on the same chart.

+4
source share
4 answers

you can calculate the moving average and add it as follows:

$('#buttonAddSeries').click(function() { var series = chart.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(null); } else { sumForAverage -= series.data[i-period].y; data.push([series.data[i].x, sumForAverage/period]); } } chart.addSeries({ name: 'Moving Average', data: data }); }); 

You can use any number of points as a period, not just 2.

+4
source

No, currently HighCharts does not do any data analysis like this. You will need to create your own moving average and create it as your own series or plotLine / plotBand.

+3
source

Today there is a plugin for Highcharts, with which you can add SMA (Simple Moving Average).

See: http://www.highcharts.com/plugin-registry/single/16/technical-indicators

+2
source

Moving averages are supported using the technical indicators plugin. It is available at: http://www.highcharts.com/plugin-registry/single/16/technical-indicators
Here is a link to the javascript source you need to include:
https://rawgit.com/laff/technical-indicators/master/technical-indicators.src.js

It is implemented by adding another series to a chart of the type: "trend line", algorithm: "SMA", which refers to the corresponding series of data by id:

 series : [ { name: 'AAPL Stock Price', type : 'line', id: 'primary', data : [100,101,105,107,104,105,106,104,...] }, { name: '15-day SMA', linkedTo: 'primary', showInLegend: true, type: 'trendline', algorithm: 'SMA', periods: 15 }] 

Here is the fiddle: http://jsfiddle.net/laff/WaEBc/

+1
source

Source: https://habr.com/ru/post/1490244/


All Articles