我在我的highcharts tooltip formatter函数中创建了一个if else语句,以便显示来自不同point.series.name的y值。数组中第一个和第二个系列的值显示正确,但最后一个series.name值(HINDCAST - SPREAD)的样式显示不正确,因为font-size和point.series.color没有显示。我想问题是表标签?请看小提琴
https://jsfiddle.net/marialaustsen/w4k87jyo/
tooltip: {
shared: true,
useHTML: true,
formatter: function() {
var aYearFromNow = new Date(this.x);
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 5);
var tooltip = '<table><span style="font-size: 16px">' +
Highcharts.dateFormat('%e/%b/%Y', new Date(this.x)) + '-' + Highcharts.dateFormat('%e/%b/%Y', aYearFromNow) + '</span><br/><tbody>';
//loop each point in this.points
$.each(this.points, function(i, point) {
if (point.series.name === 'Observations') {
tooltip += '<tr><th style="font-size: 14px; color: ' + point.series.color + '">' + point.series.name + ': </th>' +
'<td style="font-size: 14px">' + point.y + '℃' + '</td></tr>'
} else if (point.series.name === 'BOXPLOT') {
const x = this.x;
const currentData = this.series.data.find(data => data.x === x);
const boxplotValues = currentData ? currentData.options : {};
tooltip += `<span style="font-size: 14px; color: #aaeeee">
Max: ${boxplotValues.high.toFixed(2)}<br>
Q3: ${boxplotValues.q3.toFixed(2)}<br>
Median: ${boxplotValues.median.toFixed(2)}<br>
Q1: ${boxplotValues.q1.toFixed(2)}<br>
Low: ${boxplotValues.low.toFixed(2)}<br></span>`;
} else {
tooltip += '<tr><th style="font-size: 14px; color: ' + point.series.color + '">' + point.series.name + ': </th>' +
'<td style="font-size: 14px">' + point.point.low + '℃ -' + point.point.high + '℃' + '</td></tr>' +
'</tbody></table>';
}
});
return tooltip;
}
},
你在最后一个值(HINDCAST - SPREAD)之前关闭了tbody
,一旦放在for循环之外就可以了:
...
tooltip += '<tr><th style="font-size: 14px; color: ' + point.series.color + '">' + point.series.name + ': </th>' +
'<td style="font-size: 14px">' + point.point.low + '℃ -' + point.point.high + '℃' + '</td></tr>'
}
});
tooltip += '</tbody></table>';
return tooltip;
...