antv x6 的画布大小设置
在实例化 Graph 对象的时候,可以通过设置 width 和 height 固定画布大小,如果不设置,就会以画布容器大小初始化画布。
在项目实践中,经常会遇到下面两种场景:
- 画布容器还没有渲染完成(此时尺寸为 0),就实例化画布对象,导致画布元素显示异常。
- 页面的 resize 导致画布容器大小改变,导致画布元素显示异常。
我们可以使用 autoResize 配置来解决上述问题。
<!-- 注意,使用 autoResize 配置时,需要在画布容器外面再套一层宽高都是 100% 的外层容器,在外层容器上监听尺寸改变,当外层容器大小改变时,画布自动重新计算宽高以及元素位置。 -->
<div style="width:100%; height:100%">
<div id="container"></div>
</div>
const graph = new Graph({
container: document.getElementById('container'),
autoResize: true,
})
在下面的示例中,我们可以用鼠标拖动灰色的区域,来改变容器大小,可以通过背景颜色看到,三块画布的大小是自适应的。
index.tsx
/* eslint-disable no-new */
import React from 'react'
import { Graph } from '@antv/x6'
import { SplitBox } from '@antv/x6-react-components'
import '@antv/x6-react-components/es/split-box/style/index.css'
import './index.less'
export default class Example extends React.Component {
private container1: HTMLDivElement
private container2: HTMLDivElement
private container3: HTMLDivElement
componentDidMount() {
new Graph({
container: this.container1,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
new Graph({
container: this.container2,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
new Graph({
container: this.container3,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
}
refContainer1 = (container: HTMLDivElement) => {
this.container1 = container
}
refContainer2 = (container: HTMLDivElement) => {
this.container2 = container
}
refContainer3 = (container: HTMLDivElement) => {
this.container3 = container
}
render() {
return (
<div className="auto-resize-app">
<SplitBox split="horizontal">
<div className="full">
<div ref={this.refContainer1} />
</div>
<SplitBox split="vertical">
<div className="full">
<div ref={this.refContainer2} />
</div>
<div className="full">
<div ref={this.refContainer3} />
</div>
</SplitBox>
</SplitBox>
</div>
)
}
}
index.less
/* eslint-disable no-new */
import React from 'react'
import { Graph } from '@antv/x6'
import { SplitBox } from '@antv/x6-react-components'
import '@antv/x6-react-components/es/split-box/style/index.css'
import './index.less'
export default class Example extends React.Component {
private container1: HTMLDivElement
private container2: HTMLDivElement
private container3: HTMLDivElement
componentDidMount() {
new Graph({
container: this.container1,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
new Graph({
container: this.container2,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
new Graph({
container: this.container3,
background: {
color: '#F2F7FA',
},
autoResize: true,
})
}
refContainer1 = (container: HTMLDivElement) => {
this.container1 = container
}
refContainer2 = (container: HTMLDivElement) => {
this.container2 = container
}
refContainer3 = (container: HTMLDivElement) => {
this.container3 = container
}
render() {
return (
<div className="auto-resize-app">
<SplitBox split="horizontal">
<div className="full">
<div ref={this.refContainer1} />
</div>
<SplitBox split="vertical">
<div className="full">
<div ref={this.refContainer2} />
</div>
<div className="full">
<div ref={this.refContainer3} />
</div>
</SplitBox>
</SplitBox>
</div>
)
}
}
vue3自适应大小案例
<script setup>
import {Graph} from '@antv/x6'
import {onMounted} from "vue";
import {Snapline} from "@antv/x6-plugin-snapline";
const data = {
nodes: [
{
id: 'node1',
shape: 'rect',
x: 40,
y: 40,
width: 100,
height: 40,
label: 'hello',
attrs: {
// body 是选择器名称,选中的是 rect 元素
body: {
stroke: '#8f8f8f',
strokeWidth: 1,
fill: '#fff',
rx: 6,
ry: 6,
},
},
},
{
id: 'node2',
shape: 'rect',
x: 160,
y: 180,
width: 100,
height: 40,
label: 'world',
attrs: {
body: {
stroke: '#8f8f8f',
strokeWidth: 1,
fill: '#fff',
rx: 6,
ry: 6,
},
},
},
],
// 边:连接节点
edges: [
{
shape: 'edge',
source: 'node1', // 开始节点
target: 'node2', // 目标节点,会建立从开始节点到目标节点的连线
label: 'x6',
attrs: {
// line 是选择器名称,选中的边的 path 元素
line: {
stroke: '#8f8f8f',
strokeWidth: 1,
},
},
},
],
}
onMounted(() => {
const graph = new Graph({
container: document.getElementById('container'),
autoResize: true,
width: 800,
height: 600,
background: {
color: '#F2F7FA',
},
})
graph.fromJSON(data) // 渲染元素
graph.centerContent() // 居中显示
// 使用插件
graph.use(
// 自动对齐
new Snapline({
enabled: true,
}),
)
// 数据导出
console.log(graph.toJSON())
})
</script>
<template>
<div style="width:100%; height:100%">
<div id="container"></div>
</div>
</template>
<style scoped>
</style>