我想使用highcharts系列点事件,例如向用户警告点值。不知何故,API的事件似乎都不适合我。
我的图表plotOptions
看起来像
plotOptions: {
series: {
point: {
events: {
click: function () {
console.log('click event works');
}
}
}
}
}
但是,当我单击图表中的任何一点时,控制台中没有任何内容发生。我也试过这个与其他highcharts点事件,似乎没有一个工作。我在这里错过了什么吗?
这是一个官方演示,我可以在其中添加事件(我的代码在上面),并在这里以某种方式工作:demo
你的范围是错误的,plotOptions.point.events
不存在。
你需要定义plotOptions.series.point.events
。我刚用你的JSFiddle示例对它进行了测试,结果很有效。
plotOptions: {
scatter: {
width: 10,
height: 10,
depth: 10
},
series: { // <-- ▼▼▼ Here ▼▼▼
point: {
events: {
click: function() {
console.log('click event works');
}
}
}
} // <-- ▲▲▲ Here ▲▲▲
}
API有一个搜索栏,可以为您验证:
/**
* Based on: https://www.highcharts.com/demo/line-basic
*/
Highcharts.chart('container', {
title: {
text: 'Solar Employment Growth by Sector, 2010-2016'
},
subtitle: {
text: 'Source: thesolarfoundation.com'
},
yAxis: {
title: {
text: 'Number of Employees'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
plotOptions: {
series: {
label: {
connectorAllowed: false
},
pointStart: 2010,
point: { // <-- ▼▼▼ Here ▼▼▼
events: {
click: function() {
console.log('click event works');
}
}
} // <-- ▲▲▲ Here ▲▲▲
}
},
series: [{
name: 'Installation',
data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
}, {
name: 'Manufacturing',
data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
}, {
name: 'Sales & Distribution',
data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
}, {
name: 'Project Development',
data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
}, {
name: 'Other',
data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
}],
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom'
}
}
}]
}
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container"></div>