Format Highcharts y-axis labels

后端 未结 2 1158
甜味超标
甜味超标 2021-02-12 03:35

I\'m using Highcharts to generate a line chart that shows currency values. By default the y-axis labels use metric prefixes for abbreviation, e.g. 3k is displayed instead of 300

2条回答
  •  没有蜡笔的小新
    2021-02-12 04:12

    I looked in HighCharts source code and found out that if you pass a format or formatter it won't add numeric symbol. It is inside else if statement i.e. formatOption xor numericSymbol. So you need to add a formatter and do the logic yourself.

    this is a slightly modified copy-paste of their code:

           formatter: function() {
               var ret,
                   numericSymbols = ['k', 'M', 'G', 'T', 'P', 'E'],
                   i = numericSymbols.length;
               if(this.value >=1000) {
                   while (i-- && ret === undefined) {
                       multi = Math.pow(1000, i + 1);
                       if (this.value >= multi && numericSymbols[i] !== null) {
                          ret = (this.value / multi) + numericSymbols[i];
                       }
                   }
               }
               return '$' + (ret ? ret : this.value);
           }
    

    http://jsfiddle.net/x6b0onkp/1/

提交回复
热议问题