highchart用的NumberFormat(单元)(highchart with numberf

2019-10-18 00:56发布

我使用Highcharts生成折线图。

而且我有一个问题numberFormat

var test = 15975000;
numberFormat(test, 0,',','.');

其结果是: 15.975.000

但是,我想改造10001k100000100k10000001m这样。 我该如何处理这个问题呢?

Answer 1:

NUMBERFORMAT是Highcharts对象可用。

Highcharts.numberFormat(test, 0,',','.');

例如http://jsfiddle.net/DaBYc/1/

yAxis: {
        labels: {
            formatter: function () {
                return Highcharts.numberFormat(this.value,0);
            }
        }
    },


Answer 2:

写你自己的格式 (见本例 )。

formatter: function() {
  result = this.value;
  if (this.value > 1000000) { result = Math.floor(this.value / 1000000) + "M" }
  else if (this.value > 1000) { result = Math.floor(this.value / 1000) + "k" }
  return result;
}

另请参见: 如何格式化类似的堆栈溢出的声誉格式的数字



Answer 3:

你只需要做到这一点:

                labels: {
                formatter: function() {
                     return abbrNum(this.value,2); // Need to call the function for each value shown by the chart
                }
            },

这里要被插入的JavaScript用于变换数据的功能:

    function abbrNum(number, decPlaces) {
    // 2 decimal places => 100, 3 => 1000, etc
    decPlaces = Math.pow(10,decPlaces);

    // Enumerate number abbreviations
    var abbrev = [ "k", "m", "b", "t" ];

    // Go through the array backwards, so we do the largest first
    for (var i=abbrev.length-1; i>=0; i--) {

        // Convert array index to "1000", "1000000", etc
        var size = Math.pow(10,(i+1)*3);

        // If the number is bigger or equal do the abbreviation
        if(size <= number) {
             // Here, we multiply by decPlaces, round, and then divide by decPlaces.
             // This gives us nice rounding to a particular decimal place.
             number = Math.round(number*decPlaces/size)/decPlaces;

             // Handle special case where we round up to the next abbreviation
             if((number == 1000) && (i < abbrev.length - 1)) {
                 number = 1;
                 i++;
             }

             // Add the letter for the abbreviation
             number += abbrev[i];

             // We are done... stop
             break;
        }
    }

    return number;
}

希望这个作品=)



文章来源: highchart with numberformat (unit)