HighCharts饼图 - 添加每片内的文本(HighCharts Pie Chart - Add

2019-07-21 01:22发布

我创建使用代表资产配置HighCharts金融饼图。 我的目标是创建一个代表每个切片,但每张幻灯片内的实际分配值将主要显示显示各种投资工具的目标百分比第二个数据标签的图表。 需要注意的是目前的资产配置可能并不总是与目标分配值相匹配是非常重要的。

我已经得到了一切,除了每张幻灯片内部的目标标签的工作。 见下面我想完成什么形象:

以下是我迄今:

            var asset_allocation_pie_chart = new Highcharts.Chart({
            chart: { renderTo: 'asset_allocation_bottom_left_div' },
            title: { text: 'Current Asset Allocation', style: { fontSize: '17px', color: entity_color, fontWeight: 'bold', fontFamily: 'Verdana'} },
            subtitle: { text: '(As of ' + effective_date_formatted + ')', style: { fontSize: '15px', color: entity_color, fontFamily: 'Verdana', marginBottom: '10px' }, y: 40 },
            tooltip: { pointFormat: '{series.name}: <b>{point.percentage}%</b>', percentageDecimals: 0 },
            plotOptions: {
                pie: { allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: true, color: '#000000', connectorWidth: 1, connectorColor: '#000000', formatter: function() { return '<b>' + this.point.name + '</b>:<br/> ' + Math.round(this.percentage) + ' %'; } } }
            },
            series: [{
                type: 'pie',
                name: 'Asset Allocation',
                data: [['Investment Grade Bonds', InvestmentGradeBondPercentage], ['High Yield Bonds', HighYieldBondPercentage], ['Hedged Equity', HedgedEquityPercentage], ['Global Equity', GlobalEquityPercentage], ['Cash', CashPercentage]]
            }],
            exporting: { enabled: false },
            credits: { enabled: false }
       });

Answer 1:

这里是的jsfiddle ,这和代码来显示内外datalabels。

为达到这个

  1. 你需要提供两个饼图系列。 一会看着前面,其他将在后面。
  2. 如果你想模仿它,然后修改成size: '80%'
  3. 距离:使用距离是显示datalabels进出,你可以根据你想要的它的位置发生变化。
  4. allowPointSelect:此默认值是假,但如果这是用来然后饼图驻留后面将在点击前饼图的切片示出。

码:

 var asset_allocation_pie_chart = new Highcharts.Chart({
        chart: {
            renderTo: 'asset_allocation_bottom_left_div'
        },
        title: {
            text: 'Current Asset Allocation',
            style: {
                fontSize: '17px',
                color: 'red',
                fontWeight: 'bold',
                fontFamily: 'Verdana'
            }
        },
        subtitle: {
            text: '(As of ' + 'dfdf' + ')',
            style: {
                fontSize: '15px',
                color: 'red',
                fontFamily: 'Verdana',
                marginBottom: '10px'
            },
            y: 40
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage}%</b>',
            percentageDecimals: 0
        },
        plotOptions: {
            pie: {
                size: '80%',
                cursor: 'pointer',
                data: [
                    ['Investment Grade Bonds', 100],
                    ['High Yield Bonds', 200],
                    ['Hedged Equity', 300],
                    ['Global Equity', 400],
                    ['Cash', 500]
                ]
            }
        },
        series: [{
                type: 'pie',
                name: 'Asset Allocation',
                dataLabels: {
                    verticalAlign: 'top',
                    enabled: true,
                    color: '#000000',
                    connectorWidth: 1,
                    distance: -30,
                    connectorColor: '#000000',
                    formatter: function() {
                        return Math.round(this.percentage) + ' %';
                    }
                }
            }, {
                type: 'pie',
                name: 'Asset Allocation',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorWidth: 1,
                    distance: 30,
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>' + this.point.name + '</b>:<br/> ' + Math.round(this.percentage) + ' %';
                    }
                }
            }],
        exporting: {
            enabled: false
        },
        credits: {
            enabled: false
        }
    });

饼图将看上去象下面这样:



Answer 2:

我们可以负值设定distance内属性dataLabels ,它会显示内的文本slice

plotOptions: {
        pie: {
            dataLabels: {
                distance: -30
            }
        }
}

DEMO

输出:



文章来源: HighCharts Pie Chart - Add text inside each slice