ECharts中如何在旭日图上编程触发toggleSelect动作?
问题原因
ECharts 5.3.3 版本中,内置的toggleSelect动作仅适配了饼图(pie)、柱图、散点图等常规系列,旭日图(sunburst)系列没有内置对该动作的响应逻辑,因此直接派发该动作无法触发选中切换,属于版本原生能力缺失,和Angular环境、代码写法无关。
手动实现选中切换方案
旭日图本身原生支持节点选中状态,可通过组合内置的select/unselect动作手动实现切换效果,步骤如下:
- 前置配置:先给旭日图series配置添加
selectedMode属性,开启选中能力,否则所有选中相关操作均不生效:
series: [ { type: 'sunburst', selectedMode: 'single', // 单选场景传'single',多选场景传'multiple' // 保留你原有的旭日图其余配置 data: [] } ]
- 定位目标节点:不建议仅用
name属性定位节点,旭日图不同层级可能存在同名节点,最好通过遍历系列数据拿到目标节点的dataIndex做精准匹配 - 判断节点当前选中状态,派发对应反向动作实现切换
适配Angular 14环境的实现代码如下:
// labelName为你要操作的目标节点名称 const targetLabel = labelName; // 获取旭日图对应系列的model实例 const targetSeries = this.chart.getModel().getSeriesByIndex(0); // 遍历节点匹配目标项,若存在重名节点可增加depth(节点层级)判断做精准过滤 const allNodes = targetSeries.getData().getAllData(); const targetNode = allNodes.find((nodeItem: any) => nodeItem.name === targetLabel); if (targetNode) { const currentSelected = targetSeries.isSelected(targetNode.dataIndex); this.chart.dispatchAction({ type: currentSelected ? 'unselect' : 'select', seriesIndex: 0, dataIndex: targetNode.dataIndex }); }
注意:如果你的旭日图配置了
nodeClick: 'toggleSelect',上述手动实现的逻辑和点击节点的默认选中行为状态完全同步,不会出现状态冲突。
内容的提问来源于stack exchange,提问作者Adam Perea
相关产品推荐
相关产品推荐

