在Highcharts的堆叠列图中禁用列的堆栈标签

时间:2018-08-26 14:24:14

标签: javascript jquery graph highcharts

我有一个像fiddle这样的堆积柱形图。
如果在此看到一列为灰色,则表示该列已被禁用。我想从颜色为灰色/或类别为“梨”的列顶部隐藏总计。

我在this答案中尝试了这种方法,但是我不知道如何根据类别类型/列禁用堆栈总数?


由于fiddle.net的链接必须随附代码,因此为:

Highcharts.chart('container', {
  chart: {
    type: 'column'
  },
  title: {
    text: 'Stacked column chart'
  },
  xAxis: {
    categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
  },
  yAxis: {
    min: 0,
    title: {
      text: 'Total fruit consumption'
    },
    stackLabels: {
      enabled: true,
      style: {
        fontWeight: 'bold',
        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
      }
    }
  },
  legend: {
    align: 'right',
    x: -30,
    verticalAlign: 'top',
    y: 25,
    floating: true,
    backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
    borderColor: '#CCC',
    borderWidth: 1,
    shadow: false
  },
  tooltip: {
    headerFormat: '<b>{point.x}</b><br/>',
    pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
  },
  plotOptions: {
    column: {
      stacking: 'normal',
      dataLabels: {
        enabled: false,
        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
      }
    },
    series: {
      events: {
        afterAnimate: function(e) {
          var chart = e.target.chart;

          for (var j = 0; j < chart.series.length; j++) {
            for (var i = 0; i < chart.series[j].data.length; i++) {

              // alert(chart.series[j].data[i].category);
              if (chart.series[j].data[i].category == 'Pears') {
                chart.series[j].data[i].update({
                  color: 'grey'
                });
              }
            }
          }
        }
      },
    }
  },
  series: [{
    name: 'John',
    data: [5, 3, 4, 7, 2],
  }, {
    name: 'Jane',
    data: [2, 2, 3, 2, 1]
  }, {
    name: 'Joe',
    data: [3, 4, 4, 2, 5]
  }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 210px; height: 200px; margin: 0 auto"></div>

2 个答案:

答案 0 :(得分:1)

就像您认为可以使用格式化程序选项API Doc那样:

stackLabels: {
  enabled: true,
  style: {
    fontWeight: 'bold',
    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
  },
  formatter:function(){
      if (this.x !== 2) {
        return this.total;
      }
  }
}

this.x是每个类别的数组。

Fiddle

答案 1 :(得分:1)

您可以使用yAxis.stackLabels.formatterAPI)并使用当前标签的x值(this.x)查找所需的类别名称。

例如(JSFiddle):

yAxis: {
    stackLabels: {
        formatter: function() {
            // if this label is for pears, return nothing
            if(this.axis.chart.xAxis[0].categories[this.x] == 'Pears')
                return;
            // if not, return the default
            else
                return this.total; 
        }
    }
}