Highcharts中为plotLine添加点击事件失效问题求助
问题:Highcharts plotBand/plotLine点击事件无响应
我尝试为Highcharts的plotLine添加点击事件,根据官方文档说明该功能是支持的。但修改官方示例后,plotBand的点击事件无法生效(plotLine也存在同样问题)。点击系列数据点能正常触发预期的提示框,但plotBand点击无响应,请问哪里操作有误?
代码示例:
JavaScript
const report = document.getElementById('report'); Highcharts.chart('container', { xAxis: { plotBands: [{ // mark the weekend color: '#FCFFC5', from: Date.UTC(2010, 0, 2), to: Date.UTC(2010, 0, 4), events: { click: () => { alert('Band click'); } } }], tickInterval: 24 * 3600 * 1000, type: 'datetime' }, series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4], pointStart: Date.UTC(2010, 0, 1), pointInterval: 24 * 3600 * 1000, point: { events: { click: function () { alert('Series click'); } } } }] });
CSS
#container { height: 300px; }
HTML
<script src="https://code.highcharts.com/highcharts.js"></script> <div id="container"></div> <div id="report"></div>
解决方案
问题出在你没有开启plotBand的鼠标跟踪功能——Highcharts默认不会对plotBand/plotLine启用鼠标交互,必须手动设置enableMouseTracking: true才能触发点击事件。
修改后的JavaScript代码如下(重点标注新增配置项):
const report = document.getElementById('report'); Highcharts.chart('container', { xAxis: { plotBands: [{ // mark the weekend color: '#FCFFC5', from: Date.UTC(2010, 0, 2), to: Date.UTC(2010, 0, 4), enableMouseTracking: true, // 新增:开启鼠标跟踪 events: { click: () => { alert('Band click'); } } }], tickInterval: 24 * 3600 * 1000, type: 'datetime' }, series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4], pointStart: Date.UTC(2010, 0, 1), pointInterval: 24 * 3600 * 1000, point: { events: { click: function () { alert('Series click'); } } } }] });
添加enableMouseTracking: true后,点击plotBand就能正常触发点击事件弹出提示框了。plotLine的解决方式完全相同,只需在对应的plotLine配置中添加该属性即可。
内容的提问来源于stack exchange,提问作者okost
相关产品推荐
相关产品推荐

