2.6.76 • Published 2 years ago

kgraph v2.6.76

Weekly downloads
9
License
ISC
Repository
-
Last release
2 years ago

kGraph

定制化实现流程绘制工具

特点

  • 基于antv g6的二次开发

安装

npm install kgraph

开始

最简单的例子

import kg from './kgraph'

const graph = new kg.Graph()

配置项 / Options

container

string HTMLElement

选择器 or dom对象,默认body,设置则在指定容器内初始化组件。

canvas

string HTMLElement

选择器 or dom对象,不指定则默认生成一个canvas并插入容器中,宽高自适应容器大小

width

number

宽度,默认撑满全屏

height

number

高度,默认撑满全屏

diagramWidth

number

绘图范围宽度,默认 800

diagramHeight

number

绘图范围高度,默认 400

fitcanvas

boolean

绘图范围是否默认撑满画布。默认true。
指定true时绘制范围小于画布的时候会撑满画布。
指定false时绘制范围尺寸为指定高度宽度。

originRatio

number

分辨率(较低值在高分辨率显示器下会模糊),默认2

enableRubberband

boolean

rubberhand 多个框选功能

enableScroll

boolean

滚动条, 默认true

bgColor

string

画布背景色,默认#000

网格配置项 Options - Grid

网格配置

grid.show

boolean

启用网格,默认false

grid.align

boolean

网格对齐,默认false

grid.size

number

网格大小,默认10

API

Graph.addItem( type, options )

  • 参数:

    • { String } type 节点类型
    • { Object } options 节点配置
  • 用法:

    const graph = new kg.Graph()
    const shape = {} // 参考式样配置
    const parentId = '' // 父级节点的ID
    graph.addItem('outline', {
        cfgKey: 'outline', // 配置对应的key
        x: 0, // 节点所处的X轴坐标
        y: 0, // 节点所处的Y轴坐标
        parent: parentId,
        ...shape
    })

Graph.updateItem(item, options)

  • 参数:

    • { Item } item 节点实例
    • { Object } options 节点配置
  • 用法:

    item为新增完成的节点实例,options与addItem相同

Graph.remove( Id )

  • 参数:
    • { String } Id 节点ID

  • 用法:
    const graph = new kg.Graph()
    const item = graph.addItem({})
    const id = item.get('id')
    graph.removeItem(id)

Graph.abandonRemoveItem()

  • 用法: 阻止删除节点的操作,搭配beforeRemoveItem钩子使用

Graph.clear()

  • 用法:

    清空画布绘制内容

Graph.clearData()

  • 用法:

    清空画布数据

Graph.getData()

  • 用法:

    获取画布数据,可用于画布回显

Graph.getBox()

  • 用法:

    获取container的BoundingClientRect

Graph.findById(ID)

  • 用法:

    通过ID获取节点

Graph.findShapeById(ID)

  • 用法:

    通过ID获取Shape

Graph.changeSize(width, height)

  • 用法:

    改变画布容器大小

Graph.getPointByClient(x, y)

  • 用法 :

    通过x,y,获取窗口内的点

Graph.getPointByClient(x, y)

  • 用法 :

    通过x,y,获取画布内的点

Graph.setAutoPaint( Flag )

  • 参数:

    • { Boolean } flag
  • 用法:

    是否自定绘制,addItem、updatePosition等操作会触发自动绘制

    graph.setAutoPaint(false)

Graph.autoPaint()

  • 用法:

    绘制一次画布


基础节点

节点类型

  • Node // 各种 自己理解
  • Edge // 线
  • Anchor // 锚点

Vue节点

组件 test

<template>
    <div>{{ a }}</div>
</template>
<script></script>
<style><style>
  • 1.通过$vue.create创建vue对象,指定parent后插入,可以配合afterAddItem使用

      const graph = kg.Graph(options)
      
      graph.add({
          props: {
              vue: {
                  props: {
                      a: 1
                  }
              }
          }
      })
      
      graph.on('afterAddItem', item => {
          graph.$vue.create({
            parent: item
            component: test,
            item.get('props').vue
          })
      })
  • 2: node类型节点,配置vueComponent。

      const graph = kg.Graph(options)
          
      graph.add({
          vueComponent: test,
          props: {
              vue: {
                  props: {
                      a: 1
                  }
              }
          }
      })

注册节点

自定义节点类型


式样配置

<!--节点的样式-->

rect: {
    // 基础样式
    shape: {
      type: 'rect', // 形状
      size: [50, 50], // 宽高,circle只需要填一个
      style: { }, // 参考canvas的样式属性 lineWidth fill strokeStyle ..
    },
    
    // 节点不同状态下对应的样式
    stateShapeMap: {
      default: {
        type: 'rect',
        size: [50, 50],
        style: {
          stroke: '#00678a',
          fill: '#eee',
          lineWidth: 2,
        }
      },
      hover: {
        type: 'rect',
        size: [50, 50],
        style: {
          stroke: '#00678a',
          fill: '#000',
          lineWidth: 2,
        }
      }
    },
    
    // 自定义属性
    props: {
      key: 'start',
      value: 'Start'
    },
    
    
    anchorOffset: {}, // 锚点偏移
    
    anchorMatrix: [[0.5, 0], [1, 0.5], [0.5, 1], [0, 0.5]], // 锚点 matrix
    
    labelCfg: {
      content: '', // label内容
      
      offsetX: 0, // 相对偏移x 相对的是新增节点的中心坐标
      
      offsetY: 60, // 相对偏移y 相对的是新增节点的中心坐标
      
      style: {
        color: '#F00',
        size: '14px'
      },
      
      hidden: true // 是否显示label
    },
    event: true // 是否对节点绑定上所有画布事件
    
    eventArea: { r: 10 } // 节点的事件范围,不配置则以节点size作为事件范围
}

