antv g6收缩树绘图

本文档展示了如何在Vue项目中利用AntV G6库来绘制关系图。代码示例包括设置节点样式、监听节点点击事件、自定义布局以及响应窗口大小变化时的图表调整。通过获取服务数据并渲染到图表上,实现了动态更新和交互功能。

最近工作需要,用了antv g6绘图,参照了官网的例子

<template>
  <div id="container"></div>
</template>
<script>
import { defineComponent, onMounted, ref, watch } from "vue";
import { getKinship } from "@/services/study.js";
import G6 from "@antv/g6";
import { message } from "ant-design-vue";

export default defineComponent({
  props: {
    selectedId: {
      type: Object,
      default: null,
    },
  },
  setup(props) {
    watch(props, (newProps) => {
      if (newProps.selectedId) {
        chooseId.value = newProps.selectedId.id;
      }
    });
    const chooseId = ref(props.selectedId.id);
    const data = ref([]);
    const relations = ref([]);
    const graphs = ref(null);
    const container = ref();
    const resolution = ref(document.documentElement.clientWidth / 1920);

    const refresh = () => {
      getKinship(chooseId.value).then((res) => {
        if (res.data.success) {
          relations.value = res.data.data.relations[0];
          data.value = res.data.data.tables;
          graphs.value.data(relations.value);
          graphs.value.render();
          // graphs.value.translate(150, 100);
          graphs.value.fitCenter(true);
          graphs.value.zoom(resolution.value);
        } else {
          message.error(res.data.msg);
        }
      });
    };

    const init = () => {
      G6.registerNode("card-node", {
        draw: function drawShape(cfg, group) {
          const r = 2;
          const color = "#5B8FF9";
          const w = cfg.size[0];
          const h = cfg.size[1];
          const shape = group.addShape("rect", {
            attrs: {
              x: -w / 2,
              y: -h / 2,
              width: w, //200,
              height: 1.5 * h, // 60
              stroke: color,
              radius: r,
              fill: "#fff",
            },
            name: "main-box",
            draggable: true,
          });

          group.addShape("rect", {
            attrs: {
              x: -w / 2,
              y: -h / 2,
              width: w, //200,
              height: h / 2, // 60
              fill: color,
              radius: [r, r, 0, 0],
            },
            name: "title-box",
            draggable: true,
          });

          // title text
          group.addShape("text", {
            attrs: {
              textBaseline: "top",
              x: -w / 2 + 8,
              y: -h / 2 + 2,
              lineHeight: 20,
              text: cfg.name,
              fill: "#fff",
            },
            name: "title",
          });
          cfg.children &&
            group.addShape("marker", {
              attrs: {
                x: w / 2,
                y: 0,
                r: 6,
                cursor: "pointer",
                symbol: cfg.collapsed ? G6.Marker.expand : G6.Marker.collapse,
                stroke: "#666",
                lineWidth: 1,
                fill: "#fff",
              },
              name: "collapse-icon",
            });
          group.addShape("text", {
            attrs: {
              textBaseline: "top",
              x: -w / 2 + 8,
              y: -h / 2 + 24,
              lineHeight: 20,
              text: cfg.tips,
              fill: "rgba(0,0,0, 1)",
            },
            name: `description`,
          });
          return shape;
        },
        setState(name, value, item) {
          if (name === "collapsed") {
            const marker = item.get("group").find((ele) => ele.get("name") === "collapse-icon");
            const icon = value ? G6.Marker.expand : G6.Marker.collapse;
            marker.attr("symbol", icon);
          }
        },
      });

      container.value = document.getElementById("container");
      const width = container.value.scrollWidth;
      const height = container.value.scrollHeight || 500;
      graphs.value = new G6.TreeGraph({
        container: "container",
        width,
        height,
        modes: {
          default: ["drag-canvas"],
        },
        defaultNode: {
          type: "card-node",
          size: [225, 40],
        },
        defaultEdge: {
          type: "cubic-horizontal",
          style: {
            startArrow: true,
          },
        },
        layout: {
          type: "indented",
          direction: "RL",
          dropCap: false,
          indent: 275,
          getHeight: () => {
            return 80;
          },
        },
      });
      graphs.value.on("node:click", (e) => {
        if (e.target.get("name") === "collapse-icon") {
          e.item.getModel().collapsed = !e.item.getModel().collapsed;
          graphs.value.setItemState(e.item, "collapsed", e.item.getModel().collapsed);
          graphs.value.layout();
        }
      });
      if (typeof window !== "undefined")
        window.onresize = () => {
          if (!graphs.value || graphs.value.get("destroyed")) return;
          if (!container.value || !container.value.scrollWidth || !container.value.scrollHeight) return;
          graphs.value.changeSize(container.value.scrollWidth, container.value.scrollHeight);
        };
      refresh();
    };

    onMounted(() => {
      init();
    });
    return {
      data,
      chooseId,
      relations,
      refresh,
      graphs,
      container,
      resolution,
    };
  },
});
</script>
<style lang="less" scoped>
#container {
  width: 100%;
  height: 100%;
  padding: 20px;
}
</style>

在使用 AntV G6 构建形结构图时,获取形结构数据通常涉及对图数据模型的操作。AntV G6 提供了丰富的 API 来访问和操作图数据,包括获取整个图的节点和边数据、获取特定节点的子节点等。 可以通过以下方法获取形结构数据: - 使用 `graph.getNodes()` 方法获取图中所有节点对象。每个节点对象包含其 ID、位置、样式等信息,可以通过遍历这些节点提取形结构所需的层级关系和数据内容[^1]。 - 使用 `graph.getEdges()` 方法获取图中所有边对象。通过分析边的起点和终点,可以还原节点之间的父子关系,从而构建完整的形结构数据[^1]。 - 如果需要获取某个节点的直接子节点或父节点,可以使用 `node.getNeighbors(type)` 方法,其中 `type` 参数指定为 `'source'` 或 `'target'` 来获取目标节点的父节点或子节点。结合递归或迭代方法,可以构建完整的形结构。 以下是一个获取形结构数据的示例代码: ```javascript const nodes = graph.getNodes().map(node => node.getModel()); const edges = graph.getEdges().map(edge => edge.getModel()); // 构建形结构 function buildTree(nodes, edges) { const nodeMap = {}; nodes.forEach(node => { node.children = []; nodeMap[node.id] = node; }); edges.forEach(edge => { const { source, target } = edge; if (nodeMap[target] && nodeMap[source]) { nodeMap[target].children.push(nodeMap[source]); } }); // 查找根节点(没有父节点的节点) const roots = nodes.filter(node => !edges.some(edge => edge.target === node.id)); return roots; } const treeData = buildTree(nodes, edges); console.log(treeData); ``` 上述代码首先将节点和边数据转换为模型对象数组,并通过 `source` 和 `target` 字段构建父子关系,最终生成形结构数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值