Vue2.0+AntvX6——画布graph

一、平移、缩放、居中

画布平移

普通画布(未开启 scroller 模式)通过开启 panning 选项来支持拖拽平移。常用的属性如下:
enabled:true || fale,
modifiers:拖拽可能和其他操作冲突,设置修饰键后需要按下修饰键并点击鼠标才能触发画布拖拽。
eventTypes:设置触发画布拖拽的行为,支持 leftMouseDown、 rightMouseDown、mouseWheel, 默认为 [‘leftMouseDown’]

 initGraph() {
      this.graph = new Graph({
        container: this.$refs.container,
        width: 800,
        height: 600,
        // 画布背景&网格
        background: {
          color: '#fffbe6' // 设置画布背景颜色
        },
        grid: {
          size: 10, // 网格大小 10px
          visible: true // 渲染网格背景
        },
        // 画布平移
        // panning:true,
        panning: {
          enabled: true,
          //  modifiers:拖拽可能和其他操作冲突,,
          // 设置修饰键后需要按下修饰键并点击鼠标才能触发画布拖拽。
          modifiers: 'shift',
          // eventTypes:设置触发画布拖拽的行为,支持 leftMouseDown、 rightMouseDown、mouseWheel,
          // 默认为 ['leftMouseDown']
          eventTypes: ['leftMouseDown', 'rightMouseDown', 'mouseWheel']
        }
      })
      this.graph.fromJSON(this.data)
    }

通过以下 API 来启用/禁止画布平移:
会改变panning的enabled的值

this.graph.isPannable() // 获取画布是否可以平移
this.graph.enablePanning() // 启用画布平移
this.graph.disablePanning() // 禁止画布平移
this.graph.togglePanning() // 切换画布平移状态

    graphApiTest() {
      // this.graph.enablePanning();
      this.graph.disablePanning();
      console.log(this.graph.isPannable(), 'this.graph.isPannable()')
    }

在这里插入图片描述

画布缩放&内容居中

this.graph.zoom() // 获取缩放级别
this.graph.zoom(0.2) // 在原来缩放级别上增加 0.2
this.graph.zoom(-0.2) // 在原来缩放级别上减少 0.2

常用的就是将画布内容中心与视口中心对齐,使用方式:

this.graph.centerContent()
在这里插入图片描述

二、导出

step1 - 引入DataUri

import { Graph, DataUri } from '@antv/x6'

导出 SVG:

step2- 调用方法
this.graph.toSVG():
第一个参数:函数,
第二个参数:对象,用来设置图片属性(本人觉得不太好配置)

interface ToSVGOptions {
preserveDimensions?: boolean | Size
viewBox?: Rectangle.RectangleLike
copyStyles?: boolean
stylesheet?: string
serializeImages?: boolean
beforeSerialize?: (this: Graph, svg: SVGSVGElement) => any
}

toSvg() {
   this.graph.toSVG(dataUri => {
    DataUri.downloadDataUri(DataUri.svgToDataUrl(dataUri), 'chart.svg')
  })
}
//SVG图片配置
this.graph.toSVG((dataUri: string) => {
  // todo
}, {
  preserveDimensions: {
    width: 100,
    height: 100,
  }
})

(有会配置这个的大神,欢迎评论交流)
在这里插入图片描述

导出 PNG/JPEG:

step2 - 调用方法
this.graph.toPNG():
第一个参数:函数,
第二个参数:对象,用来设置图片属性

interface ToImageOptions {
width?: number
height?: number
backgroundColor?: string
padding?: NumberExt.SideOptions
quality?: number,
…以及svg的所有参数…
}

// 下载
    toPngJpeg() {
      this.graph.toPNG(
        dataUri => {
          DataUri.downloadDataUri(dataUri, 'chart.png')
        },
        {
          width: 300,
          heihgt: 300,
          backgroundColor: 'pink',
          padding: {
            top: 20,
            right: 30,
            bottom: 40,
            left: 50
          },
          //图片质量,可以从 0 到 1 的区间内选择图片的质量。
          //如果超出取值范围,将会使用默认值 0.92
          quality:0.93
        }
      )
    }

在这里插入图片描述

三、销毁画布

调用 graph.dispose() 方法进行画布的销毁以及资源的回收。
el元素被销毁

    // 销毁画布
    graphDispose() {
      this.graph.dispose()
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值