自定义Highcharts中的默认y轴标签
我想在默认y轴标签前加一个$。我的条形图使用的是以百万为单位的值,因此图表返回的值是MM(80MM,30MM)。我想做的是将y轴格式化为$-value MM($80MMm$30MM)。我尝试了下面的代码,但无法使其工作自定义Highcharts中的默认y轴标签,highcharts,Highcharts,我想在默认y轴标签前加一个$。我的条形图使用的是以百万为单位的值,因此图表返回的值是MM(80MM,30MM)。我想做的是将y轴格式化为$-value MM($80MMm$30MM)。我尝试了下面的代码,但无法使其工作 yAxis: [{ // Primary yAxis labels: { formatter: function () { return '$' + this.value; } }, title: {
yAxis: [{ // Primary yAxis
labels: {
formatter: function () {
return '$' + this.value;
}
},
title: {
text: 'Revenue',
实现这一点的一个相当复杂的方法是,对数值轴重复使用Highcharts在其内部
defaultLabelFormatter
中使用的代码,并在轴格式化程序中使用它
例如,添加了前缀():
一个实验性的简短形式是调用defaultLabelFormatter
,其中包含所需上下文的基本部分。这()的一个例子是:
由于上下文不完整,如果轴是
datetime
或categories
或可能是对数轴,它将无法按预期工作,但应适用于数字轴。对于完整的图片,我建议查看完整的defaultLabelFormatter
实现。如果我理解正确,您的数据已经有了“MM”后缀,您希望添加前缀“$”
试试看
您是否已将其自定义为
MM
?我希望这是百万美元的M,伙计,如果有任何解决方案有用的话,将其标记为答案,以便遇到相同问题的人能够轻松找到。值得注意的是,使用format
和formatter
的区别在于后者允许Highcharts自动将值缩写为最接近的顺序。e、 g.10万到10万,1 000 000到1百万等,而格式将按原样显示:
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
}
},
}
yAxis: {
labels: {
formatter: function() {
return "$" + this.axis.defaultLabelFormatter.call({
axis: this.axis,
value: this.value
});
}
},
}
yAxis: {
labels: {
format: '${value}'
}
}