react用ECharts实现组织架构图

找到ECharts中路径图。

然后开始爆改。

<div id={'org-' + name} style={{ width: '100%', height: 650, display: 'flex', justifyContent: 'center' }}></div>
// data的数据格式
interface ChartData {
  name: string;
  value: number;
  children: ChartData[];
}

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();
  }, []);

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值