自定义标签显示百分比图表

时间:2019-04-22 04:07:19

标签: highcharts

我想在highchats上自定义非堆叠的列标签。
代替这个
enter image description here 我想显示这样的标签
enter image description here

我已经尝试过的是做另一个显示百分比的系列,但是它没有显示出预期的结果。 在Highcharts可以做吗?

<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: 310px; height: 400px; margin: 0 auto"></div>
<script>
  Highcharts.chart('container', {
    chart: {
        type: 'column'
    },

    title: {
        text: 'Show Percentage'
    },
    subtitle: {
        text: ''
    },
    xAxis: {
        categories: [
            'Jan',
            'Feb'
        ],
        crosshair: true
    },
    yAxis: {
        min: 0,
    },
    tooltip: {
        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
            '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
    },
    plotOptions: {
        column: {
            pointPadding: 0,
            borderWidth: 0
        },
        series : {
        	dataLabels : {
          	enabled :true
          }
        }
    },
    series: [
    {
        name: 'A',
        data: [100, 80]

    }, {
        name: 'B',
        data: [50, 40]

    }]
});
</script>

1 个答案:

答案 0 :(得分:0)

您只能为第一个系列启用dataLabels,并使用render事件来正确放置标签:

chart: {
    type: 'column',
    events: {
        render: function() {
            var points = this.series[0].points,
                shapeArgs,
                y;

            points.forEach(function(point, i) {
                shapeArgs = point.shapeArgs;

                if (point.y > this.series[1].points[i].y) {
                    y = shapeArgs.y;
                } else {
                    y = this.series[1].points[i].shapeArgs.y;
                }

                y -= point.dataLabel.height;

                point.dataLabel.attr({
                    x: shapeArgs.x + shapeArgs.width - point.dataLabel.width / 2,
                    y: y
                });
            }, this);
        }
    }
},

实时演示: http://jsfiddle.net/BlackLabel/rqp5nb0a/

API参考:

https://api.highcharts.com/class-reference/Highcharts.SVGElement#attr

https://api.highcharts.com/highcharts/chart.events.render