Angular中能否编程触发ECharts旭日图的悬停与点击效果?
结论
完全支持。Angular 框架下使用 ECharts 旭日图时,可以直接在 TypeScript 自定义函数里通过 ECharts 实例的 dispatchAction 方法,主动触发指定节点的悬停、点击交互效果,你给出的实现思路没问题,只是 action 类型参数写错了。
- 悬停效果:ECharts 没有提供
hover类型的 action,触发节点悬停高亮对应的合法 type 是highlight,取消悬停高亮对应downplay,需要同步显示节点 tooltip 的话可以搭配showTip类型 action 使用。 - 点击效果:没有
click类型的 action,模拟节点点击选中效果对应的 type 是select,取消选中对应unselect,使用前需要确保旭日图系列配置里开启了selectedMode。
修正后的可直接运行的代码如下:
customFunction(labelName: string, actionType: 'hover' | 'click'): void { // 先校验实例是否初始化完成,避免空对象报错 if (!this.chart) return; const baseConfig = { seriesIndex: 0, // 旭日图对应的系列索引,多图表场景自行核对 name: labelName, // 匹配目标节点的name字段 }; if (actionType === 'hover') { // 触发节点高亮 this.chart.dispatchAction({ type: 'highlight', ...baseConfig }); // 可选:同步展示该节点的提示框 this.chart.dispatchAction({ type: 'showTip', ...baseConfig }); } if (actionType === 'click') { // 触发节点选中效果 this.chart.dispatchAction({ type: 'select', ...baseConfig }); } } // 配套的重置节点状态方法 resetNodeState(labelName: string): void { if (!this.chart) return; this.chart.dispatchAction({ type: 'downplay', seriesIndex: 0, name: labelName }); this.chart.dispatchAction({ type: 'hideTip' }); this.chart.dispatchAction({ type: 'unselect', seriesIndex: 0, name: labelName }); }
注意:如果旭日图里有多个重名节点,仅传
name字段可能匹配到错误节点,这种场景建议额外传入目标节点的dataIndex做精准定位。调用自定义触发方法的时机要放在图表实例初始化完成之后,比如ngAfterViewInit生命周期、ECharts 初始化 ready 回调触发后再调用,否则会报实例不存在的错误。
内容的提问来源于stack exchange,提问作者Adam Perea
相关产品推荐
相关产品推荐

