G6 总结

1 篇文章 0 订阅

我学习G6看的几篇文章:
1、G6入门
2、关于antV G6中的on事件、util.each事件及update方法等的使用总结
3、tooltip 节点提示框
4、G6图形化

1、拖拽、缩放——内置的交互行为
在 G6 中使用内置 Behavior 的方式非常简单,只需要在图实例化时配置 modes。拖拽和缩放属于 G6 内置交互行为,修改代码如下:

const graph = new G6.Graph({
  // ...                                          // 其他配置项
  modes: {
    default: ['drag-canvas', 'zoom-canvas', 'drag-node'], // 允许拖拽画布、放缩画布、拖拽节点
  },
});

2、点击事件

     // 节点左击
      graph.on('node:click', function(evt) {
        //获取当前节点数据信息
        console.log('节点左击', evt)
        alert('节点左击')
      }),
        //  节点右击
      graph.on('node:contextmenu', function(evt) {
          //当前节点定位
          console.log('节点右击', evt)
          alert('节点右击')
      })

3、线路

      defaultEdge: {
          type: 'line',
          style: {
            endArrow: true,
            endArrow: {
              path: G6.Arrow.triangle(5, 10, 10), // 使用内置箭头路径函数,参数为箭头的 宽度、长度、偏移量(默认为 0,与 d 对应)
              d: 10,
                // path: G6.Arrow.triangle(5, 10, 10),
                // d: 8
                // path: 'M 0,0 L 8,4 L 8,-4 Z',
                // fill: '#e2e2e2'
              fill: '#ccc'
            },
            style: {
              stroke: '#eaff8f',
              lineWidth: 0
            }
          }
        }

4、布局

     layout: {
          type: 'dagre',
          rankdir: 'LR'
     },

5、 贝塞尔曲线 + 线上加圆点
6、填充颜色

 style: {
   fill: '#67c23a' ,
   lineWidth: 1
 },

7、线上加文字

edges  数据:
     target: item.target,
     source: item.source,
     style: item.style,
     label: '正在执行',

8、节点使用图片

nodes  数据:
	shape: 'image',
	img: this.pngSrc,
	size: item.type == '1' ? [50, 50] : 50,

9、画布的清空

 this.graph.destroy()
 this.init()

10、画布自适应 显示全部的节点

	fitView:'autoZoom',

11、缩略图

    const minimap = new G6.Minimap({
        size: [100, 100],
        className: 'minimap',
        type: 'delegate'
      })
	plugins: [minimap]  //配置插件

12、更新节点,不重新绘制

		const node = this.graph.findById(id)
        this.graph.updateItem(node, {
          //节点大小
          size: 50,
          // 节点上文本的样式
          labelCfg: {
            style: {
              fill: '#000',
              fontSize: 15
            }
          }
        })

12、动态计算长度 (解决label文字长短不一的方案)

nodeStyleConfig(label) {
      let width = label.length
      let height = 30
      let size
      let reg = /[a-z,A-Z,_]/
      if (reg.test(label)) {
        size = [(width * this.fontSize) / 2 + 30, height]
      } else {
        size = [width * this.fontSize, height]
      }
      return size
    },

13、保持缩放比例

//在拉取新数据重新渲染页面之前先获取当前缩放比例
const zoom = graph.getZoom();

//此处获取数据
await getData()

//缩放至之前的比例
graph.zoomTo(zoom);

14、保持画布位置

//在拉取新数据重新渲染页面之前先获取点(0, 0)在画布上的位置
const lastPoint = graph.getCanvasByPoint(0, 0);

//此处获取数据。。。
await getData()

//获取重新渲染之后点(0, 0)在画布的位置
const newPoint = graph.getCanvasByPoint(0, 0);

//移动画布相对位移
graph.translate(lastPoint.x - newPoint.x, lastPoint.y - newPoint.y);

15、画布宽高为 自适应

	  //parentContent 为父级 div
      this.canvasWidth = this.$refs.parentContent.clientWidth
      this.canvasHeight = this.$refs.parentContent.clientHeight
      this.graph = new G6.Graph({
        container: 'container',
        width: this.canvasWidth,
        height: this.canvasHeight
      })

15、tooltip 节点提示框
在这里插入图片描述

modes: {
    default: [
      'drag-canvas',
      'zoom-canvas',
      'drag-node',
      {
        type: 'tooltip',
        // offset: -50,
        formatText: function formatText(model) {
          return model.oriLabel || model.label          //   <----------- 关键
        },
        shouldUpdate: function shouldUpdate(e) {
          return true
        }
      }
    ] // 允许拖拽画布、放缩画布、拖拽节点
  }

16、G6画布大小自适应 关键 ===> changeSize
浏览器窗口发生变化时,画布自适应窗口大小
G6图 进行放大缩小,全屏操作

fullScreen() {
   this.isFullScreen = true
   this.$emit('isFullScreenFn', this.isFullScreen)
   // this.graph.destroy()
   setTimeout(() => {
     // this.render()
     var canvasWidth = parseInt(document.getElementsByClassName('bloodBorne')[0].offsetWidth)
     var canvasHeight = parseInt(document.getElementsByClassName('bloodBorne')[0].offsetHeight)
     this.graph.changeSize(canvasWidth, canvasHeight)
     //this.graph.fitCenter()  // 将图移动到画布中心位置
   }, 300)
 },
  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
AntV G6 是一款基于 JavaScript 的图形绘制库,它提供了丰富的图形绘制能力和交互功能,可以用于构建各种类型的可视化应用。接下来,我们将重点介绍 G6 的重做功能文档。 ## 重做功能概述 重做功能是指用户在撤销操作后可以重新执行之前撤销的操作。在 G6 库中,重做功能可以帮助用户恢复之前已经撤销的图形编辑操作,提高用户的使用体验。 ## 重做功能使用说明 ### 实现步骤 G6 中实现重做功能的步骤如下: 1. 定义一个 undoStack 数组,用于记录用户的撤销操作。 ```javascript const undoStack = []; ``` 2. 在用户执行操作时,将操作记录到 undoStack 数组中。 ```javascript graph.updateItem(item, attrs); // 执行更新操作 undoStack.push({ type: 'update', item, attrs }); // 记录操作 ``` 3. 当用户撤销操作时,从 undoStack 数组中取出最后一个操作记录,并执行相应的撤销操作。 ```javascript const lastAction = undoStack.pop(); switch (lastAction.type) { case 'update': graph.updateItem(lastAction.item, lastAction.attrs); break; // 处理其他类型的操作 } ``` 4. 当用户重做操作时,从 undoStack 数组中取出最后一个撤销操作记录,并执行相应的操作。 ```javascript const lastUndoAction = undoStack.pop(); // 将操作记录到 redoStack 数组中 redoStack.push(lastUndoAction); switch (lastUndoAction.type) { case 'update': graph.updateItem(lastUndoAction.item, lastUndoAction.attrs); break; // 处理其他类型的操作 } ``` 5. 当用户进行新的操作时,清空 redoStack 数组。 ```javascript redoStack = []; ``` ### 注意事项 在实现重做功能时,需要注意以下几点: 1. 操作记录的数据结构需要包含操作类型、操作对象和操作属性等信息,以便在撤销和重做操作时能够正确地还原操作。 2. 操作记录需要及时地存储到 undoStack 数组中,否则当用户进行撤销操作时无法找到之前的操作记录。 3. 在进行撤销和重做操作时,需要判断 undoStack 和 redoStack 数组是否为空,以避免出现数组越界等错误。 4. 当用户进行新的操作时,需要清空 redoStack 数组,否则之前的撤销操作记录将无法再次执行。 ## 总结 通过以上介绍,我们可以看到 G6 中实现重做功能的方法非常简单,只需要记录用户的操作记录,并在撤销和重做操作时正确地还原操作即可。在实际应用中,我们可以根据具体场景灵活地使用重做功能,提高用户的使用体验。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值