使用Antv X6在react中实现流程图后续

继上篇:react使用AntvX6实现流程图-CSDN博客文章浏览阅读145次。如何使用AntV X6在react中插入流程图功能?https://blog.csdn.net/weixin_48133091/article/details/141189733?spm=1001.2014.3001.5501的一些实现细节及问题的解决和优化:

实现流程图的保存

1、点击保存按钮,实现流程图的保存

保存功能的实现基于浏览器的localStorage,将流程图转化为可存储的结构,以下为点击保存按钮后的操作

//1.获取画布上所有节点和边的信息
 const nodes = graph.getNodes().map((node) => {
    return node.toJSON()
  })
 const edges = graph.getEdges().map((edge) => {
    return edge.toJSON()
  })
//2.创建一个数据对象来存储节点和边的信息
 const flowchartData = {
    nodes:nodes,
    edges:edges
 }
//3.序列化数据并将数据存储到本地存储中
 const serializedData = JSON.stringify(flowchartData)
 localStorage.setItem('flowchartData',serializedData)
 console.log('flowchartData',serializedData)//打印数据

2、在图的初始化的时候读取localStorage的数据,从内存中拿到保存的数据展示为流程图呈现在画布上,以下写在图的初始化部分,在图进行初始化的时候会从localStorage中拿到上次绘制的流程图:

    //从localStorage中读取数据
    const data = localStorage.getItem('flowchartData')
    if(data){
      //解析JSON数据
      const parsedData = JSON.parse(data)
      //重新添加节点
      parsedData.nodes.forEach((nodeData:any)=>{
        //添加节点
        this.graph.addNode(nodeData)
      })
      //重新添加边
      parsedData.edges.forEach((edgeData:any)=>{
        //添加边
        this.graph.addEdge(edgeData)
      })
    }

放大和缩小的实现

在useEffect函数中增加

    // zoom
    setZoom(graph.zoom())
    graph.on('scale', () => {
      setZoom(graph.zoom())
    })

在按钮中,

      case 'zoomIn':
        graph.zoom(0.1)
        break
      case 'zoomOut':
        graph.zoom(-0.1)
        break

撤销和重做

在useEffect中:

    // history
    const { history } = graph
    setCanUndo(history.canUndo())
    setCanRedo(history.canRedo())
    history.on('change', () => {
      setCanUndo(history.canUndo())
      setCanRedo(history.canRedo())
    })

在按钮中:

case 'undo':
     graph.history.undo()
     break
case 'redo':
    graph.history.redo()
    break

使用AntV X6的人工智能建模DAG图需要以下步骤: 1. 安装AntV X6库 可以使用npm进行安装,命令如下: ``` npm install @antv/x6 --save ``` 2. 创建画布 使用X6创建一个画布,将其添加到页面。代码如下: ```javascript import { Graph } from '@antv/x6'; const graph = new Graph({ container: document.getElementById('container'), width: 800, height: 600 }); ``` 3. 创建节点 使用X6创建一个节点,并设置其参数。代码如下: ```javascript const node = graph.addNode({ x: 40, y: 40, width: 80, height: 40, label: 'AI', shape: 'rect', attrs: { body: { rx: 5, ry: 5, fill: '#a0d911', stroke: '#096dd9', strokeWidth: 1, }, label: { textAnchor: 'left', refX: 10, fill: '#fff', fontSize: 14, fontFamily: 'Microsoft YaHei', }, }, }); ``` 4. 创建连线 使用X6创建两个节点之间的连线。需要设置起点、终点、样式等参数。代码如下: ```javascript const edge = graph.addEdge({ source: { cell: node1 }, target: { cell: node2 }, attrs: { line: { stroke: '#1890ff', strokeWidth: 1, targetMarker: { name: 'classic', size: 8, }, }, }, }); ``` 5. 添加交互 可以使用X6添加节点、连线的拖动、缩放等交互。代码如下: ```javascript graph.addNodeTool('my-custom-tool', MyCustomTool); graph.addEdgeTool('my-custom-tool', MyCustomTool); graph.bindKey('backspace', () => { const cells = graph.getSelectedCells(); if (cells.length) { graph.removeCells(cells); } }); graph.on('node:click', ({ e, x, y, node }) => { console.log(`click on node: ${node.id}`); }); ``` 以上就是使用AntV X6库创建人工智能建模DAG图的基本步骤。需要根据实际需求进行调整和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值