Skip to content

Commit d75ac56

Browse files
author
heshenglei
committed
docs: 优化【基础-画布】文档
1 parent 10c25b0 commit d75ac56

1 file changed

Lines changed: 239 additions & 31 deletions

File tree

site/docs/tutorial/basic/graph.zh.md

Lines changed: 239 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,277 @@ redirect_from:
66
- /zh/docs/tutorial
77
- /zh/docs/tutorial/basic
88
---
9+
## 简介
10+
X6 中的 **画布(Graph)** 是一个**容器**,用来渲染和管理 节点(Node)、边(Edge) 以及其他图形元素等等内容,画布本身也包含很多功能(缩放、网格、平移等等)。
911

10-
:::info{title=在本章节中主要介绍画布相关的知识,通过阅读你可以了解到}
12+
在 X6 中,画布是通过 `Graph` 类来实例化的,同时可以指定[一系列选项](/api/graph/graph)来配置画布:
1113

12-
- 画布大小如何做到自适应
13-
- 通过设置背景和网格优化画布样式
14-
- 怎样才能使画布可拖拽可缩放
15-
- 常用的画布尺寸和位置操作 API
14+
```ts
15+
import { Graph } from '@antv/x6'
1616

17-
:::
17+
const graph = new Graph({
18+
// 指定画布挂载的容器
19+
container: document.getElementById('container'),
20+
// ...其他配置
21+
})
22+
```
1823

1924
## 画布大小
25+
在实例化 `Graph` 对象的时候,X6 支持多种方式来设置画布大小:
26+
1. **固定画布大小**:设置 `width``height` 的固定数值方式来设置画布大小,例如 `width: 800`, `height: 500`,来将画布大小设置为 `800x500`
27+
```js
28+
const graph = new Graph({
29+
container: document.getElementById('container'),
30+
width: 800,
31+
height: 500,
32+
})
33+
```
34+
2. **容器大小**:不设置 `width``height`,默认就会以画布容器大小初始化画布,例如容器大小为 `800x500`,画布大小也会是 `800x500`
35+
```js
36+
const graph = new Graph({
37+
container: document.getElementById('container'),
38+
// 不设置 width 和 height,默认以容器大小初始化画布
39+
// width: 800,
40+
// height: 500,
41+
})
42+
```
43+
3. **自适应容器大小**:通过设置 `autoResize: true` 来开启自适应容器大小,以容器大小作为画布大小,当画布容器大小改变时,画布大小也会自动重新计算。
44+
```js {3}
45+
const graph = new Graph({
46+
container: document.getElementById('container'),
47+
autoResize: true,
48+
})
49+
```
2050

21-
在实例化 `Graph` 对象的时候,可以通过设置 `width``height` 固定画布大小,如果不设置,就会以画布容器大小初始化画布。
22-
23-
在项目实践中,经常会遇到下面两种场景:
24-
25-
- 画布容器还没有渲染完成(此时尺寸为 0),就实例化画布对象,导致画布元素显示异常。
26-
- 页面的 `resize` 导致画布容器大小改变,导致画布元素显示异常。
51+
使用 `autoResize` 还可以解决以下两个问题:
2752

28-
我们可以使用 `autoResize` 配置来解决上述问题。
53+
- 画布容器还没有渲染完成(此时尺寸为 0),并且没有通过 `width``height` 配置画布大小,就实例化画布对象,导致渲染不出画布。
54+
- 设置了画布大小,但是页面的 `resize`(例如用户改变浏览器窗口大小)导致画布容器大小改变,但是画布大小并没有跟着一起改变。
2955

56+
:::info{title=提示}
57+
使用 `autoResize` 配置时,需要在画布容器外面再套一层宽高都是 100% 的外层容器,在外层容器上监听尺寸改变,当外层容器大小改变时,画布自动重新计算宽高以及元素位置
3058
```html
31-
<!-- 注意,使用 autoResize 配置时,需要在画布容器外面再套一层宽高都是 100% 的外层容器,在外层容器上监听尺寸改变,当外层容器大小改变时,画布自动重新计算宽高以及元素位置。 -->
3259
<div style="width:100%; height:100%">
3360
<div id="container"></div>
3461
</div>
3562
```
63+
:::
64+
65+
## 背景与网格
66+
67+
一个常见的需求是设置画布的背景颜色以及给画布添加网格,在 X6 画布中可以直接通过 `background` 以及 `grid` 配置来实现效果:
68+
```js {5-6}
69+
import { Graph } from '@antv/x6'
3670

37-
```ts
3871
const graph = new Graph({
39-
container: document.getElementById('container'),
40-
autoResize: true,
72+
// ...其他配置
73+
background: { color: "#F2F7FA" },
74+
grid: 10
4175
})
4276
```
4377

44-
在下面的示例中,我们可以用鼠标拖动灰色的区域,来改变容器大小,可以通过背景颜色看到,三块画布的大小是自适应的。
45-
46-
<code id="auto-resize" src="@/src/tutorial/basic/graph/auto-resize/index.tsx"></code>
78+
这是一个完整的例子,注意背景颜色以及背景上的网格:
79+
```js | ob { inject: true, pin: false }
80+
import { Graph } from '@antv/x6'
4781

48-
## 背景与网格
82+
const graph = new Graph({
83+
container: document.getElementById('container'),
84+
height: 300,
85+
background: {
86+
color: '#F2F7FA', // 背景颜色
87+
},
88+
grid: {
89+
visible: true,
90+
type: 'doubleMesh',
91+
args: [
92+
{
93+
color: '#eee', // 主网格线颜色
94+
thickness: 1, // 主网格线宽度
95+
},
96+
{
97+
color: '#ddd', // 次网格线颜色
98+
thickness: 1, // 次网格线宽度
99+
factor: 4, // 主次网格线间隔
100+
},
101+
],
102+
},
103+
})
49104

50-
可以通过 `background``grid` 两个配置来设置画布的背景以及网格。
105+
graph.addNode({
106+
shape: 'rect',
107+
label: "hello X6",
108+
width: 100,
109+
height: 50,
110+
attrs: {
111+
body: {
112+
strokeWidth: 1,
113+
rx: 6,
114+
ry: 6,
115+
},
116+
},
117+
})
51118

52-
<code id="background-grid" src="@/src/tutorial/basic/graph/background-grid/index.tsx"></code>
119+
graph.centerContent() // 居中显示
120+
```
53121

54122
:::info{title=提示}
55-
在 X6 中,网格是渲染/移动节点的最小单位,默认是 10px ,也就是说位置为 `{ x: 24, y: 38 }` 的节点渲染到画布后的实际位置为 `{ x: 20, y: 40 }`
123+
在 X6 中,网格是渲染/移动节点的最小单位,默认是 10px ,也就是说位置为 `{ x: 24, y: 38 }` 的节点渲染到画布后的实际位置为 `{ x: 20, y: 40 }`,最小移动单位可以通过 grid 配置中的 `size` [属性](/api/graph/grid#配置)来设置。
56124
:::
57125

58-
背景不仅支持颜色,还支持背景图片,详细的配置与方法参考 [API](/api/graph/background)。同时,网格支持四种不同类型,并且能配置网格线的颜色以及宽度,详细的配置与方法参考 [API](/api/graph/grid)
126+
背景不仅支持颜色,还支持背景图片,网格也支持四种不同类型,并且能配置网格线的颜色以及宽度,详细的配置与方法可见:
127+
- [背景配置](/api/graph/background)
128+
- [网格配置](/api/graph/grid)
59129

60130
## 缩放与平移
61131

62132
画布的拖拽、缩放也是常用操作,Graph 中通过 `panning``mousewheel` 配置来实现这两个功能,鼠标按下画布后移动时会拖拽画布,滚动鼠标滚轮会缩放画布。
63133

64-
```ts
134+
```ts {5-6}
135+
import { Graph } from '@antv/x6'
136+
65137
const graph = new Graph({
66-
...,
138+
// ...其他配置
67139
panning: true,
68140
mousewheel: true
69141
})
70142
```
71143

