高图 - 在图表上显示值

时间:2016-12-21 16:55:47

标签: jquery highcharts

我正在使用来自Highcharts的饼图并且工作正常,但我要求显示百分比值,如下图所示。

Image

我尝试使用legand选项但是没有用。

我的设置如下。

    $('#' + source).highcharts({
        credits: {
            enabled: false
        },
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: chartTitle
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: false,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                },
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                },
                connectorColor: 'silver',
                showInLegend: true
            }
        },
        legend: {
            enabled: true,
            layout: 'horizontal',
            verticalAlign: 'bottom',
            adjustChartSize: true
        },
        series: [{
            name: 'Total of ',
            colorByPoint: true,
            data: chartData
        }]
    });

3 个答案:

答案 0 :(得分:1)

为了在图表外显示一个标签,您必须计算其x和y值,并考虑标签角度。

sectionIndexTitlesForTableView

实例: https://jsfiddle.net/16d8y6yj/

输出: enter image description here

答案 1 :(得分:0)

尝试使用distance属性:

{{1}}

当馅饼切片小得多时,您需要注意。

答案 2 :(得分:0)

获取dataLabels及其distance属性和formatter功能的帮助。使用此fiddle

<强> JS:

var container = new Highcharts.Chart('container',{
        chart: {
             plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        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
        }
    });

<强> HTML:

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