I want to implement moving average in hightchart. Is there any option in highchart for this.
Like: I have series 10, 20, 30, 40, 50, 60, 70
and here the moving average would be 2
.
Then second series will generate on average values of series 1
like: 15, 35, 105
(taking average of each two datapoints)
And embedding this moving average series of series1
on the same chart.
In normal mean, it’s value get changed with the changing data but in this type of mean it also changes with the time interval . We get the mean for some period t and then we remove some previous data . Again we get new mean and this process continues . This is why it is moving average . This have a great application in financial market . Examples:
This condition is implemented using a synchronous reset “ i_sync_reset ”. This VHDL implementation of moving average algorithm configures the moving average length as a power of two. The generic value for moving average length is passed as log2 value so it will be simple to perform the output right shift.
The generic value for moving average length is passed as log2 value so it will be simple to perform the output right shift. In the next figures is reported a ModelSim simulation of the VHDL code implementing moving average algorithm. Figure3 is relative to input sample with continuous data enable.
Approach Given a series of numbers and a fixed subset size, the first element of the moving average is obtained by taking the average of the initial fixed subset of the number series. Then the subset is modified by “shifting forward”, i.e excluding the first number of the series and including the next value in the subset.
you can calculate the moving average and add it like this:
$('#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 the period, not only 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With