添加节点
业务数据
// 添加默认节点 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' // 指定分组名称 } ] } }) },
/* * @Description: X6 node 节点配置 * @Author: huhaonan@sics.ac.cn * @Date: 2021-04-17 14:48:36 * @LastEditors: huhaonan@sics.ac.cn * @LastEditTime: 2021-04-19 09:29:54 */// 节点新增时默认的 attrs 样式export const defaultNodeAttrs = { body: { // refWidth: '200%', // refHeight: '100%', fill: 'var(--white8)', stroke: 'var(--white30)', strokeWidth: 1, strokeDasharray: '4,3' }, label: { textWrap: { ellipsis: true, width: -10 }, textAnchor: 'middle', textVerticalAnchor: 'middle', refX: '50%', refY: '50%', text: '实体待配置', fill: 'var(--main)', fontSize: 14 }}// 节点设置业务数据后的 attrs 样式export const hasDataNodeAttrs = {}// 节点 tool 工具export const nodeTool = [ { // 根据节点的包围盒渲染一个包围节点的矩形 name: 'boundary', args: { attrs: { fill: '#7c68fc', stroke: 'var(--main)', 'stroke-width': 1, 'fill-opacity': 0.1 } } }, { // 在指定的位置处,渲染一个删除按钮 name: 'button-remove', args: { x: '100%', y: 0, offset: { x: -10, y: 10 } } }]
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()) }})