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

如何为ECharts桑基图的各层级添加标题标签?

ECharts桑基图层级标签实现方案

ECharts桑基图的series-sankey.levels配置确实没有提供直接添加整层级标题标签的选项,你可以通过以下两种方式实现需求:

方法一:使用graphic组件手动添加全局层级标签

通过echarts的graphic配置项,直接在对应层级的位置绘制固定样式的文本标签,完全自定义位置和样式:

option = {
  // 你的桑基图核心配置(nodes、links、levels等)
  graphic: [
    {
      type: 'text',
      left: '12%', // 根据桑基图布局调整水平位置
      top: 'center',
      style: {
        text: '第一层标签',
        fill: '#ff0000', // 红色文本
        fontSize: 14,
        fontWeight: 'bold'
      }
    },
    {
      type: 'text',
      left: '50%',
      top: 'center',
      style: {
        text: '第二层标签',
        fill: '#ff0000',
        fontSize: 14,
        fontWeight: 'bold'
      }
    }
    // 按层级依次添加更多标签
  ]
};

这种方式适合层级固定的场景,需要根据实际布局微调位置参数。

方法二:通过levels配置给节点批量设置标签样式

利用series-sankey.levels里的label配置,给对应层级的所有节点统一设置标签样式,模拟层级标题效果:

option = {
  series: [{
    type: 'sankey',
    levels: [
      {
        depth: 0, // 对应第0层(最左侧)
        label: {
          show: true,
          color: '#ff0000',
          position: 'left', // 统一对齐到层级外侧
          formatter: () => '第一层' // 所有该层级节点显示统一标签文本
        }
      },
      {
        depth: 1,
        label: {
          show: true,
          color: '#ff0000',
          position: 'right',
          formatter: () => '第二层'
        }
      }
    ],
    nodes: [/* 你的节点数据 */],
    links: [/* 你的链接数据 */]
  }]
};

这种方式的标签位置会随节点布局自适应,若同层级节点较多,可通过调整position(如left/right)避免标签重叠。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 06:02:54