Javascript 数字格式的海图(单位)
我正在使用生成折线图 我对Javascript 数字格式的海图(单位),javascript,highcharts,Javascript,Highcharts,我正在使用生成折线图 我对numberFormat有问题: var test = 15975000; numberFormat(test, 0,',','.'); 结果是:15.975.000 但是我想像这样把1000转换成1k,100000转换成100k,1000000转换成1m。 我如何处理这个问题?自己写(见此) 另请参见:数字格式在Highcharts对象中可用 Highcharts.numberFormat(test, 0,',','.'); 范例 您只需要这样做:
numberFormat
有问题:
var test = 15975000;
numberFormat(test, 0,',','.');
结果是:15.975.000
但是我想像这样把1000
转换成1k
,100000
转换成100k
,1000000
转换成1m
。
我如何处理这个问题?自己写(见此)
另请参见:数字格式在Highcharts对象中可用
Highcharts.numberFormat(test, 0,',','.');
范例
您只需要这样做:
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;
}
函数缩写(数字、小数点){
//小数点后2位=>100,3位=>1000等
deplaces=数学功率(10,deplaces);
//列举数字缩写
var abbrev=[“k”、“m”、“b”、“t”];
//向后遍历数组,因此我们首先执行最大值
对于(变量i=abbrev.length-1;i>=0;i--){
//将数组索引转换为“1000”、“1000000”等
变量大小=数学功率(10,(i+1)*3);
//如果数字较大或相等,请使用缩写
如果(大小如果您要格式化高库存图表:
tooltip: {
pointFormatter: function() {
var result = this.y;
let header = '<table>';
let body = '<tr><td style = "color: ' + this.series.color + ';padding:0">'
+ this.series.name + ': </td><td style = "padding:0"><b>';
if (result > 1000000) {
result = Math.floor(result / 1000000) + "M"
}
else if (result > 1000) {
result = Math.floor(result / 1000) + "k"
}
return header + body + result + '</b></td></tr></table>';
}
},
工具提示:{
pointFormatter:函数(){
var结果=这个.y;
让标题=“”;
让body=''
+this.series.name+':';
如果(结果>1000000){
结果=数学下限(结果/1000000)+“M”
}
否则,如果(结果>1000){
结果=数学地板(结果/1000)+“k”
}
返回页眉+正文+结果+“”;
}
},
在不妨碍数据分组功能或日期的情况下,我很难找到一种添加数百万和数千的方法。“但我想这样将1000到1k、100000到100k、1000000到1m进行转换。我如何处理这个问题?”我看不出这能解决这个问题?
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;
}
tooltip: {
pointFormatter: function() {
var result = this.y;
let header = '<table>';
let body = '<tr><td style = "color: ' + this.series.color + ';padding:0">'
+ this.series.name + ': </td><td style = "padding:0"><b>';
if (result > 1000000) {
result = Math.floor(result / 1000000) + "M"
}
else if (result > 1000) {
result = Math.floor(result / 1000) + "k"
}
return header + body + result + '</b></td></tr></table>';
}
},