事件监听

/*
* type: 参考下方事件列表及内置钩子
* fn: 回调函数
* 默认阻止一切冒泡
*/
graph.on(type, fn)

事件触发

/*
* type: 参考下方事件列表及内置钩子
*/
graph.emit(type)

全局事件

名称说明回调参数
click单击event
dblclick双击event
contextmenu右键菜单event
mousedown鼠标按下event
mouseup鼠标松开event
mouseenter鼠标移入event
mouseleave鼠标移出event
mousemove鼠标移动event
focus焦点在画布上event
blur焦点移出画布event

节点事件

名称说明回调参数
click单击event
dblclick双击event
contextmenu右键菜单event
mousedown鼠标按下event
mouseup鼠标松开event
mouseenter鼠标移入event
mouseout鼠标移出event
mouseover鼠标移入event
mousemove鼠标移动event
mouseleave鼠标移出event
dragstart拖拽开始event
dragend拖拽结束event
drag拖拽中event
dragenter拖拽进入event
dragleave拖拽离开event
drop放下event
focus聚焦在节点上event
blur焦点离开节点event

回调参数Event

名称说明
type事件类型
origin原生event
items触发时间的节点列表
clientX画布中的绝对坐标X
clientY画布中的绝对坐标Y
target监听事件的节点对象

内置钩子

名称说明回调参数
beforeAddItem节点添加前config 配置参数
afterAddItem节点添加后item 节点
beforeAddShape图形添加前
afterAddShape图形添加后
beforeUpdateItem节点更新前item 节点 config 配置参数
afterUpdateItem节点更新后item 节点
beforeRemoveItem节点删除前item 作用节点
afterRemoveItem节点删除后item 已删节点
beforePaint流程绘制前
afterPaint流程绘制后

演示地址

2.6.48

3 years ago

2.6.49

3 years ago

2.6.44

3 years ago

2.6.45

3 years ago

2.6.46

3 years ago

2.6.47

3 years ago

2.6.43

3 years ago

2.6.50

3 years ago

2.6.59

3 years ago

2.6.55

3 years ago

2.6.56

3 years ago

2.6.57

3 years ago

2.6.58

3 years ago

2.6.51

3 years ago

2.6.52

3 years ago

2.6.53

3 years ago

2.6.54

3 years ago

2.6.60

3 years ago

2.6.61

3 years ago

2.6.66

2 years ago

2.6.67

2 years ago

2.6.68

2 years ago

2.6.69

2 years ago

2.6.62

3 years ago

2.6.63

2 years ago

2.6.64

2 years ago

2.6.65

2 years ago

2.6.70

2 years ago

2.6.71

2 years ago

2.6.72

2 years ago

2.6.73

2 years ago

2.6.74

2 years ago

2.6.75

2 years ago

2.6.76

2 years ago

2.6.19

3 years ago

2.6.15

3 years ago

2.6.16

3 years ago

2.6.17

3 years ago

2.6.18

3 years ago

2.6.11

3 years ago

2.6.12

3 years ago

2.6.13

3 years ago

2.6.14

3 years ago

2.6.26

3 years ago

2.6.27

3 years ago

2.6.28

3 years ago

2.6.29

3 years ago

2.6.22

3 years ago

2.6.23

3 years ago

2.6.24

3 years ago

2.6.25

3 years ago

2.6.20

3 years ago

2.6.21

3 years ago

2.6.37

3 years ago

2.6.38

3 years ago

2.6.39

3 years ago

2.6.33

3 years ago

2.6.34

3 years ago

2.6.35

3 years ago

2.6.36

3 years ago

2.6.30

3 years ago

2.6.31

3 years ago

2.6.32

3 years ago

2.6.40

3 years ago

2.6.41

3 years ago

2.6.42

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.98

3 years ago

2.5.99

3 years ago

2.5.97

3 years ago

2.5.96

3 years ago

2.5.93

3 years ago

2.5.94

3 years ago

2.5.95

3 years ago

2.5.92

3 years ago

2.5.91

3 years ago

2.5.9

3 years ago

2.5.6

3 years ago

2.5.5

3 years ago

2.5.8

3 years ago

2.5.7

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.5.4

3 years ago

2.5.3

3 years ago

2.4.3

3 years ago

2.4.2

3 years ago

2.4.4

3 years ago

2.5.0

3 years ago

2.4.7

3 years ago

2.4.6

3 years ago

2.4.9

3 years ago

2.4.8

3 years ago

2.4.61

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.81

3 years ago

2.3.8

3 years ago

2.3.7

3 years ago

2.3.9

3 years ago

2.3.4

3 years ago

2.3.6

3 years ago

2.3.5

3 years ago

2.3.3

3 years ago

2.3.0

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.2.9

3 years ago

2.2.5

3 years ago

2.2.7

3 years ago

2.2.6

3 years ago

2.2.8

3 years ago

2.2.3

3 years ago

2.2.4

3 years ago

2.2.2

3 years ago

2.1.9

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.2

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago