Javascript 在 highcharts 的饼图中显示百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12529934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:38:06  来源:igfitidea点击:

Displaying percentage inside pie item for highcharts

javascripthighcharts

提问by ryanzec

I am trying to figure out if it is possible to display the percentage for each pie item inside the actual pie item for highcharts? Example of what I mean is something like this:

我想弄清楚是否可以在 highcharts 的实际馅饼项目中显示每个馅饼项目的百分比?我的意思的例子是这样的:

https://developers.google.com/chart/interactive/docs/gallery/piechart

https://developers.google.com/chart/interactive/docs/gallery/piechart

回答by Jugal Thakkar

You may want to look @ dataLabels.formatter. this.percentageoption is available for pie

你可能想看看 @ dataLabels.formatterthis.percentage选项可用于馅饼

Also dataLabels.distancecan be set to a negative value for dataLabels inside the pie.

dataLabels.distance可以将饼图内的 dataLabels 设置为负值。

Pie with percentage values inside @ jsFiddle

@ jsFiddle 中带有百分比值的饼图

回答by Mark

Its suprisingly easy. In your tooltip or dataLabels section change your format.

它非常容易。在您的工具提示或 dataLabels 部分更改您的格式。

FROM:
  format: '<b>{point.name}</b>: {point.y:.1f} Rs.',
TO:
  format: '<b>{point.name}</b>: {point.percentage:.1f} %',

回答by Sarvar Nishonboev

Try this code. It works for me:

试试这个代码。这个对我有用:

  'labelFormatter':function () { 
       var total = 0, percentage; 
       $.each(this.series.data, function() { 
          total+=this.y; 
       }); 

       percentage=((this.y/total)*100).toFixed(1); 
       return this.name+' '+percentage+'%'; 
    }