highcharts - 一个图表中多个饼图系列的图表名称

时间:2013-05-20 00:18:27

标签: highcharts

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

在我尝试将标签/标题放在每个饼图上之前,这一切都没有问题。似乎我只能拥有整个集团的头衔。见jsfiddle

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

See above jsfiddle for example

4 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,通过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
        },

答案 1 :(得分:1)

为了进一步改进,下面的代码正确处理了标题的左,中,右对齐。有关示例,请参阅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));

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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
                                 }
                );

                }
            }); 
     }

USAGE

                    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'
                    });