简体   繁体   English

vega-lite 和 vuejs - 关闭工具提示

[英]vega-lite and vuejs - dismiss tooltip

I'm drawing a bar chart with vega-lite in a Vue.js SPA.我正在 Vue.js SPA 中使用 vega-lite 绘制条形图。 The encoding section of the main data layer looks like this:主数据层的编码部分如下所示:

encoding: {
    x: {field: 'word', type: 'ordinal', sort: '-count'},
    y: {field: 'count', type: 'quantitative'},
    tooltip: [{field: 'word'}, {field: 'count'}, {field: 'doc_count'}]
  }

… and in the Vue component method that updates the chart, I have this: …在更新图表的 Vue 组件方法中,我有这个:

vegaEmbed('#vis', spec)
   .then(({_spec, view}) => {
     view.addEventListener('click', function (event, item) {
       if (item) {  
          vueComponent.onWordClicked(item.datum.word);
          }
        })
      })

… which, to complete all of that, calls this: ......为了完成所有这些,称之为:

onWordClicked(word) {
  this.$router.push({path: 'words', params: {word: word}});
}

All of this works exactly as I expect: you hover over a bar, a tooltip is displayed, you click the bar, and you are navigated to a different route in the SPA.所有这一切都完全按照我的预期工作:您在一个条形图上 hover,显示一个工具提示,您单击该条形图,您将被导航到 SPA 中的另一条路线。

BUT… the tooltip stays on screen, unless and until you navigate back to the chart page and hover over the bars, in which case the tooltip can be reinvoked and then dismissed when you mouse out.但是……工具提示会一直显示在屏幕上,除非您导航回图表页面并在条形图上显示 hover,在这种情况下,可以重新调用工具提示,然后在鼠标移出时将其关闭。

Any ideas on how to make the tooltip go away when the Vue path changes?当 Vue 路径发生变化时,关于如何使工具提示 go 消失的任何想法? I've tried playing around with the view.tooltip(...) method, but suspect that's overkill (I'm happy with the default tooltip) and may not even solve my problem.我尝试过使用view.tooltip(...)方法,但怀疑这太过分了(我对默认的工具提示很满意),甚至可能无法解决我的问题。

Thanks.谢谢。

So, when the route changes the component that triggered the tooltip is replaced by a different component.因此,当路由更改时,触发工具提示的组件会被不同的组件替换。 Your component's beforeDestroy and destroyed methods will be called and you can hook into this and clean up anything that should be cleaned up.你的组件的beforeDestroydestroyed方法将被调用,你可以挂钩并清理任何应该清理的东西。

destroyed() { 
    // close the any open vega views. (dont know the specific of vega-embed)
}

If a components cleans up all it's side effects on destroy you can call this a well-behaved component.如果一个组件清除了它对销毁的所有副作用,则可以将其称为行为良好的组件。 Side effects can be timeouts and intervals that have been set, popups and dialogs that have been opened and what not.副作用可能是已设置的超时和间隔、已打开的弹出窗口和对话框等等。 If you want to be able to clean up you need to save the handles so you can close them later on.如果您希望能够进行清理,则需要保存句柄,以便稍后关闭它们。 I'll demonstrate a well-behaved clock component:我将演示一个表现良好的时钟组件:

Vue.component("clock", {
    template: `
    <div>{{time}}</div>
    
`,
    data() {
        return {
            time: null
        }
    },
    mounted() {
        // save the handle this.interval so we can clear it later on.
        this.interval = setInterval(() => {
            var d = new Date;
            this.time = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds()
        }, 750);
    },
    destroyed() {
        clearInterval(this.interval);
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM