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();
  }, []);
相关推荐
鱼人19 分钟前
Web Components:未来的前端组件化标准?
前端
果汁华23 分钟前
Chrome DevTools MCP:让 AI 编码助手拥有浏览器调试超能力
前端·人工智能·chrome devtools
二月龙32 分钟前
移动端适配必杀技:Viewport与响应式布局全解
前端
大萝卜呼呼32 分钟前
Next.js第十七课 - 部署
前端·typescript·next.js
只会写Bug1 小时前
后台管理项目中关于新增、编辑弹框使用的另一种展示形式
前端·vue.js
weixin199701080161 小时前
《废旧物资商品详情页前端性能优化实战》
前端·性能优化
用户52709648744901 小时前
Vite 开发代理里的 `ws` 是什么,什么时候该开
前端
冰水不凉1 小时前
robot_localization实现imu和odom融合
前端·slam
M ? A1 小时前
Vue v-bind 转 React:VuReact 怎么处理?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
军军君012 小时前
数字孪生监控大屏实战模板:政务服务大数据
前端·javascript·vue.js·typescript·前端框架·echarts·less