我们可以将事件侦听器添加到“ Vega-Lite”规范中吗?

JVM*_*JVM 2 data-visualization d3.js vega vega-lite

我是Vega和Vega-Lite的新手。我正在使用Vega-Lite创建一个简单的条形图,但无法添加任何事件侦听器,例如“悬停”。

我想悬停一个栏并更改栏的颜色。

cap*_*lam 6

如果您使用的是Vega-Embed,它将返回一个带有对视图的引用的promise,该视图允许您使用addEventListener- 在此处的文档中进行了说明

这是一个例子:

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)
    })
})
Run Code Online (Sandbox Code Playgroud)