【问题标题】:jQuery HighchartsjQuery Highcharts
【发布时间】:2025-12-24 12:10:07
【问题描述】:

所以我使用 jQuery 插件“Highcharts”制作了这个饼图,它返回给我的是输入到其中的总百分比。我还想做的是专门显示传递给插件的数字。这是否可能以及如何通过登录系统阻止我所拥有的,但此链接指向我正在使用的确切演示:

http://www.highcharts.com/demo/pie-legend

如果你查看源代码,你会看到插件的这个:

$(function () {
    var chart;

    $(document).ready(function () {

        // Build the chart
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Browser market shares at a specific website, 2010'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: false
                    },
                    showInLegend: true
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: [
                    ['Firefox',   45.0],
                    ['IE',       26.8],
                    {
                        name: 'Chrome',
                        y: 12.8,
                        sliced: true,
                        selected: true
                    },
                    ['Safari',    8.5],
                    ['Opera',     6.2],
                    ['Others',   0.7]
                ]
            }]
        });
    });

});

在“数据”下是系列标题,然后是值。我特别想知道使用什么语法来显示像“Firefox”的 45.0 这样的数字。要显示您使用 {series.name} 的标题,所以我猜测它类似于 {series.number} 但文档中没有任何地方指定这一点,所以我不确定它是否可能。任何帮助将不胜感激!

【问题讨论】:

    标签: jquery jquery-plugins plugins charts highcharts


    【解决方案1】:

    我实际上找到了答案,但不是通过 Highcharts 文档,如果您对格式化程序感到疑惑,可以参考您传递给插件的值:

    this.point.y
    

    总的来说,我的代码现在看起来像这样:

           plotOptions: {
                pie: {
                     allowPointSelect: true,
                     cursor: 'pointer',
                     dataLabels: {
                          enabled: true,
                          color: '#000000',
                          connectorColor: '#000000',
                          formatter: function() {
                               return '<b>'+ this.point.name +'</b>: $'+ this.point.y +' '         + Math.round(this.percentage*10)/10 +' %';
                          }
                     }
                }
           },
    

    Customize tooltip and format the number to 2 decimal places of highcharts

    【讨论】:

      【解决方案2】:

      有两种不同的方式可以满足您的需求。通过使用

      1. pointFormat
      2. 格式化工具提示。

      使用 pointFormat

      添加以下sn-p

       tooltip: {
                  pointFormat: '{series.name}: <b>{point.y}</b>'                 
                }
      

      找到演示here

      使用 Formatter

      代码

      tooltip: {
                  formatter: function () {
                      return '<b>' + this.point.name + '</b>: ' + this.point.y
                  }
              }
      

      找到演示here

      在您的代码中,

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

      替换,

       pointFormat: '{series.name}: <b>{point.percentage}%</b>'
      

       pointFormat: '{series.name}: <b>{point.y}</b>',
      

      【讨论】: