Highcharts - javascript - 不正确的堆叠列格式

时间:2017-03-05 08:45:17

标签: highcharts

对于highcharts,我看到堆叠列和"%"重叠的奇怪行为。您可以在此处查看http://jsfiddle.net/nqjpkc05/,代码如下。

如何在不更改数据结构的情况下修复它?

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>

和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}]
    }]
});

1 个答案:

答案 0 :(得分:1)

您可以在加载/重绘事件上移动列顶部的堆栈标签 - 可以从point.shapeArgs属性获取位置。

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
    });
  });
}

设置事件:

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

实例和输出

http://jsfiddle.net/mkfn5w15/

reposition stack labels