react用ECharts实现组织架构图

找到ECharts中路径图。

然后开始爆改。

html 复制代码
<div id={'org-' + name} style={{ width: '100%', height: 650, display: 'flex', justifyContent: 'center' }}></div>
TypeScript 复制代码
// data的数据格式
interface ChartData {
  name: string;
  value: number;
  children: ChartData[];
}
TypeScript 复制代码
const treeDepth = useRef(0);  
const initChart = () => {
    const demoId = document.getElementById('org-' + name);
    let myChart = echarts.getInstanceByDom(demoId);
    if (!myChart) {
      myChart = echarts.init(demoId);
      const options = {
        tooltip: {
          trigger: 'item',
          triggerOn: 'mousemove',
          formatter: function (params: any) {
            // 获取当前节点的所有父级名称
            const parentNames = params.treeAncestors.map((node: any, index: number) =>                 (index > 0 ? node.name : null)).filter((item: string) => item);
            // 返回父级名称,不包含值
            return parentNames.join('<br/> ');
          },
        },
        series: [
          {
            type: 'tree',
            id: 0,
            name: '工厂模型',
            layout: 'orthogonal', // 水平
            orient: 'TB', // 从上到下
            data,
            top: '10%',
            left: '8%',
            bottom: '22%',
            right: '20%',
            edgeShape: 'polyline',
            edgeForkPosition: '63%',
            initialTreeDepth: treeDepth.current,
            lineStyle: {
              // 每个节点所对应的文本标签的样式
              width: 4,
              color: '#40a9ff',
            },
            label: {
              // 每个节点所对应的文本标签的样式
              backgroundColor: '#096dd9',
              verticalAlign: 'middle',
              align: 'center',
              fontSize: 22,
              padding: [10, 10, 5, 10],
              color: '#fff',
            },
            leaves: {
              // 叶子节点的特殊配置
              label: {
                verticalAlign: 'middle',
                align: 'center',
              },
            },
            emphasis: {
              focus: 'descendant',
            },
            expandAndCollapse: true,
            animationDuration: 550,
            animationDurationUpdate: 750,
          },
        ],
      };
      myChart.setOption(options, true);
    }
  };


  useEffect(() => {
    initChart();
  }, []);
相关推荐
计算机魔术师5 分钟前
OpenAI不单独发o3了,Altman说GPT-5才是终局
前端
一座古城42 分钟前
Claude Code 架构源码解析:AI 应用开发的范式跃迁
前端·后端
K哥爬虫43 分钟前
【JS 逆向百例】Vaptcha V4 手势验证码逆向分析
前端·后端
用户921080262861 小时前
复盘 AI Coding 工作台的流式链路:从 Sender 输入到 SSE 分流,再到 BubbleList 和右侧预览
前端
了不起的小明1 小时前
SwiftMesh:本地 3D 模型查看器,加密交付 + 转盘录屏一站式搞定,开源免费
前端
默_笙1 小时前
👍 我的代码被 ESLint 抓包了:单引号、var、没分号——全被当场点名
前端·javascript
搞个锤子哟1 小时前
vue项目引入icon的问题
前端
树上有只程序猿1 小时前
当低代码遇上AI,一个开发者的真实感悟
前端
郭邯1 小时前
从零手写一个 UA 解析工具:我用 AI 辅助开发,踩了这些坑
前端
Heo1 小时前
怎么保证缓存和数据库一致性?
前端·后端·面试