You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AmCharts堆叠柱状图悬停单柱时展示所有同类别系列tooltip的实现方法

amCharts 堆叠柱状图同系列tooltip同步显示解决方案

该功能完全可实现,核心实现逻辑是监听柱体的悬停事件,手动控制同系列所有柱体的tooltip显隐,具体实现方案如下:

  • 给每个系列的柱体模板添加悬停、移出事件监听,接管tooltip的显隐逻辑
  • 悬停触发时,遍历当前所属系列的所有数据项,统一调用tooltip显示方法
  • 移出触发时,遍历当前所属系列的所有数据项,统一调用tooltip隐藏方法

你可以在原有代码的系列创建完成后,补充如下代码即可生效:

// 遍历所有系列,统一添加事件监听
chart.series.each(function(series) {
  // 给柱体模板添加悬停事件
  series.columns.template.events.on("over", function(event) {
    const targetSeries = event.target.dataItem.component;
    // 遍历当前系列的所有数据项,展示全部tooltip
    targetSeries.dataItems.each(function(dataItem) {
      if (dataItem.column) {
        dataItem.column.showTooltip();
      }
    });
  });
  
  // 给柱体模板添加移出事件
  series.columns.template.events.on("out", function(event) {
    const targetSeries = event.target.dataItem.component;
    // 遍历当前系列的所有数据项,隐藏全部tooltip
    targetSeries.dataItems.each(function(dataItem) {
      if (dataItem.column) {
        dataItem.column.hideTooltip();
      }
    });
  });
});

// 可选:关闭默认光标悬停同分类展示所有系列tooltip的行为,避免冲突
chart.cursor.behavior = "none";

内容的提问来源于stack exchange,提问作者hesa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 08:48:03