Highchart, get total of visible series data after setExtremes

僤鯓⒐⒋嵵緔 提交于 2019-12-13 18:16:01

问题


How can I get the total of only those data points in series which are visible after I have updated the highchart line graph using setExtremes.

In my jsfiddle clicking on button show get the result 4 but it's stays 5 :(

http://jsfiddle.net/deepakgc/hz8fopan/

$(function () {
$('#container').highcharts({
    chart:{
        events:{
            load:function(){

        $('#total').text(arraySum(this.series[0].processedYData));

            }
        }
    },
    xAxis: {
        type: 'datetime'
    },

    series: [{
        data: [1, 1, 1, 1, 1],
        pointStart: Date.UTC(2010, 0, 1),
        pointInterval: 24 * 3600 * 1000
    }]
});


// the button action
$('#button').click(function () {
    var chart = $('#container').highcharts();
    chart.xAxis[0].setExtremes(Date.UTC(2010, 0, 2), Date.UTC(2010, 0, 8));
        $('#total').text('calculating again');
    setTimeout(function(){
        $('#total').text(arraySum(chart.series[0].processedYData));
        },200);


});

});

function arraySum(data) {

var sum = 0;

for (var i = 0; i < data.length; i++) {

    if (data[i]) {

        sum += data[i];

    }

}

return sum;

}


回答1:


I think that simpler is catch afterSetExtremes event and then call loop over all series points. Add condition which checks extremes (min/max) and sum.

afterSetExtremes:function(min,max){
                var serie = this.chart.series[0],
                    min = this.getExtremes().min,
                    max = this.getExtremes().max,
                    sum = 0;

                $.each(serie.data, function(i, p){
                    if(p.x >= min && p.x <=max) {
                        sum += p.y;
                    }
                });

                    $('#sum').text(sum);
            }

Example: http://jsfiddle.net/yagpzkup/1/



来源:https://stackoverflow.com/questions/29578322/highchart-get-total-of-visible-series-data-after-setextremes

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