Antv 使用

Antv 使用

在线文档

https://antv.gitee.io/zh

安装

执行命令 npm install @antv/x6 --save
在文件中引入 x6 import { Graph } from '@antv/x6';

初始化

步骤:

    1. 创建容器
    1. 准备数据
    1. 渲染画布
<template>
  <div class="antv_components">
// 1.  创建容器
    <div id="container"></div>
  </div>
</template>

<script>
import { Graph } from "@antv/x6";
export default {
  name: "antv",
  mounted() {
    this.initGraph();
  },
  data() {
    return {
        // 2. 准备数据
      antViewData: {
        // 节点
        nodes: [
          {
            id: "node1", // String,可选,节点的唯一标识
            x: 40, // Number,必选,节点位置的 x 值
            y: 40, // Number,必选,节点位置的 y 值
            width: 80, // Number,可选,节点大小的 width 值
            height: 40, // Number,可选,节点大小的 height 值
            label: "hello", // String,节点标签
          },
          {
            id: "node2", // String,节点的唯一标识
            x: 160, // Number,必选,节点位置的 x 值
            y: 180, // Number,必选,节点位置的 y 值
            width: 80, // Number,可选,节点大小的 width 值
            height: 40, // Number,可选,节点大小的 height 值
            label: "world", // String,节点标签
            shape: "ellipse",
          },
        ],
        // 边
        edges: [
          {
            source: "node1", // String,必须,起始节点 id
            target: "node2", // String,必须,目标节点 id
          },
        ],
      },
    };
  },
  methods: {
    initGraph() {
        // 3. 渲染画布
      const graph = new Graph({
        container: document.getElementById("container"),
        width: 800,
        height: 600,
        background: {
          color: "tomato",
        },
        grid: {
          size: 20,
          visible: true,
        },
      });
      graph.fromJSON(this.antViewData);
    },
  },
};
</script>

<style lang='less' scope>
.antv_components {
  color: tomato;
  div {
    margin: 0 auto;
  }
}
</style>
AntV X6 是一个基于 React 的可视化库,它提供了丰富的图形绘制、交互和布局功能,可以用于绘制各种类型的图表、流程图、组织结构图等。 使用 AntV X6 绘制图形需要引入相关的组件和样式,并创建一个画布元素。以下是一个简单示例: ```jsx import React, { useEffect, useRef } from 'react'; import { Graph } from '@antv/x6'; const MyGraph = () => { const container = useRef(null); useEffect(() => { const graph = new Graph({ container: container.current, width: 600, height: 400, }); const rect = graph.addNode({ x: 100, y: 100, width: 80, height: 40, label: 'Hello', style: { fill: '#eee', stroke: '#333', strokeWidth: 1, }, }); const circle = graph.addNode({ x: 300, y: 200, width: 40, height: 40, shape: 'ellipse', label: 'World', style: { fill: '#ccc', stroke: '#666', strokeWidth: 2, }, }); graph.addEdge({ source: rect, target: circle, attrs: { line: { stroke: '#333', strokeWidth: 1, }, }, }); }, []); return <div ref={container} />; }; export default MyGraph; ``` 在上面的示例中,我们创建了一个名为 `MyGraph` 的组件,使用 `useRef` 创建了一个画布元素的引用 `container`,并在 `useEffect` 中创建了一个 `Graph` 实例并添加了两个节点和一条边。 节点的属性包括位置、大小、形状、标签和样式等,可以根据具体需要进行调整。边的属性包括起点、终点和样式等。 在实际使用中,可以根据业务需求和数据结构进行更复杂的图形绘制和交互操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值