如果您正在使用Vega嵌入 https://github.com/vega/vega-embed,它返回一个带有对视图的引用的承诺,该视图允许您使用addEventListener
- 在此处的文档中进行了解释 https://github.com/vega/vega-view#event-handling.
这是一个例子:
const width = 600
const color = blue
embed(element, {
$schema: 'https://vega.github.io/schema/vega-lite/3.0.0-rc6.json',
data: { 'values': data },
mark: {
type: 'line',
color,
point: {
color,
}
},
width,
height: width / 2,
encoding: {
'x': {
field: 'label',
type: 'temporal',
},
'y': {
field: 'value',
type: 'quantitative',
},
}
}).then(({spec, view}) => {
view.addEventListener('mouseover', function (event, item) {
console.log(item.datum)
})
})