【问题标题】:vega-lite and vuejs - dismiss tooltipvega-lite 和 vuejs - 关闭工具提示
【发布时间】:2021-04-12 11:14:41
【问题描述】:

我正在 Vue.js SPA 中使用 vega-lite 绘制条形图。主数据层的编码部分如下所示:

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

…在更新图表的 Vue 组件方法中,我有这个:

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

……为了完成所有这些,它称之为:

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

所有这些都按我的预期工作:将鼠标悬停在一个栏上,显示一个工具提示,单击该栏,然后导航到 SPA 中的另一条路线。

但是……工具提示会一直显示在屏幕上,除非您导航回图表页面并将鼠标悬停在条形上,在这种情况下,可以重新调用工具提示,然后在鼠标移出时将其关闭。

关于如何在 Vue 路径更改时使工具提示消失的任何想法?我尝试过使用view.tooltip(...) 方法,但怀疑这有点矫枉过正(我对默认工具提示很满意),甚至可能无法解决我的问题。

谢谢。

【问题讨论】:

    标签: javascript vue.js vega-lite vega


    【解决方案1】:

    因此,当路由更改时,触发工具提示的组件会被另一个组件替换。你的组件的 beforeDestroydestroyed 方法将被调用,你可以挂钩并清理任何应该清理的东西。

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

    如果一个组件清除了它在销毁时的所有副作用,您可以称其为行为良好的组件。副作用可能是已设置的超时和间隔、已打开的弹出窗口和对话框等等。如果您希望能够进行清理,则需要保存句柄,以便稍后关闭它们。我将演示一个表现良好的时钟组件:

    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);
        }
    
    });
    
    

    【讨论】:

    • 谢谢@joshua-angnoe……在我工作日结束时得到这个。明天会检查它是否有效。显然,在拆除 vega-embed 视图的细节方面还有一些工作要做,但这是一个非常好的起点,如果它能让我到达那里,我会接受它!
    • 嗨,RiqueW,感谢您的热情回复,这是一个在 vue + vue 路由器上下文中工作演示时钟组件的小提琴:jsfiddle.net/jangnoe/b8vq7Lwj/3
    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 2020-03-19
    • 2021-09-08
    相关资源
    最近更新 更多