veronikauly
Posts: 7
Joined: Tue Jun 21, 2022 9:59 am

Customize pie chart: show series name as a label, legend aggregated

Hello, I created the following pie/donut chart:

Code: Select all

<div id="container"></div>

  $('#container').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                spacingRight: 30,

            },
            legend: {
                align: 'left',
                verticalAlign: 'bottom',
                layout: 'vertical',
                itemMarginTop: 5,
                itemStyle: {
                    fontWeight: 'normal'
                },
                useHTML: true,
                labelFormatter: function () {
                    return `<span>${this.name}</span>`
                }
            },
            title: {
                text: ''
            },
           tooltip: {
                formatter: function () {
                return `<b style="color: ${this.color}">${this.key}</b>: ${this.percentage.toFixed(1)} %`
                    }

            },
            plotOptions: {
                pie: {
                    minPointLength: 2,
                    allowPointSelect: true,
                    cursor: 'pointer',
                    showInLegend: false,
                }
            },

            series: [

                    {
                        type: 'pie',
                        dataLabels: {
                        enabled: true,
                        format: '<b Ring1</b>',

                    },
                        size: '66%',
                        innerSize: '33%',
                        name: 'Ring1',
                        data: [

                                {
                                    name: 'green_data', y: 22,
                                    color: 'green',

                                },
                            {
                                    name: 'red_data', y: 88,
                                    color: 'red',

                                },


                        ],
                        animation: false,

                    } ,
                   {
                        type: 'pie',
                        dataLabels: {
                        enabled: true,
                        format: '<b>Ring2</b>',

                    },
                        size: '100%',
                        innerSize: '66%',
                        name: 'Ring2',
                        data: [

                                {
                                    name: 'green_data', y: 44,
                                    color: 'green',

                                },
                            {
                                    name: 'red_data', y: 23,
                                    color: 'red',

                                },


                        ],
                        animation: false

                    }]


        });
Things that I want to customize:
- Display series name (Ring1, Ring2) -each label should be displayed once on the chart on the relevant ring (currently it is displayed for each data point - twice for each ring
- create a legend that will accumulate data point names, i.e the legend will contain only 2 rows - green_data and red_data and clicking on it will hide/display all data point with the same name (the default legend will display a row for each of the data points - green_data and red_data will appear twice (each for a ring). Thank you in advance, Veronika
veronikauly
Posts: 7
Joined: Tue Jun 21, 2022 9:59 am

Re: Customize pie chart: show series name as a label, legend aggregated

Found a great solution for series labels on pie chart - https://jsfiddle.net/Bastss/yzopskb9/
Still looking for the legend solution
hubert.k
Posts: 1164
Joined: Mon Apr 11, 2022 11:48 am

Re: Customize pie chart: show series name as a label, legend aggregated

Hello Veronika!
Welcome to our forum and thanks for contacting us with your question!

I have prepared for you the demo with common legend items and the hovering feature on legend. Legend items mouse over and mouse out events are not implemented in Highcharts, so I added them with custom code, especially for you. To make a legend only with "two rows" there is a need to hide some series in the legend by setting a showInLegend property to false.

Demo: https://jsfiddle.net/BlackLabel/ya85qh2b/

Let me know if that was what you were looking for.
Kind regards!
Hubert Kozik
Highcharts Developer
veronikauly
Posts: 7
Joined: Tue Jun 21, 2022 9:59 am

Re: Customize pie chart: show series name as a label, legend aggregated

Thank you very much! Works great!
hubert.k
Posts: 1164
Joined: Mon Apr 11, 2022 11:48 am

Re: Customize pie chart: show series name as a label, legend aggregated

You're welcome! In case of any further questions, feel free to contact us again.
Hubert Kozik
Highcharts Developer

Return to “Highcharts Usage”