关系图 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AntV G6 是一个基于 JavaScript 的流程绘制工具,可以帮助用户快速绘制各种类型的流程。使用 G6 绘制流程的基本步骤如下: 1. 安装 G6 库 在终端中使用 npm 命令安装 G6 库:npm install @antv/g6。 2. 创建画布 使用 G6 提供的 Graph 类创建一个画布,示例代码如下: ```javascript import G6 from '@antv/g6'; const graph = new G6.Graph({ container: 'container', width: 800, height: 500, }); ``` 其中,container 参数指定画布所在的容器,width 和 height 参数指定画布的宽度和高度。 3. 创建节点和边 使用 G6 提供的 API 创建节点和边,示例代码如下: ```javascript graph.addNode('node1', { x: 100, y: 100, size: 50, label: 'Node 1', }); graph.addNode('node2', { x: 300, y: 100, size: 50, label: 'Node 2', }); graph.addEdge('edge1', { source: 'node1', target: 'node2', }); ``` 其中,addNode 方法用于创建节点,addEdge 方法用于创建边。节点和边的属性可以通过第二个参数传入。 4. 渲染画布 使用 G6 提供的 render 方法渲染画布,示例代码如下: ```javascript graph.render(); ``` 5. 绘制节点和边的样式 使用 CSS 样式表为节点和边设置样式,示例代码如下: ```css .g6-node { fill: #fff; stroke: #666; stroke-width: 1px; } .g6-edge { stroke: #666; stroke-width: 1px; } ``` 以上就是使用 AntV G6 绘制流程的基本步骤。除了流程,G6 还支持绘制其他类型的表,例如关系、树形等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值