按照帖子中的示例,我们使用了以下语法,该语法在迁移到 HighCharts 11 之前一直有效:
events: {
render: function() {
var series = this.series[0],
seriesCenter = series.center,
x = seriesCenter[0] + this.plotLeft,
y = seriesCenter[1] + this.plotTop,
text = 'Total: ' + series.total,
fontMetrics = this.renderer.fontMetrics(16);
if (!this.customTitle) {
this.customTitle = this.renderer.text(
text,
null,
null,
true
)
.css({
transform: 'translate(-50%)',
fontSize: '16px',
color: 'red'
})
.add();
}
this.customTitle.attr({
x,
y: y + fontMetrics.f / 2
});
}
}
HC 11 中的问题是由于 this.renderer.fontMetrics 生成错误(Uncaught TypeError: Failed toexecute 'getCompulatedStyle' on 'Window':parameter 1 is not of type 'Element'.),如示例中所示:https://jsfiddle.net/BlackLabel/mrg2qt6c/
请问您有解决办法吗? 预先感谢
这是因为,在较新的 Highcharts 版本中,
fontMetrics
方法中的第一个参数需要是有效的 SVG 元素,而不是数字。您可以使用:
const fontMetrics = this.renderer.fontMetrics(this.customTitle);
this.customTitle.attr({
x,
y: y + fontMetrics.f / 2
});
现场演示:https://jsfiddle.net/BlackLabel/h31862sy/
API 参考: https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#fontMetrics