![](/img/trans.png)
[英]Change Highcharts tooltip formatter from chart Object , after chart is rendered
[英]Highcharts: dynamically change the tooltip formatter?
我想根据全局值格式化可重用的Highcharts工具提示。 (我使用相同的图表在货币和数值之间切换:如果我在图表上显示货币数据,我想将工具提示格式化为货币。)
但是,在Highcharts工具提示函数中, this
似乎只涉及本地数据点,而我似乎无法传递值。
如何传入值或获取全局值? 这是我现在的代码,它失败了:
getChartTooltip: function() {
return function(graphType) {
var prefix = (graphType === 'currency') ? '$' : ''; // Fails
return prefix + Highcharts.numberFormat(this.y, 0);
};
},
initializeChart: function() {
var graphType = 'currency';
var chartOptions = {};
chartOptions.tooltip = {
formatter: getChartTooltip(graphType)
};
// create chart, etc...
$('change-chart-type').on('click', function() {
// Try to update the tooltip formatter, fail horribly...
graphType = 'number';
chart.series[0].update({
tooltip: {
formatter: _this.getChartTooltip(graphType)
}
});
});
有什么更好的方法呢?
您无需更改tooltip.formatter
,因为formatter
本身将更改tooltip
。
我会尝试类似的东西:
tooltip: {
formatter: function() {
var unit = ' $',
result = this.value;
if(this.series.name == 'your_currency_serie_name'){
result += unit;
}
return result;
}
}
其中'your_currency_serie_name'
是引用货币值的系列的名称。
您还可以通过valueDecimals
, valuePrefix
和valueSuffix
选项设置每个系列中的tooltip
选项。 然后, tooltip
将使用这些选项显示数据。 现场演示 。
通用设置:
...
series: [{
tooltip: {
valueDecimals: 0,
valuePrefix: '',
valueSuffix: ''
},
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
tooltip: {
valueDecimals: 2,
valuePrefix: '$',
valueSuffix: ' USD'
},
data: [129.9, 171.5, 1106.4, 1129.2, 1144.0, 1176.0, 1135.6, 1148.5, 1216.4, 1194.1, 195.6, 154.4]
}]
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.