本文介绍了Highchart,在setExtremes之后获得可见的系列数据的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的jsfiddle中点击,我怎样才能得到只有那些在我使用setExtremes更新了高图线图后可见的系列数据点的总数。按钮显示获得结果4,但它是停留5:(



  $(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
}]
});


//按钮操作
$('#button')。click(function(){
var chart = $('的#container)highcharts();
chart.xAxis [0] .setExtremes(Date.UTC(2010,0,2),Date.UTC(2010,0,8));
$('#total')。text('calculate again');
setTimeout(function(){
$('#total')。text(arraySum(chart.series [0] .processedYData));
},200);


});

});

函数arraySum数据){

  var sum = 0; 

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

if(data [i]){

sum + = data [i];

}

}

归还金额;

}

解决方案 div>

我认为更简单的是抓住SetExtremes事件,然后调用所有系列点的循环。添加检查极值(最小/最大)和总和的条件。

  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(px> = min&& px< = max){
sum + = py;
}
});

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

示例:


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;

}

解决方案

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/

这篇关于Highchart,在setExtremes之后获得可见的系列数据的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:27