AntV X6(流程图的制作dome)

image.png

安装

通过 npm 或 yarn 命令安装 X6。

  1. # npm
  2. $ npm install @antv/x6 --save
  3. # yarn
  4. $ yarn add @antv/x6

安装完成之后,使用 import 或 require 进行引用。

  1. import { Graph } from '@antv/x6';

demo

  1. <!--
  2. * @description:
  3. * @author: wanghao
  4. * @Date: 2022-03-01 20:36:46
  5. * @Modified By:
  6. * @version: 1.0.0
  7. -->
  8. <template>
  9. <div id="container">你好</div>
  10. </template>
  11. <script>
  12. import { Graph } from "@antv/x6";
  13. const data = {
  14. nodes: [
  15. {
  16. id: "node1",
  17. x: 40,
  18. y: 40,
  19. width: 100,
  20. height: 40,
  21. attrs: {
  22. body: {
  23. fill: "#2ECC71",
  24. stroke: "#000",
  25. strokeDasharray: "10,2",
  26. },
  27. label: {
  28. text: "Hello",
  29. fill: "#333",
  30. fontSize: 13,
  31. },
  32. },
  33. },
  34. {
  35. id: "node2",
  36. x: 180,
  37. y: 240,
  38. width: 100,
  39. height: 40,
  40. attrs: {
  41. body: {
  42. fill: "#F39C12",
  43. stroke: "#000",
  44. rx: 16,
  45. ry: 16,
  46. },
  47. label: {
  48. text: "World",
  49. fill: "#333",
  50. fontSize: 18,
  51. fontWeight: "bold",
  52. fontVariant: "small-caps",
  53. },
  54. },
  55. },
  56. ],
  57. edges: [
  58. {
  59. source: "node1",
  60. target: "node2",
  61. attrs: {
  62. line: {
  63. stroke: "orange",
  64. },
  65. },
  66. },
  67. ],
  68. };
  69. export default {
  70. name: "index",
  71. mounted() {
  72. this.init();
  73. },
  74. methods: {
  75. init() {
  76. // 渲染画布
  77. const graph = new Graph({
  78. container: document.getElementById("container"),
  79. width: 800,
  80. height: 600,
  81. background: {
  82. color: "pink", // 设置画布背景颜色
  83. },
  84. grid: {
  85. size: 20, // 网格大小 10px
  86. visible: true, // 渲染网格背景
  87. },
  88. });
  89. graph.fromJSON(data);
  90. },
  91. },
  92. };
  93. </script>

开始使用

接下来我们就一起来创建一个最简单的 Hello —> World 应用。

Step 1 创建容器

在页面中创建一个用于容纳 X6 绘图的容器,可以是一个 div 标签。

  1. <div id="container"></div>

Step 2 准备数据

X6 支持 JSON 格式数据,该对象中需要有节点 nodes 和边 edges 字段,分别用数组表示:

  1. const data = {
  2. // 节点
  3. nodes: [
  4. {
  5. id: 'node1', // String,可选,节点的唯一标识
  6. x: 40, // Number,必选,节点位置的 x 值
  7. y: 40, // Number,必选,节点位置的 y 值
  8. width: 80, // Number,可选,节点大小的 width 值
  9. height: 40, // Number,可选,节点大小的 height 值
  10. label: 'hello', // String,节点标签
  11. },
  12. {
  13. id: 'node2', // String,节点的唯一标识
  14. x: 160, // Number,必选,节点位置的 x 值
  15. y: 180, // Number,必选,节点位置的 y 值
  16. width: 80, // Number,可选,节点大小的 width 值
  17. height: 40, // Number,可选,节点大小的 height 值
  18. label: 'world', // String,节点标签
  19. },
  20. ],
  21. // 边
  22. edges: [
  23. {
  24. source: 'node1', // String,必须,起始节点 id
  25. target: 'node2', // String,必须,目标节点 id
  26. },
  27. ],
  28. };

Step 3 渲染画布

首先,我们需要创建一个 Graph 对象,并为其指定一个页面上的绘图容器,通常也会指定画布的大小。

  1. import { Graph } from '@antv/x6';
  2. // 使用 CDN 引入时暴露了 X6 全局变量
  3. // const { Graph } = X6
  4. const graph = new Graph({
  5. container: document.getElementById('container'),
  6. width: 800,
  7. height: 600,
  8. });

如果是通过 script 标签引入方式,我们的 Graph 对象是挂载在 X6 这个全局变量下面:

  1. <script src="https://unpkg.com/@antv/x6/dist/x6.js"></script>
  2. <script>
  3. const graph = new X6.Graph({
  4. container: document.getElementById('container'),
  5. width: 800,
  6. height: 600,
  7. });
  8. </script>

然后,我们就可以使用刚刚创建的 graph 来渲染我们的节点和边。

  1. graph.fromJSON(data)

到此,我们就得到一个最简单的 Hello —> World 示例,看下面的完整代码。
image.png

画布

背景和网格

接下来,我们来给画布设置一个背景颜色和网格,另外还支持背景图片、网格类型等配置,点击查看完整的背景配置网格配置

  1. import { Graph } from '@antv/x6';
  2. const graph = new Graph({
  3. container: document.getElementById('container'),
  4. width: 800,
  5. height: 600,
  6. background: {
  7. color: '#fffbe6', // 设置画布背景颜色
  8. },
  9. grid: {
  10. size: 10, // 网格大小 10px
  11. visible: true, // 渲染网格背景
  12. },
  13. });

image.png

缩放和平移

创建画布后,可以调用graph.zoom(factor: number)graph.translate(tx: number, ty: number) 来缩放和平移画布。

  1. graph.zoom(-0.5)
  2. graph.translate(80, 40)

节点

使用图形

在上面示例中,我们使用了默认图形 rect 来渲染节点,除此之外,我们在 X6 中也内置了 circle、ellipse、polygon 等基础图形,可以通过 shape 属性为节点指定渲染的图形,如果你对 SVG 图形还不熟悉,可以参考 SVG 图像入门教程

  1. const data = {
  2. nodes: [
  3. {
  4. id: 'node1',
  5. shape: 'rect', // 使用 rect 渲染
  6. x: 100,
  7. y: 200,
  8. width: 80,
  9. height: 40,
  10. label: 'hello',
  11. },
  12. {
  13. id: 'node2',
  14. shape: 'ellipse', // 使用 ellipse 渲染
  15. x: 300,
  16. y: 200,
  17. width: 80,
  18. height: 40,
  19. label: 'world',
  20. },
  21. ],
  22. edges: [
  23. {
  24. source: 'node1',
  25. target: 'node2',
  26. },
  27. ],
  28. };

image.png

定制样式

在创建节点或准备节点数据时,我们可以通过 attrs 对象来配置节点样式,该对象的 Key 是节点 SVG 元素的选择器(Selector),对应的值是应用到该 SVG 元素的 SVG 属性值(如 fillstroke),如果你对 SVG 属性还不熟悉,可以参考 MDN 提供的填充和边框入门教程。
我们对 ‘rect’ 图形中定义了 ‘body’(代表 元素) 和 ‘label’(代表 元素) 两个选择器(Selector),每种图形都有属于自己的选择器定义,X6 内置图形参考这里

  1. const data = {
  2. nodes: [
  3. {
  4. id: 'node1',
  5. x: 40,
  6. y: 40,
  7. width: 100,
  8. height: 40,
  9. attrs: {
  10. body: {
  11. fill: '#2ECC71',
  12. stroke: '#000',
  13. strokeDasharray: '10,2',
  14. },
  15. label: {
  16. text: 'Hello',
  17. fill: '#333',
  18. fontSize: 13,
  19. }
  20. }
  21. },
  22. {
  23. id: 'node2',
  24. x: 180,
  25. y: 240,
  26. width: 100,
  27. height: 40,
  28. attrs: {
  29. body: {
  30. fill: '#F39C12',
  31. stroke: '#000',
  32. rx: 16,
  33. ry: 16,
  34. },
  35. label: {
  36. text: 'World',
  37. fill: '#333',
  38. fontSize: 18,
  39. fontWeight: 'bold',
  40. fontVariant: 'small-caps',
  41. },
  42. },
  43. },
  44. ],
  45. edges: [
  46. {
  47. source: 'node1',
  48. target: 'node2',
  49. },
  50. ],
  51. };

image.png

使用图形

在上面示例中,我们使用了默认图形 edge 来渲染边,除此之外,在 X6 中还内置了 double-edge 和 shadow-edge 两种图形,可以通过 shape 属性为边指定渲染的图形,例如:

  1. onst data = {
  2. nodes: [
  3. {
  4. id: 'node1',
  5. shape: 'rect',
  6. x: 100,
  7. y: 100,
  8. width: 80,
  9. height: 40,
  10. label: 'hello',
  11. },
  12. {
  13. id: 'node2',
  14. shape: 'ellipse',
  15. x: 240,
  16. y: 300,
  17. width: 80,
  18. height: 40,
  19. label: 'world',
  20. },
  21. ],
  22. edges: [
  23. {
  24. source: 'node1',
  25. target: 'node2',
  26. shape: 'double-edge',
  27. },
  28. ],
  29. }

定制样式

定制节点样式一样,我们使用 attrs 对象来配置边的样式,默认的 edge 图形定义了 ‘line’( 元素) 和 ‘wrap’(透明的 元素,更宽但不可见,为了方便交互)两个选择器(Selector),我们像下面这样来定制边的样式。

  1. const data = {
  2. nodes: [
  3. {
  4. id: 'node1',
  5. shape: 'rect',
  6. x: 100,
  7. y: 100,
  8. width: 80,
  9. height: 40,
  10. label: 'hello',
  11. },
  12. {
  13. id: 'node2',
  14. shape: 'ellipse',
  15. x: 240,
  16. y: 300,
  17. width: 80,
  18. height: 40,
  19. label: 'world',
  20. },
  21. ],
  22. edges: [
  23. {
  24. source: 'node1',
  25. target: 'node2',
  26. attrs: {
  27. line: {
  28. stroke: 'orange',
  29. },
  30. },
  31. },
  32. ],
  33. }

image.png