Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highcharts - chart names for multiple pie chart series in one chart

Tags:

highcharts

I need to display multiple pie charts that are all related to the same data. As such I want them all to be part of the same chart and implement them as individual series.

This all works no problems until I tried to put labels/titles over each individual pie chart. It seems that I can only have a title over the entire group. See jsfiddle

Is there any way to have the titles over each chart?

See above jsfiddle for example
like image 731
tocallaghan Avatar asked May 20 '13 00:05

tocallaghan


1 Answers

I encountered the same problem, and found this solution via highcharts support forum : http://highcharts.uservoice.com/forums/55896-general/suggestions/3073133-pie-title

The Highcharts dude has written a plugin that you can see working on the following jsfiddle : http://jsfiddle.net/highcharts/tnSRA/

I have copy-pasted this plugin in a highcharts-plugins.js file that I included in my website, works like a charm!

Here's the plugin code :

/**
 * 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));

And this is how you configure your title (put this in your series elements) :

title: {
            // align: 'left',
            // x: 0
            // style: { color: XXX, fontStyle: etc }
            text: '<b>Pie 1</b><br>Subtext',
            verticalAlign: 'top',
            y: -40
        },
like image 185
Vincent V. Avatar answered Nov 13 '22 19:11

Vincent V.