带数字格式(单位)的高图
highchart with numberformat (unit)
我使用Highcharts生成折线图。
我对numberFormat
有问题:
var test = 15975000;
numberFormat(test, 0,',','.');
结果是:15.975.000
但是我想把1000
变成1k
, 100000
变成100k
, 1000000
变成1m
,就像这样。我该如何处理这个问题?
numberFormat在Highcharts对象中可用。
Highcharts.numberFormat(test, 0,',','.');
http://jsfiddle.net/DaBYc/1/ 例子
yAxis: {
labels: {
formatter: function () {
return Highcharts.numberFormat(this.value,0);
}
}
},
编写您自己的格式化程序(参见此示例)。
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;
}
参见:如何格式化类似于Stack Overflow信誉格式的数字
你只需要这样做:
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;
}
希望这行得通=)
如果你想要格式化Hilitock图表:
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>';
}
},
我很难找到一种方法来添加百万和千,而不妨碍数据分组功能或日期。
相关文章:
- 动态更改高图中的系列颜色
- 如何为高图中的区域线创建z索引
- 将高图饼图中的文本居中显示为响应
- 可以对一个图表进行高图深化,反映在另一个图表上
- 如何将数据标签与数据一起传递,以便在高图中的工具提示中显示
- 如何在悬停时更改高图的不透明度
- 升级到 jQuery 1.8 后出现高图缩放问题
- 高图3.0.7是否允许高图
- 指定饼图高图的颜色
- 高图热图:单击按钮或复选框时选择所有单元格
- 高图 - 分组/近似后保持多个系列之间的关系
- 高图折线图:点应该是可点击的,点之间的线不是.这可能吗
- 如何防止高图工具提示弹出隐藏,在图形区域或浏览器窗口上调整大小
- 数据库中的动态样条曲线高图
- 删除高图中的第一条和最后一条网格线
- 解决同时使用高图和树状图时的冲突(f.inArray 不是函数)
- 如何将页眉或徽标添加到高图导出的PDF中
- 放大折线图的高图无法正常工作
- 如何根据高图中的值获得动态颜色变化
- 带数字格式(单位)的高图