HighCharts: Possible to summarize series data and show in title?

落花浮王杯 提交于 2019-12-11 23:36:10

问题


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



来源:https://stackoverflow.com/questions/22335366/highcharts-possible-to-summarize-series-data-and-show-in-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!