GWorking
GWorking

Reputation: 4341

Highcharts - javascript - incorrect stacked column formatting

With highcharts, I am seeing an overlapping strange behaviour with stacked columns and "percent". You can see it here http://jsfiddle.net/nqjpkc05/, the code is below.

How could I fix it without changing the data structure?

the html

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

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

And the js

Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Stacked column chart'
    },
    xAxis: {
        type: 'category',
    },
    yAxis: {
  //      min: 0,
  //minRange:1,
        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: {
                grouping: true,
            stacking: 'percent',
            dataLabels: {
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
            }
        }
    },
    series: [{
        name: 'John',
        data: [{info:'P1',name:0,x:0,y:27},{info:'P2',name:0,x:0,y:8},{info:'P3',name:0,x:0,y:3}]
    }, {
        name: 'Jane',
        data: [{info:'P1',name:1,x:1,y:35},{info:'P2',name:1,x:1,y:6},{info:'P3',name:1,x:1,y:0}]
    }, {
        name: 'Joe',
        data: [{info:'P1',name:2,x:2,y:12},{info:'P2',name:2,x:2,y:5},{info:'P3',name:2,x:2,y:18}]
    }]
});

Upvotes: 0

Views: 80

Answers (1)

morganfree
morganfree

Reputation: 12472

You can move the stack labels on top of the column on load/redraw event - the position can be obtain from point.shapeArgs property.

function positionStackLabels() {
  const stacks = this.yAxis[0].stacks.column;

  Object.keys(stacks).forEach(s => {
    const series = this.series[s];
    const topPoint = series.data[0];

    stacks[s].label.attr({
      y: topPoint.shapeArgs.y - 5
    });
  });
}

Set events:

  chart: {
    type: 'column',
    events: {
      load: positionStackLabels,
      redraw: positionStackLabels
    }
  },

Live example and output

http://jsfiddle.net/mkfn5w15/

reposition stack labels

Upvotes: 1

Related Questions