Aniket Tiwari
Aniket Tiwari

Reputation: 3998

Hide datalabels inside stacked column chart if we have 1 data

I need to hide the inside datalabels if there is only one dataset which is greater than zero. What I mean by dataset is

series: [{
        name: 'John',
        data: [5, 3, 0, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 0, 2, 0]
    }, {
        name: 'Joe',
        data: [3, 4, 3, 2, 0]
    }]

If series.data[i] all are zero except one then hide the inside data labels. In the above case the 3rd and 5th dataset has 0,0,3 and 2,0,0 values only 1 non zero value so hide the inside datalabel.

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: ( // theme
                    Highcharts.defaultOptions.title.style &&
                    Highcharts.defaultOptions.title.style.color
                ) || 'gray'
            }
        }
    },
    legend: {
        align: 'right',
        x: -30,
        verticalAlign: 'top',
        y: 25,
        floating: true,
        backgroundColor:
            Highcharts.defaultOptions.legend.backgroundColor || '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: true,
                formatter:function() {
                  if(this.y != 0) {
                    return this.y;
                  }
        }
            }
        }
    },
    series: [{
        name: 'John',
        data: [5, 3, 0, 7, 2]
    }, {
        name: 'Jane',
        data: [2, 2, 0, 2, 0]
    }, {
        name: 'Joe',
        data: [3, 4, 3, 2, 0]
    }]
});

Here I have highlighted which datalabel should need to be remove from inside.datalabels.

Upvotes: 2

Views: 582

Answers (1)

ppotaczek
ppotaczek

Reputation: 39139

In the formatter function you can check if at least two of the series have y value greater than 0.

plotOptions: {
  column: {
    stacking: 'normal',
    dataLabels: {
      enabled: true,
      formatter: function() {
        var series = this.series.chart.series,
          xPos = this.point.x,
          filteredSeries;

        if (this.y != 0) {
          filteredSeries = series.filter((s) => (s.yData[xPos]));

          return filteredSeries.length > 1 ? this.y : '';
        }
      }
    }
  }
}

Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4971/

API Reference: https://api.highcharts.com/highcharts/series.column.dataLabels.formatter

Upvotes: 2

Related Questions