• vue antv X6流程图


    第一 下载2.0插件

    第二 引入代码

    1. <!-- -->
    2. <template>
    3. <div id="container" style="min-width: 400px; min-height: 600px"></div>
    4. </template>
    5. <script >
    6. //这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
    7. //例如:import 《组件名称》 from '《组件路径》';
    8. import { Graph, Shape } from "@antv/x6";
    9. import { Stencil } from "@antv/x6-plugin-stencil";
    10. import { Transform } from "@antv/x6-plugin-transform";import { Selection } from "@antv/x6-plugin-selection";
    11. import { Snapline } from "@antv/x6-plugin-snapline";
    12. import { Keyboard } from "@antv/x6-plugin-keyboard";
    13. import { Clipboard } from "@antv/x6-plugin-clipboard";
    14. import { History } from "@antv/x6-plugin-history";
    15. import insertCss from "insert-css";
    16. export default {
    17. //import引入的组件需要注入到对象中才能使用
    18. components: {},
    19. data() {
    20. //这里存放数据
    21. return {};
    22. },
    23. //监听属性 类似于data概念
    24. computed: {},
    25. //监控data中的数据变化
    26. watch: {},
    27. //方法集合
    28. methods: {
    29. init() {
    30. const data = {
    31. nodes: [
    32. {
    33. id: "node1",
    34. shape: "rect",
    35. x: 40,
    36. y: 40,
    37. width: 100,
    38. height: 40,
    39. label: "hello",
    40. attrs: {
    41. // body 是选择器名称,选中的是 rect 元素
    42. body: {
    43. stroke: "#8f8f8f",
    44. strokeWidth: 1,
    45. fill: "#fff",
    46. rx: 6,
    47. ry: 6,
    48. },
    49. },
    50. },
    51. {
    52. id: "node2",
    53. shape: "rect",
    54. x: 160,
    55. y: 180,
    56. width: 100,
    57. height: 40,
    58. label: "world",
    59. attrs: {
    60. body: {
    61. stroke: "#8f8f8f",
    62. strokeWidth: 1,
    63. fill: "#fff",
    64. rx: 6,
    65. ry: 6,
    66. },
    67. },
    68. },
    69. ],
    70. edges: [
    71. {
    72. shape: "edge",
    73. source: "node1",
    74. target: "node2",
    75. label: "x6",
    76. attrs: {
    77. // line 是选择器名称,选中的边的 path 元素
    78. line: {
    79. stroke: "#8f8f8f",
    80. strokeWidth: 1,
    81. },
    82. },
    83. },
    84. ],
    85. };
    86. const graph = new Graph({
    87. container: document.getElementById("graph-container"),
    88. grid: true,
    89. mousewheel: {
    90. enabled: true,
    91. zoomAtMousePosition: true,
    92. modifiers: "ctrl",
    93. minScale: 0.5,
    94. maxScale: 3,
    95. },
    96. connecting: {
    97. router: "manhattan",
    98. connector: {
    99. name: "rounded",
    100. args: {
    101. radius: 8,
    102. },
    103. },
    104. anchor: "center",
    105. connectionPoint: "anchor",
    106. allowBlank: false,
    107. snap: {
    108. radius: 20,
    109. },
    110. createEdge() {
    111. return new Shape.Edge({
    112. attrs: {
    113. line: {
    114. stroke: "#A2B1C3",
    115. strokeWidth: 2,
    116. targetMarker: {
    117. name: "block",
    118. width: 12,
    119. height: 8,
    120. },
    121. },
    122. },
    123. zIndex: 0,
    124. });
    125. },
    126. validateConnection({ targetMagnet }) {
    127. return !!targetMagnet;
    128. },
    129. },
    130. highlighting: {
    131. magnetAdsorbed: {
    132. name: "stroke",
    133. args: {
    134. attrs: {
    135. fill: "#5F95FF",
    136. stroke: "#5F95FF",
    137. },
    138. },
    139. },
    140. },
    141. });
    142. // #region 使用插件
    143. graph
    144. .use(
    145. new Transform({
    146. resizing: true,
    147. rotating: true,
    148. })
    149. )
    150. .use(
    151. new Selection({
    152. rubberband: true,
    153. showNodeSelectionBox: true,
    154. })
    155. )
    156. .use(new Snapline())
    157. .use(new Keyboard())
    158. .use(new Clipboard())
    159. .use(new History());
    160. // #endregion
    161. // #region 初始化 stencil
    162. const stencil = new Stencil({
    163. title: "流程图",
    164. target: graph,
    165. stencilGraphWidth: 200,
    166. stencilGraphHeight: 180,
    167. collapsable: true,
    168. groups: [
    169. {
    170. title: "基础流程图",
    171. name: "group1",
    172. },
    173. {
    174. title: "系统设计图",
    175. name: "group2",
    176. graphHeight: 250,
    177. layoutOptions: {
    178. rowHeight: 70,
    179. },
    180. },
    181. ],
    182. layoutOptions: {
    183. columns: 2,
    184. columnWidth: 80,
    185. rowHeight: 55,
    186. },
    187. });
    188. document.getElementById("stencil").appendChild(stencil.container);
    189. // #endregion
    190. // #region 快捷键与事件
    191. graph.bindKey(["meta+c", "ctrl+c"], () => {
    192. const cells = graph.getSelectedCells();
    193. if (cells.length) {
    194. graph.copy(cells);
    195. }
    196. return false;
    197. });
    198. graph.bindKey(["meta+x", "ctrl+x"], () => {
    199. const cells = graph.getSelectedCells();
    200. if (cells.length) {
    201. graph.cut(cells);
    202. }
    203. return false;
    204. });
    205. graph.bindKey(["meta+v", "ctrl+v"], () => {
    206. if (!graph.isClipboardEmpty()) {
    207. const cells = graph.paste({ offset: 32 });
    208. graph.cleanSelection();
    209. graph.select(cells);
    210. }
    211. return false;
    212. });
    213. // undo redo
    214. graph.bindKey(["meta+z", "ctrl+z"], () => {
    215. if (graph.canUndo()) {
    216. graph.undo();
    217. }
    218. return false;
    219. });
    220. graph.bindKey(["meta+shift+z", "ctrl+shift+z"], () => {
    221. if (graph.canRedo()) {
    222. graph.redo();
    223. }
    224. return false;
    225. });
    226. // select all
    227. graph.bindKey(["meta+a", "ctrl+a"], () => {
    228. const nodes = graph.getNodes();
    229. if (nodes) {
    230. graph.select(nodes);
    231. }
    232. });
    233. // delete
    234. graph.bindKey("backspace", () => {
    235. const cells = graph.getSelectedCells();
    236. if (cells.length) {
    237. graph.removeCells(cells);
    238. }
    239. });
    240. // zoom
    241. graph.bindKey(["ctrl+1", "meta+1"], () => {
    242. const zoom = graph.zoom();
    243. if (zoom < 1.5) {
    244. graph.zoom(0.1);
    245. }
    246. });
    247. graph.bindKey(["ctrl+2", "meta+2"], () => {
    248. const zoom = graph.zoom();
    249. if (zoom > 0.5) {
    250. graph.zoom(-0.1);
    251. }
    252. });
    253. // 控制连接桩显示 / 隐藏;
    254. const showPorts = (ports, show) => {
    255. for (let i = 0, len = ports.length; i < len; i += 1) {
    256. ports[i].style.visibility = show ? "visible" : "hidden";
    257. }
    258. };
    259. graph.on("node:mouseenter", () => {
    260. const container = document.getElementById("graph-container");
    261. const ports = container.querySelectorAll(".x6-port-body");
    262. showPorts(ports, true);
    263. });
    264. graph.on("node:mouseleave", () => {
    265. const container = document.getElementById("graph-container");
    266. const ports = container.querySelectorAll(".x6-port-body");
    267. // if (this.isPortsShow) return
    268. showPorts(ports, false);
    269. });
    270. // #endregion
    271. // #region 初始化图形
    272. const ports = {
    273. groups: {
    274. top: {
    275. position: "top",
    276. attrs: {
    277. circle: {
    278. r: 4,
    279. magnet: true,
    280. stroke: "#5F95FF",
    281. strokeWidth: 1,
    282. fill: "#fff",
    283. style: {
    284. visibility: "hidden",
    285. },
    286. },
    287. },
    288. },
    289. right: {
    290. position: "right",
    291. attrs: {
    292. circle: {
    293. r: 4,
    294. magnet: true,
    295. stroke: "#5F95FF",
    296. strokeWidth: 1,
    297. fill: "#fff",
    298. style: {
    299. visibility: "hidden",
    300. },
    301. },
    302. },
    303. },
    304. bottom: {
    305. position: "bottom",
    306. attrs: {
    307. circle: {
    308. r: 4,
    309. magnet: true,
    310. stroke: "#5F95FF",
    311. strokeWidth: 1,
    312. fill: "#fff",
    313. style: {
    314. visibility: "hidden",
    315. },
    316. },
    317. },
    318. },
    319. left: {
    320. position: "left",
    321. attrs: {
    322. circle: {
    323. r: 4,
    324. magnet: true,
    325. stroke: "#5F95FF",
    326. strokeWidth: 1,
    327. fill: "#fff",
    328. style: {
    329. visibility: "hidden",
    330. },
    331. },
    332. },
    333. },
    334. },
    335. items: [
    336. {
    337. group: "top",
    338. },
    339. {
    340. group: "right",
    341. },
    342. {
    343. group: "bottom",
    344. },
    345. {
    346. group: "left",
    347. },
    348. ],
    349. };
    350. Graph.registerNode(
    351. "custom-rect",
    352. {
    353. inherit: "rect",
    354. width: 66,
    355. height: 36,
    356. attrs: {
    357. body: {
    358. strokeWidth: 1,
    359. stroke: "#5F95FF",
    360. fill: "#EFF4FF",
    361. },
    362. text: {
    363. fontSize: 12,
    364. fill: "#262626",
    365. },
    366. },
    367. ports: { ...ports },
    368. },
    369. true
    370. );
    371. Graph.registerNode(
    372. "custom-polygon",
    373. {
    374. inherit: "polygon",
    375. width: 66,
    376. height: 36,
    377. attrs: {
    378. body: {
    379. strokeWidth: 1,
    380. stroke: "#5F95FF",
    381. fill: "#EFF4FF",
    382. },
    383. text: {
    384. fontSize: 12,
    385. fill: "#262626",
    386. },
    387. },
    388. ports: {
    389. ...ports,
    390. items: [
    391. {
    392. group: "top",
    393. },
    394. {
    395. group: "bottom",
    396. },
    397. ],
    398. },
    399. },
    400. true
    401. );
    402. Graph.registerNode(
    403. "custom-circle",
    404. {
    405. inherit: "circle",
    406. width: 45,
    407. height: 45,
    408. attrs: {
    409. body: {
    410. strokeWidth: 1,
    411. stroke: "#5F95FF",
    412. fill: "#EFF4FF",
    413. },
    414. text: {
    415. fontSize: 12,
    416. fill: "#262626",
    417. },
    418. },
    419. ports: { ...ports },
    420. },
    421. true
    422. );
    423. Graph.registerNode(
    424. "custom-image",
    425. {
    426. inherit: "rect",
    427. width: 52,
    428. height: 52,
    429. markup: [
    430. {
    431. tagName: "rect",
    432. selector: "body",
    433. },
    434. {
    435. tagName: "image",
    436. },
    437. {
    438. tagName: "text",
    439. selector: "label",
    440. },
    441. ],
    442. attrs: {
    443. body: {
    444. stroke: "#5F95FF",
    445. fill: "#5F95FF",
    446. },
    447. image: {
    448. width: 26,
    449. height: 26,
    450. refX: 13,
    451. refY: 16,
    452. },
    453. label: {
    454. refX: 3,
    455. refY: 2,
    456. textAnchor: "left",
    457. textVerticalAnchor: "top",
    458. fontSize: 12,
    459. fill: "#fff",
    460. },
    461. },
    462. ports: { ...ports },
    463. },
    464. true
    465. );
    466. const r1 = graph.createNode({
    467. shape: "custom-rect",
    468. label: "开始",
    469. attrs: {
    470. body: {
    471. rx: 20,
    472. ry: 26,
    473. },
    474. },
    475. });
    476. const r2 = graph.createNode({
    477. shape: "custom-rect",
    478. label: "过程",
    479. });
    480. const r3 = graph.createNode({
    481. shape: "custom-rect",
    482. attrs: {
    483. body: {
    484. rx: 6,
    485. ry: 6,
    486. },
    487. },
    488. label: "可选过程",
    489. });
    490. const r4 = graph.createNode({
    491. shape: "custom-polygon",
    492. attrs: {
    493. body: {
    494. refPoints: "0,10 10,0 20,10 10,20",
    495. },
    496. },
    497. label: "决策",
    498. });
    499. const r5 = graph.createNode({
    500. shape: "custom-polygon",
    501. attrs: {
    502. body: {
    503. refPoints: "10,0 40,0 30,20 0,20",
    504. },
    505. },
    506. label: "数据",
    507. });
    508. const r6 = graph.createNode({
    509. shape: "custom-circle",
    510. label: "连接",
    511. });
    512. stencil.load([r1, r2, r3, r4, r5, r6], "group1");
    513. const imageShapes = [
    514. {
    515. label: "Client",
    516. image:
    517. "https://gw.alipayobjects.com/zos/bmw-prod/687b6cb9-4b97-42a6-96d0-34b3099133ac.svg",
    518. },
    519. {
    520. label: "Http",
    521. image:
    522. "https://gw.alipayobjects.com/zos/bmw-prod/dc1ced06-417d-466f-927b-b4a4d3265791.svg",
    523. },
    524. {
    525. label: "Api",
    526. image:
    527. "https://gw.alipayobjects.com/zos/bmw-prod/c55d7ae1-8d20-4585-bd8f-ca23653a4489.svg",
    528. },
    529. {
    530. label: "Sql",
    531. image:
    532. "https://gw.alipayobjects.com/zos/bmw-prod/6eb71764-18ed-4149-b868-53ad1542c405.svg",
    533. },
    534. {
    535. label: "Clound",
    536. image:
    537. "https://gw.alipayobjects.com/zos/bmw-prod/c36fe7cb-dc24-4854-aeb5-88d8dc36d52e.svg",
    538. },
    539. {
    540. label: "Mq",
    541. image:
    542. "https://gw.alipayobjects.com/zos/bmw-prod/2010ac9f-40e7-49d4-8c4a-4fcf2f83033b.svg",
    543. },
    544. ];
    545. const imageNodes = imageShapes.map((item) =>
    546. graph.createNode({
    547. shape: "custom-image",
    548. label: item.label,
    549. attrs: {
    550. image: {
    551. "xlink:href": item.image,
    552. },
    553. },
    554. })
    555. );
    556. stencil.load(imageNodes, "group2");
    557. },
    558. preWork() {
    559. // 这里协助演示的代码,在实际项目中根据实际情况进行调整
    560. const container = document.getElementById("container");
    561. const stencilContainer = document.createElement("div");
    562. stencilContainer.id = "stencil";
    563. const graphContainer = document.createElement("div");
    564. graphContainer.id = "graph-container";
    565. container.appendChild(stencilContainer);
    566. container.appendChild(graphContainer);
    567. insertCss(`
    568. #container {
    569. display: flex;
    570. border: 1px solid #dfe3e8;
    571. }
    572. #stencil {
    573. width: 180px;
    574. height: 100%;
    575. position: relative;
    576. border-right: 1px solid #dfe3e8;
    577. }
    578. #graph-container {
    579. width: calc(100% - 180px);
    580. height: 100%;
    581. }
    582. .x6-widget-stencil {
    583. background-color: #fff;
    584. }
    585. .x6-widget-stencil-title {
    586. background-color: #fff;
    587. }
    588. .x6-widget-stencil-group-title {
    589. background-color: #fff !important;
    590. }
    591. .x6-widget-transform {
    592. margin: -1px 0 0 -1px;
    593. padding: 0px;
    594. border: 1px solid #239edd;
    595. }
    596. .x6-widget-transform > div {
    597. border: 1px solid #239edd;
    598. }
    599. .x6-widget-transform > div:hover {
    600. background-color: #3dafe4;
    601. }
    602. .x6-widget-transform-active-handle {
    603. background-color: #3dafe4;
    604. }
    605. .x6-widget-transform-resize {
    606. border-radius: 0;
    607. }
    608. .x6-widget-selection-inner {
    609. border: 1px solid #239edd;
    610. }
    611. .x6-widget-selection-box {
    612. opacity: 0;
    613. } `);
    614. },
    615. },
    616. //生命周期 - 创建完成(可以访问当前this实例)
    617. created() {},
    618. //生命周期 - 挂载完成(可以访问DOM元素)
    619. mounted() {
    620. this.preWork();
    621. this.init();
    622. },
    623. beforeCreate() {}, //生命周期 - 创建之前
    624. beforeMount() {}, //生命周期 - 挂载之前
    625. beforeUpdate() {}, //生命周期 - 更新之前
    626. updated() {}, //生命周期 - 更新之后
    627. beforeDestroy() {}, //生命周期 - 销毁之前
    628. destroyed() {}, //生命周期 - 销毁完成
    629. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
    630. };
    631. </script>
    632. <style scoped>
    633. /* @import url(); 引入公共css类 */
    634. #container {
    635. width: 100%;
    636. height: 600px;
    637. }
    638. </style>

    第三 运行

  • 相关阅读:
    基于单片机双路压力监测报警系统
    Web会话跟踪技术
    【前端】彻底搞懂HTTP协议
    JS-重绘与回流
    leetcode每日一题——Split With Minimum Sum
    SpringBoot 常用注解的原理和使用
    HTML5期末大作业:游戏网站设计与实现——基于bootstrap响应式游戏资讯网站制作HTML+CSS+JavaScript
    基于SpringBoot的停车位智能管理系统的设计与实现_kaic
    【高等数学】导数的应用
    C/C++语言 数据结构 创建邻接表存储的无向图及其邻接表的输出
  • 原文地址:https://blog.csdn.net/qq_30940855/article/details/132673446