使用 auto-drawing 画一个鱼骨图

auto-drawing 官方文档

基于 vue 环境

安装

1
yarn add  auto-drawing

代码

fishbone.vue

1
<template>
2
  <div class="container">
3
    <div ref="mainElementRef" class="main-element" />
4
  </div>
5
</template>
6
7
<script lang="ts" setup>
8
  // 鱼骨图
9
  import { ElMessage } from 'element-plus'
10
  import {
11
    onMounted,
12
    onBeforeUnmount,
13
    shallowReactive,
14
    ref,
15
    reactive
16
  } from 'vue'
17
  import type { ZRenderType, ZRenderGroup, ShapeCoreType } from 'auto-drawing'
18
  import { createCanvas, createGroup, renderCanvas } from 'auto-drawing'
19
  import type { Params } from './utils'
20
  import { getCircle, getLine, getText } from './utils'
21
22
  interface IState {
23
    zr: Nullable<ZRenderType>
24
    group: Nullable<ZRenderGroup>
25
    clickGroup: Nullable<ZRenderGroup>
26
  }
27
28
  type Direction = 'left' | 'right'
29
30
  const props = defineProps({
31
    /**
32
     * 画布宽
33
     */
34
    width: {
35
      type: Number,
36
      default: 300
37
    },
38
    /**
39
     * 画布高
40
     */
41
    height: {
42
      type: Number,
43
      default: 300
44
    }
45
  })
46
47
  const state = shallowReactive<IState>({
48
    zr: null,
49
    group: null,
50
    clickGroup: null
51
  })
52
  const mainElementRef = ref<any>(null)
53
  // 基本配置
54
  const baseOptions = { x: props.width / 2, y: props.height / 2 }
55
56
  onMounted(() => {
57
    state.zr = createCanvas(
58
      mainElementRef.value as HTMLDivElement
59
    ) as ZRenderType
60
    state.group = createGroup(baseOptions) as ZRenderGroup
61
    state.clickGroup = createGroup(baseOptions) as ZRenderGroup
62
    state.zr.setBackgroundColor('#fff')
63
64
    // 原数据
65
    const originData: string[] = [...new Array(10)].map((_, index) =>
66
      String(index)
67
    )
68
69
    // 画布两边留白
70
    const gutter = 40
71
    // 鱼刺往后斜的距离
72
    const angleLength = props.width / 20
73
    // 鱼刺方向
74
    const direction: Direction = 'left'
75
    // 鱼刺长度
76
    const fishboneLength = (props.height / 2 - gutter) / 2
77
    // 主轴的基本坐标
78
    const base = props.width / 2 - gutter
79
    // 主轴数据
80
    const main = originData.slice(0, 2)
81
    // 鱼刺数据
82
    const body = originData.slice(2)
83
    // 主轴上面鱼刺数据
84
    const bodyTop = body.slice(0, Math.ceil(body.length / 2))
85
    // 主轴下面鱼刺数据
86
    const bodyBottom = body.slice(Math.ceil(body.length / 2))
87
    // 主抽的点数量
88
    const pointCount = Math.max(bodyTop.length, bodyBottom.length)
89
    // 主抽每个点之间的间距
90
    const pointStep = (props.width - gutter * 2) / (pointCount + 1)
91
92
    // 生成主轴点数据
93
    const point = [...new Array(pointCount)].map((item, index) => {
94
      return [-base + (index + 1) * pointStep, 0]
95
    })
96
97
    // 生成鱼刺方法
98
    const getData = (item: string, index: number, type: string) => {
99
      const mark = type === 'top' ? -1 : 1
100
      const directionMark = direction === 'left' ? -1 : 1
101
      const [baseX, baseY] = point[index]
102
      const params: Params = {
103
        start: point[index],
104
        end: [baseX + angleLength * directionMark, fishboneLength * mark],
105
        title: item,
106
        pointType: 'basicPoint',
107
        tag: 'start'
108
      }
109
      // 生成主轴基点圆图形数据
110
      const baseCircle = getCircle(baseX, baseY, params)
111
      const [x1, y1] = baseCircle.params.start as number[]
112
      const [x2, y2] = baseCircle.params.end as number[]
113
      // 生成鱼刺圆点图形数据
114
      const bodyTop = getCircle(x2, y2, {
115
        ...baseCircle.params,
116
        pointType: 'endpoint',
117
        tag: 'end'
118
      })
119
      // 生成鱼刺直线图形数据
120
      const line = getLine(x1, y1, x2, y2)
121
      // 生成鱼刺文字图形数据
122
      const offset = mark === -1 ? -24 : 14
123
      const text = getText(x2 - 10, y2 + offset, baseCircle.params.title)
124
      return { type: 'group', data: [baseCircle, bodyTop, line, text], params }
125
    }
126
127
    /** 上鱼刺数据处理 */
128
    const bodyTopData = bodyTop.map((item, index) =>
129
      getData(item, index, 'top')
130
    )
131
132
    /** 下鱼刺数据处理 */
133
    const bodyBottomData = bodyBottom.map((item, index) =>
134
      getData(item, index, 'bottom')
135
    )
136
137
    /** 主抽数据处理 */
138
    const mainCircleData = main.map((item, index) => {
139
      const cx = index === 0 ? -base : base
140
      const cy = 0
141
      const title = item
142
      const params: Params = {
143
        title: item,
144
        cx,
145
        cy,
146
        pointType: 'endpoint',
147
        point: [cx, cy]
148
      }
149
      // 主抽圆点图数据
150
      const circle = getCircle(cx, 0, params)
151
      // 主抽文字图数据
152
      const x = cx - 6
153
      const y = cy - 24
154
      const text = getText(x, y, title)
155
      return { type: 'group', data: [circle, text], params }
156
    })
157
    const mainLineMap = mainCircleData.map(
158
      item => item.params.point as number[]
159
    )
160
    const [[x1, y1], [x2, y2]] = mainLineMap
161
    // 主抽直线图数据
162
    const mainLineData = getLine(x1, y1, x2, y2)
163
    // 主抽整体图数据
164
    const mainData = [...mainCircleData, mainLineData]
165
166
    // 所有数据
167
    const data = [
168
      ...mainData,
169
      ...bodyTopData,
170
      ...bodyBottomData
171
    ] as ShapeCoreType[]
172
173
    renderCanvas(state.zr, state.group, data, { scale: true, translate: true })
174
    renderCanvas(state.zr, state.clickGroup, [], {
175
      scale: true,
176
      translate: true
177
    })
178
179
    state.zr.on('click', (e: any) => {
180
      console.log(e?.target)
181
      const { shape, type } = e?.target || {}
182
      const params = (e?.target?.params as Params) || {}
183
      if (!shape || type !== 'circle' || params.pointType !== 'endpoint') return
184
      const data: ShapeCoreType = {
185
        type: 'circle',
186
        ...shape,
187
        stroke: 'red',
188
        fill: 'red',
189
        zlevel: 2
190
      }
191
      // 移除之前的图形
192
      state.clickGroup?.removeAll()
193
      renderCanvas(
194
        state.zr as ZRenderType,
195
        state.clickGroup as ZRenderGroup,
196
        [data],
197
        {
198
          scale: true,
199
          translate: true
200
        }
201
      )
202
      setTimeout(() => {
203
        ElMessage.success('点了我:' + params.title)
204
      })
205
    })
206
  })
207
208
  onBeforeUnmount(() => {
209
    // 销毁画布
210
    state.zr && state.zr.dispose()
211
  })
212
213
  const containerCSS = reactive({
214
    width: props.width + 'px',
215
    height: props.height + 'px'
216
  })
217
</script>
218
219
<style lang="scss" scoped>
220
  .container {
221
    height: v-bind('containerCSS.height');
222
    width: v-bind('containerCSS.width');
223
    overflow: hidden;
224
    padding: 0;
225
    .main-element {
226
      padding: 0;
227
    }
228
  }
229
</style>

utils.ts

1
export type Params = {
2
  /**
3
   * 开始坐标
4
   */
5
  start?: number[]
6
  /**
7
   * 结束坐标
8
   */
9
  end?: number[]
10
  /**
11
   * 点坐标
12
   */
13
  point?: number[]
14
  /**
15
   * 标题
16
   */
17
  title: string
18
  /**
19
   * 圆点的类型  basicPoint:基点 不可点击   endpoint:端点 可点击
20
   */
21
  pointType: 'basicPoint' | 'endpoint' | ''
22
  /**
23
   * 圆的圆心x坐标
24
   */
25
  cx?: number
26
  /**
27
   * 圆的圆心y坐标
28
   */
29
  cy?: number
30
31
  /**
32
   * 当前图形标记
33
   */
34
  tag?: string
35
}
36
37
/**
38
 * 生成圆
39
 * @param cx
40
 * @param cy
41
 * @param params
42
 * @returns
43
 */
44
export const getCircle = (
45
  cx: number,
46
  cy: number,
47
  params: Params = { title: '', pointType: '' }
48
) => ({
49
  type: 'circle',
50
  cx: cx,
51
  cy: cy,
52
  r: 8,
53
  fill: '#fa8423',
54
  stroke: '#fa8423',
55
  zlevel: 1,
56
  params: { ...params }
57
})
58
59
/**
60
 * 生成直线
61
 * @param x1
62
 * @param y1
63
 * @param x2
64
 * @param y2
65
 * @returns
66
 */
67
export const getLine = (x1: number, y1: number, x2: number, y2: number) => ({
68
  type: 'line',
69
  x1,
70
  y1,
71
  x2,
72
  y2,
73
  stroke: '#000',
74
  fill: '#000'
75
})
76
77
/**
78
 * 生成文字
79
 * @param x
80
 * @param y
81
 * @param text
82
 * @returns
83
 */
84
export const getText = (x: number, y: number, text: string) => ({
85
  type: 'text',
86
  x,
87
  y,
88
  text: text,
89
  fontSize: 14,
90
  fontWeight: 400,
91
  stroke: '#000',
92
  fill: '#000',
93
  zlevel: 10
94
})

效果

https://l-x-f.github.io/auto-drawing-doc/example/example.html#%E9%B1%BC%E9%AA%A8%E5%9B%BE

官方文档

官方文档 https://l-x-f.github.io/auto-drawing-doc/