【问题标题】:redraw pie chart in highchart在highchart中重绘饼图
【发布时间】:2026-02-06 15:45:01
【问题描述】:

假设我有 4 个切片,分别为 20%、30%、30% 和 20%。如果我使第 4 个切片(20%)处于非活动状态,其他切片应自行调整并占据 100%。如何在highcharts中做到这一点?谢谢。

【问题讨论】:

    标签: charts highcharts pie-chart


    【解决方案1】:

    我认为不可能改变这种行为。相反,您需要将点全部删除,其他切片的总和为 100。以下示例显示了图例切换和点删除之间的区别:jsfiddle

    【讨论】:

      【解决方案2】:

      我认为这应该是标准行为:)

      opts.plotOptions.pie.point.events.legendItemClick = function() {
          if (this.visible) {
              this['y_old'] = this.y;
              this.update(0);
          }
          else {
              this.update(this.y_old);
          }
      };
      

      现在,当您单击图例项时,饼图切片将消失

      如果您想显示百分比(100% 没有现在丢失的切片),您必须将工具提示(或图例)定义为:

      opts.tooltip.formatter = function() {
          var s = '<b>' + this.point.name + '</b>: ' + this.percentage.toFixed(2) + '%';
      
          return s;
      };
      

      【讨论】:

        【解决方案3】:

        此功能现在可通过 plotOptions.pie.ignoreHiddenPoint 开箱使用

         series: [{
            ignoreHiddenPoint: true,
            type: 'pie',
            ...
          }]
        

        Auto Redraw/Recalculate Pie on Legend | Highchart & Highstock @ jsFiddle

        【讨论】: