Search code examples
javascripthighchartspie-chart

how to change the point format for drill down in pie charts?


I am using a pie chart with drill down, is there any way to change the point format in tool-tip function. I want to display; first pie chart in Percentage(%) format, and then drill down data in Metric Ton (MT)format.

I am getting the data in respective formats from the back end service. Products are coming in metric Tons and remaining-percentage, actual percentage are coming in percentage.

My pie chart code is given below.

function visitorData(data){
    var products = data.products;
    var remainingPercentage=data.remainingPercentage;
     var actualPercentage=data.actualPercentage
    Highcharts.chart('current', {
         credits: {
        enabled: false
      },
        chart: {
            type: 'pie'
        },
        title: {
            text: 'Production report of current quarter'
        },
        subtitle: {

        },
        plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                }
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
            pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
        },
        series: [{
            name: 'Planned Production',
            colorByPoint: true,
            data: [{
                name: 'Remaining Production Qty. ',
                y: data.remainingPercentage,
            },
             {
                name: 'Actual Production Qty.',
                y: data.actualPercentage,
                drilldown: 'ap'
            }
            ]
        }],
        drilldown: {
            series: [{
                name: 'production report',
                id: 'po',
                data: [

                ]
            }, {
                name: 'Actual Production Qty.',
                id: 'ap',
                data: data.products

            }  ]
        }
    }); 

}

Thanks in advance.


Solution

  • A way to solve this is to appoint a tooltip: {pointFormatter: } to your first level series. This will override the main tooltip formatter and show you the data you want it to show.

    // Create the chart
     Highcharts.chart('container', {
             credits: {
            enabled: false
          },
            chart: {
                type: 'pie'
            },
            title: {
                text: 'Production report of current quarter'
            },
            subtitle: {
    
            },
            plotOptions: {
                series: {
                    dataLabels: {
                        enabled: true,
                    }
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
                 //MT format for the lower drilldowns
                 pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}MT</b> of total<br/>' 
            },
            series: [{
             tooltip: {
                //Pointformat for the percentage series
                pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>' 
            },
                name: 'Planned Production',
                colorByPoint: true,
                data: [{
                    name: 'Remaining Production Qty. ',
                    y: 3,
                },
                 {
                    name: 'Actual Production Qty.',
                    y: 5,
                    drilldown: 'ap'
                }
                ]
            }],
            drilldown: {
                series: [{
                    name: 'production report',
                    id: 'po',
                    data: [1]
                }, {
                    name: 'Actual Production Qty.',
                    id: 'ap',
                    data: [2]
    
                },
    
    
                ]
            }
        }); 
    

    The reason why I opted to let the first level override the tooltip is because of the construction of your drilldown, and I figure all of those drilldowns will use the same tooltip format.

    Here you can find a working JSFiddle.