G6框架Dagre流程图第三个自左向右的 Dagre 上对齐改造,对齐结点和边添加样式,并添加修改节点和展示结点详细信息交互

​​在这里插入图片描述

标题修改具体项

  • 设置结点的样式
  • 设置边的样式
  • 添加修改结点名称功能
  • 添加展示结点详细信息功能

参考链接

  • 基本图形:https://g6.antv.vision/zh/examples/net/dagreFlow#lrDagreUL
  • 展示结点详细信息功能:https://g6.antv.vision/zh/examples/tool/tooltip#tooltipClick
  • 修改结点名称功能:https://g6.antv.vision/zh/examples/interaction/label#update

完整代码(直接建一个html文件拷进去就能用)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Tutorial Demo</title>
  </head>
  <body>
    <!-- 图的画布容器 -->
    <div id="container"></div>

    <!--  引入 G6  -->
    <script src="https://gw.alipayobjects.com/os/lib/antv/g6/4.3.11/dist/g6.min.js"></script>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

    <script>
      // 定义数据源
      const data = {
        // 点集
        nodes: [
          {
            id: '0',
            label: '硅材料',
            style: {
              lineDash: [6],
            },
            description: ['text1','text2','text3','text4',],
          },
          {
            id: '1',
            label: '光伏级单晶硅片',
            description: ['text1','text2','text3','text4',],
          },
          {
            id: '2',
            label: '组件',
            description: ['text1','text2','text3','text4',],
          },
          {
            id: '3',
            label: '半导体硅片',
            style: {
              lineDash: [6],
            },
            description: ['text1','text2','text3','text4',],
          },
          {
            id: '4',
            label: 'IGBT芯片',
            style: {
              fill: '#00FF00', //填充颜色
            },
          },
          {
            id: '5',
            label: 'IGBT模块1700V及以上',
            style: {
              fill: '#00FF00', //填充颜色
            },
          },
          {
            id: '6',
            label: 'IGBT模块1700V及以下',
          },
          {
            id: '7',
            label: '变流器',
          },
          {
            id: '8',
            label: '换流阀',
          },
          {
            id: '9',
            label: '中车',
          },
          {
            id: '10',
            label: '轨道交通',
            type: 'rect',
          },
          {
            id: '11',
            label: '智能电网',
            type: 'rect',
          },
          {
            id: '12',
            label: '逆变器',
            style: {
              lineDash: [6],
            },
          },
          {
            id: '13',
            label: '电机控制器',
          },
          {
            id: '14',
            label: '新能源汽车',
            type: 'rect',
          },
        ],
        // 边集
        edges: [
          {
            source: '0',
            target: '1',
            description: ['text1','text2','text3','text4',],
          },
          {
            source: '1',
            target: '2',
            description: ['text1','text2','text3','text4',],
          },
          {
            source: '0',
            target: '3',
            style: {
              lineDash: [6],
            },
            description: ['text1','text2','text3','text4',],
          },
          {
            source: '3',
            target: '4',
            style: {
              lineDash: [6],
            },
          },
          {
            source: '4',
            target: '5',
            style: {
              lineDash: [3],
            },
          },
          {
            source: '4',
            target: '6',
          },
          {
            source: '5',
            target: '7',
            style: {
              lineDash: [6],
            },
          },
          {
            source: '5',
            target: '8',
            style: {
              lineDash: [6],
            },
          },
          {
            source: '7',
            target: '9',
          },
          {
            source: '9',
            target: '10',
          },
          {
            source: '8',
            target: '11',
          },
          {
            source: '6',
            target: '12',
            style: {
              lineDash: [6],
            },
          },
          {
            source: '12',
            target: '13',
            style: {
              lineDash: [6],
            },
          },
          {
            source: '13',
            target: '14',
          },
        ],
      };

      // 定义提示文本框--结点详细信息描述
      const tooltip = new G6.Tooltip({
        offsetX: 10,
        offsetY: 10,
        // v4.2.1 起支持配置 trigger,click 代表点击后出现 tooltip。默认为 mouseenter
        trigger: 'dbclick',
        // the types of items that allow the tooltip show up
        // 允许出现 tooltip 的 item 类型
        itemTypes: ['node', 'edge'],
        // custom the tooltip's content
        // 自定义 tooltip 内容
        getContent: (e) => {
          const outDiv = document.createElement('div');
          outDiv.style.width = 'fit-content';
          //outDiv.style.padding = '0px 0px 20px 0px';
          outDiv.innerHTML = `
            <h4>Custom Content</h4>
            <ul>
              <li>Label: ${e.item.getModel().description[0]}</li>
              <li>Label: ${e.item.getModel().description[1]}</li>
              <li>Label: ${e.item.getModel().description[2]}</li>
              <li>Label: ${e.item.getModel().description[3]}</li>
            </ul>`;
          return outDiv;
        },
      });

      // 定义修改文本框--结点名称修改
      const tooltip1 = new G6.Tooltip({
        offsetX: 10,
        offsetY: 10,
        // v4.2.1 起支持配置 trigger,click 代表点击后出现 tooltip。默认为 mouseenter
        trigger: 'click',
        // the types of items that allow the tooltip show up
        // 允许出现 tooltip 的 item 类型
        itemTypes: ['node', 'edge'],
        // custom the tooltip's content
        // 自定义 tooltip 内容
        getContent: (e) => {
          const outDiv = document.createElement('div');
          outDiv.style.width = 'fit-content';
          //outDiv.style.padding = '0px 0px 20px 0px';
          outDiv.innerHTML = `
            <input type="text" id="newLabel" value="${e.item.getModel().label}"/>
            <button type="submit" id="btn_submit" οnclick="btnAction()">确定</button>`;
          return outDiv;
        },
      });

      const container = document.getElementById('container');
      const width = container.scrollWidth;
      const height = container.scrollHeight || 500;
      // 创建 G6 图实例
      const graph = new G6.Graph({
        container: 'container', // 指定图画布的容器 id,与第 9 行的容器对应
        // 画布宽高
        // width: 800,
        // height: 500,
        width,
        height,
        fitView: true,  //自适应布局
        plugins: [tooltip,tooltip1],   //两个文本插件
        modes: {
          default: ['drag-canvas', 'drag-node'],
          edit: ['click-select'],
        },
        layout: {
          type: 'dagre',
          rankdir: 'LR',
          align: 'UL',
          controlPoints: true,
          nodesepFunc: () => 1,
          ranksepFunc: () => 1,
        },
        defaultNode: {
          size: [120, 60],
          type: 'ellipse',
          style: {
            lineWidth: 0.5,
            stroke: '#000000', //边框颜色
            fill: '#FFFFFF', //填充颜色
          },
        },
        defaultEdge: {
          type: 'polyline',
          size: 1,
          color: '#e2e2e2',
          style: {
            endArrow: {
              path: 'M 0,0 L 8,4 L 8,-4 Z',
              fill: '#e2e2e2',
            },
            radius: 20,
          },
        },
      });
      // 读取数据
      graph.data(data);
      // 渲染图
      graph.render();

      //针对结点和边的详细信息提示
      graph.on('node:mouseenter', (e) => {
        graph.setItemState(e.item, 'active', true);
      });
      graph.on('node:mouseleave', (e) => {
        graph.setItemState(e.item, 'active', false);
      });
      graph.on('edge:mouseenter', (e) => {
        graph.setItemState(e.item, 'active', true);
      });
      graph.on('edge:mouseleave', (e) => {
        graph.setItemState(e.item, 'active', false);
      });

      //针对结点的名称修改
      var cur_e;
      graph.on('node:click', (e) => {
        cur_e = e;
      });

      //针对结点的名称修改确定按钮调用函数
      function btnAction(){
        console.log($("#newLabel").val());
        console.log(cur_e);
        graph.updateItem(cur_e.item, {
          label: $("#newLabel").val(),
        });
      }
      
      if (typeof window !== 'undefined')
        window.onresize = () => {
          if (!graph || graph.get('destroyed')) return;
          if (!container || !container.scrollWidth || !container.scrollHeight) return;
          graph.changeSize(container.scrollWidth, container.scrollHeight);
      };
    </script>
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。针对你的问题,我可以给出以下的解答: 首先,我们需要明确一下什么是 Ant G6 和 Combo。Ant G6 是一个基于 G6 开发的图可视化引擎,而 Combo 则是 G6 中的一种元素类型,它可以将多个节点组合成一个整体。 在 Ant G6 中,我们可以通过监听节点的点击事件来为节点添加 Combo。具体的实现步骤如下: 1. 在初始化 G6 图实例时,需要开启 Combo 功能: ```javascript const graph = new G6.Graph({ container: 'container', ... enabledCombo: true, ... }); ``` 2. 监听节点的 click 事件: ```javascript graph.on('node:click', (evt) => { const { item } = evt; const model = item.getModel(); const comboId = `combo-${model.id}`; // 判断该节点是否已经被包含在某个 Combo 中 const comboItem = graph.findById(comboId); if (comboItem) { // 如果已经被包含,则将节点从 Combo 中移除 graph.updateCombo(comboId, [item]); } else { // 如果未被包含,则创建一个新的 Combo 并将节点加入其中 graph.createCombo({ id: comboId, padding: 10, children: [item], }); } }); ``` 在上面的代码中,我们首先获取到被点击的节点的 model,然后根据节点的 id 创建一个对应的 Combo id。接着,我们在图中查找是否已经存在该 Combo,如果存在,则将该节点从 Combo 中移除;如果不存在,则创建一个新的 Combo 并将该节点加入其中。 以上就是通过点击事件为节点添加 Combo 的具体步骤。希望这个回答能够帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值