From 656ea3139db54b8dc9a29b8cb239d0f0df9a6c05 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期二, 27 六月 2023 18:19:42 +0800 Subject: [PATCH] 2023-06-27 --- src/menu/components/chart/antv-X6/index.jsx | 577 ++++++++++++++++++++++++--------------------------------- 1 files changed, 245 insertions(+), 332 deletions(-) diff --git a/src/menu/components/chart/antv-X6/index.jsx b/src/menu/components/chart/antv-X6/index.jsx index c6c84cd..0cfcbf6 100644 --- a/src/menu/components/chart/antv-X6/index.jsx +++ b/src/menu/components/chart/antv-X6/index.jsx @@ -1,21 +1,16 @@ import React, {Component} from 'react' import PropTypes from 'prop-types' import { is, fromJS } from 'immutable' -import { Popover, Tooltip, message } from 'antd' -import { ToolOutlined, DeleteOutlined, FontColorsOutlined, VerticalAlignTopOutlined, VerticalAlignBottomOutlined, SaveOutlined, ZoomInOutlined, ZoomOutOutlined, OneToOneOutlined, DoubleLeftOutlined } from '@ant-design/icons' +import { Popover, Tooltip } from 'antd' +import { ToolOutlined, DeleteOutlined, FontColorsOutlined, VerticalAlignTopOutlined, VerticalAlignBottomOutlined, SaveOutlined, ZoomInOutlined, ZoomOutOutlined, OneToOneOutlined, DownloadOutlined } from '@ant-design/icons' import { Graph, Shape } from '@antv/x6' -import { Stencil } from '@antv/x6-plugin-stencil' -import { Transform } from '@antv/x6-plugin-transform' -import { Selection } from '@antv/x6-plugin-selection' -import { Snapline } from '@antv/x6-plugin-snapline' -import { Keyboard } from '@antv/x6-plugin-keyboard' -import { Clipboard } from '@antv/x6-plugin-clipboard' -import { History } from '@antv/x6-plugin-history' import MKEmitter from '@/utils/events.js' import asyncComponent from '@/utils/asyncComponent' import asyncIconComponent from '@/utils/asyncIconComponent' import { resetStyle, getTables, checkComponent } from '@/utils/utils-custom.js' +import lanes from './lane.json' +import xflows from './xflow.json' import './index.scss' const SettingComponent = asyncIconComponent(() => import('@/menu/datasource')) @@ -88,6 +83,53 @@ } Graph.registerNode( + 'lane', + { + inherit: 'rect', + markup: [ + { + tagName: 'rect', + selector: 'body', + }, + { + tagName: 'rect', + selector: 'name-rect', + }, + { + tagName: 'text', + selector: 'name-text', + }, + ], + attrs: { + body: { + fill: '#FFF', + stroke: '#5F95FF', + strokeWidth: 1, + }, + 'name-rect': { + width: 200, + height: 36, + fill: '#5F95FF', + stroke: '#fff', + strokeWidth: 1, + x: -1, + }, + 'name-text': { + ref: 'name-rect', + refY: 0.5, + refX: 0.5, + textAnchor: 'middle', + fontWeight: 'bold', + fill: '#fff', + fontSize: 14, + }, + }, + zIndex: 0 + }, + true, +) + +Graph.registerNode( 'mk-rect', { inherit: 'rect', @@ -96,8 +138,8 @@ attrs: { body: { strokeWidth: 1, - stroke: '#5F95FF', - fill: '#EFF4FF' + stroke: '#000000', + fill: '#FFFFFF' }, text: { fontSize: 12, @@ -126,8 +168,8 @@ attrs: { body: { strokeWidth: 1, - stroke: '#5F95FF', - fill: '#EFF4FF' + stroke: '#000000', + fill: '#FFFFFF' }, text: { fontSize: 12, @@ -156,8 +198,8 @@ attrs: { body: { strokeWidth: 1, - stroke: '#5F95FF', - fill: '#EFF4FF' + stroke: '#000000', + fill: '#FFFFFF' }, text: { fontSize: 12, @@ -186,8 +228,8 @@ attrs: { body: { strokeWidth: 1, - stroke: '#5F95FF', - fill: '#EFF4FF' + stroke: '#000000', + fill: '#FFFFFF' }, text: { fontSize: 12, @@ -216,8 +258,8 @@ points: '100,10 40,198 190,78 10,78 160,198', attrs: { body: { - fill: '#EFF4FF', - stroke: '#5F95FF', + fill: '#FFFFFF', + stroke: '#000000', strokeWidth: 1, fillRule: 'nonzero' }, @@ -239,6 +281,26 @@ true ) +Graph.registerNode( + 'mk-text', + { + inherit: 'rect', + width: 66, + height: 36, + attrs: { + body: { + strokeWidth: 0, + fill: 'transparent' + }, + text: { + fontSize: 12, + fill: '#262626' + } + } + }, + true +) + class antvX6Chart extends Component { static propTpyes = { card: PropTypes.object, @@ -247,15 +309,8 @@ } state = { - card: null, - eventListener: null, - toolunfold: true, - nodeunfold: true, - node: null + card: null } - - selectNode = null - mkGraph = null UNSAFE_componentWillMount () { const { card } = this.props @@ -263,9 +318,19 @@ if (card.isNew) { let _plot = { width: card.width || 24, - height: 400, + height: 500, subtype: card.subtype, - name: card.name + name: card.name, + grid: { + visible: true, + type: 'dot', + args: { + color: '#a0a0a0', + thickness: 1 + } + }, + gridType: 'dot', + export: 'png' } let _card = { @@ -317,51 +382,55 @@ }, 1000) } - viewrender = () => { - // const { card } = this.state - - // if (card.plot.subtype === 'mindmap') { - // this.ponitrender() - // } else if (card.plot.subtype === 'indentTree') { - // this.indentrender() - // } else if (card.plot.subtype === 'kapmap') { - this.kapmaprender() - // } - } - - kapmaprender = () => { + plotchange = (res) => { const { card } = this.state - // #region 鍒濆鍖栫敾甯� + if ( + card.plot.subtype !== res.plot.subtype || + (res.plot.gridType && card.plot.gridType !== res.plot.gridType) || + (res.plot.gridType && card.plot.backgroundColor !== res.plot.backgroundColor) + ) { + let _element = document.getElementById(card.uuid + 'container') + if (_element) { + _element.innerHTML = '' + } + setTimeout(() => { + this.viewrender() + }, 50) + } + + this.updateComponent(res) + } + + viewrender = () => { + const { card } = this.state + + if (card.plot.subtype === 'xflow') { + this.xflowrender() + } else if (card.plot.subtype === 'lane') { + this.lanerender() + } + } + + xflowrender = () => { + const { card } = this.state + const graph = new Graph({ container: document.getElementById(card.uuid + 'container'), - // grid: true, - grid: { - visible: true, - type: 'doubleMesh', - args: [ - { - color: '#eee', // 涓荤綉鏍肩嚎棰滆壊 - thickness: 1 // 涓荤綉鏍肩嚎瀹藉害 - }, - { - color: '#ddd', // 娆$綉鏍肩嚎棰滆壊 - thickness: 1, // 娆$綉鏍肩嚎瀹藉害 - factor: 4 // 涓绘缃戞牸绾块棿闅� - } - ] + grid: card.plot.grid, + scaling: { + min: 0.5, + max: 2 }, autoResize: true, panning: true, background: { - color: '#ffffff' + color: card.plot.backgroundColor || 'transparent' }, mousewheel: { enabled: true, zoomAtMousePosition: true, - modifiers: 'ctrl', - minScale: 0.5, - maxScale: 2 + modifiers: 'ctrl' }, connecting: { router: 'manhattan', @@ -381,8 +450,8 @@ return new Shape.Edge({ attrs: { line: { - stroke: '#A2B1C3', - strokeWidth: 2, + stroke: '#000000', + strokeWidth: 1, targetMarker: { name: 'block', width: 12, @@ -410,258 +479,111 @@ } }) - // #region 浣跨敤鎻掍欢 - graph - .use(new Transform({ - resizing: true, - rotating: true - })) - .use(new Selection()) - .use(new Snapline()) - .use(new Keyboard()) - .use(new Clipboard()) - .use(new History()) + let cells = [] + xflows.forEach((item) => { + if (item.shape === 'edge') { + cells.push(graph.createEdge(item)) + } else { + cells.push(graph.createNode(item)) + } + }) + graph.resetCells(cells) + graph.positionContent('center') + } - // #region 鍒濆鍖� stencil - const stencil = new Stencil({ - title: '娴佺▼鍥�', - target: graph, - stencilGraphWidth: 180, - stencilGraphHeight: 180, - groups: [ - { - title: '閫氱敤鑺傜偣', - name: 'group1' + lanerender = () => { + const { card } = this.state + + const graph = new Graph({ + container: document.getElementById(card.uuid + 'container'), + scaling: { + min: 0.5, + max: 2 + }, + autoResize: true, + panning: true, + background: { color: '#ffffff' }, + mousewheel: { + enabled: true, + zoomAtMousePosition: true, + modifiers: 'ctrl' + }, + connecting: { + router: 'manhattan', + connector: { + name: 'rounded', + args: { + radius: 8 + } }, - { - title: '鑷畾涔�', - name: 'group2', - graphHeight: 120, - layoutOptions: { - rowHeight: 70 + anchor: 'center', + connectionPoint: 'anchor', + allowBlank: false, + snap: { + radius: 20 + }, + createEdge() { + return new Shape.Edge({ + attrs: { + line: { + stroke: '#000000', + strokeWidth: 1, + targetMarker: { + name: 'block', + width: 12, + height: 8 + } + } + }, + zIndex: 2 + }) + }, + validateConnection({ targetMagnet }) { + return !!targetMagnet + } + }, + highlighting: { + magnetAdsorbed: { + name: 'stroke', + args: { + attrs: { + fill: '#5F95FF', + stroke: '#5F95FF' + } } } - ], - layoutOptions: { - columns: 2, - columnWidth: 80, - rowHeight: 55 - } - }) - - document.getElementById(card.uuid + 'stencil').appendChild(stencil.container) - - // #region 蹇嵎閿笌浜嬩欢 - graph.bindKey(['meta+c', 'ctrl+c'], () => { - const cells = graph.getSelectedCells() - if (cells.length) { - graph.copy(cells) - } - return false - }) - graph.bindKey(['meta+x', 'ctrl+x'], () => { - const cells = graph.getSelectedCells() - if (cells.length) { - graph.cut(cells) - } - return false - }) - graph.bindKey(['meta+v', 'ctrl+v'], () => { - if (!graph.isClipboardEmpty()) { - const cells = graph.paste({ offset: 32 }) - graph.cleanSelection() - graph.select(cells) - } - return false - }) - - // undo redo - graph.bindKey(['meta+z', 'ctrl+z'], () => { - if (graph.canUndo()) { - graph.undo() - } - return false - }) - graph.bindKey(['meta+shift+z', 'ctrl+shift+z'], () => { - if (graph.canRedo()) { - graph.redo() - } - return false - }) - - // select all - graph.bindKey(['meta+a', 'ctrl+a'], () => { - const nodes = graph.getNodes() - if (nodes) { - graph.select(nodes) - } - }) - - // delete - graph.bindKey('backspace', () => { - const cells = graph.getSelectedCells() - if (cells.length) { - graph.removeCells(cells) - } - }) - - // zoom - // graph.bindKey(['ctrl+1', 'meta+1'], () => { - // const zoom = graph.zoom() - // if (zoom < 1.5) { - // graph.zoom(0.1) - // } - // }) - // graph.bindKey(['ctrl+2', 'meta+2'], () => { - // const zoom = graph.zoom() - // if (zoom > 0.5) { - // graph.zoom(-0.1) - // } - // }) - - // 鎺у埗杩炴帴妗╂樉绀�/闅愯棌 - const showPorts = (ports, show) => { - for (let i = 0, len = ports.length; i < len; i += 1) { - ports[i].style.visibility = show ? 'visible' : 'hidden' - } - } - graph.on('node:mouseenter', () => { - const container = document.getElementById(card.uuid + 'container') - const ports = container.querySelectorAll('.x6-port-body') - showPorts(ports, true) - }) - graph.on('node:mouseleave', () => { - const container = document.getElementById(card.uuid + 'container') - const ports = container.querySelectorAll('.x6-port-body') - showPorts(ports, false) - }) - - // graph.on('cell:click', ({ e, x, y, cell, view }) => { - // cell.attr('body/stroke', "red") - // }) - graph.on('node:click', ({ e, x, y, node, view }) => { - this.selectNode = node - - this.setState({node: this.selectNode.store.data}) - }) - graph.on('edge:click', ({ e, x, y, edge, view }) => { - this.selectNode = edge - - this.setState({node: this.selectNode.store.data}) - - let trans = document.querySelectorAll('.x6-widget-transform') - if (trans && trans[0]) { - trans[0].style.display = 'none' - } - }) - graph.on('blank:click', ({ e, x, y }) => { - this.selectNode = null - - this.setState({node: null}) - }) - - const r1 = graph.createNode({ - shape: 'mk-rect', - label: '寮�濮�', - attrs: { - body: { - rx: 20, - ry: 26 - } - } - }) - const r2 = graph.createNode({ - shape: 'mk-rect', - label: '杩囩▼' - }) - const r3 = graph.createNode({ - shape: 'mk-rect', - attrs: { - body: { - rx: 6, - ry: 6 - } }, - label: '鍙�夎繃绋�' - }) - const r4 = graph.createNode({ - shape: 'mk-polygon', - attrs: { - body: { - refPoints: '0,10 10,0 20,10 10,20' + translating: { + restrict(cellView) { + const cell = cellView.cell + const parentId = cell.prop('parent') + + if (parentId) { + const parentNode = graph.getCellById(parentId) + if (parentNode) { + return parentNode.getBBox().moveAndExpand({ + x: 0, + y: 36, + width: 0, + height: -36, + }) + } + } + return cell.getBBox() } - }, - label: '鍐崇瓥' + } }) - const r5 = graph.createNode({ - shape: 'mk-polygon', - attrs: { - body: { - refPoints: '10,0 40,0 30,20 0,20' - } - }, - label: '鏁版嵁' + + let cells = [] + lanes.forEach((item) => { + if (item.shape === 'edge') { + cells.push(graph.createEdge(item)) + } else { + cells.push(graph.createNode(item)) + } }) - const r6 = graph.createNode({ - shape: 'mk-circle', - label: '杩炴帴' - }) - - stencil.load([r1, r2, r3, r4, r5, r6], 'group1') - - const p1 = graph.createNode({ - shape: 'mk-ellipse', - label: 'ellipse' - }) - const p2 = graph.createNode({ - shape: 'mk-star', - label: '' - }) - - stencil.load([p1, p2], 'group2') - - this.mkGraph = graph - } - - setTop = () => { - if (!this.selectNode) { - message.warning('璇烽�夋嫨鑺傜偣锛�') - return - } - this.selectNode.toFront() - } - - setBottom = () => { - if (!this.selectNode) { - message.warning('璇烽�夋嫨鑺傜偣锛�') - return - } - // let cells = this.mkGraph.getCells() - this.selectNode.toBack() - } - - setZoomIn = () => { - const zoom = this.mkGraph.zoom() - - if (zoom < 2) { - this.mkGraph.zoom(0.1) - } - } - - setZoomOut = () => { - const zoom = this.mkGraph.zoom() - - if (zoom > 0.5) { - this.mkGraph.zoom(-0.1) - } - } - - setZoomInt = () => { - this.mkGraph.zoomTo(1) - } - - save = () => { - // let nodes = this.mkGraph.toJSON() + graph.resetCells(cells) + graph.positionContent('top') } updateComponent = (card) => { @@ -707,14 +629,14 @@ } render() { - const { card, toolunfold, nodeunfold, node } = this.state + const { card } = this.state let _style = resetStyle(card.style) return ( <div className="menu-x6-chart-edit-box" style={_style} onClick={this.clickComponent} id={card.uuid}> <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={ <div className="mk-popover-control"> - <ChartCompileForm config={card} plotchange={this.updateComponent}/> + <ChartCompileForm config={card} plotchange={this.plotchange}/> <CopyComponent type="antvG6" card={card}/> <FontColorsOutlined className="style" title="璋冩暣鏍峰紡" onClick={this.changeStyle}/> <DeleteOutlined className="close" title="delete" onClick={() => this.props.deletecomponent(card.uuid)}/> @@ -726,42 +648,33 @@ <NormalHeader config={card} updateComponent={this.updateComponent}/> <div className="mk-toolbar"> <div className="left-tool"> - <Tooltip title="缃墠"> - <VerticalAlignTopOutlined onClick={this.setTop}/> - </Tooltip> - <Tooltip title="缃悗"> - <VerticalAlignBottomOutlined onClick={this.setBottom}/> - </Tooltip> + {card.plot.subtype === 'xflow' ? <Tooltip title="缃墠"> + <VerticalAlignTopOutlined/> + </Tooltip> : null} + {card.plot.subtype === 'xflow' ? <Tooltip title="缃悗"> + <VerticalAlignBottomOutlined/> + </Tooltip> : null} <Tooltip title="淇濆瓨"> - <SaveOutlined onClick={this.save}/> + <SaveOutlined/> </Tooltip> + {card.plot.export === 'png' ? <Tooltip title="瀵煎嚭鍥剧墖"> + <DownloadOutlined/> + </Tooltip> : null} </div> <div className="right-tool"> <Tooltip title="鏀惧ぇ"> - <ZoomInOutlined onClick={this.setZoomIn}/> + <ZoomInOutlined/> </Tooltip> <Tooltip title="缂╁皬"> - <ZoomOutOutlined onClick={this.setZoomOut}/> + <ZoomOutOutlined/> </Tooltip> <Tooltip title="1:1"> - <OneToOneOutlined onClick={this.setZoomInt}/> + <OneToOneOutlined/> </Tooltip> </div> </div> <div className="canvas" style={{width: '100%', minHeight: card.plot.height, height: card.plot.height}} id={card.uuid + 'canvas'}> - <div id={card.uuid + 'stencil'} className={'mk-stencil ' + (toolunfold ? '' : 'merge')}> - <div className="tool-control" onClick={() => this.setState({toolunfold: !toolunfold})}> - <DoubleLeftOutlined /> - </div> - </div> <div id={card.uuid + 'container'} className="mk-container"></div> - <div className={'mk-node-edit ' + (nodeunfold ? '' : 'merge')}> - <div className="tool-control" onClick={() => this.setState({nodeunfold: !nodeunfold})}> - <DoubleLeftOutlined /> - </div> - <div className="header">璁剧疆</div> - {!node ? <div className="empty">鏈�変腑</div> : <div></div>} - </div> </div> <div className="component-name"> <div className="center"> -- Gitblit v1.8.0