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

Logo

前往低代码交流专区

更多推荐