@orbcharts/plugins-basic 3.0.0-beta.1 → 3.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +2863 -2860
  3. package/dist/orbcharts-plugins-basic.umd.js +46 -46
  4. package/lib/core-types.ts +7 -7
  5. package/lib/core.ts +6 -6
  6. package/lib/plugins-basic-types.ts +6 -6
  7. package/package.json +44 -44
  8. package/src/base/BaseBarStack.ts +780 -780
  9. package/src/base/BaseBars.ts +765 -765
  10. package/src/base/BaseBarsTriangle.ts +674 -674
  11. package/src/base/BaseDots.ts +515 -515
  12. package/src/base/BaseGroupAxis.ts +679 -679
  13. package/src/base/BaseLegend.ts +680 -680
  14. package/src/base/BaseLineAreas.ts +629 -629
  15. package/src/base/BaseLines.ts +706 -706
  16. package/src/base/BaseTooltip.ts +385 -382
  17. package/src/base/BaseValueAxis.ts +580 -580
  18. package/src/base/types.ts +2 -2
  19. package/src/const.ts +30 -30
  20. package/src/grid/defaults.ts +244 -244
  21. package/src/grid/gridObservables.ts +545 -545
  22. package/src/grid/index.ts +16 -16
  23. package/src/grid/plugins/BarStack.ts +64 -64
  24. package/src/grid/plugins/Bars.ts +69 -69
  25. package/src/grid/plugins/BarsPN.ts +66 -66
  26. package/src/grid/plugins/BarsTriangle.ts +73 -73
  27. package/src/grid/plugins/Dots.ts +68 -68
  28. package/src/grid/plugins/GridLegend.ts +107 -107
  29. package/src/grid/plugins/GridTooltip.ts +66 -66
  30. package/src/grid/plugins/GridZoom.ts +218 -218
  31. package/src/grid/plugins/GroupAux.ts +1098 -1098
  32. package/src/grid/plugins/GroupAxis.ts +97 -97
  33. package/src/grid/plugins/LineAreas.ts +65 -65
  34. package/src/grid/plugins/Lines.ts +59 -59
  35. package/src/grid/plugins/ValueAxis.ts +93 -93
  36. package/src/grid/plugins/ValueStackAxis.ts +95 -95
  37. package/src/index.ts +9 -9
  38. package/src/multiGrid/defaults.ts +224 -224
  39. package/src/multiGrid/index.ts +14 -14
  40. package/src/multiGrid/multiGridObservables.ts +49 -49
  41. package/src/multiGrid/plugins/MultiBarStack.ts +106 -106
  42. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  43. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  44. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  45. package/src/multiGrid/plugins/MultiGridLegend.ts +159 -159
  46. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  47. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  48. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  49. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  50. package/src/multiGrid/plugins/MultiValueAxis.ts +133 -133
  51. package/src/multiGrid/plugins/MultiValueStackAxis.ts +133 -133
  52. package/src/multiGrid/plugins/OverlappingValueAxes.ts +299 -299
  53. package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +298 -298
  54. package/src/noneData/defaults.ts +102 -102
  55. package/src/noneData/index.ts +3 -3
  56. package/src/noneData/plugins/Container.ts +27 -27
  57. package/src/noneData/plugins/Tooltip.ts +373 -373
  58. package/src/series/defaults.ts +206 -206
  59. package/src/series/index.ts +9 -9
  60. package/src/series/plugins/Bubbles.ts +603 -603
  61. package/src/series/plugins/Pie.ts +623 -623
  62. package/src/series/plugins/PieEventTexts.ts +283 -283
  63. package/src/series/plugins/PieLabels.ts +640 -640
  64. package/src/series/plugins/Rose.ts +516 -516
  65. package/src/series/plugins/RoseLabels.ts +600 -600
  66. package/src/series/plugins/SeriesLegend.ts +107 -107
  67. package/src/series/plugins/SeriesTooltip.ts +66 -66
  68. package/src/series/seriesObservables.ts +145 -145
  69. package/src/series/seriesUtils.ts +51 -51
  70. package/src/tree/defaults.ts +78 -78
  71. package/src/tree/index.ts +4 -4
  72. package/src/tree/plugins/TreeLegend.ts +107 -107
  73. package/src/tree/plugins/TreeMap.ts +333 -333
  74. package/src/tree/plugins/TreeTooltip.ts +66 -66
  75. package/src/utils/commonUtils.ts +21 -21
  76. package/src/utils/d3Graphics.ts +174 -174
  77. package/src/utils/d3Utils.ts +73 -73
  78. package/src/utils/observables.ts +14 -14
  79. package/src/utils/orbchartsUtils.ts +100 -100
  80. package/tsconfig.base.json +13 -13
  81. package/tsconfig.json +2 -2
  82. package/vite.config.js +22 -22
@@ -1,1099 +1,1099 @@
1
- import * as d3 from 'd3'
2
- import {
3
- // of,
4
- iif,
5
- EMPTY,
6
- combineLatest,
7
- switchMap,
8
- map,
9
- filter,
10
- first,
11
- takeUntil,
12
- distinctUntilChanged,
13
- shareReplay,
14
- Subject,
15
- Observable } from 'rxjs'
16
- import {
17
- defineGridPlugin } from '../../../lib/core'
18
- import type { DefinePluginConfig } from '../../../lib/core-types'
19
- import type {
20
- TransformData,
21
- DataFormatterGrid,
22
- ChartParams } from '../../../lib/core-types'
23
- import { DEFAULT_GROUP_AREA_PARAMS } from '../defaults'
24
- import { parseTickFormatValue } from '../../utils/d3Utils'
25
- import { measureTextWidth } from '../../utils/commonUtils'
26
- import { getColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
27
- import { d3EventObservable } from '../../utils/observables'
28
- import { gridGroupPosition } from '../gridObservables'
29
- import type { GroupAuxParams } from '../../../lib/plugins-basic-types'
30
- import { gridSelectionsObservable } from '../gridObservables'
31
- import { renderTspansOnAxis } from '../../utils/d3Graphics'
32
- import { LAYER_INDEX_OF_AUX } from '../../const'
33
-
34
- interface LineDatum {
35
- id: string
36
- x1: number
37
- x2: number
38
- y1: number
39
- y2: number
40
- }
41
-
42
- interface LabelDatum {
43
- id: string
44
- text: string
45
- textArr: string[]
46
- textWidth: number
47
- textHeight: number
48
- x: number
49
- y: number
50
- }
51
-
52
- const pluginName = 'GroupAux'
53
- const labelClassName = getClassName(pluginName, 'label-box')
54
-
55
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_GROUP_AREA_PARAMS> = {
56
- name: pluginName,
57
- defaultParams: DEFAULT_GROUP_AREA_PARAMS,
58
- layerIndex: LAYER_INDEX_OF_AUX,
59
- validator: (params, { validateColumns }) => {
60
- const result = validateColumns(params, {
61
- showLine: {
62
- toBeTypes: ['boolean']
63
- },
64
- showLabel: {
65
- toBeTypes: ['boolean']
66
- },
67
- lineDashArray: {
68
- toBeTypes: ['string']
69
- },
70
- lineColorType: {
71
- toBeOption: 'ColorType'
72
- },
73
- labelColorType: {
74
- toBeOption: 'ColorType'
75
- },
76
- labelTextColorType: {
77
- toBeOption: 'ColorType'
78
- },
79
- labelTextFormat: {
80
- toBeTypes: ['string', 'Function']
81
- },
82
- labelPadding: {
83
- toBeTypes: ['number']
84
- },
85
- labelRotate: {
86
- toBeTypes: ['number']
87
- }
88
- })
89
- return result
90
- }
91
- }
92
-
93
- function createLineData ({ groupLabel, axisX, axisHeight, fullParams }: {
94
- groupLabel: string
95
- axisX: number
96
- axisHeight: number
97
- fullParams: GroupAuxParams
98
- }): LineDatum[] {
99
- return fullParams.showLine && groupLabel
100
- ? [{
101
- id: groupLabel,
102
- x1: axisX,
103
- x2: axisX,
104
- y1: 0,
105
- y2: axisHeight
106
- }]
107
- : []
108
- }
109
-
110
- function createLabelData ({ groupLabel, axisX, fullParams, textSizePx }: {
111
- groupLabel: string
112
- axisX: number
113
- fullParams: GroupAuxParams
114
- textSizePx: number
115
- }) {
116
- const text = parseTickFormatValue(groupLabel, fullParams.labelTextFormat)
117
- const textArr = text.split('\n')
118
- const maxLengthText = textArr.reduce((acc, current) => current.length > acc.length ? current : acc, '')
119
- const textWidth = measureTextWidth(maxLengthText, textSizePx)
120
- const textHeight = textSizePx * textArr.length
121
- return fullParams.showLabel && groupLabel
122
- ? [{
123
- id: groupLabel,
124
- x: axisX,
125
- y: - fullParams.labelPadding,
126
- text,
127
- textArr,
128
- textWidth,
129
- textHeight
130
- }]
131
- : []
132
- }
133
-
134
- function renderLine ({ selection, pluginName, lineData, fullParams, fullChartParams }: {
135
- selection: d3.Selection<any, string, any, unknown>
136
- pluginName: string
137
- lineData: LineDatum[]
138
- fullParams: GroupAuxParams
139
- fullChartParams: ChartParams
140
- }) {
141
- const gClassName = getClassName(pluginName, 'auxline')
142
- const update = selection
143
- .selectAll<SVGLineElement, LineDatum>(`line.${gClassName}`)
144
- .data(lineData)
145
- const enter = update
146
- .enter()
147
- .append('line')
148
- .classed(gClassName, true)
149
- // .style('stroke', '#E4E7ED')
150
- .style('stroke', d => getColor(fullParams.lineColorType, fullChartParams))
151
- .style('stroke-width', 1)
152
- .style('stroke-dasharray', fullParams.lineDashArray ?? 'none')
153
- .style('pointer-events', 'none')
154
- // .attr('opacity', 0)
155
- const auxLineSelection = update.merge(enter)
156
- // .attr('opacity', (d) => {
157
- // return d.active == true ? 1 : 0
158
- // })
159
- update.exit().remove()
160
- enter
161
- .attr('x1', d => d.x1)
162
- .attr('y1', d => d.y1)
163
- .attr('x2', d => d.x2)
164
- .attr('y2', d => d.y2)
165
- update
166
- .transition()
167
- .duration(50)
168
- .attr('x1', d => d.x1)
169
- .attr('y1', d => d.y1)
170
- .attr('x2', d => d.x2)
171
- .attr('y2', d => d.y2)
172
-
173
- return auxLineSelection
174
- }
175
-
176
- function removeLine (selection: d3.Selection<any, string, any, unknown>) {
177
- const update = selection
178
- .selectAll<SVGLineElement, LineDatum>('line')
179
- .data([])
180
-
181
- update.exit().remove()
182
- }
183
-
184
- function renderLabel ({ selection, labelData, fullParams, fullDataFormatter, fullChartParams, textReverseTransformWithRotate, textSizePx }: {
185
- selection: d3.Selection<any, string, any, unknown>
186
- labelData: LabelDatum[]
187
- fullParams: GroupAuxParams
188
- fullDataFormatter: DataFormatterGrid
189
- fullChartParams: ChartParams
190
- // gridAxesReverseTransformValue: string
191
- textReverseTransformWithRotate: string
192
- textSizePx: number
193
- }) {
194
- // const rectHeight = textSizePx + 6
195
-
196
- const gUpdate = selection
197
- .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
198
- .data(labelData)
199
- const gEnter = gUpdate
200
- .enter()
201
- .append('g')
202
- .classed(labelClassName, true)
203
- .style('cursor', 'pointer')
204
- const axisLabelSelection = gEnter.merge(gUpdate)
205
- gEnter
206
- .attr("transform", (d, i) => {
207
- return `translate(${d.x}, ${d.y})`
208
- })
209
- gUpdate
210
- .transition()
211
- .duration(50)
212
- .attr("transform", (d, i) => {
213
- return `translate(${d.x}, ${d.y})`
214
- })
215
- gUpdate.exit().remove()
216
-
217
- axisLabelSelection.each((datum, i, n) => {
218
- // const rectWidth = measureTextWidth(datum.text, textSizePx) + 12
219
- const rectWidth = datum.textWidth + 12
220
- const rectHeight = datum.textHeight + 6
221
- // -- label偏移位置 --
222
- let rectX = - rectWidth / 2
223
- let rectY = -2
224
- if (fullDataFormatter.grid.groupAxis.position === 'bottom') {
225
- rectX = fullParams.labelRotate
226
- ? - rectWidth + rectHeight // 有傾斜時以末端對齊(+height是為了修正移動太多)
227
- : - rectWidth / 2
228
- rectY = 2
229
- } else if (fullDataFormatter.grid.groupAxis.position === 'left') {
230
- rectX = - rectWidth + 2
231
- rectY = - rectHeight / 2
232
- } else if (fullDataFormatter.grid.groupAxis.position === 'right') {
233
- rectX = - 2
234
- rectY = - rectHeight / 2
235
- } else if (fullDataFormatter.grid.groupAxis.position === 'top') {
236
- rectX = fullParams.labelRotate
237
- ? - rectWidth + rectHeight // 有傾斜時以末端對齊(+height是為了修正移動太多)
238
- : - rectWidth / 2
239
- rectY = - rectHeight + 2
240
- }
241
-
242
- // -- rect --
243
- d3.select(n[i])
244
- .selectAll<SVGRectElement, LabelDatum>('rect')
245
- .data([datum])
246
- .join(
247
- enter => enter.append('rect')
248
- .style('cursor', 'pointer')
249
- .attr('rx', 5)
250
- .attr('ry', 5),
251
- update => update,
252
- exit => exit.remove()
253
- )
254
- .attr('width', d => `${rectWidth}px`)
255
- .attr('height', `${rectHeight}px`)
256
- .attr('fill', d => getColor(fullParams.labelColorType, fullChartParams))
257
- .attr('x', rectX)
258
- .attr('y', rectY - 3) // 奇怪的偏移修正
259
- .style('transform', textReverseTransformWithRotate)
260
-
261
- // const rectUpdate = d3.select(n[i])
262
- // .selectAll<SVGRectElement, LabelDatum>('rect')
263
- // .data([datum])
264
- // const rectEnter = rectUpdate
265
- // .enter()
266
- // .append('rect')
267
- // .attr('height', `${rectHeight}px`)
268
- // .attr('fill', d => getColor(fullParams.labelColorType, fullChartParams))
269
- // .attr('x', rectX)
270
- // .attr('y', rectY - 3) // 奇怪的偏移修正
271
- // .attr('rx', 5)
272
- // .attr('ry', 5)
273
- // .style('cursor', 'pointer')
274
- // // .style('pointer-events', 'none')
275
- // const rect = rectUpdate.merge(rectEnter)
276
- // .attr('width', d => `${rectWidth}px`)
277
- // .style('transform', textReverseTransformWithRotate)
278
- // rectUpdate.exit().remove()
279
-
280
- // -- text --
281
- d3.select(n[i])
282
- .selectAll<SVGTextElement, LabelDatum>('text')
283
- .data([datum])
284
- .join(
285
- enter => enter.append('text')
286
- .style('dominant-baseline', 'hanging')
287
- .style('cursor', 'pointer'),
288
- update => update,
289
- exit => exit.remove()
290
- )
291
- .style('transform', textReverseTransformWithRotate)
292
- .attr('fill', d => getColor(fullParams.labelTextColorType, fullChartParams))
293
- .attr('font-size', fullChartParams.styles.textSize)
294
- .attr('x', rectX + 6)
295
- .attr('y', rectY)
296
- .each((d, i, n) => {
297
- renderTspansOnAxis(d3.select(n[i]), {
298
- textArr: datum.textArr,
299
- textSizePx,
300
- groupAxisPosition: fullDataFormatter.grid.groupAxis.position
301
- })
302
- })
303
-
304
- // const textUpdate = d3.select(n[i])
305
- // .selectAll<SVGTextElement, LabelDatum>('text')
306
- // .data([datum])
307
- // const textEnter = textUpdate
308
- // .enter()
309
- // .append('text')
310
- // .style('dominant-baseline', 'hanging')
311
- // .style('cursor', 'pointer')
312
- // // .style('pointer-events', 'none')
313
- // const text = textUpdate.merge(textEnter)
314
- // // .text(d => d.text)
315
- // .style('transform', textReverseTransformWithRotate)
316
- // .attr('fill', d => getColor(fullParams.labelTextColorType, fullChartParams))
317
- // .attr('font-size', fullChartParams.styles.textSize)
318
- // .attr('x', rectX + 6)
319
- // .attr('y', rectY)
320
- // textUpdate.exit().remove()
321
-
322
- // text.each((d, i, n) => {
323
- // renderTspansOnAxis(d3.select(n[i]), {
324
- // textArr: datum.textArr,
325
- // textSizePx,
326
- // groupAxisPosition: fullDataFormatter.grid.groupAxis.position
327
- // })
328
- // })
329
- })
330
-
331
- return axisLabelSelection
332
- }
333
-
334
- function removeLabel (selection: d3.Selection<any, string, any, unknown>) {
335
- const gUpdate = selection
336
- .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
337
- .data([])
338
-
339
- gUpdate.exit().remove()
340
- }
341
-
342
-
343
- export const GroupAux = defineGridPlugin(pluginConfig)(({ selection, rootSelection, name, subject, observer }) => {
344
- const destroy$ = new Subject()
345
-
346
- let isLabelMouseover = false
347
-
348
- const rootRectSelection: d3.Selection<SVGRectElement, any, any, any> = rootSelection
349
- .insert('rect', 'g')
350
- .classed(getClassName(pluginName, 'rect'), true)
351
- .attr('opacity', 0)
352
-
353
- // const axisSelection: d3.Selection<SVGGElement, any, any, any> = selection
354
- // .append('g')
355
-
356
- const {
357
- seriesSelection$,
358
- axesSelection$,
359
- defsSelection$,
360
- graphicGSelection$
361
- } = gridSelectionsObservable({
362
- selection,
363
- pluginName,
364
- clipPathID: 'test',
365
- seriesLabels$: observer.isSeriesSeprate$.pipe(
366
- switchMap(isSeriesSeprate => {
367
- return iif(
368
- () => isSeriesSeprate,
369
- observer.seriesLabels$,
370
- // 如果沒分開的話只取一筆
371
- observer.seriesLabels$.pipe(
372
- map(d => [d[0]])
373
- )
374
- )
375
- })
376
- ),
377
- gridContainerPosition$: observer.gridContainerPosition$,
378
- gridAxesTransform$: observer.gridAxesTransform$,
379
- gridGraphicTransform$: observer.gridGraphicTransform$
380
- })
381
-
382
- observer.layout$.pipe(
383
- takeUntil(destroy$),
384
- ).subscribe(d => {
385
- rootRectSelection
386
- .attr('width', d.rootWidth)
387
- .attr('height', d.rootHeight)
388
- })
389
-
390
- // observer.gridAxesTransform$
391
- // .pipe(
392
- // takeUntil(destroy$),
393
- // map(d => d.value),
394
- // distinctUntilChanged()
395
- // ).subscribe(d => {
396
- // axisSelection
397
- // .style('transform', d)
398
- // })
399
-
400
- // const visibleComputedData$ = observer.computedData$.pipe(
401
- // takeUntil(destroy$),
402
- // map(data => {
403
- // const visibleComputedData = data
404
- // .map(d => {
405
- // return d.filter(_d => {
406
- // return _d.visible == true
407
- // })
408
- // })
409
- // .filter(d => d.length)
410
- // // console.log('visibleComputedData', visibleComputedData)
411
- // return visibleComputedData
412
- // })
413
- // )
414
-
415
- // const SeriesDataMap$ = visibleComputedData$.pipe(
416
- // map(d => makeGridSeriesDataMap(d))
417
- // )
418
-
419
- // const GroupDataMap$ = visibleComputedData$.pipe(
420
- // map(d => makeGridGroupDataMap(d))
421
- // )
422
-
423
- // const contentTransform$: Observable<string> = new Observable(subscriber => {
424
- // combineLatest({
425
- // fullParams: observer.fullParams$,
426
- // gridAxesTransform: observer.gridAxesTransform$
427
- // }).pipe(
428
- // takeUntil(destroy$),
429
- // // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
430
- // switchMap(async (d) => d),
431
- // ).subscribe(data => {
432
-
433
- // const transformData = Object.assign({}, data.gridAxesTransform)
434
-
435
- // // const value = getAxesTransformValue({
436
- // // translate: [0, 0],
437
- // // scale: [transformData.scale[0] * -1, transformData.scale[1] * -1],
438
- // // rotate: transformData.rotate * -1,
439
- // // rotateX: transformData.rotateX * -1,
440
- // // rotateY: transformData.rotateY * -1
441
- // // })
442
-
443
- // subscriber.next(transformData.value)
444
- // })
445
- // })
446
- // const reverseTransform$: Observable<TransformData> = observer.gridAxesTransform$.pipe(
447
- // takeUntil(destroy$),
448
- // map(d => {
449
- // const translate: [number, number] = [d.translate[0] * -1, d.translate[1] * -1]
450
- // const scale: [number, number] = [d.scale[0] * -1, d.scale[1] * -1]
451
- // const rotate = d.rotate * -1
452
- // const rotateX = d.rotateX * -1
453
- // const rotateY = d.rotateY * -1
454
- // return {
455
- // translate,
456
- // scale,
457
- // rotate,
458
- // rotateX,
459
- // rotateY,
460
- // value: ''
461
- // }
462
- // }),
463
- // )
464
- // const contentTransform$ = combineLatest({
465
- // fullParams: observer.fullParams$,
466
- // reverseTransform: reverseTransform$
467
- // }).pipe(
468
- // takeUntil(destroy$),
469
- // switchMap(async data => {
470
- // const translate = [0, 0]
471
- // return `translate(${translate[0]}px, ${translate[1]}px) rotate(${data.reverseTransform.rotate}deg) rotateX(${data.reverseTransform.rotateX}deg) rotateY(${data.reverseTransform.rotateY}deg)`
472
- // }),
473
- // distinctUntilChanged()
474
- // )
475
-
476
- // const groupScale$: Observable<d3.ScalePoint<string>> = new Observable(subscriber => {
477
- // combineLatest({
478
- // fullDataFormatter: observer.fullDataFormatter$,
479
- // gridAxesSize: observer.gridAxesSize$,
480
- // computedData: observer.computedData$
481
- // }).pipe(
482
- // takeUntil(destroy$),
483
- // switchMap(async (d) => d),
484
- // ).subscribe(data => {
485
- // const groupMin = 0
486
- // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
487
- // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
488
- // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
489
- // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
490
- // const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'auto'
491
- // ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
492
- // : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
493
-
494
- // const groupingLength = data.computedData[0]
495
- // ? data.computedData[0].length
496
- // : 0
497
-
498
- // let _labels = data.fullDataFormatter.grid.seriesDirection === 'row'
499
- // // ? data.fullDataFormatter.grid.columnLabels
500
- // // : data.fullDataFormatter.grid.rowLabels
501
- // ? (data.computedData[0] ?? []).map(d => d.groupLabel)
502
- // : data.computedData.map(d => d[0].groupLabel)
503
-
504
- // const axisLabels = new Array(groupingLength).fill(0)
505
- // .map((d, i) => {
506
- // return _labels[i] != null
507
- // ? _labels[i]
508
- // : String(i) // 沒有label則用序列號填充
509
- // })
510
- // .filter((d, i) => {
511
- // return i >= groupScaleDomainMin && i <= groupScaleDomainMax
512
- // })
513
-
514
-
515
- // const padding = data.fullDataFormatter.grid.groupAxis.scalePadding
516
-
517
- // const groupScale = createAxisPointScale({
518
- // axisLabels,
519
- // axisWidth: data.gridAxesSize.width,
520
- // padding
521
- // })
522
-
523
- // subscriber.next(groupScale)
524
- // })
525
- // })
526
-
527
- const groupScaleDomain$ = combineLatest({
528
- fullDataFormatter: observer.fullDataFormatter$,
529
- gridAxesSize: observer.gridAxesSize$,
530
- computedData: observer.computedData$
531
- }).pipe(
532
- takeUntil(destroy$),
533
- switchMap(async (d) => d),
534
- map(data => {
535
- const groupMin = 0
536
- const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
537
- // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
538
- // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
539
- // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
540
- const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] - data.fullDataFormatter.grid.groupAxis.scalePadding
541
- const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
542
- ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
543
- : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
544
-
545
- return [groupScaleDomainMin, groupScaleDomainMax]
546
- }),
547
- shareReplay(1)
548
- )
549
-
550
- const groupScale$ = combineLatest({
551
- groupScaleDomain: groupScaleDomain$,
552
- gridAxesSize: observer.gridAxesSize$
553
- }).pipe(
554
- takeUntil(destroy$),
555
- switchMap(async (d) => d),
556
- map(data => {
557
- const groupScale: d3.ScaleLinear<number, number> = d3.scaleLinear()
558
- .domain(data.groupScaleDomain)
559
- .range([0, data.gridAxesSize.width])
560
- return groupScale
561
- })
562
- )
563
-
564
- // // 取得事件座標的group資料
565
- // const gridGroupPositionFn$ = gridGroupPositionFnObservable({
566
- // fullDataFormatter$: observer.fullDataFormatter$,
567
- // gridAxesSize$: observer.gridAxesSize$,
568
- // computedData$: observer.computedData$,
569
- // fullChartParams$: observer.fullChartParams$,
570
- // })
571
-
572
- const highlightTarget$ = observer.fullChartParams$.pipe(
573
- takeUntil(destroy$),
574
- map(d => d.highlightTarget),
575
- distinctUntilChanged()
576
- )
577
-
578
- // combineLatest({
579
- // computedData: observer.computedData$,
580
- // gridAxesSize: observer.gridAxesSize$,
581
- // fullParams: observer.fullParams$,
582
- // fullChartParams: observer.fullChartParams$,
583
- // highlightTarget: highlightTarget$,
584
- // SeriesDataMap: observer.SeriesDataMap$,
585
- // GroupDataMap: observer.GroupDataMap$,
586
- // gridGroupPositionFn: gridGroupPositionFn$,
587
- // groupScale: groupScale$,
588
- // }).pipe(
589
- // takeUntil(destroy$),
590
- // switchMap(async (d) => d),
591
- // ).subscribe(data => {
592
-
593
- // // store.selection
594
- // rootSelection
595
- // .on('mouseover', (event, datum) => {
596
- // // event.stopPropagation()
597
-
598
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
599
-
600
- // subject.event$.next({
601
- // type: 'grid',
602
- // pluginName: name,
603
- // eventName: 'mouseover',
604
- // highlightTarget: data.highlightTarget,
605
- // datum: null,
606
- // gridIndex: 0, // @Q@ 暫不處理
607
- // series: [],
608
- // seriesIndex: -1,
609
- // seriesLabel: '',
610
- // groups: data.GroupDataMap.get(groupLabel) ?? [],
611
- // // groups: [],
612
- // groupIndex,
613
- // groupLabel,
614
- // event,
615
- // data: data.computedData
616
- // })
617
- // })
618
- // .on('mousemove', (event, datum) => {
619
- // // event.stopPropagation()
620
-
621
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
622
-
623
- // subject.event$.next({
624
- // type: 'grid',
625
- // pluginName: name,
626
- // eventName: 'mousemove',
627
- // highlightTarget: data.highlightTarget,
628
- // datum: null,
629
- // gridIndex: 0, // @Q@ 暫不處理
630
- // series: [],
631
- // seriesIndex: -1,
632
- // seriesLabel: '',
633
- // groups: data.GroupDataMap.get(groupLabel) ?? [],
634
- // // groups: [],
635
- // groupIndex,
636
- // groupLabel,
637
- // event,
638
- // data: data.computedData
639
- // })
640
- // })
641
- // .on('mouseout', (event, datum) => {
642
- // // event.stopPropagation()
643
-
644
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
645
-
646
- // subject.event$.next({
647
- // type: 'grid',
648
- // pluginName: name,
649
- // eventName: 'mouseout',
650
- // highlightTarget: data.highlightTarget,
651
- // datum: null,
652
- // gridIndex: 0, // @Q@ 暫不處理
653
- // series: [],
654
- // seriesIndex: -1,
655
- // seriesLabel: '',
656
- // groups: data.GroupDataMap.get(groupLabel) ?? [],
657
- // // groups: [],
658
- // groupIndex,
659
- // groupLabel,
660
- // event,
661
- // data: data.computedData
662
- // })
663
- // })
664
- // .on('click', (event, datum) => {
665
- // event.stopPropagation()
666
-
667
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
668
-
669
- // subject.event$.next({
670
- // type: 'grid',
671
- // pluginName: name,
672
- // eventName: 'click',
673
- // highlightTarget: data.highlightTarget,
674
- // datum: null,
675
- // gridIndex: 0, // @Q@ 暫不處理
676
- // series: [],
677
- // seriesIndex: -1,
678
- // seriesLabel: '',
679
- // // groups: data.GroupDataMap.get(groupLabel) ?? [],
680
- // groups: [],
681
- // groupIndex,
682
- // groupLabel,
683
- // event,
684
- // data: data.computedData
685
- // })
686
- // })
687
-
688
- // // barSelection$.next(barSelection!)
689
- // })
690
-
691
- const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove').pipe(
692
- takeUntil(destroy$),
693
- )
694
-
695
- // const mousemoveGroupLabel$ = combineLatest({
696
- // rootMousemove: rootMousemove$,
697
- // gridGroupPositionFn: gridGroupPositionFn$,
698
- // }).pipe(
699
- // takeUntil(destroy$),
700
- // switchMap(async d => d),
701
- // map(data => {
702
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(data.rootMousemove)
703
- // return { groupIndex, groupLabel }
704
- // }),
705
- // shareReplay(1)
706
- // )
707
-
708
-
709
- const textReverseTransform$ = combineLatest({
710
- gridAxesReverseTransform: observer.gridAxesReverseTransform$,
711
- gridContainerPosition: observer.gridContainerPosition$
712
- }).pipe(
713
- takeUntil(destroy$),
714
- switchMap(async (d) => d),
715
- map(data => {
716
- // const axisReverseTranslateValue = `translate(${data.gridAxesReverseTransform.translate[0]}px, ${data.gridAxesReverseTransform.translate[1]}px)`
717
- const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
718
- const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
719
- const containerScaleReverseValue = `scale(${1 / data.gridContainerPosition[0].scale[0]}, ${1 / data.gridContainerPosition[0].scale[1]})`
720
- // 必須按照順序(先抵消外層rotate,再抵消最外層scale)
721
- return `${axesRotateXYReverseValue} ${axesRotateReverseValue} ${containerScaleReverseValue}`
722
- }),
723
- distinctUntilChanged()
724
- )
725
-
726
- const textReverseTransformWithRotate$ = combineLatest({
727
- textReverseTransform: textReverseTransform$,
728
- fullParams: observer.fullParams$,
729
- }).pipe(
730
- takeUntil(destroy$),
731
- switchMap(async (d) => d),
732
- map(data => {
733
- // 必須按照順序(先抵消外層rotate,再抵消最外層scale,最後再做本身的rotate)
734
- return `${data.textReverseTransform} rotate(${data.fullParams.labelRotate}deg)`
735
- })
736
- )
737
-
738
- const columnAmount$ = observer.gridContainerPosition$.pipe(
739
- takeUntil(destroy$),
740
- map(gridContainerPosition => {
741
- const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
742
- return current.columnIndex > acc ? current.columnIndex : acc
743
- }, 0)
744
- return maxColumnIndex + 1
745
- }),
746
- distinctUntilChanged()
747
- )
748
-
749
- const rowAmount$ = observer.gridContainerPosition$.pipe(
750
- takeUntil(destroy$),
751
- map(gridContainerPosition => {
752
- const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
753
- return current.rowIndex > acc ? current.rowIndex : acc
754
- }, 0)
755
- return maxRowIndex + 1
756
- }),
757
- distinctUntilChanged()
758
- )
759
-
760
- const gridGroupPosition$ = gridGroupPosition({
761
- rootSelection,
762
- fullDataFormatter$: observer.fullDataFormatter$,
763
- gridAxesSize$: observer.gridAxesSize$,
764
- computedData$: observer.computedData$,
765
- fullChartParams$: observer.fullChartParams$,
766
- gridContainerPosition$: observer.gridContainerPosition$,
767
- layout$: observer.layout$
768
- }).pipe(
769
- takeUntil(destroy$)
770
- )
771
-
772
- combineLatest({
773
- axesSelection: axesSelection$,
774
- columnAmount: columnAmount$,
775
- rowAmount: rowAmount$,
776
- layout: observer.layout$,
777
- rootMousemove: rootMousemove$,
778
- // gridGroupPositionFn: gridGroupPositionFn$,
779
- gridGroupPosition: gridGroupPosition$,
780
- computedData: observer.computedData$,
781
- groupScale: groupScale$,
782
- gridAxesSize: observer.gridAxesSize$,
783
- fullParams: observer.fullParams$,
784
- fullDataFormatter: observer.fullDataFormatter$,
785
- fullChartParams: observer.fullChartParams$,
786
- highlightTarget: highlightTarget$,
787
- // gridAxesReverseTransform: observer.gridAxesReverseTransform$,
788
- textReverseTransformWithRotate: textReverseTransformWithRotate$,
789
- GroupDataMap: observer.GroupDataMap$,
790
- textSizePx: observer.textSizePx$
791
- }).pipe(
792
- takeUntil(destroy$),
793
- switchMap(async d => d),
794
- ).subscribe(data => {
795
- // // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
796
- // const eventData = {
797
- // offsetX: data.rootMousemove.offsetX * data.columnAmount % data.layout.rootWidth,
798
- // offsetY: data.rootMousemove.offsetY * data.rowAmount % data.layout.rootHeight
799
- // }
800
- // 依event的座標取得group資料
801
- const { groupIndex, groupLabel } = data.gridGroupPosition
802
- // console.log('gridGroupPosition', groupIndex, groupLabel)
803
- const axisX = data.groupScale(groupIndex) ?? 0
804
- // console.log('axisX', axisX)
805
- const lineData = createLineData({
806
- groupLabel: groupLabel,
807
- axisX,
808
- axisHeight: data.gridAxesSize.height,
809
- fullParams: data.fullParams,
810
- })
811
- // console.log('lineData', lineData)
812
- renderLine({
813
- // selection: axisSelection,
814
- selection: data.axesSelection,
815
- pluginName: name,
816
- lineData,
817
- fullParams: data.fullParams,
818
- fullChartParams: data.fullChartParams
819
- })
820
- const labelData = createLabelData({
821
- groupLabel: groupLabel,
822
- axisX,
823
- fullParams: data.fullParams,
824
- textSizePx: data.textSizePx
825
- })
826
- const labelSelection = renderLabel({
827
- // selection: axisSelection,
828
- selection: data.axesSelection,
829
- labelData,
830
- fullParams: data.fullParams,
831
- fullDataFormatter: data.fullDataFormatter,
832
- fullChartParams: data.fullChartParams,
833
- // gridAxesReverseTransformValue: data.gridAxesReverseTransform.value,
834
- textReverseTransformWithRotate: data.textReverseTransformWithRotate,
835
- textSizePx: data.textSizePx
836
- })
837
-
838
- // label的事件
839
- labelSelection
840
- .on('mouseover', (event, datum) => {
841
- event.stopPropagation()
842
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
843
-
844
- isLabelMouseover = true
845
-
846
- subject.event$.next({
847
- type: 'grid',
848
- pluginName: name,
849
- eventName: 'mouseover',
850
- highlightTarget: data.highlightTarget,
851
- datum: null,
852
- gridIndex: 0, // @Q@ 暫不處理
853
- series: [],
854
- seriesIndex: -1,
855
- seriesLabel: '',
856
- groups: data.GroupDataMap.get(groupLabel) ?? [],
857
- groupIndex,
858
- groupLabel,
859
- event,
860
- data: data.computedData
861
- })
862
- })
863
- .on('mousemove', (event, datum) => {
864
- event.stopPropagation()
865
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
866
-
867
- subject.event$.next({
868
- type: 'grid',
869
- pluginName: name,
870
- eventName: 'mousemove',
871
- highlightTarget: data.highlightTarget,
872
- datum: null,
873
- gridIndex: 0, // @Q@ 暫不處理
874
- series: [],
875
- seriesIndex: -1,
876
- seriesLabel: '',
877
- groups: data.GroupDataMap.get(groupLabel) ?? [],
878
- groupIndex,
879
- groupLabel,
880
- event,
881
- data: data.computedData
882
- })
883
- })
884
- .on('mouseout', (event, datum) => {
885
- event.stopPropagation()
886
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
887
-
888
- isLabelMouseover = false
889
-
890
- subject.event$.next({
891
- type: 'grid',
892
- pluginName: name,
893
- eventName: 'mouseout',
894
- highlightTarget: data.highlightTarget,
895
- datum: null,
896
- gridIndex: 0, // @Q@ 暫不處理
897
- series: [],
898
- seriesIndex: -1,
899
- seriesLabel: '',
900
- groups: data.GroupDataMap.get(groupLabel) ?? [],
901
- groupIndex,
902
- groupLabel,
903
- event,
904
- data: data.computedData
905
- })
906
- })
907
- .on('click', (event, datum) => {
908
- event.stopPropagation()
909
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
910
-
911
- subject.event$.next({
912
- type: 'grid',
913
- pluginName: name,
914
- eventName: 'click',
915
- highlightTarget: data.highlightTarget,
916
- datum: null,
917
- gridIndex: 0, // @Q@ 暫不處理
918
- series: [],
919
- seriesIndex: -1,
920
- seriesLabel: '',
921
- groups: data.GroupDataMap.get(groupLabel) ?? [],
922
- groupIndex,
923
- groupLabel,
924
- event,
925
- data: data.computedData
926
- })
927
- })
928
-
929
- })
930
-
931
- // // -- highlight(無論highlightTarget設定為何,一律依從groupLabel來顯示) --
932
- // combineLatest({
933
- // event: subject.event$.pipe(
934
- // filter(d => d.eventName === 'mouseover' || d.eventName === 'mousemove')
935
- // ),
936
- // computedData: observer.computedData$,
937
- // groupScale: groupScale$,
938
- // gridAxesSize: observer.gridAxesSize$,
939
- // fullParams: observer.fullParams$,
940
- // fullChartParams: observer.fullChartParams$,
941
- // highlightTarget: highlightTarget$,
942
- // gridAxesReverseTransform: observer.gridAxesReverseTransform$,
943
- // GroupDataMap: observer.GroupDataMap$,
944
- // gridGroupPositionFn: gridGroupPositionFn$,
945
- // textSizePx: observer.textSizePx$
946
- // }).pipe(
947
- // takeUntil(destroy$),
948
- // switchMap(async d => d)
949
- // ).subscribe(data => {
950
- // // const groups = data.event.eventName === 'mouseover' || data.event.eventName === 'mousemove'
951
- // // ? data.event.groups
952
- // // : []
953
-
954
- // // const groupLabel = data.event.eventName === 'mouseover' || data.event.eventName === 'mousemove'
955
- // // ? data.event.groupLabel
956
- // // : ''
957
- // const axisX = data.groupScale(data.event.groupLabel) ?? 0
958
-
959
- // const lineData = createLineData({
960
- // groupLabel: data.event.groupLabel,
961
- // axisX,
962
- // axisHeight: data.gridAxesSize.height,
963
- // fullParams: data.fullParams,
964
- // })
965
- // renderLine({
966
- // selection: axisSelection,
967
- // pluginName: name,
968
- // lineData,
969
- // fullParams: data.fullParams,
970
- // fullChartParams: data.fullChartParams
971
- // })
972
- // const labelData = createLabelData({
973
- // groupLabel: data.event.groupLabel,
974
- // axisX,
975
- // fullParams: data.fullParams
976
- // })
977
- // const labelSelection = renderLabel({
978
- // selection: axisSelection,
979
- // labelData,
980
- // fullParams: data.fullParams,
981
- // fullChartParams: data.fullChartParams,
982
- // gridAxesReverseTransformValue: data.gridAxesReverseTransform.value,
983
- // textSizePx: data.textSizePx
984
- // })
985
-
986
- // // label的事件
987
- // labelSelection
988
- // .on('mouseover', (event, datum) => {
989
-
990
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
991
-
992
- // subject.event$.next({
993
- // type: 'grid',
994
- // pluginName: name,
995
- // eventName: 'mouseover',
996
- // highlightTarget: data.highlightTarget,
997
- // datum: null,
998
- // gridIndex: 0, // @Q@ 暫不處理
999
- // series: [],
1000
- // seriesIndex: -1,
1001
- // seriesLabel: '',
1002
- // groups: data.event.groups,
1003
- // groupIndex,
1004
- // groupLabel,
1005
- // event,
1006
- // data: data.computedData
1007
- // })
1008
- // })
1009
- // .on('mousemove', (event, datum) => {
1010
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
1011
-
1012
- // subject.event$.next({
1013
- // type: 'grid',
1014
- // pluginName: name,
1015
- // eventName: 'mousemove',
1016
- // highlightTarget: data.highlightTarget,
1017
- // datum: null,
1018
- // gridIndex: 0, // @Q@ 暫不處理
1019
- // series: [],
1020
- // seriesIndex: -1,
1021
- // seriesLabel: '',
1022
- // groups: data.event.groups,
1023
- // groupIndex,
1024
- // groupLabel,
1025
- // event,
1026
- // data: data.computedData
1027
- // })
1028
- // })
1029
- // .on('mouseout', (event, datum) => {
1030
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
1031
-
1032
- // subject.event$.next({
1033
- // type: 'grid',
1034
- // pluginName: name,
1035
- // eventName: 'mouseout',
1036
- // highlightTarget: data.highlightTarget,
1037
- // datum: null,
1038
- // gridIndex: 0, // @Q@ 暫不處理
1039
- // series: [],
1040
- // seriesIndex: -1,
1041
- // seriesLabel: '',
1042
- // groups: data.event.groups,
1043
- // groupIndex,
1044
- // groupLabel,
1045
- // event,
1046
- // data: data.computedData
1047
- // })
1048
- // })
1049
- // .on('click', (event, datum) => {
1050
- // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
1051
-
1052
- // subject.event$.next({
1053
- // type: 'grid',
1054
- // pluginName: name,
1055
- // eventName: 'click',
1056
- // highlightTarget: data.highlightTarget,
1057
- // datum: null,
1058
- // gridIndex: 0, // @Q@ 暫不處理
1059
- // series: [],
1060
- // seriesIndex: -1,
1061
- // seriesLabel: '',
1062
- // groups: data.event.groups,
1063
- // groupIndex,
1064
- // groupLabel,
1065
- // event,
1066
- // data: data.computedData
1067
- // })
1068
- // })
1069
- // })
1070
-
1071
-
1072
-
1073
- const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
1074
- takeUntil(destroy$),
1075
- )
1076
-
1077
- combineLatest({
1078
- rootRectMouseout: rootRectMouseout$,
1079
- axesSelection: axesSelection$,
1080
- }).pipe(
1081
- takeUntil(destroy$),
1082
- switchMap(async d => d)
1083
- ).subscribe(data => {
1084
- setTimeout(() => {
1085
- // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
1086
- if (isLabelMouseover == true) {
1087
- return
1088
- }
1089
-
1090
- removeLine(data.axesSelection)
1091
- removeLabel(data.axesSelection)
1092
- })
1093
- })
1094
-
1095
- return () => {
1096
- destroy$.next(undefined)
1097
- rootRectSelection.remove()
1098
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ // of,
4
+ iif,
5
+ EMPTY,
6
+ combineLatest,
7
+ switchMap,
8
+ map,
9
+ filter,
10
+ first,
11
+ takeUntil,
12
+ distinctUntilChanged,
13
+ shareReplay,
14
+ Subject,
15
+ Observable } from 'rxjs'
16
+ import {
17
+ defineGridPlugin } from '../../../lib/core'
18
+ import type { DefinePluginConfig } from '../../../lib/core-types'
19
+ import type {
20
+ TransformData,
21
+ DataFormatterGrid,
22
+ ChartParams } from '../../../lib/core-types'
23
+ import { DEFAULT_GROUP_AREA_PARAMS } from '../defaults'
24
+ import { parseTickFormatValue } from '../../utils/d3Utils'
25
+ import { measureTextWidth } from '../../utils/commonUtils'
26
+ import { getColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
27
+ import { d3EventObservable } from '../../utils/observables'
28
+ import { gridGroupPosition } from '../gridObservables'
29
+ import type { GroupAuxParams } from '../../../lib/plugins-basic-types'
30
+ import { gridSelectionsObservable } from '../gridObservables'
31
+ import { renderTspansOnAxis } from '../../utils/d3Graphics'
32
+ import { LAYER_INDEX_OF_AUX } from '../../const'
33
+
34
+ interface LineDatum {
35
+ id: string
36
+ x1: number
37
+ x2: number
38
+ y1: number
39
+ y2: number
40
+ }
41
+
42
+ interface LabelDatum {
43
+ id: string
44
+ text: string
45
+ textArr: string[]
46
+ textWidth: number
47
+ textHeight: number
48
+ x: number
49
+ y: number
50
+ }
51
+
52
+ const pluginName = 'GroupAux'
53
+ const labelClassName = getClassName(pluginName, 'label-box')
54
+
55
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_GROUP_AREA_PARAMS> = {
56
+ name: pluginName,
57
+ defaultParams: DEFAULT_GROUP_AREA_PARAMS,
58
+ layerIndex: LAYER_INDEX_OF_AUX,
59
+ validator: (params, { validateColumns }) => {
60
+ const result = validateColumns(params, {
61
+ showLine: {
62
+ toBeTypes: ['boolean']
63
+ },
64
+ showLabel: {
65
+ toBeTypes: ['boolean']
66
+ },
67
+ lineDashArray: {
68
+ toBeTypes: ['string']
69
+ },
70
+ lineColorType: {
71
+ toBeOption: 'ColorType'
72
+ },
73
+ labelColorType: {
74
+ toBeOption: 'ColorType'
75
+ },
76
+ labelTextColorType: {
77
+ toBeOption: 'ColorType'
78
+ },
79
+ labelTextFormat: {
80
+ toBeTypes: ['string', 'Function']
81
+ },
82
+ labelPadding: {
83
+ toBeTypes: ['number']
84
+ },
85
+ labelRotate: {
86
+ toBeTypes: ['number']
87
+ }
88
+ })
89
+ return result
90
+ }
91
+ }
92
+
93
+ function createLineData ({ groupLabel, axisX, axisHeight, fullParams }: {
94
+ groupLabel: string
95
+ axisX: number
96
+ axisHeight: number
97
+ fullParams: GroupAuxParams
98
+ }): LineDatum[] {
99
+ return fullParams.showLine && groupLabel
100
+ ? [{
101
+ id: groupLabel,
102
+ x1: axisX,
103
+ x2: axisX,
104
+ y1: 0,
105
+ y2: axisHeight
106
+ }]
107
+ : []
108
+ }
109
+
110
+ function createLabelData ({ groupLabel, axisX, fullParams, textSizePx }: {
111
+ groupLabel: string
112
+ axisX: number
113
+ fullParams: GroupAuxParams
114
+ textSizePx: number
115
+ }) {
116
+ const text = parseTickFormatValue(groupLabel, fullParams.labelTextFormat)
117
+ const textArr = text.split('\n')
118
+ const maxLengthText = textArr.reduce((acc, current) => current.length > acc.length ? current : acc, '')
119
+ const textWidth = measureTextWidth(maxLengthText, textSizePx)
120
+ const textHeight = textSizePx * textArr.length
121
+ return fullParams.showLabel && groupLabel
122
+ ? [{
123
+ id: groupLabel,
124
+ x: axisX,
125
+ y: - fullParams.labelPadding,
126
+ text,
127
+ textArr,
128
+ textWidth,
129
+ textHeight
130
+ }]
131
+ : []
132
+ }
133
+
134
+ function renderLine ({ selection, pluginName, lineData, fullParams, fullChartParams }: {
135
+ selection: d3.Selection<any, string, any, unknown>
136
+ pluginName: string
137
+ lineData: LineDatum[]
138
+ fullParams: GroupAuxParams
139
+ fullChartParams: ChartParams
140
+ }) {
141
+ const gClassName = getClassName(pluginName, 'auxline')
142
+ const update = selection
143
+ .selectAll<SVGLineElement, LineDatum>(`line.${gClassName}`)
144
+ .data(lineData)
145
+ const enter = update
146
+ .enter()
147
+ .append('line')
148
+ .classed(gClassName, true)
149
+ // .style('stroke', '#E4E7ED')
150
+ .style('stroke', d => getColor(fullParams.lineColorType, fullChartParams))
151
+ .style('stroke-width', 1)
152
+ .style('stroke-dasharray', fullParams.lineDashArray ?? 'none')
153
+ .style('pointer-events', 'none')
154
+ // .attr('opacity', 0)
155
+ const auxLineSelection = update.merge(enter)
156
+ // .attr('opacity', (d) => {
157
+ // return d.active == true ? 1 : 0
158
+ // })
159
+ update.exit().remove()
160
+ enter
161
+ .attr('x1', d => d.x1)
162
+ .attr('y1', d => d.y1)
163
+ .attr('x2', d => d.x2)
164
+ .attr('y2', d => d.y2)
165
+ update
166
+ .transition()
167
+ .duration(50)
168
+ .attr('x1', d => d.x1)
169
+ .attr('y1', d => d.y1)
170
+ .attr('x2', d => d.x2)
171
+ .attr('y2', d => d.y2)
172
+
173
+ return auxLineSelection
174
+ }
175
+
176
+ function removeLine (selection: d3.Selection<any, string, any, unknown>) {
177
+ const update = selection
178
+ .selectAll<SVGLineElement, LineDatum>('line')
179
+ .data([])
180
+
181
+ update.exit().remove()
182
+ }
183
+
184
+ function renderLabel ({ selection, labelData, fullParams, fullDataFormatter, fullChartParams, textReverseTransformWithRotate, textSizePx }: {
185
+ selection: d3.Selection<any, string, any, unknown>
186
+ labelData: LabelDatum[]
187
+ fullParams: GroupAuxParams
188
+ fullDataFormatter: DataFormatterGrid
189
+ fullChartParams: ChartParams
190
+ // gridAxesReverseTransformValue: string
191
+ textReverseTransformWithRotate: string
192
+ textSizePx: number
193
+ }) {
194
+ // const rectHeight = textSizePx + 6
195
+
196
+ const gUpdate = selection
197
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
198
+ .data(labelData)
199
+ const gEnter = gUpdate
200
+ .enter()
201
+ .append('g')
202
+ .classed(labelClassName, true)
203
+ .style('cursor', 'pointer')
204
+ const axisLabelSelection = gEnter.merge(gUpdate)
205
+ gEnter
206
+ .attr("transform", (d, i) => {
207
+ return `translate(${d.x}, ${d.y})`
208
+ })
209
+ gUpdate
210
+ .transition()
211
+ .duration(50)
212
+ .attr("transform", (d, i) => {
213
+ return `translate(${d.x}, ${d.y})`
214
+ })
215
+ gUpdate.exit().remove()
216
+
217
+ axisLabelSelection.each((datum, i, n) => {
218
+ // const rectWidth = measureTextWidth(datum.text, textSizePx) + 12
219
+ const rectWidth = datum.textWidth + 12
220
+ const rectHeight = datum.textHeight + 6
221
+ // -- label偏移位置 --
222
+ let rectX = - rectWidth / 2
223
+ let rectY = -2
224
+ if (fullDataFormatter.grid.groupAxis.position === 'bottom') {
225
+ rectX = fullParams.labelRotate
226
+ ? - rectWidth + rectHeight // 有傾斜時以末端對齊(+height是為了修正移動太多)
227
+ : - rectWidth / 2
228
+ rectY = 2
229
+ } else if (fullDataFormatter.grid.groupAxis.position === 'left') {
230
+ rectX = - rectWidth + 2
231
+ rectY = - rectHeight / 2
232
+ } else if (fullDataFormatter.grid.groupAxis.position === 'right') {
233
+ rectX = - 2
234
+ rectY = - rectHeight / 2
235
+ } else if (fullDataFormatter.grid.groupAxis.position === 'top') {
236
+ rectX = fullParams.labelRotate
237
+ ? - rectWidth + rectHeight // 有傾斜時以末端對齊(+height是為了修正移動太多)
238
+ : - rectWidth / 2
239
+ rectY = - rectHeight + 2
240
+ }
241
+
242
+ // -- rect --
243
+ d3.select(n[i])
244
+ .selectAll<SVGRectElement, LabelDatum>('rect')
245
+ .data([datum])
246
+ .join(
247
+ enter => enter.append('rect')
248
+ .style('cursor', 'pointer')
249
+ .attr('rx', 5)
250
+ .attr('ry', 5),
251
+ update => update,
252
+ exit => exit.remove()
253
+ )
254
+ .attr('width', d => `${rectWidth}px`)
255
+ .attr('height', `${rectHeight}px`)
256
+ .attr('fill', d => getColor(fullParams.labelColorType, fullChartParams))
257
+ .attr('x', rectX)
258
+ .attr('y', rectY - 3) // 奇怪的偏移修正
259
+ .style('transform', textReverseTransformWithRotate)
260
+
261
+ // const rectUpdate = d3.select(n[i])
262
+ // .selectAll<SVGRectElement, LabelDatum>('rect')
263
+ // .data([datum])
264
+ // const rectEnter = rectUpdate
265
+ // .enter()
266
+ // .append('rect')
267
+ // .attr('height', `${rectHeight}px`)
268
+ // .attr('fill', d => getColor(fullParams.labelColorType, fullChartParams))
269
+ // .attr('x', rectX)
270
+ // .attr('y', rectY - 3) // 奇怪的偏移修正
271
+ // .attr('rx', 5)
272
+ // .attr('ry', 5)
273
+ // .style('cursor', 'pointer')
274
+ // // .style('pointer-events', 'none')
275
+ // const rect = rectUpdate.merge(rectEnter)
276
+ // .attr('width', d => `${rectWidth}px`)
277
+ // .style('transform', textReverseTransformWithRotate)
278
+ // rectUpdate.exit().remove()
279
+
280
+ // -- text --
281
+ d3.select(n[i])
282
+ .selectAll<SVGTextElement, LabelDatum>('text')
283
+ .data([datum])
284
+ .join(
285
+ enter => enter.append('text')
286
+ .style('dominant-baseline', 'hanging')
287
+ .style('cursor', 'pointer'),
288
+ update => update,
289
+ exit => exit.remove()
290
+ )
291
+ .style('transform', textReverseTransformWithRotate)
292
+ .attr('fill', d => getColor(fullParams.labelTextColorType, fullChartParams))
293
+ .attr('font-size', fullChartParams.styles.textSize)
294
+ .attr('x', rectX + 6)
295
+ .attr('y', rectY)
296
+ .each((d, i, n) => {
297
+ renderTspansOnAxis(d3.select(n[i]), {
298
+ textArr: datum.textArr,
299
+ textSizePx,
300
+ groupAxisPosition: fullDataFormatter.grid.groupAxis.position
301
+ })
302
+ })
303
+
304
+ // const textUpdate = d3.select(n[i])
305
+ // .selectAll<SVGTextElement, LabelDatum>('text')
306
+ // .data([datum])
307
+ // const textEnter = textUpdate
308
+ // .enter()
309
+ // .append('text')
310
+ // .style('dominant-baseline', 'hanging')
311
+ // .style('cursor', 'pointer')
312
+ // // .style('pointer-events', 'none')
313
+ // const text = textUpdate.merge(textEnter)
314
+ // // .text(d => d.text)
315
+ // .style('transform', textReverseTransformWithRotate)
316
+ // .attr('fill', d => getColor(fullParams.labelTextColorType, fullChartParams))
317
+ // .attr('font-size', fullChartParams.styles.textSize)
318
+ // .attr('x', rectX + 6)
319
+ // .attr('y', rectY)
320
+ // textUpdate.exit().remove()
321
+
322
+ // text.each((d, i, n) => {
323
+ // renderTspansOnAxis(d3.select(n[i]), {
324
+ // textArr: datum.textArr,
325
+ // textSizePx,
326
+ // groupAxisPosition: fullDataFormatter.grid.groupAxis.position
327
+ // })
328
+ // })
329
+ })
330
+
331
+ return axisLabelSelection
332
+ }
333
+
334
+ function removeLabel (selection: d3.Selection<any, string, any, unknown>) {
335
+ const gUpdate = selection
336
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
337
+ .data([])
338
+
339
+ gUpdate.exit().remove()
340
+ }
341
+
342
+
343
+ export const GroupAux = defineGridPlugin(pluginConfig)(({ selection, rootSelection, name, subject, observer }) => {
344
+ const destroy$ = new Subject()
345
+
346
+ let isLabelMouseover = false
347
+
348
+ const rootRectSelection: d3.Selection<SVGRectElement, any, any, any> = rootSelection
349
+ .insert('rect', 'g')
350
+ .classed(getClassName(pluginName, 'rect'), true)
351
+ .attr('opacity', 0)
352
+
353
+ // const axisSelection: d3.Selection<SVGGElement, any, any, any> = selection
354
+ // .append('g')
355
+
356
+ const {
357
+ seriesSelection$,
358
+ axesSelection$,
359
+ defsSelection$,
360
+ graphicGSelection$
361
+ } = gridSelectionsObservable({
362
+ selection,
363
+ pluginName,
364
+ clipPathID: 'test',
365
+ seriesLabels$: observer.isSeriesSeprate$.pipe(
366
+ switchMap(isSeriesSeprate => {
367
+ return iif(
368
+ () => isSeriesSeprate,
369
+ observer.seriesLabels$,
370
+ // 如果沒分開的話只取一筆
371
+ observer.seriesLabels$.pipe(
372
+ map(d => [d[0]])
373
+ )
374
+ )
375
+ })
376
+ ),
377
+ gridContainerPosition$: observer.gridContainerPosition$,
378
+ gridAxesTransform$: observer.gridAxesTransform$,
379
+ gridGraphicTransform$: observer.gridGraphicTransform$
380
+ })
381
+
382
+ observer.layout$.pipe(
383
+ takeUntil(destroy$),
384
+ ).subscribe(d => {
385
+ rootRectSelection
386
+ .attr('width', d.rootWidth)
387
+ .attr('height', d.rootHeight)
388
+ })
389
+
390
+ // observer.gridAxesTransform$
391
+ // .pipe(
392
+ // takeUntil(destroy$),
393
+ // map(d => d.value),
394
+ // distinctUntilChanged()
395
+ // ).subscribe(d => {
396
+ // axisSelection
397
+ // .style('transform', d)
398
+ // })
399
+
400
+ // const visibleComputedData$ = observer.computedData$.pipe(
401
+ // takeUntil(destroy$),
402
+ // map(data => {
403
+ // const visibleComputedData = data
404
+ // .map(d => {
405
+ // return d.filter(_d => {
406
+ // return _d.visible == true
407
+ // })
408
+ // })
409
+ // .filter(d => d.length)
410
+ // // console.log('visibleComputedData', visibleComputedData)
411
+ // return visibleComputedData
412
+ // })
413
+ // )
414
+
415
+ // const SeriesDataMap$ = visibleComputedData$.pipe(
416
+ // map(d => makeGridSeriesDataMap(d))
417
+ // )
418
+
419
+ // const GroupDataMap$ = visibleComputedData$.pipe(
420
+ // map(d => makeGridGroupDataMap(d))
421
+ // )
422
+
423
+ // const contentTransform$: Observable<string> = new Observable(subscriber => {
424
+ // combineLatest({
425
+ // fullParams: observer.fullParams$,
426
+ // gridAxesTransform: observer.gridAxesTransform$
427
+ // }).pipe(
428
+ // takeUntil(destroy$),
429
+ // // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
430
+ // switchMap(async (d) => d),
431
+ // ).subscribe(data => {
432
+
433
+ // const transformData = Object.assign({}, data.gridAxesTransform)
434
+
435
+ // // const value = getAxesTransformValue({
436
+ // // translate: [0, 0],
437
+ // // scale: [transformData.scale[0] * -1, transformData.scale[1] * -1],
438
+ // // rotate: transformData.rotate * -1,
439
+ // // rotateX: transformData.rotateX * -1,
440
+ // // rotateY: transformData.rotateY * -1
441
+ // // })
442
+
443
+ // subscriber.next(transformData.value)
444
+ // })
445
+ // })
446
+ // const reverseTransform$: Observable<TransformData> = observer.gridAxesTransform$.pipe(
447
+ // takeUntil(destroy$),
448
+ // map(d => {
449
+ // const translate: [number, number] = [d.translate[0] * -1, d.translate[1] * -1]
450
+ // const scale: [number, number] = [d.scale[0] * -1, d.scale[1] * -1]
451
+ // const rotate = d.rotate * -1
452
+ // const rotateX = d.rotateX * -1
453
+ // const rotateY = d.rotateY * -1
454
+ // return {
455
+ // translate,
456
+ // scale,
457
+ // rotate,
458
+ // rotateX,
459
+ // rotateY,
460
+ // value: ''
461
+ // }
462
+ // }),
463
+ // )
464
+ // const contentTransform$ = combineLatest({
465
+ // fullParams: observer.fullParams$,
466
+ // reverseTransform: reverseTransform$
467
+ // }).pipe(
468
+ // takeUntil(destroy$),
469
+ // switchMap(async data => {
470
+ // const translate = [0, 0]
471
+ // return `translate(${translate[0]}px, ${translate[1]}px) rotate(${data.reverseTransform.rotate}deg) rotateX(${data.reverseTransform.rotateX}deg) rotateY(${data.reverseTransform.rotateY}deg)`
472
+ // }),
473
+ // distinctUntilChanged()
474
+ // )
475
+
476
+ // const groupScale$: Observable<d3.ScalePoint<string>> = new Observable(subscriber => {
477
+ // combineLatest({
478
+ // fullDataFormatter: observer.fullDataFormatter$,
479
+ // gridAxesSize: observer.gridAxesSize$,
480
+ // computedData: observer.computedData$
481
+ // }).pipe(
482
+ // takeUntil(destroy$),
483
+ // switchMap(async (d) => d),
484
+ // ).subscribe(data => {
485
+ // const groupMin = 0
486
+ // const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
487
+ // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
488
+ // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
489
+ // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
490
+ // const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'auto'
491
+ // ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
492
+ // : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
493
+
494
+ // const groupingLength = data.computedData[0]
495
+ // ? data.computedData[0].length
496
+ // : 0
497
+
498
+ // let _labels = data.fullDataFormatter.grid.seriesDirection === 'row'
499
+ // // ? data.fullDataFormatter.grid.columnLabels
500
+ // // : data.fullDataFormatter.grid.rowLabels
501
+ // ? (data.computedData[0] ?? []).map(d => d.groupLabel)
502
+ // : data.computedData.map(d => d[0].groupLabel)
503
+
504
+ // const axisLabels = new Array(groupingLength).fill(0)
505
+ // .map((d, i) => {
506
+ // return _labels[i] != null
507
+ // ? _labels[i]
508
+ // : String(i) // 沒有label則用序列號填充
509
+ // })
510
+ // .filter((d, i) => {
511
+ // return i >= groupScaleDomainMin && i <= groupScaleDomainMax
512
+ // })
513
+
514
+
515
+ // const padding = data.fullDataFormatter.grid.groupAxis.scalePadding
516
+
517
+ // const groupScale = createAxisPointScale({
518
+ // axisLabels,
519
+ // axisWidth: data.gridAxesSize.width,
520
+ // padding
521
+ // })
522
+
523
+ // subscriber.next(groupScale)
524
+ // })
525
+ // })
526
+
527
+ const groupScaleDomain$ = combineLatest({
528
+ fullDataFormatter: observer.fullDataFormatter$,
529
+ gridAxesSize: observer.gridAxesSize$,
530
+ computedData: observer.computedData$
531
+ }).pipe(
532
+ takeUntil(destroy$),
533
+ switchMap(async (d) => d),
534
+ map(data => {
535
+ const groupMin = 0
536
+ const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
537
+ // const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
538
+ // ? groupMin - data.fullDataFormatter.grid.groupAxis.scalePadding
539
+ // : data.fullDataFormatter.grid.groupAxis.scaleDomain[0] as number - data.fullDataFormatter.grid.groupAxis.scalePadding
540
+ const groupScaleDomainMin = data.fullDataFormatter.grid.groupAxis.scaleDomain[0] - data.fullDataFormatter.grid.groupAxis.scalePadding
541
+ const groupScaleDomainMax = data.fullDataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
542
+ ? groupMax + data.fullDataFormatter.grid.groupAxis.scalePadding
543
+ : data.fullDataFormatter.grid.groupAxis.scaleDomain[1] as number + data.fullDataFormatter.grid.groupAxis.scalePadding
544
+
545
+ return [groupScaleDomainMin, groupScaleDomainMax]
546
+ }),
547
+ shareReplay(1)
548
+ )
549
+
550
+ const groupScale$ = combineLatest({
551
+ groupScaleDomain: groupScaleDomain$,
552
+ gridAxesSize: observer.gridAxesSize$
553
+ }).pipe(
554
+ takeUntil(destroy$),
555
+ switchMap(async (d) => d),
556
+ map(data => {
557
+ const groupScale: d3.ScaleLinear<number, number> = d3.scaleLinear()
558
+ .domain(data.groupScaleDomain)
559
+ .range([0, data.gridAxesSize.width])
560
+ return groupScale
561
+ })
562
+ )
563
+
564
+ // // 取得事件座標的group資料
565
+ // const gridGroupPositionFn$ = gridGroupPositionFnObservable({
566
+ // fullDataFormatter$: observer.fullDataFormatter$,
567
+ // gridAxesSize$: observer.gridAxesSize$,
568
+ // computedData$: observer.computedData$,
569
+ // fullChartParams$: observer.fullChartParams$,
570
+ // })
571
+
572
+ const highlightTarget$ = observer.fullChartParams$.pipe(
573
+ takeUntil(destroy$),
574
+ map(d => d.highlightTarget),
575
+ distinctUntilChanged()
576
+ )
577
+
578
+ // combineLatest({
579
+ // computedData: observer.computedData$,
580
+ // gridAxesSize: observer.gridAxesSize$,
581
+ // fullParams: observer.fullParams$,
582
+ // fullChartParams: observer.fullChartParams$,
583
+ // highlightTarget: highlightTarget$,
584
+ // SeriesDataMap: observer.SeriesDataMap$,
585
+ // GroupDataMap: observer.GroupDataMap$,
586
+ // gridGroupPositionFn: gridGroupPositionFn$,
587
+ // groupScale: groupScale$,
588
+ // }).pipe(
589
+ // takeUntil(destroy$),
590
+ // switchMap(async (d) => d),
591
+ // ).subscribe(data => {
592
+
593
+ // // store.selection
594
+ // rootSelection
595
+ // .on('mouseover', (event, datum) => {
596
+ // // event.stopPropagation()
597
+
598
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
599
+
600
+ // subject.event$.next({
601
+ // type: 'grid',
602
+ // pluginName: name,
603
+ // eventName: 'mouseover',
604
+ // highlightTarget: data.highlightTarget,
605
+ // datum: null,
606
+ // gridIndex: 0, // @Q@ 暫不處理
607
+ // series: [],
608
+ // seriesIndex: -1,
609
+ // seriesLabel: '',
610
+ // groups: data.GroupDataMap.get(groupLabel) ?? [],
611
+ // // groups: [],
612
+ // groupIndex,
613
+ // groupLabel,
614
+ // event,
615
+ // data: data.computedData
616
+ // })
617
+ // })
618
+ // .on('mousemove', (event, datum) => {
619
+ // // event.stopPropagation()
620
+
621
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
622
+
623
+ // subject.event$.next({
624
+ // type: 'grid',
625
+ // pluginName: name,
626
+ // eventName: 'mousemove',
627
+ // highlightTarget: data.highlightTarget,
628
+ // datum: null,
629
+ // gridIndex: 0, // @Q@ 暫不處理
630
+ // series: [],
631
+ // seriesIndex: -1,
632
+ // seriesLabel: '',
633
+ // groups: data.GroupDataMap.get(groupLabel) ?? [],
634
+ // // groups: [],
635
+ // groupIndex,
636
+ // groupLabel,
637
+ // event,
638
+ // data: data.computedData
639
+ // })
640
+ // })
641
+ // .on('mouseout', (event, datum) => {
642
+ // // event.stopPropagation()
643
+
644
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
645
+
646
+ // subject.event$.next({
647
+ // type: 'grid',
648
+ // pluginName: name,
649
+ // eventName: 'mouseout',
650
+ // highlightTarget: data.highlightTarget,
651
+ // datum: null,
652
+ // gridIndex: 0, // @Q@ 暫不處理
653
+ // series: [],
654
+ // seriesIndex: -1,
655
+ // seriesLabel: '',
656
+ // groups: data.GroupDataMap.get(groupLabel) ?? [],
657
+ // // groups: [],
658
+ // groupIndex,
659
+ // groupLabel,
660
+ // event,
661
+ // data: data.computedData
662
+ // })
663
+ // })
664
+ // .on('click', (event, datum) => {
665
+ // event.stopPropagation()
666
+
667
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
668
+
669
+ // subject.event$.next({
670
+ // type: 'grid',
671
+ // pluginName: name,
672
+ // eventName: 'click',
673
+ // highlightTarget: data.highlightTarget,
674
+ // datum: null,
675
+ // gridIndex: 0, // @Q@ 暫不處理
676
+ // series: [],
677
+ // seriesIndex: -1,
678
+ // seriesLabel: '',
679
+ // // groups: data.GroupDataMap.get(groupLabel) ?? [],
680
+ // groups: [],
681
+ // groupIndex,
682
+ // groupLabel,
683
+ // event,
684
+ // data: data.computedData
685
+ // })
686
+ // })
687
+
688
+ // // barSelection$.next(barSelection!)
689
+ // })
690
+
691
+ const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove').pipe(
692
+ takeUntil(destroy$),
693
+ )
694
+
695
+ // const mousemoveGroupLabel$ = combineLatest({
696
+ // rootMousemove: rootMousemove$,
697
+ // gridGroupPositionFn: gridGroupPositionFn$,
698
+ // }).pipe(
699
+ // takeUntil(destroy$),
700
+ // switchMap(async d => d),
701
+ // map(data => {
702
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(data.rootMousemove)
703
+ // return { groupIndex, groupLabel }
704
+ // }),
705
+ // shareReplay(1)
706
+ // )
707
+
708
+
709
+ const textReverseTransform$ = combineLatest({
710
+ gridAxesReverseTransform: observer.gridAxesReverseTransform$,
711
+ gridContainerPosition: observer.gridContainerPosition$
712
+ }).pipe(
713
+ takeUntil(destroy$),
714
+ switchMap(async (d) => d),
715
+ map(data => {
716
+ // const axisReverseTranslateValue = `translate(${data.gridAxesReverseTransform.translate[0]}px, ${data.gridAxesReverseTransform.translate[1]}px)`
717
+ const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
718
+ const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
719
+ const containerScaleReverseValue = `scale(${1 / data.gridContainerPosition[0].scale[0]}, ${1 / data.gridContainerPosition[0].scale[1]})`
720
+ // 必須按照順序(先抵消外層rotate,再抵消最外層scale)
721
+ return `${axesRotateXYReverseValue} ${axesRotateReverseValue} ${containerScaleReverseValue}`
722
+ }),
723
+ distinctUntilChanged()
724
+ )
725
+
726
+ const textReverseTransformWithRotate$ = combineLatest({
727
+ textReverseTransform: textReverseTransform$,
728
+ fullParams: observer.fullParams$,
729
+ }).pipe(
730
+ takeUntil(destroy$),
731
+ switchMap(async (d) => d),
732
+ map(data => {
733
+ // 必須按照順序(先抵消外層rotate,再抵消最外層scale,最後再做本身的rotate)
734
+ return `${data.textReverseTransform} rotate(${data.fullParams.labelRotate}deg)`
735
+ })
736
+ )
737
+
738
+ const columnAmount$ = observer.gridContainerPosition$.pipe(
739
+ takeUntil(destroy$),
740
+ map(gridContainerPosition => {
741
+ const maxColumnIndex = gridContainerPosition.reduce((acc, current) => {
742
+ return current.columnIndex > acc ? current.columnIndex : acc
743
+ }, 0)
744
+ return maxColumnIndex + 1
745
+ }),
746
+ distinctUntilChanged()
747
+ )
748
+
749
+ const rowAmount$ = observer.gridContainerPosition$.pipe(
750
+ takeUntil(destroy$),
751
+ map(gridContainerPosition => {
752
+ const maxRowIndex = gridContainerPosition.reduce((acc, current) => {
753
+ return current.rowIndex > acc ? current.rowIndex : acc
754
+ }, 0)
755
+ return maxRowIndex + 1
756
+ }),
757
+ distinctUntilChanged()
758
+ )
759
+
760
+ const gridGroupPosition$ = gridGroupPosition({
761
+ rootSelection,
762
+ fullDataFormatter$: observer.fullDataFormatter$,
763
+ gridAxesSize$: observer.gridAxesSize$,
764
+ computedData$: observer.computedData$,
765
+ fullChartParams$: observer.fullChartParams$,
766
+ gridContainerPosition$: observer.gridContainerPosition$,
767
+ layout$: observer.layout$
768
+ }).pipe(
769
+ takeUntil(destroy$)
770
+ )
771
+
772
+ combineLatest({
773
+ axesSelection: axesSelection$,
774
+ columnAmount: columnAmount$,
775
+ rowAmount: rowAmount$,
776
+ layout: observer.layout$,
777
+ rootMousemove: rootMousemove$,
778
+ // gridGroupPositionFn: gridGroupPositionFn$,
779
+ gridGroupPosition: gridGroupPosition$,
780
+ computedData: observer.computedData$,
781
+ groupScale: groupScale$,
782
+ gridAxesSize: observer.gridAxesSize$,
783
+ fullParams: observer.fullParams$,
784
+ fullDataFormatter: observer.fullDataFormatter$,
785
+ fullChartParams: observer.fullChartParams$,
786
+ highlightTarget: highlightTarget$,
787
+ // gridAxesReverseTransform: observer.gridAxesReverseTransform$,
788
+ textReverseTransformWithRotate: textReverseTransformWithRotate$,
789
+ GroupDataMap: observer.GroupDataMap$,
790
+ textSizePx: observer.textSizePx$
791
+ }).pipe(
792
+ takeUntil(destroy$),
793
+ switchMap(async d => d),
794
+ ).subscribe(data => {
795
+ // // 由於event座標是基於底層的,但是container會有多欄,所以要重新計算
796
+ // const eventData = {
797
+ // offsetX: data.rootMousemove.offsetX * data.columnAmount % data.layout.rootWidth,
798
+ // offsetY: data.rootMousemove.offsetY * data.rowAmount % data.layout.rootHeight
799
+ // }
800
+ // 依event的座標取得group資料
801
+ const { groupIndex, groupLabel } = data.gridGroupPosition
802
+ // console.log('gridGroupPosition', groupIndex, groupLabel)
803
+ const axisX = data.groupScale(groupIndex) ?? 0
804
+ // console.log('axisX', axisX)
805
+ const lineData = createLineData({
806
+ groupLabel: groupLabel,
807
+ axisX,
808
+ axisHeight: data.gridAxesSize.height,
809
+ fullParams: data.fullParams,
810
+ })
811
+ // console.log('lineData', lineData)
812
+ renderLine({
813
+ // selection: axisSelection,
814
+ selection: data.axesSelection,
815
+ pluginName: name,
816
+ lineData,
817
+ fullParams: data.fullParams,
818
+ fullChartParams: data.fullChartParams
819
+ })
820
+ const labelData = createLabelData({
821
+ groupLabel: groupLabel,
822
+ axisX,
823
+ fullParams: data.fullParams,
824
+ textSizePx: data.textSizePx
825
+ })
826
+ const labelSelection = renderLabel({
827
+ // selection: axisSelection,
828
+ selection: data.axesSelection,
829
+ labelData,
830
+ fullParams: data.fullParams,
831
+ fullDataFormatter: data.fullDataFormatter,
832
+ fullChartParams: data.fullChartParams,
833
+ // gridAxesReverseTransformValue: data.gridAxesReverseTransform.value,
834
+ textReverseTransformWithRotate: data.textReverseTransformWithRotate,
835
+ textSizePx: data.textSizePx
836
+ })
837
+
838
+ // label的事件
839
+ labelSelection
840
+ .on('mouseover', (event, datum) => {
841
+ event.stopPropagation()
842
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
843
+
844
+ isLabelMouseover = true
845
+
846
+ subject.event$.next({
847
+ type: 'grid',
848
+ pluginName: name,
849
+ eventName: 'mouseover',
850
+ highlightTarget: data.highlightTarget,
851
+ datum: null,
852
+ gridIndex: 0, // @Q@ 暫不處理
853
+ series: [],
854
+ seriesIndex: -1,
855
+ seriesLabel: '',
856
+ groups: data.GroupDataMap.get(groupLabel) ?? [],
857
+ groupIndex,
858
+ groupLabel,
859
+ event,
860
+ data: data.computedData
861
+ })
862
+ })
863
+ .on('mousemove', (event, datum) => {
864
+ event.stopPropagation()
865
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
866
+
867
+ subject.event$.next({
868
+ type: 'grid',
869
+ pluginName: name,
870
+ eventName: 'mousemove',
871
+ highlightTarget: data.highlightTarget,
872
+ datum: null,
873
+ gridIndex: 0, // @Q@ 暫不處理
874
+ series: [],
875
+ seriesIndex: -1,
876
+ seriesLabel: '',
877
+ groups: data.GroupDataMap.get(groupLabel) ?? [],
878
+ groupIndex,
879
+ groupLabel,
880
+ event,
881
+ data: data.computedData
882
+ })
883
+ })
884
+ .on('mouseout', (event, datum) => {
885
+ event.stopPropagation()
886
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
887
+
888
+ isLabelMouseover = false
889
+
890
+ subject.event$.next({
891
+ type: 'grid',
892
+ pluginName: name,
893
+ eventName: 'mouseout',
894
+ highlightTarget: data.highlightTarget,
895
+ datum: null,
896
+ gridIndex: 0, // @Q@ 暫不處理
897
+ series: [],
898
+ seriesIndex: -1,
899
+ seriesLabel: '',
900
+ groups: data.GroupDataMap.get(groupLabel) ?? [],
901
+ groupIndex,
902
+ groupLabel,
903
+ event,
904
+ data: data.computedData
905
+ })
906
+ })
907
+ .on('click', (event, datum) => {
908
+ event.stopPropagation()
909
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
910
+
911
+ subject.event$.next({
912
+ type: 'grid',
913
+ pluginName: name,
914
+ eventName: 'click',
915
+ highlightTarget: data.highlightTarget,
916
+ datum: null,
917
+ gridIndex: 0, // @Q@ 暫不處理
918
+ series: [],
919
+ seriesIndex: -1,
920
+ seriesLabel: '',
921
+ groups: data.GroupDataMap.get(groupLabel) ?? [],
922
+ groupIndex,
923
+ groupLabel,
924
+ event,
925
+ data: data.computedData
926
+ })
927
+ })
928
+
929
+ })
930
+
931
+ // // -- highlight(無論highlightTarget設定為何,一律依從groupLabel來顯示) --
932
+ // combineLatest({
933
+ // event: subject.event$.pipe(
934
+ // filter(d => d.eventName === 'mouseover' || d.eventName === 'mousemove')
935
+ // ),
936
+ // computedData: observer.computedData$,
937
+ // groupScale: groupScale$,
938
+ // gridAxesSize: observer.gridAxesSize$,
939
+ // fullParams: observer.fullParams$,
940
+ // fullChartParams: observer.fullChartParams$,
941
+ // highlightTarget: highlightTarget$,
942
+ // gridAxesReverseTransform: observer.gridAxesReverseTransform$,
943
+ // GroupDataMap: observer.GroupDataMap$,
944
+ // gridGroupPositionFn: gridGroupPositionFn$,
945
+ // textSizePx: observer.textSizePx$
946
+ // }).pipe(
947
+ // takeUntil(destroy$),
948
+ // switchMap(async d => d)
949
+ // ).subscribe(data => {
950
+ // // const groups = data.event.eventName === 'mouseover' || data.event.eventName === 'mousemove'
951
+ // // ? data.event.groups
952
+ // // : []
953
+
954
+ // // const groupLabel = data.event.eventName === 'mouseover' || data.event.eventName === 'mousemove'
955
+ // // ? data.event.groupLabel
956
+ // // : ''
957
+ // const axisX = data.groupScale(data.event.groupLabel) ?? 0
958
+
959
+ // const lineData = createLineData({
960
+ // groupLabel: data.event.groupLabel,
961
+ // axisX,
962
+ // axisHeight: data.gridAxesSize.height,
963
+ // fullParams: data.fullParams,
964
+ // })
965
+ // renderLine({
966
+ // selection: axisSelection,
967
+ // pluginName: name,
968
+ // lineData,
969
+ // fullParams: data.fullParams,
970
+ // fullChartParams: data.fullChartParams
971
+ // })
972
+ // const labelData = createLabelData({
973
+ // groupLabel: data.event.groupLabel,
974
+ // axisX,
975
+ // fullParams: data.fullParams
976
+ // })
977
+ // const labelSelection = renderLabel({
978
+ // selection: axisSelection,
979
+ // labelData,
980
+ // fullParams: data.fullParams,
981
+ // fullChartParams: data.fullChartParams,
982
+ // gridAxesReverseTransformValue: data.gridAxesReverseTransform.value,
983
+ // textSizePx: data.textSizePx
984
+ // })
985
+
986
+ // // label的事件
987
+ // labelSelection
988
+ // .on('mouseover', (event, datum) => {
989
+
990
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
991
+
992
+ // subject.event$.next({
993
+ // type: 'grid',
994
+ // pluginName: name,
995
+ // eventName: 'mouseover',
996
+ // highlightTarget: data.highlightTarget,
997
+ // datum: null,
998
+ // gridIndex: 0, // @Q@ 暫不處理
999
+ // series: [],
1000
+ // seriesIndex: -1,
1001
+ // seriesLabel: '',
1002
+ // groups: data.event.groups,
1003
+ // groupIndex,
1004
+ // groupLabel,
1005
+ // event,
1006
+ // data: data.computedData
1007
+ // })
1008
+ // })
1009
+ // .on('mousemove', (event, datum) => {
1010
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
1011
+
1012
+ // subject.event$.next({
1013
+ // type: 'grid',
1014
+ // pluginName: name,
1015
+ // eventName: 'mousemove',
1016
+ // highlightTarget: data.highlightTarget,
1017
+ // datum: null,
1018
+ // gridIndex: 0, // @Q@ 暫不處理
1019
+ // series: [],
1020
+ // seriesIndex: -1,
1021
+ // seriesLabel: '',
1022
+ // groups: data.event.groups,
1023
+ // groupIndex,
1024
+ // groupLabel,
1025
+ // event,
1026
+ // data: data.computedData
1027
+ // })
1028
+ // })
1029
+ // .on('mouseout', (event, datum) => {
1030
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
1031
+
1032
+ // subject.event$.next({
1033
+ // type: 'grid',
1034
+ // pluginName: name,
1035
+ // eventName: 'mouseout',
1036
+ // highlightTarget: data.highlightTarget,
1037
+ // datum: null,
1038
+ // gridIndex: 0, // @Q@ 暫不處理
1039
+ // series: [],
1040
+ // seriesIndex: -1,
1041
+ // seriesLabel: '',
1042
+ // groups: data.event.groups,
1043
+ // groupIndex,
1044
+ // groupLabel,
1045
+ // event,
1046
+ // data: data.computedData
1047
+ // })
1048
+ // })
1049
+ // .on('click', (event, datum) => {
1050
+ // const { groupIndex, groupLabel } = data.gridGroupPositionFn(event)
1051
+
1052
+ // subject.event$.next({
1053
+ // type: 'grid',
1054
+ // pluginName: name,
1055
+ // eventName: 'click',
1056
+ // highlightTarget: data.highlightTarget,
1057
+ // datum: null,
1058
+ // gridIndex: 0, // @Q@ 暫不處理
1059
+ // series: [],
1060
+ // seriesIndex: -1,
1061
+ // seriesLabel: '',
1062
+ // groups: data.event.groups,
1063
+ // groupIndex,
1064
+ // groupLabel,
1065
+ // event,
1066
+ // data: data.computedData
1067
+ // })
1068
+ // })
1069
+ // })
1070
+
1071
+
1072
+
1073
+ const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
1074
+ takeUntil(destroy$),
1075
+ )
1076
+
1077
+ combineLatest({
1078
+ rootRectMouseout: rootRectMouseout$,
1079
+ axesSelection: axesSelection$,
1080
+ }).pipe(
1081
+ takeUntil(destroy$),
1082
+ switchMap(async d => d)
1083
+ ).subscribe(data => {
1084
+ setTimeout(() => {
1085
+ // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
1086
+ if (isLabelMouseover == true) {
1087
+ return
1088
+ }
1089
+
1090
+ removeLine(data.axesSelection)
1091
+ removeLabel(data.axesSelection)
1092
+ })
1093
+ })
1094
+
1095
+ return () => {
1096
+ destroy$.next(undefined)
1097
+ rootRectSelection.remove()
1098
+ }
1099
1099
  })