user1288241
user1288241

Reputation: 43

highcharts - create a total sumline of an existing serie

I have a simple line graph with irregular x values. What I need is a function to create a totalsum line like: for each x-value calculate:ysum=ysum + y

I could prepare this in the database but it would be nice to calculate it on clientside. Could someone help me to start with this? Unfortunately I am a total javascript newbie. Perhaps I can use the technical indicator plugin (https://github.com/laff/technical-indicators) and add a total-sum function?

Thanks in advance for any help

grassu

Upvotes: 0

Views: 1410

Answers (1)

Blundering Philosopher
Blundering Philosopher

Reputation: 6805

It sounds like you need a function like this:

function getSumTotal(data) {
    var totalList = [], total = 0;
    for (var i = 0; i < data.length; i++) {
        total += data[i];
        totalList.push(total);
    }
    return totalList;
}
var tokyo = [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6];
var tokyoSumTotal = getSumTotal(tokyo);

Which you can put in highcharts like this:

series: [{
    name: 'Tokyo',
    data: tokyo
}, {
    name: 'Tokyo TotalSum',
    data: tokyoSumTotal
}]

Here's an example: Fiddle

Upvotes: 1

Related Questions