echarts手动控制tooltip和axisPointer的展示隐藏

在移动H5使用了 ECharts 开发可视化图表,遇到一个特殊需求:折线趋势图中需要默认展示最后一个坐标轴对应的数据,也就是主动触发 tooltip 的展示。

一、组件解释说明

  1. axisPointer 是坐标轴指示器,上图 红框 中展示部分。
  2. tooltip 是提示框组件,上图 蓝框 中展示部分。

二、ECharts API 

1.echartsInstance.dispatchAction: 触发图表行为,例如图例开关 legendToggleSelect, 数据区域缩放 dataZoom,显示提示框 showTip 等等
2.action.tooltip: 提示框组件相关的行为。
showTip: 显示提示框。
hideTip: 隐藏提示框。
3.action.axisPointer: 坐标轴指示器配置项相关的行为。
updateAxisPointer: 更新坐标轴指示器。

 API使用说明:

// showTip 展示 tooltip
dispatchAction({ 
    type: 'showTip',
    // 系列的 index,在 tooltip 的 trigger 为 axis 的时候可选。
    seriesIndex?: number,
    // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项
    dataIndex?: number,
    // 可选,数据项名称,在有 dataIndex 的时候忽略
    name?: string, 
    // 本次显示 tooltip 的位置。只在本次 action 中生效。
    // 缺省则使用 option 中定义的 tooltip 位置。
    position: number[] | string | Function,
});

// hideTip 隐藏 tooltip
dispatchAction({ 
    type: 'hideTip'
});

三、使用示例

在 Vue 项目中使用:

export default {
  data() {
    return {
      lastIndex: 0,
    }
  },
  methods: {
    // 主动触发展示 tooltip 提示框
    showTip() {
      this.chartInstance.dispatchAction({ 
        type: 'showTip',
        seriesIndex: 0,
        dataIndex: this.lastIndex,
      });
    },
    // 重新渲染后隐藏 tooltip 和 axisPointer
    hide() {
      // 隐藏 tooltip
      this.chartInstance.dispatchAction({ type: 'hideTip' });
      // 隐藏 axisPointer
      this.chartInstance.dispatchAction({
        type: 'updateAxisPointer',
        currTrigger: 'leave',
      });
    },
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值