我想要根据全局变量格式化可重复使用的Highcharts工具提示。 (我正在使用相同的图表在货币和数字值之间切换:如果我在图表上显示货币数据,我希望将工具提示格式化为货币)。
但是,在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)
}
});
});
有更好的方法来做这件事吗?