自定义标签显示Highcharts的百分比

问题描述 投票:0回答:1

我想在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>
highcharts
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.