Highcharts percentage of total for simple bar chart

前端 未结 4 883
野的像风
野的像风 2020-11-30 07:12

I have a simple 1-series bar chart where each bar has a nominal value. I can plot this fine with the data labels and axis representing the value for each bar but I\'d like t

4条回答
  •  庸人自扰
    2020-11-30 07:30

    @jlbriggs had a great answer and lead me onto the path of creating this formatter function. This function always checks the current values in the data. If the data is updated programmatically at a later time, the percentages will reflect the newly updated data. No dataSum or loop is necessary as the .map().reduce() takes care of that for you.

    dataLabels: {
      enabled: true,
      formatter: function() {
        var pcnt = (this.y / this.series.data.map(p => p.y).reduce((a, b) => a + b, 0)) * 100;
        return Highcharts.numberFormat(pcnt) + '%';
      }
    }
    

提交回复
热议问题