HighCharts: Possible to summarize series data and

2019-09-05 22:16发布

问题:

I'm looking for an answer if it's possible to sum the HighCharts series and show it in the title? I'm using JSON data for the series, so if the data looks like this:

[0,0,0,1,1,1,2,2,2,3,3,3]

The sum should of course be: 18.

I've tried asking Google but I haven't found anything. I know I could handle it elsewhere but it would be nice to handle it directly inside the HighCharts script.

Thx.

/CRH

回答1:

How about summing it in Javascript and then using the result when creating the title?

var sum = 0;
for(var i in series)
    sum += i;

Then, just set the title:

chart = new Highcharts.Chart({
    ...

    title: {
        text: 'SUM: ' + sum
    },

    ...
)};

That should work, right?



回答2:

Because you pass an object literal to Highcharts, you best bet might be to do something like this:

http://jsfiddle.net/WRa43/

var data = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];

$('#container').highcharts({
    series: [{
        data: data
    }],

    title: {
        text: "Total is " + data.reduce(function(i,a) { return i+a; })
    }
});

Reference for array.reduce