Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add additional data to Pie Tooltip on Highcharts

I'm trying to add additional data to my pie chart using highcharts. Try to explain: I have a Pie Chart of Browsers:

  • Firefox - 10%
  • Chrome - 12%
  • Explorer - 65%
  • Opera - 13%

I would like to add more info to display in the tooltip: For example:

  • Firefox - 10% of which woman users: 5%
  • Chrome - 12% of which woman users: 10%
  • Explorer - 65% of which woman users: 30%
  • Opera - 13% of which woman users: 5%

The values I putted are invented, I would like to understand how to customize the tooltip and add some more data to the series.

My JsFiddle code

This is my JS code for the Pie:

<script>
    $(function () {
        $('#container').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type:'pie'
            },

            title: {
                text: 'Browsers'
            },

            subtitle: {
                text:false,
            },

            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },

            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                        style: {
                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                        }
                    }
                }
            },

            series: [{
                name: "Total",
                colorByPoint: true,
                data: [{
                    name: "Firefox",
                    y: 10,
                }, {
                    name: "Chrome",
                    y: 12,
                }, {
                    name: "Explorer",
                    y: 65,
                }, {
                    name: "Opera",
                    y: 13,
                }]
            }],

        });
    });
</script>

This is an image to understand what I would like to do: enter image description here

Thanks

like image 253
Maki Avatar asked Dec 21 '15 13:12

Maki


2 Answers

you can put custom data in series and then use in tooltip

data: [{
            name: "Firefox",
            y: 10,
    custom:"5% "
        }, {
            name: "Chrome",
            y: 12,
     custom:"10% "
        }, {
            name: "Explorer",
            y: 65,
     custom:"15%"
        }, {
            name: "Opera",
            y: 13,
     custom:"25% "
        }]

Use in tooltip

  tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b> <br>of which woman users {point.custom}'
    }

See updated fiddle here

like image 74
Nishith Kant Chaturvedi Avatar answered Sep 27 '22 00:09

Nishith Kant Chaturvedi


You can also use tooltip.formatter and extract values from this.point.options.custom (where custom if name of field like in examples above)

http://api.highcharts.com/highcharts#tooltip.formatter

like image 22
Sebastian Bochan Avatar answered Sep 26 '22 00:09

Sebastian Bochan