我想在默认 y 轴标签前添加 $ 前缀。我的条形图使用数百万的值,因此图表返回值-MM(80MM、30MM)。我想做的是将 y 轴格式化为 $-value-MM ($80MMm $30MM)。我已经尝试了下面的代码,但无法使其工作?
yAxis: [{ // Primary yAxis
labels: {
formatter: function () {
return '$' + this.value;
}
},
title: {
text: 'Revenue',
实现此目的的一种相当复杂的方法是重新使用 Highcharts 在其内部
defaultLabelFormatter
中使用的代码,用于数字轴,并在轴格式化程序中使用它。
一个示例,带有您添加的前缀 (JSFiddle):
yAxis: {
labels: {
formatter: function() {
var numericSymbols = Highcharts.getOptions().lang.numericSymbols;
var i = numericSymbols && numericSymbols.length;
var numericSymbolDetector = this.axis.isLog ? this.value : this.axis.tickInterval;
var UNDEFINED, ret, multi;
while (i-- && ret === UNDEFINED) {
multi = Math.pow(1000, i + 1);
if (numericSymbolDetector >= multi && (this.value * 10) % multi === 0 && numericSymbols[i] !== null) {
ret = Highcharts.numberFormat(this.value / multi, -1) + numericSymbols[i];
}
}
if (ret === UNDEFINED) {
if (Math.abs(this.value) >= 10000) {
ret = Highcharts.numberFormat(this.value, -1);
} else {
ret = Highcharts.numberFormat(this.value, -1, UNDEFINED, '');
}
}
return "$"+ret; // Adding the prefix
}
},
}
一个实验性的简短形式是用它所需的上下文的基本部分来调用
defaultLabelFormatter
。一个例子(JSFiddle):
yAxis: {
labels: {
formatter: function() {
return "$" + this.axis.defaultLabelFormatter.call(this);
}
},
}
由于上下文不完整,如果您的轴是
datetime
或 categories
或者可能是对数轴,则它不会按预期工作,但应该适用于数字轴。要了解完整情况,我建议查看完整的 defaultLabelFormatter
实现。
如果我正确理解问题,您的数据已经具有“MM”后缀,并且您想添加前缀“$”。
尝试一下,
yAxis: {
labels: {
format: '${value}'
}
}