Add additional data to Pie Tooltip on Highcharts

假如想象 提交于 2021-02-07 19:31:22

问题


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:

Thanks


回答1:


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




回答2:


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




回答3:


can try like this

tooltip: { headerFormat: '{series.name}: {point.y}',

      pointFormat: '{series.name}: <b><br/>Totalamount: {point.amount}'

    },


来源:https://stackoverflow.com/questions/34397125/add-additional-data-to-pie-tooltip-on-highcharts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!