Search code examples
highchartsaxis-labels

HighCharts display total of stacked column in labels


I have a stacked group column graph as shown in the fiddle provided below. In the xAxis labels (red blocks), I would like to display the total of the stacked amounts subtracted from the total of the second column. For example, for "Value1" I want to display 42 in the label (100-(43+15)). Right now, I am only able to access x values, which I am returning in the formatter function (this.value). https://jsfiddle.net/er1187/n6sr0znx/

xAxis: [{
    offset: -280,
    tickWidth: 0,
    lineWidth: 0,
    categories: ['Value1', 'Value2', 'Value3'],
    labels: {
        x: 5,
        useHTML: true,
        style:{
        backgroundColor: 'red',
        color: 'white'
        },
        formatter: function () {
            return this.value;
        }
    }
}, {
    linkedTo: 0,
    categories: ['Value1', 'Value2', 'Value3']
}]

Solution

  • In the axis formatter you dont have access to the processed data yet. However, you can access the options of series and obtain the raw data.

    formatter: function () {
      const axis = this.axis;
      const points = axis.series.map(series =>
        series.options.data[axis.categories.indexOf(this.value)]
      );
    
      return points[2] - (points[0] + points[1]);    
    }
    

    example: https://jsfiddle.net/n6sr0znx/2/