Antv X6布局layout的使用

相信很多用过Antv G6或者echarts关系图等的大佬都知道,关于图形的layout布局都是已经内置好的,只需要修改layout就行了,但是,,,,,在Antv X6这块就行不通了。。。。。。。。。。。

在文档中翻到了layout的使用,需要进行安装,所以感觉用起来就没有别的那么方便了。

文档地址

下面我简单介绍一下安装及使用的过程吧:

(1)X6中layout的安装与引用

        1.1在项目中使用NPM包引入

npm install @antv/layout --save

        在需要layout的X6的组件或JS文件中导入:

import { GridLayout } from '@antv/layout' // umd模式下, const { GridLayout } = window.layout

导入后 { GridLayout  }会报错,别急,跟 Graph一样, 下面代码中需要创建 GridLayout 对象,才不会报错
         1.2 如果是直接通过 script 标签引入,可以使用下面两个 CDN 中的任何一个:
   (2)demo示例
    代码布局:

 主要代码:

this.gridLayout = new GridLayout({
  type: 'grid',
  width: 600,
  height: 400,
  center: [300, 200],
  rows: 4,
  cols: 4
})
this.newModel = this.gridLayout.layout(this.data)
this.graph.fromJSON(this.newModel)

完整代码:

<template>
  <div id="container"></div>
</template>


<script>
import { Graph } from '@antv/x6'
import { GridLayout } from '@antv/layout' // umd模式下, const { GridLayout } = window.layout
export default {
  name: 'X6',
  mounted () {
    this.initComponent()
  },
  data () {
    return {
      data: {}, // X6 数据
      graph: undefined, // new X6
      gridLayout: undefined, // new GridLayout
      newModel: undefined
    }
  },
  methods: {
    initComponent () {
      this.data = {
        nodes: [// 节点
          {
            id: 'node1', // String,可选,节点的唯一标识
            width: 80, // Number,可选,节点大小的 width 值
            height: 40, // Number,可选,节点大小的 height 值
            label: 'hello' // String,节点标签
          },
          {
            id: 'node2', // String,节点的唯一标识
            width: 80, // Number,可选,节点大小的 width 值
            height: 40, // Number,可选,节点大小的 height 值
            label: 'world' // String,节点标签
          }
        ],
        edges: [// 边
          {
            source: 'node1', // String,必须,起始节点 id
            target: 'node2' // String,必须,目标节点 id
          }
        ]
      }
      this.graph = new Graph({
        container: document.getElementById('container'),
        width: 800,
        height: 600
      })
      this.gridLayout = new GridLayout({
        type: 'grid',
        width: 600,
        height: 400,
        center: [300, 200],
        rows: 4,
        cols: 4
      })
      this.newModel = this.gridLayout.layout(this.data)
      this.graph.fromJSON(this.newModel)
    }


  }
}
</script>


<style scoped>


</style>

效果如下:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
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` 实例并添加了两个节点和一条边。 节点的属性包括位置、大小、形状、标签和样式等,可以根据具体需要进行调整。边的属性包括起点、终点和样式等。 在实际使用中,可以根据业务需求和数据结构进行更复杂的图形绘制和交互操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值