添加节点
业务数据
// 添加默认节点 addNode() { this.graph.centerContent() // TODO 添加节点位置的计算 // 生成随机数 this.graph.addNode({ shape: 'rect', // 指定使用何种图形,默认值为 'rect' x: 10, y: 50, width: 110, height: 38, // html | svg DOM 结构 markup: [ // svg 矩形 边框 { tagName: 'rect', selector: 'body' }, // svg text 放置实体名称 { tagName: 'text', selector: 'label' }, // svg text 放置实体属性 { tagName: 'text', selector: 'subLabel' } ], // 业务数据 // data: { // label: 'label', // attributes: [{ key: '', value: '' }] // }, // 默认样式属性 attrs: defaultNodeAttrs, // 链接桩 ports: { // 链接桩组定义 groups: { left: { position: 'left', attrs: defaultPortAttrs }, top: { position: 'top', attrs: defaultPortAttrs }, right: { position: 'right', attrs: defaultPortAttrs }, bottom: { position: 'bottom', attrs: defaultPortAttrs } }, // 链接桩 items: [ { id: 'port1', group: 'left', // 指定分组名称 islined: false }, { id: 'port2', group: 'top' // 指定分组名称 }, { id: 'port3', group: 'bottom' // 指定分组名称 }, { id: 'port4', group: 'right' // 指定分组名称 } ] } }) },
import i18n from '@/lang'const $t = i18n.global.t// 默认节点宽高export const DEFAULT_NODE_WIDTH = 110export const DEFAULT_NODE_HEIGHT = 38// 配置后节点宽度export const CONFIG_NODE_WIDTH = 144// 配置后高度根据属性行动态计算export const MAX_ATTRIBUTE_LINES = 5 + 1 // 最多显示的属性行数 + 溢出行...export const LINE_HEIGHT = 22export const LABEL_FONT_SIZE = 14export const ATTR_FONT_SIZE = 12// 发现对象起始点标记图标svg path 路径export const START_POINT_ICON_PATH = 'M227.2 374.4c57.6-57.6 121.6-54.4 195.2-16l272-150.4-16-105.6 240 240-105.6-16-150.4 272c38.4 76.8 41.6 140.8-16 195.2L480 627.2l-345.6 256 256-348.8c-73.6-70.4-163.2-160-163.2-160z M134.4 905.6c-6.4 0-9.6-3.2-16-6.4-6.4-6.4-9.6-19.2-3.2-28.8l246.4-332.8c-70.4-67.2-150.4-147.2-150.4-147.2-9.6-9.6-9.6-22.4 0-28.8 57.6-57.6 124.8-64 211.2-25.6L672 195.2l-12.8-89.6c0-9.6 3.2-19.2 12.8-22.4 9.6-3.2 19.2-3.2 25.6 3.2l240 240c6.4 6.4 9.6 16 3.2 25.6-3.2 9.6-12.8 12.8-22.4 12.8l-89.6-12.8-137.6 249.6c28.8 57.6 51.2 140.8-25.6 211.2-9.6 6.4-22.4 6.4-28.8 0l-153.6-153.6-336 240c-3.2 3.2-9.6 6.4-12.8 6.4z m124.8-531.2c28.8 28.8 92.8 92.8 147.2 144 6.4 6.4 9.6 19.2 3.2 28.8l-172.8 233.6 233.6-172.8c9.6-6.4 19.2-6.4 28.8 3.2l150.4 150.4c32-41.6 32-86.4-3.2-153.6-3.2-6.4-3.2-12.8 0-19.2l150.4-272c3.2-6.4 12.8-12.8 22.4-9.6l41.6 6.4-147.2-147.2 6.4 41.6c0 9.6-3.2 16-9.6 22.4l-272 150.4c-6.4 3.2-12.8 3.2-19.2 0-70.4-38.4-118.4-38.4-160-6.4z'// 无添加按钮的节点 markup 结构const defaultNodeMarkup = [ // svg 矩形 边框 { tagName: 'rect', selector: 'body' }, // svg text 放置实体名称 { tagName: 'text', selector: 'label' }, // svg text 放置实体属性 { tagName: 'text', selector: 'attrs' }]// 有添加按钮的节点markup结构const hasAddBtnNodeMarkup = [ // svg 矩形 边框 { tagName: 'rect', selector: 'body' }, // svg text 放置实体名称 { tagName: 'text', selector: 'label' }, // svg text 放置实体属性 { tagName: 'text', selector: 'attrs' }, // 右侧添加按钮 { tagName: 'g', attrs: { class: 'btn add' }, children: [ // 为优化移动端,增大点击触发范围的不可见圆环 { tagName: 'circle', attrs: { class: 'hidden-circle' } }, // 圆环 { tagName: 'circle', attrs: { class: 'add-circle' } }, // + 号 { tagName: 'text', attrs: { class: 'add' } } ] }]// 默认节点的 attrs 样式: 虚线边框 无右侧添加按钮const defaultNodeAttrs = { body: { fill: 'var(--gray-3)', stroke: 'var(--gray-6)', strokeWidth: 1, strokeDasharray: '4,3', event: 'label:modal' //自定义点击事件,点击时触发弹窗编辑 }, label: { textAnchor: 'middle', textVerticalAnchor: 'middle', refX: '50%', refY: '50%', text: $t('dataSource.prepare.entityWaitEdit'), fill: 'var(--gray-7)', fontSize: LABEL_FONT_SIZE, textWrap: { ellipsis: true, width: DEFAULT_NODE_WIDTH - 24, height: LINE_HEIGHT }, event: 'label:modal' // 自定义点击事件,点击时触发弹窗编辑 }, attrs: { textWrap: { ellipsis: true, width: CONFIG_NODE_WIDTH - 24 }, event: 'label:modal' }}// 默认节点配置数据后的样式const hasDataNodeAttrs = JSON.parse(JSON.stringify(defaultNodeAttrs))hasDataNodeAttrs.body.fill = 'var(--gray-1)'hasDataNodeAttrs.body.stroke = 'var(--primary)'hasDataNodeAttrs.label.fill = 'var(--primary)'// 默认节点在连线模式下 点击选中时的 attrs 样式const selectionNodeAttrs = JSON.parse(JSON.stringify(defaultNodeAttrs))selectionNodeAttrs.body.fill = 'var(--primary)'selectionNodeAttrs.body.stroke = 'var(--primary)'selectionNodeAttrs.body.strokeDasharray = 'none'selectionNodeAttrs.label.fill = 'var(--gray-1)'// 有右侧添加按钮的节点 attr 样式const hasAddBtnNodeAttrs = { body: { fill: 'var(--gray-1)', stroke: 'var(--primary)', strokeWidth: 1, strokeDasharray: '4,3', event: 'label:modal' }, label: { textAnchor: 'middle', textVerticalAnchor: 'middle', refX: '50%', refY: '50%', text: $t('dataSource.prepare.entityWaitEdit'), fill: 'var(--primary)', fontSize: LABEL_FONT_SIZE, event: 'label:modal' // 自定义点击事件,点击时触发属性编辑弹窗 }, attrs: { textWrap: { ellipsis: true, width: DEFAULT_NODE_WIDTH - 24 }, fill: 'var(--primary)', event: 'label:modal' }, '.btn.add': { refDx: 0, refY: '50%', event: 'node:add' // 自定义点击事件,点击时添加节点 }, '.btn.add > .hidden-circle': { r: 16, fill: 'transparent', stroke: 'var(--primary)', strokeWidth: 0 }, '.btn.add > .add-circle': { r: 7, fill: 'var(--gray-1)', stroke: 'var(--primary)', strokeWidth: 1 }, '.btn.add > text': { fontSize: 12, stroke: 'var(--primary)', strokeWidth: 1.5, refDx: '-150%', refY: '0%', fontFamily: 'Times New Roman', text: '+' }}export default { defaultNodeMarkup, hasAddBtnNodeMarkup, defaultNodeAttrs, hasDataNodeAttrs, hasAddBtnNodeAttrs, selectionNodeAttrs}
markup 节点结构
markup 自定义事件
// 监听自定义事件this.graph.on('node:add', ({ e, node }) => { console.log('e=>', e) if (node.id == 1) { this.graph.addNode({ id: 2, shape: 'rect', width: 110, height: 38, x: 400, y: 100, // 节点 html | svg DOM 结构 markup: NODE.hasAddBtnNodeMarkup, attrs: NODE.hasAddBtnNodeAttrs }) this.graph.addEdge({ source: 1, target: 2, labels: EDGE.defaultEdge.labels, attrs: EDGE.defaultEdge.attrs }) this.graph.centerContent() this.addIconSrc = require('@/assets/add2x_disabled.png') this.lineIconSrc = require('@/assets/lined2x.png') this.$refs.lineIcon.style.cursor = 'pointer' this.$refs.addIcon.style.cursor = 'not-allowed' console.log(this.graph.getNodes()) } else { this.graph.addNode({ id: 1, shape: 'rect', width: 110, height: 38, x: 100, y: 100, // 节点 html | svg DOM 结构 markup: NODE.hasAddBtnNodeMarkup, attrs: NODE.hasAddBtnNodeAttrs }) this.graph.addEdge({ source: 2, target: 1, labels: EDGE.defaultEdge.labels, attrs: EDGE.defaultEdge.attrs }) this.graph.centerContent() this.addIconSrc = require('@/assets/add2x_disabled.png') this.lineIconSrc = require('@/assets/lined2x.png') this.$refs.lineIcon.style.cursor = 'pointer' this.$refs.addIcon.style.cursor = 'not-allowed' console.log(this.graph.getNodes()) }})