问题描述
在以下方法中,我遇到一个问题,无法在工具提示选项中显示连字符(-)代替零(0)值。当我添加条件语句及其抛出错误消息时。
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} %</b></td></tr>',footerFormat: '</table>',shared: true,useHTML: true
}
解决方法
您需要使用tooltip.formatter
函数:
tooltip: {
...,formatter: function() {
var points = this.points,result = '<span style="font-size:10px">' + points[0].key + '</span><table>';
points.forEach(p => {
result += '<tr><td style="color:' + p.series.color + ';padding:0">' + p.series.name + ': </td>' +
'<td style="padding:0"><b>' + (p.y !== 0 ? Math.round(p.y * 10) / 10 : 'hyphen') + ' %</b></td></tr>';
});
result += '</table>';
return result;
}
}
实时演示: http://jsfiddle.net/BlackLabel/v7oqasb5/
API参考: https://api.highcharts.com/highcharts/tooltip.formatter