7

我需要显示多个与相同数据相关的饼图。因此,我希望它们都成为同一个图表的一部分,并将它们作为单独的系列来实现。

在我尝试将标签/标题放在每个单独的饼图上之前,这一切都没有问题。看来我只能对整个组有一个头衔。见jsfiddle

有没有办法在每个图表上都有标题?

See above jsfiddle for example
4

4 回答 4

5

我遇到了同样的问题,并通过 highcharts 支持论坛找到了这个解决方案:http: //highcharts.uservoice.com/forums/55896-general/suggestions/3073133-pie-title

Highcharts 花花公子编写了一个插件,您可以看到它在以下 jsfiddle 上工作:http: //jsfiddle.net/highcharts/tnSRA/

我已将此插件复制粘贴到我的网站中包含的 highcharts-plugins.js 文件中,就像一个魅力!

这是插件代码:

/**
 * Pie title plugin
 * Last revision: 2012-12-21
 */
(function (Highcharts) {
    Highcharts.wrap(Highcharts.seriesTypes.pie.prototype, 'render', function (proceed) {

        var chart = this.chart,
            center = this.center || (this.yAxis && this.yAxis.center), 
            titleOption = this.options.title,
            box;

        proceed.call(this);

        if (center && titleOption) {
            box = {
                x: chart.plotLeft + center[0] - 0.5 * center[2],
                y: chart.plotTop + center[1] - 0.5 * center[2],
                width: center[2],
                height: center[2]
            };
            if (!this.title) {
                this.title = this.chart.renderer.label(titleOption.text)
                    .css(titleOption.style)
                    .add()
                    .align(titleOption, null, box);
            } else {
                this.title.align(titleOption, null, box);
            }
        }
    });

}(Highcharts));

这就是你配置标题的方式(把它放在你的系列元素中):

title: {
            // align: 'left',
            // x: 0
            // style: { color: XXX, fontStyle: etc }
            text: '<b>Pie 1</b><br>Subtext',
            verticalAlign: 'top',
            y: -40
        },
于 2015-04-21T15:13:26.627 回答
2

为了进一步改进这一点,下面的代码正确处理了标题的左对齐、居中对齐和右对齐。有关示例,请参见http://jsfiddle.net/9y4Lj4yr/上的小提琴。

    /**
* Pie title plugin
* Last revision: 2015-5-20
*/
(function (Highcharts) {
    Highcharts.wrap(Highcharts.seriesTypes.pie.prototype, 'render', function (proceed) {

        var chart = this.chart,
        center = this.center || (this.yAxis && this.yAxis.center),
        titleOption = this.options.title,
        box;

        proceed.call(this);

        if (center && titleOption) {
            box = {
                x: chart.plotLeft + center[0] - 0.5 * center[2],
                y: chart.plotTop + center[1] - 0.5 * center[2],
                width: center[2],
                height: center[2]
            };
            if (!this.title) {
                this.title = this.chart.renderer.label(titleOption.text)
                .css(titleOption.style)
                .add()
            }
            var labelBBox = this.title.getBBox();
            if (titleOption.align == "center")
                box.x -= labelBBox.width/2;
            else if (titleOption.align == "right")
                box.x -= labelBBox.width;
            this.title.align(titleOption, null, box);
        }
    });

} (Highcharts));
于 2015-05-20T14:42:16.193 回答
1

您可以使用允许在任何地方添加文本的渲染器。

http://api.highcharts.com/highcharts#Renderer.text()

于 2013-05-20T11:03:58.207 回答
1

改进文森特的答案。这就是我将它用于简单文本标题的方式。

extendHighcharts = function(){
        Highcharts.wrap(Highcharts.seriesTypes.pie.prototype, 'render', function (proceed) {

            //Clear the title if added previously
            //if your chart data does not change, this reference storage for removal can be left out
            if(this.chart.subChartTitleElement){                            
                this.chart.subChartTitleElement[this.id].destroy();
            }   

            proceed.call(this);


            if (this.center && this.options.title) {


                //first, add the title, at center or anywhere, we need the container width to center it
                this.chart.subChartTitleElement[this.id] = this.chart.renderer.text(this.options.title.text,this.center[0],this.center[1]);
                this.chart.subChartTitleElement[this.id].css(this.options.title.style)
                this.chart.subChartTitleElement[this.id].add();

                //Center the title using the container(Bounding Box = BBox) width
                var xCenter = this.chart.plotLeft + this.center[0] - (this.chart.subChartTitleElement[this.id].getBBox().width/2),
                    yCenter = this.chart.plotTop + this.center[1] - 0.6 * this.center[2];

                this.chart.subChartTitleElement[this.id].attr(
                                 {
                                     x:xCenter,
                                     y:yCenter
                                 }
                );

                }
            }); 
     }

用法

                    this.chart.addSeries({
                        type: 'pie', 
                        name: 'pie_chart_1',
                        data: pieChartData,
                        center: ['80%','25%'],
                        size: '35%',
                        showInLegend: false,
                        dataLabels: {
                            enabled: false
                        },

                        //this part adds the relevant information
                        title: {
                            text:'Pie Chart Title',
                            verticalAlign: 'top',
                            y: -40
                        },
                        id:'a_unique_id_or_name'
                    }); 
于 2015-04-28T12:27:36.450 回答