繁体   English   中英

Highcharts堆栈列总和为正和负

[英]Highcharts Stack Column Sum Positive and Negative Together

我有一个堆积的柱状图,该图显示每个月3个键/值,一个堆积在另一个之上。 某些月份可能会有负面影响。 当前的功能是使hig​​hcharts每月放置两个堆叠的标签。 一个用于正数(在顶部),另一个用于负数(在底部)。

请参见下面的代码和js小提琴作为示例:

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            stackLabels: {
                enabled: true,
                align: 'center'
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal',
                pointPadding: 0,
                groupPadding: 0,
                dataLabels: {
                    enabled: false,
                    color: '#FFFFFF'
                }
            }
        },

        series: [{
            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]
        }, {
            data: [144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2]
        },
        {
            data: [55.9, 90.5, 106.4, 350.2, 144.0, 52.0, 130.6, 148.5, 216.4, 194.1, 95.6, 54.4]
        }]
    });
});

http://jsfiddle.net/sph1LjtL/

我想要的功能是实际上有一个堆叠的标签,其中包括所有三个值的总和,而不是两个单独的标签。 因此,对于2月(在小提琴中),它会在列上方显示195,因为266.50-71.50 = 195。

我一直在尝试找到一种方法来执行此操作,但未成功,因为高图会将正值和负值视为单独的图表。 任何帮助,将不胜感激! 谢谢。

这可能是一种解决方案,使用yAxis.stackLabels.formatter函数,并在处理正堆栈时查找负堆栈。 在代码中:

yAxis: {
    stackLabels: {
        formatter: function() {
            if(this.total >= 0) {
                if(this.axis.stacks["-column"][this.x] != null)
                    return this.total + this.axis.stacks["-column"][this.x].total;
                else
                    return this.total;
            }
        }
        // ...
    }
}

请参见此JSFiddle更新演示如何工作。

请参阅此处的工作小提琴

此链接中使用了Pawel Fus(Highcharts)提供的解决方案

 yAxis: {
        stackLabels: {
            enabled: true,
            align: 'center',
                      formatter: function() {
                var sum = 0;
                var series = this.axis.series;

                for (var i in series) {
                    if (series[i].visible && series[i].options.stacking == 'normal') 
                        sum += series[i].yData[this.x];
                }
                if(this.total > 0 ) {
                    return Highcharts.numberFormat(sum,1); 
                } else {
                    return '';    
                }
            }
        }
    }

可以使用yAxis.stackLabels.formatter并遍历系列数据项(以及在要应用标签的轴上应用过滤器)来完成此操作。 这是一个完整的示例(非常详细):

yAxis: {
  stackLabels: {
    enabled: true,
    align: 'center',
    formatter: function() {
      console.log(this);
      var theIndex = this.x;
      var theSeries = this.axis.series;
      var theSum = 0;

      for (var i = 0; i < theSeries.length; ++i) {
        theSum = theSum + theSeries[i].yData[theIndex]; //console.log(theSeries[i].yData[theIndex]);
      }
      if (this.isNegative == false) {
        return theSum;
      }
    }
  }
},

formatter内部,我正在获取点的索引( x ),以供以后选择正确的数据点求和。 然后设置循环遍历所有系列项目。 最终,我将yData点从系列对象中取出,并对它们进行累加以得到适当的xAxis索引。 下一步是仅将滤镜应用于正轴位置。 功能齐全的jsFiddle 请注意,您将不得不使用十进制精度来做一些工作,而这取决于您。

Pawel Fus对答案的改进,以及Nishith在上面链接,以容纳缺失的数据点以及负和(在这种情况下,总和显示在给定x轴索引的条形下方):

yAxis: {
        stackLabels: {
            enabled: true,
            formatter: function() {
                var sum = 0;
                var series = this.axis.series;

                for (var i in series) {
                    if (series[i].visible 
                    && series[i].options.stacking == 'normal' 
                    && this.x in series[i].yData) 
                        sum += series[i].yData[this.x];
                }
                if (sum >= 0 && this.isNegative == false
                    || sum < 0 && this.isNegative == true) {
                    return Highcharts.numberFormat(sum,1); 
                } else {
                    return '';    
                }
            }
        }
    }

在这里看到JSFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM