简体   繁体   English

Highcharts- Javascript- 默认情况下如何显示所有图例系列的累积计数,以及图例中的链接系列

[英]Highcharts- Javascript- How to show cumulative count of all the series of legend by default, and linked series inside a legend

I am using highcharts to render a column chart and my legends as well.我正在使用 highcharts 来渲染柱状图和我的图例。 I want to be get the total count of all the legends together (default legend and linkedTo legend).我想获得所有图例的总数(默认图例和链接图例)。

Ex: in the fiddle below: I have 2 legends: now when I click on one of them, I want to see the cumulative count of all the series pertaining to that legend clicked.例如:在下面的小提琴中:我有 2 个图例:现在当我单击其中一个时,我想查看与单击的该图例有关的所有系列的累积计数。 http://jsfiddle.net/5eb0utxq/ is that possible, currently its getting some count, but im not sure if that is the total count of all the series related to that legend. http://jsfiddle.net/5eb0utxq/是可能的,目前它得到了一些计数,但我不确定这是否是与该传说相关的所有系列的总数。

code:代码:

$(function() {
  $('#container').highcharts({

    chart: {
      type: 'bar'
    },

    title: {
      text: 'Total fruit consumtion, grouped by gender'
    },

    xAxis: {
      categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
    },

    yAxis: {
      allowDecimals: false,
      min: 0,
      title: {
        text: 'Number of fruits'
      }
    },
      plotOptions: {
        series: {
          showInLegend: true,
          }
       },
     legend: {
        labelFormatter: function() {
          var totalLegend = this.yData.reduce(function(pv, cv) { return pv + cv; }, 0);
          return this.name +"  :  "+ totalLegend;
        }
      },
   

    series: [{
      data:  [4740, 1378, 8],
      name: "Confirmed",
      id: "Confirmed",
      type: "column",
y: "86",
      stack: "ACTIVE"
    }, {
      data: [2932, 141],
      name: "Potential", id: "Potential",
      stack: "ACTIVE",type: "column",
y: "86"
    }, {
      data:[1752, 11],
      linkedTo: "Confirmed",
      name: "Confirmed",showInLegend: false,
      type: "column",
y: "86",
      stack: "REOPENED"
    }, {
      data: [1089, 2],
      linkedTo: "Potential",showInLegend: false,
      name: "Potential",
      type: "column",
y: "86",
      stack: "REOPENED"
    }, {
      data: [2332, 1960, 42],
      linkedTo: "Potential",
      name: "Potential",showInLegend: false,
      type: "column",
y: "86",
      stack: "NEW"
    }, {
      data: [480, 4684, 2258],
      linkedTo: "Confirmed",
      name: "Confirmed",showInLegend: false,
      type: "column",
y: "86",
      stack: "NEW",
    }]
  });
});

You need to count yData from all linked series, this is the main series, so you can use the below code:您需要从所有链接系列中计算yDatathis是主要系列,因此您可以使用以下代码:

legend: {
  ...,
  labelFormatter: function() {
    var totalLegend = 0;

    [...this.linkedSeries, this].forEach(function(lSeries) {
      lSeries.yData.forEach(el => {
        totalLegend += el
      });
    });

    return this.name + ' (' + totalLegend + ')';
  }
}

Live demo: http://jsfiddle.net/BlackLabel/kfger78b/现场演示: http : //jsfiddle.net/BlackLabel/kfger78b/

API Reference: https://api.highcharts.com/highcharts/legend.labelFormatter API参考: https : //api.highcharts.com/highcharts/legend.labelFormatter

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

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