关系图 antv G6

1、安装antv G6

npm install --save @antv/g6
# 或者
# pnpm install --save @antv/g6

2、引入antv G6

import G6 from "@antv/g6";

3、初始化G6工具

// 节点Tooltip插件
   const tooltip = new G6.Tooltip({
    offsetX: 10,
    offsetY: 20,
    getContent(e) {
      let name = e?.item?.getModel().name as string
      return name ? name : "";
    },
    itemTypes: ["node"],
    shouldBegin(e) {
      let states = e?.item?.getStates()
      if (states && states.indexOf("dark") >= 0) {
        return false;
      } else {
        return true;
      }
    },
  });
  // 连线Tooltip插件
  const edgeTooltip = new G6.Tooltip({
    offsetX: 10,
    offsetY: 20,
    getContent(e) {
      let prop = e?.item?.getModel().properties;
      if (prop instanceof Object) {
        let { role } = prop as { role?: string };
        return "关系:" + role;
      } else {
        return "关系:无";
      }
    },
    itemTypes: ["edge"],
    shouldBegin(e) {
      let states = e?.item?.getStates()
      if (states && states.indexOf("dark") >= 0) {
        return false;
      } else {
        return true;
      }
    }
  });

  // 操作
  const toolbar = new G6.ToolBar({
    zoomSensitivity: 120,
    minZoom: 120,
    maxZoom: 240,
    className: "g6-component-toolbar",
    handleClick: (code, graph) => {
      switch (code) {
        case "zoomIn":
          graph.zoomTo(graph.getZoom() * 1.1);
          break;
        case "zoomOut":
          graph.zoomTo(graph.getZoom() * 0.9);
          break;
        default:
          toolbar.handleDefaultOperator(code);
          break;
      }
    },
  });

4、初始化G6对象

html代码

<div id="mountNode"></div>

js代码

graph = new G6.Graph({
    container: "mountNode",
    width: window.innerWidth,
    height: window.innerHeight,
    plugins: initPlugins(), // 配置 Tooltip 插件
    // fitView: true,
    layout: {
      type: "forceAtlas2", // 建议(force2, forceAtlas2),值:random, radial, mds, circular, fruchterman, force, gForce, force2, forceAtlas2, dagre, concentric, grid
    },
    modes: {
      default: ["drag-canvas", "drag-node", "zoom-canvas"],
    },
    defaultNode: {
      // 节点配置
      size: [40, 40],
      style: {
        fill: "steelblue", // 节点填充色
        // stroke: '#666', // 节点描边色
        opacity: 1, // 设置绘图的当前 alpha 或透明值
        lineWidth: 2, // 节点描边粗细
      },
      labelCfg: {
        // 节点上的标签文本配置
        style: {
          // 节点上的标签文本样式配置
          fill: "#fff", // 节点标签文字颜色
          opacity: 1,
        },
      },
    },
    defaultEdge: {
      // 连线配置
      type: "quadratic",
      size: 1,
      style: {
        stroke: "#e2e2e2",
        lineAppendWidth: 2,
        opacity: 1,
        endArrow: {
          // 连线箭头
          path: "M 0,0 L 4,2 L 3,0 L 4,-2 Z",
          fill: "#e2e2e2",
        },
      },
      labelCfg: {
        // 节点上的标签文本配置
        style: {
          // 节点上的标签文本样式配置
          opacity: 1,
        },
      },
    },
    nodeStateStyles: {
      // 不同状态节点样式
      highlight: {
        opacity: 1,
        fill: "steelblue", // 节点填充色
      },
      dark: {
        opacity: 0.2,
        "text-shape": {
          opacity: 0,
        },
      },
    },
    edgeStateStyles: {
      // 不同状态连线样式
      highlight: {
        stroke: "#999",
      },
      dark: {
        opacity: 0.2,
        "text-shape": {
          opacity: 0,
        },
      },
    },
  });

  // 事件(点击、移动)
  // graph.on('node:mouseenter', lightNode);
  // graph.on('node:mouseleave', clearAllStats);
  graph.on("node:click", lightNode);
  graph.on("canvas:click", clearAllStats);

  //处理数据并渲染
  graph.clear();
  graph.data({
    nodes: data.nodes.map(function (node) {
      if (node.name && typeof node.name == "string") {
        node.label = node.name.length > 3 ? node.name.substring(0, 2) + "..." : node.name;
      }
      return Object.assign({}, node)
    }),
    edges: data.edges.map(function (edge, i) {
      edge.id = "edge" + i;
      edge.curveOffset = computeCurveOffset(edge);
      if (edge.properties instanceof Object) {
        let { role } = edge.properties as { role?: string }
        edge.label = role ? (role.length > 2 ? role.substring(0, 2) + "..." : "") : ""
      }
      return Object.assign({}, edge);
    }),
  });
  graph.render();

5、其他链接

官方文档:antv G6

源码地址: vue3+typescript+element plus实现

效果演示:http://101.43.32.67:8081

### 使用 AntV G6 绘制流程图 AntV G6 是一款强大的图形可视化引擎,支持多种类型的图表绘制,其中包括流程图。以下是创建一个简单流程图的指南和示例代码。 #### 初始化图实例 为了使用 AntV G6 创建流程图,首先需要初始化 `G6.Graph` 实例,并指定其挂载的 DOM 容器以及宽高属性[^1]: ```javascript const graph = new G6.Graph({ container: 'mountNode', // 挂载到 HTML 中 id="mountNode" 的容器 width: 800, // 设置画布宽度 height: 500 // 设置画布高度 }); ``` #### 配置节点与边数据 流程图的核心在于定义节点(nodes)和连接这些节点的边(edges)。可以通过 JSON 数据结构来描述它们的关系。以下是一个简单的配置例子: ```javascript const data = { nodes: [ { id: 'node1', x: 100, y: 200, label: 'Start' }, { id: 'node2', x: 300, y: 200, label: 'Process A' }, { id: 'node3', x: 500, y: 200, label: 'End' } ], edges: [ { source: 'node1', target: 'node2' }, { source: 'node2', target: 'node3' } ] }; ``` 在此配置中: - 节点通过 `id`, `x`, 和 `y` 属性定位并带有标签。 - 边由 `source` 和 `target` 属性决定起始和终止节点。 #### 渲染流程图 完成数据准备后,调用 `graph.data()` 方法加载数据,并执行 `render()` 来呈现流程图[^1]: ```javascript graph.data(data); graph.render(); ``` 完整的代码如下所示: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flowchart with AntV G6</title> <!-- 引入 G6 库 --> <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.g6-4.7.9/dist/g6.min.js"></script> </head> <body> <div id="mountNode"></div> <script type="text/javascript"> const graph = new G6.Graph({ container: 'mountNode', width: 800, height: 500 }); const data = { nodes: [ { id: 'node1', x: 100, y: 200, label: 'Start' }, { id: 'node2', x: 300, y: 200, label: 'Process A' }, { id: 'node3', x: 500, y: 200, label: 'End' } ], edges: [ { source: 'node1', target: 'node2' }, { source: 'node2', target: 'node3' } ] }; graph.data(data); graph.render(); </script> </body> </html> ``` 此代码片段展示了如何利用 AntV G6 构建基本的流程图。可以根据实际需求调整样式、布局以及其他高级功能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值