72-
下面通过一个例子体验一下:
144+
这是一个完整的例子,按住鼠标可以拖动画布,滚动鼠标滚轮可以缩放画布:
145+
146+
```js | ob { inject: true, pin: false }
147+
import { Graph } from '@antv/x6'
148+
149+
const graph = new Graph({
150+
container: document.getElementById('container'),
151+
height: 300,
152+
background: {
153+
color: '#F2F7FA',
154+
},
155+
panning: true,
156+
mousewheel: true,
157+
})
158+
159+
graph.addNode({
160+
shape: 'rect',
161+
label: "hello X6",
162+
width: 100,
163+
height: 50,
164+
attrs: {
165+
body: {
166+
strokeWidth: 1,
167+
rx: 6,
168+
ry: 6,
169+
},
170+
},
171+
})
172+
173+
graph.centerContent() // 居中显示
174+
```
175+
当然,`panning``mousewheel` 也支持很多其他的配置,比如修饰键(按下修饰键才能触发相应的行为)、缩放因子(速率)等等,详细的配置与方法可见:
176+
- [画布平移](/api/graph/panning)
177+
- [画布缩放](/api/graph/mousewheel)
178+
179+
## 画布插件
180+
画布除了支持 网格、背景、缩放等功能之外,还通过插件支持了很多其他实用的功能,例如 滚动画布、对齐线等,可以按需来引入和使用:
181+
```ts
182+
import { Graph, Scroller, Snapline } from '@antv/x6'
183+
184+
const graph = new Graph({
185+
container: document.getElementById('container'),
186+
height: 300,
187+
width:800,
188+
})
189+
190+
// Snapline 插件可以使节点在拖动过程中显示与其他节点的对齐线
191+
graph.use(
192+
new Snapline({
193+
enabled: true,
194+
}),
195+
)
196+
// Scroller 插件可以使画布支持滚动
197+
graph.use(
198+
new Scroller({
199+
enabled: true,
200+
}),
201+
)
202+
```
203+
这是一个完整的例子,使用 `Scroller` 插件后画布可以进行滚动了,使用 `Snapline` 插件后拖动节点靠近其他节点会出现对齐线:
204+
```js | ob { inject: true, pin: false }
205+
import { Graph, Scroller, Snapline } from '@antv/x6'
206+
207+
const data = {
208+
// 表示节点
209+
nodes: [
210+
{
211+
id: 'node1',
212+
shape: 'rect',
213+
x: 40,
214+
y: 160,
215+
width: 100,
216+
height: 40,
217+
label: 'hello',
218+
attrs: {
219+
// body 是选择器名称,选中的是 rect 元素
220+
body: {
221+
stroke: '#8f8f8f',
222+
strokeWidth: 1,
223+
fill: '#fff',
224+
rx: 6,
225+
ry: 6,
226+
},
227+
},
228+
},
229+
{
230+
id: 'node2',
231+
shape: 'rect',
232+
x: 160,
233+
y: 180,
234+
width: 100,
235+
height: 40,
236+
label: 'world',
237+
attrs: {
238+
body: {
239+
stroke: '#8f8f8f',
240+
strokeWidth: 1,
241+
fill: '#fff',
242+
rx: 6,
243+
ry: 6,
244+
},
245+
},
246+
},
247+
],
248+
}
249+
250+
const graph = new Graph({
251+
container: document.getElementById('container'),
252+
height: 300,
253+
background: {
254+
color: '#F2F7FA', // 背景颜色
255+
},
256+
})
257+
258+
graph.use(
259+
new Snapline({
260+
enabled: true,
261+
}),
262+
)
73263

74-
<code id="panning-mousewheel" src="@/src/tutorial/basic/graph/panning-mousewheel/index.tsx"></code>
264+
graph.use(
265+
new Scroller({
266+
enabled: true,
267+
}),
268+
)
75269

76-
当然,`panning``mousewheel` 也支持很多其他的配置,比如修饰键(按下修饰键才能触发相应的行为)、缩放因子(速率)等等,我们可以通过 [API](/api/graph/mousewheel) 了解更多内容。
270+
graph.fromJSON(data) // 渲染元素
271+
graph.centerContent() // 居中显示
272+
```
273+
更多使用插件和详细功能可见:
274+
- [对齐线](/tutorial/plugins/snapline)
275+
- [滚动画布](/tutorial/plugins/scroller)
77276

78277
## 常用 API
79278

80-
除了上述的一些配置,X6 还有丰富的 API 对画布尺寸、位置进行操作,下面列举一些常用的 API,更详细的内容见 [API](/api/graph/transform)
279+
除了上述的一些配置,X6 还有丰富的 API 对画布尺寸、位置进行操作,下面列举一些常用的 API
81280

82281
```ts
83282
graph.resize(800, 600) // resize 画布大小
@@ -91,3 +290,12 @@ graph.centerContent() // 将画布中元素居中展示
91290
```
92291

93292
<code id="transform" src="@/src/tutorial/basic/graph/transform/index.tsx"></code>
293+
294+
以上是一些常用的 API,更多 API 可见:
295+
- [配置项](/api/graph/graph)
296+
- [网格](/api/graph/grid)
297+
- [背景](/api/graph/background)
298+
- [画布平移](/api/graph/panning)
299+
- [画布缩放](/api/graph/mousewheel)
300+
- [视口变换](/api/graph/transform)
301+
- [坐标系](/api/graph/coordinate)

0 commit comments

Comments
 (0)