@orbcharts/plugins-basic 3.0.5 → 3.0.7

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 (116) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +3414 -3390
  3. package/dist/orbcharts-plugins-basic.umd.js +35 -35
  4. package/lib/core-types.ts +7 -7
  5. package/lib/core.ts +6 -6
  6. package/lib/gridObservables.ts +6 -6
  7. package/lib/plugins-basic-types.ts +6 -6
  8. package/package.json +48 -48
  9. package/src/base/BaseBars.ts +765 -765
  10. package/src/base/BaseBarsTriangle.ts +676 -676
  11. package/src/base/BaseDots.ts +464 -464
  12. package/src/base/BaseGroupAxis.ts +691 -691
  13. package/src/base/BaseLegend.ts +684 -684
  14. package/src/base/BaseLineAreas.ts +629 -629
  15. package/src/base/BaseLines.ts +706 -706
  16. package/src/base/BaseOrdinalBubbles.ts +729 -729
  17. package/src/base/BaseRacingBars.ts +582 -582
  18. package/src/base/BaseRacingLabels.ts +404 -404
  19. package/src/base/BaseRacingValueLabels.ts +403 -403
  20. package/src/base/BaseStackedBars.ts +782 -782
  21. package/src/base/BaseTooltip.ts +408 -386
  22. package/src/base/BaseValueAxis.ts +600 -600
  23. package/src/base/BaseXAxis.ts +427 -427
  24. package/src/base/BaseXZoom.ts +241 -241
  25. package/src/base/BaseYAxis.ts +389 -389
  26. package/src/base/types.ts +2 -2
  27. package/src/const.ts +30 -30
  28. package/src/grid/defaults.ts +213 -213
  29. package/src/grid/gridObservables.ts +635 -635
  30. package/src/grid/index.ts +16 -16
  31. package/src/grid/plugins/Bars.ts +69 -69
  32. package/src/grid/plugins/BarsPN.ts +66 -66
  33. package/src/grid/plugins/BarsTriangle.ts +73 -73
  34. package/src/grid/plugins/Dots.ts +68 -68
  35. package/src/grid/plugins/GridLegend.ts +107 -107
  36. package/src/grid/plugins/GridTooltip.ts +66 -66
  37. package/src/grid/plugins/GroupAux.ts +1095 -1095
  38. package/src/grid/plugins/GroupAxis.ts +73 -73
  39. package/src/grid/plugins/GroupZoom.ts +218 -218
  40. package/src/grid/plugins/LineAreas.ts +65 -65
  41. package/src/grid/plugins/Lines.ts +59 -59
  42. package/src/grid/plugins/StackedBars.ts +64 -64
  43. package/src/grid/plugins/StackedValueAxis.ts +96 -96
  44. package/src/grid/plugins/ValueAxis.ts +94 -94
  45. package/src/index.ts +6 -6
  46. package/src/multiGrid/defaults.ts +244 -244
  47. package/src/multiGrid/index.ts +14 -14
  48. package/src/multiGrid/multiGridObservables.ts +50 -50
  49. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  50. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  51. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  52. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  53. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  54. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  55. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  56. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  57. package/src/multiGrid/plugins/MultiStackedBars.ts +106 -106
  58. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +134 -134
  59. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  60. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  61. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  62. package/src/multiValue/defaults.ts +523 -523
  63. package/src/multiValue/index.ts +16 -16
  64. package/src/multiValue/multiValueObservables.ts +781 -781
  65. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  66. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  67. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  68. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  69. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  70. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  71. package/src/multiValue/plugins/RacingBars.ts +375 -375
  72. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  73. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  74. package/src/multiValue/plugins/Scatter.ts +486 -486
  75. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  76. package/src/multiValue/plugins/XAxis.ts +107 -107
  77. package/src/multiValue/plugins/XYAux.ts +683 -683
  78. package/src/multiValue/plugins/XYAxes.ts +194 -194
  79. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  80. package/src/multiValue/plugins/XZoom.ts +40 -40
  81. package/src/noneData/defaults.ts +102 -102
  82. package/src/noneData/index.ts +3 -3
  83. package/src/noneData/plugins/Container.ts +27 -27
  84. package/src/noneData/plugins/Tooltip.ts +373 -373
  85. package/src/relationship/defaults.ts +221 -221
  86. package/src/relationship/index.ts +5 -5
  87. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  88. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  89. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  90. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  91. package/src/relationship/relationshipObservables.ts +49 -49
  92. package/src/series/defaults.ts +223 -222
  93. package/src/series/index.ts +9 -9
  94. package/src/series/plugins/Bubbles.ts +784 -766
  95. package/src/series/plugins/Pie.ts +622 -622
  96. package/src/series/plugins/PieEventTexts.ts +283 -283
  97. package/src/series/plugins/PieLabels.ts +639 -639
  98. package/src/series/plugins/Rose.ts +515 -515
  99. package/src/series/plugins/RoseLabels.ts +599 -599
  100. package/src/series/plugins/SeriesLegend.ts +107 -107
  101. package/src/series/plugins/SeriesTooltip.ts +66 -66
  102. package/src/series/seriesObservables.ts +168 -168
  103. package/src/series/seriesUtils.ts +51 -51
  104. package/src/tree/defaults.ts +102 -102
  105. package/src/tree/index.ts +4 -4
  106. package/src/tree/plugins/TreeLegend.ts +100 -100
  107. package/src/tree/plugins/TreeMap.ts +341 -341
  108. package/src/tree/plugins/TreeTooltip.ts +66 -66
  109. package/src/utils/commonUtils.ts +31 -31
  110. package/src/utils/d3Graphics.ts +176 -176
  111. package/src/utils/d3Utils.ts +92 -92
  112. package/src/utils/observables.ts +14 -14
  113. package/src/utils/orbchartsUtils.ts +129 -129
  114. package/tsconfig.base.json +13 -13
  115. package/tsconfig.json +2 -2
  116. package/vite.config.js +22 -22
@@ -1,661 +1,661 @@
1
- import * as d3 from 'd3'
2
- import {
3
- // of,
4
- iif,
5
- debounceTime,
6
- EMPTY,
7
- combineLatest,
8
- switchMap,
9
- map,
10
- filter,
11
- first,
12
- takeUntil,
13
- distinctUntilChanged,
14
- shareReplay,
15
- Subject,
16
- Observable } from 'rxjs'
17
- import {
18
- defineMultiValuePlugin } from '../../../lib/core'
19
- import type { DefinePluginConfig } from '../../../lib/core-types'
20
- import type {
21
- ColorType,
22
- TransformData,
23
- DataFormatterMultiValue,
24
- ChartParams,
25
- Layout,
26
- } from '../../../lib/core-types'
27
- import { DEFAULT_ORDINAL_AUX_PARAMS } from '../defaults'
28
- import { parseTickFormatValue } from '../../utils/d3Utils'
29
- import { measureTextWidth } from '../../utils/commonUtils'
30
- import { getColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
31
- import { d3EventObservable } from '../../utils/observables'
32
- import { ordinalPositionObservable } from '../multiValueObservables'
33
- import type { OrdinalAuxParams } from '../../../lib/plugins-basic-types'
34
- import { multiValueSelectionsObservable } from '../multiValueObservables'
35
- import { renderTspansOnAxis } from '../../utils/d3Graphics'
36
- import { LAYER_INDEX_OF_AUX } from '../../const'
37
-
38
- interface LineDatum {
39
- id: string
40
- x1: number
41
- x2: number
42
- y1: number
43
- y2: number
44
- dashArray: string
45
- colorType: ColorType
46
- }
47
-
48
- interface LabelDatum {
49
- id: string
50
- text: string
51
- textArr: string[]
52
- textWidth: number
53
- textHeight: number
54
- colorType: ColorType
55
- textColorType: ColorType
56
- x: number
57
- y: number
58
- rectWidth: number
59
- rectHeight: number
60
- rectX: number
61
- rectY: number
62
- textX: number
63
- textY: number
64
- }
65
-
66
- interface ValueLabelData {
67
- text: string
68
- textArr: string[]
69
- }
70
-
71
- const pluginName = 'OrdinalAux'
72
- const labelClassName = getClassName(pluginName, 'label-box')
73
-
74
- const rectPaddingWidth = 6
75
- const rectPaddingHeight = 3
76
-
77
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_ORDINAL_AUX_PARAMS> = {
78
- name: pluginName,
79
- defaultParams: DEFAULT_ORDINAL_AUX_PARAMS,
80
- layerIndex: LAYER_INDEX_OF_AUX,
81
- validator: (params, { validateColumns }) => {
82
- const result = validateColumns(params, {
83
- showLine: {
84
- toBeTypes: ['boolean']
85
- },
86
- showLabel: {
87
- toBeTypes: ['boolean']
88
- },
89
- lineDashArray: {
90
- toBeTypes: ['string']
91
- },
92
- lineColorType: {
93
- toBeOption: 'ColorType'
94
- },
95
- labelColorType: {
96
- toBeOption: 'ColorType'
97
- },
98
- labelTextColorType: {
99
- toBeOption: 'ColorType'
100
- },
101
- labelTextFormat: {
102
- toBeTypes: ['Function']
103
- },
104
- labelPadding: {
105
- toBeTypes: ['number']
106
- },
107
- })
108
- return result
109
- }
110
- }
111
-
112
- function createValueLabelData (groupLabels: string[], tickFormat: (text: any) => string): ValueLabelData[] {
113
- return groupLabels.map((_text, i) => {
114
- const text = parseTickFormatValue(_text, tickFormat)
115
- const textArr = typeof text === 'string' ? text.split('\n') : [text]
116
-
117
- return {
118
- text,
119
- textArr
120
- }
121
- })
122
- }
123
-
124
- function createLineData ({ axisX, layout, fullParams }: {
125
- axisX: number
126
- // axisY: number
127
- layout: Layout
128
- fullParams: OrdinalAuxParams
129
- }): LineDatum[] {
130
- if ((axisX >= 0 && axisX <= layout.width) === false) {
131
- return []
132
- }
133
- return [
134
- {
135
- id: 'line-x',
136
- x1: axisX,
137
- x2: axisX,
138
- y1: 0,
139
- y2: layout.height,
140
- dashArray: fullParams.lineDashArray ?? 'none',
141
- colorType: fullParams.lineColorType
142
- },
143
- ]
144
- }
145
-
146
- function createLabelData ({ valueLabelData, axisX, xValue, fullParams, textSizePx, layout, rowAmount }: {
147
- valueLabelData: ValueLabelData[]
148
- axisX: number
149
- // axisY: number
150
- xValue: number
151
- // yValue: number
152
- fullParams: OrdinalAuxParams
153
- textSizePx: number
154
- layout: Layout
155
- // columnAmount: number
156
- rowAmount: number
157
- }): LabelDatum[] {
158
- if ((axisX >= 0 && axisX <= layout.width) === false) {
159
- return []
160
- }
161
-
162
-
163
- // x
164
- const xX = axisX
165
- const xY = - fullParams.labelPadding * rowAmount // rowAmount 是為了把外部 container 的變形逆轉回來
166
- // const xText = fullParams.labelTextFormat(xValue)
167
- // const xTextArr = xText.split('\n')
168
- const xText = valueLabelData[xValue] ? valueLabelData[xValue].text : ''
169
- const xTextArr = valueLabelData[xValue] ? valueLabelData[xValue].textArr : []
170
- const xMaxLengthText = xTextArr.reduce((acc, current) => current.length > acc.length ? current : acc, '')
171
- const xTextWidth = measureTextWidth(xMaxLengthText, textSizePx)
172
- const xTextHeight = textSizePx * xTextArr.length
173
- const xRectWidth = xTextWidth + (rectPaddingWidth * 2)
174
- const xRectHeight = xTextHeight + (rectPaddingHeight * 2)
175
- const xRectX = - xRectWidth / 2
176
- const xRectY = - rectPaddingHeight
177
- const xTextX = xRectX + rectPaddingWidth
178
- const xTextY = xRectY + rectPaddingHeight
179
- return [
180
- {
181
- id: 'label-x',
182
- x: xX,
183
- y: xY - xRectHeight / 2,
184
- text: xText,
185
- textArr: xTextArr,
186
- textWidth: xTextWidth,
187
- textHeight: xTextHeight,
188
- colorType: fullParams.labelColorType,
189
- textColorType: fullParams.labelTextColorType,
190
- rectWidth: xRectWidth,
191
- rectHeight: xRectHeight,
192
- rectX: xRectX,
193
- rectY: xRectY,
194
- textX: xTextX,
195
- textY: xTextY
196
- },
197
- ]
198
- }
199
-
200
- function renderLine ({ selection, pluginName, lineData, fullParams, fullChartParams }: {
201
- selection: d3.Selection<any, string, any, unknown>
202
- pluginName: string
203
- lineData: LineDatum[]
204
- fullParams: OrdinalAuxParams
205
- fullChartParams: ChartParams
206
- }) {
207
- const gClassName = getClassName(pluginName, 'auxline')
208
- const auxLineSelection = selection
209
- .selectAll<SVGLineElement, LineDatum>(`line.${gClassName}`)
210
- .data(lineData)
211
- .join(
212
- enter => {
213
- return enter
214
- .append('line')
215
- .classed(gClassName, true)
216
- .style('stroke-width', 1)
217
- .style('pointer-events', 'none')
218
- .style('vector-effect', 'non-scaling-stroke')
219
- .attr('x1', d => d.x1)
220
- .attr('y1', d => d.y1)
221
- .attr('x2', d => d.x2)
222
- .attr('y2', d => d.y2)
223
- },
224
- update => {
225
- const updateSelection = update
226
- .transition()
227
- .duration(50)
228
- .attr('x1', d => d.x1)
229
- .attr('y1', d => d.y1)
230
- .attr('x2', d => d.x2)
231
- .attr('y2', d => d.y2)
232
- return updateSelection
233
- },
234
- exit => exit.remove()
235
- )
236
- .style('stroke', d => getColor(d.colorType, fullChartParams))
237
- .style('stroke-dasharray', d => d.dashArray)
238
-
239
- return auxLineSelection
240
- }
241
-
242
- function removeLine (selection: d3.Selection<any, string, any, unknown>) {
243
- const update = selection
244
- .selectAll<SVGLineElement, LineDatum>('line')
245
- .data([])
246
-
247
- update.exit().remove()
248
- }
249
-
250
- function renderLabel ({ selection, labelData, fullParams, fullDataFormatter, fullChartParams, textReverseTransform, textSizePx }: {
251
- selection: d3.Selection<any, string, any, unknown>
252
- labelData: LabelDatum[]
253
- fullParams: OrdinalAuxParams
254
- fullDataFormatter: DataFormatterMultiValue
255
- fullChartParams: ChartParams
256
- // gridAxesReverseTransformValue: string
257
- textReverseTransform: string
258
- textSizePx: number
259
- }) {
260
- // const rectHeight = textSizePx + 6
261
-
262
- const axisLabelSelection = selection
263
- .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
264
- .data(labelData)
265
- .join(
266
- enter => {
267
- return enter
268
- .append('g')
269
- .classed(labelClassName, true)
270
- .style('cursor', 'pointer')
271
- .attr("transform", (d, i) => {
272
- return `translate(${d.x}, ${d.y})`
273
- })
274
- },
275
- update => {
276
- const updateSelection = update
277
- .transition()
278
- .duration(50)
279
- .attr("transform", (d, i) => {
280
- return `translate(${d.x}, ${d.y})`
281
- })
282
- return updateSelection
283
- },
284
- exit => exit.remove()
285
- )
286
- .each((datum, i, n) => {
287
- const gSelection = d3.select(n[i])
288
-
289
- // -- rect --
290
- const rect = gSelection
291
- .selectAll<SVGRectElement, LabelDatum>('rect')
292
- .data([datum])
293
- .join(
294
- enter => enter.append('rect')
295
- .style('cursor', 'pointer')
296
- .attr('rx', 5)
297
- .attr('ry', 5),
298
- update => update,
299
- exit => exit.remove()
300
- )
301
- .attr('width', d => `${d.rectWidth}px`)
302
- .attr('height', d => `${d.rectHeight}px`)
303
- .attr('fill', d => getColor(d.colorType, fullChartParams))
304
- .attr('x', d => d.rectX)
305
- .attr('y', d => d.rectY)
306
- .style('transform', textReverseTransform)
307
-
308
- // -- text --
309
- const text = gSelection
310
- .selectAll<SVGTextElement, LabelDatum>('text')
311
- .data([datum])
312
- .join(
313
- enter => enter.append('text')
314
- .style('dominant-baseline', 'hanging')
315
- .style('cursor', 'pointer')
316
- .style('pointer-events', 'none'),
317
- update => update,
318
- exit => exit.remove()
319
- )
320
- .style('transform', textReverseTransform)
321
- .attr('fill', d => getColor(d.textColorType, fullChartParams))
322
- .attr('font-size', fullChartParams.styles.textSize)
323
- .attr('x', d => d.textX)
324
- .attr('y', d => d.textY)
325
- .each((d, i, n) => {
326
- renderTspansOnAxis(d3.select(n[i]), {
327
- textArr: datum.textArr,
328
- textSizePx,
329
- groupAxisPosition: i === 0
330
- ? 'bottom' // x axis
331
- : 'left', // y axis
332
- isContainerRotated: false
333
- })
334
- })
335
-
336
- // -- 第二次文字寬度(因為原本計算的文字寬度有可能因為字體差異會有誤差) --
337
- // 取得文字寬度
338
- let textWidthArr: number[] = []
339
- text.selectAll('tspan')
340
- .each((d, i, n) => {
341
- const tspan = d3.select(n[i])
342
- const textNode: SVGTextElement = tspan.node() as SVGTextElement
343
- if (textNode && textNode.getBBox()) {
344
- textWidthArr.push(textNode.getBBox().width)
345
- }
346
- })
347
- const maxTextWidth = Math.max(...textWidthArr)
348
- // 依文字寬度設定矩形寬度
349
- rect.attr('width', maxTextWidth + rectPaddingWidth * 2)
350
-
351
- })
352
-
353
- return axisLabelSelection
354
- }
355
-
356
- function removeLabel (selection: d3.Selection<any, string, any, unknown>) {
357
- const gUpdate = selection
358
- .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
359
- .data([])
360
-
361
- gUpdate.exit().remove()
362
- }
363
-
364
-
365
- export const OrdinalAux = defineMultiValuePlugin(pluginConfig)(({ selection, rootSelection, name, subject, observer }) => {
366
- const destroy$ = new Subject()
367
-
368
- let isLabelMouseover: boolean = false
369
-
370
- const rootRectSelection: d3.Selection<SVGRectElement, any, any, any> = rootSelection
371
- .insert('rect', 'g')
372
- .classed(getClassName(pluginName, 'rect'), true)
373
- .attr('opacity', 0)
374
-
375
- // const axisSelection: d3.Selection<SVGGElement, any, any, any> = selection
376
- // .append('g')
377
-
378
- const {
379
- categorySelection$,
380
- axesSelection$,
381
- defsSelection$,
382
- graphicGSelection$
383
- } = multiValueSelectionsObservable({
384
- selection,
385
- pluginName,
386
- clipPathID: 'test',
387
- categoryLabels$: observer.isCategorySeprate$.pipe(
388
- switchMap(isCategorySeprate => {
389
- return iif(
390
- () => isCategorySeprate,
391
- observer.categoryLabels$,
392
- // 如果沒分開的話只取一筆
393
- observer.categoryLabels$.pipe(
394
- map(d => [d[0]])
395
- )
396
- )
397
- })
398
- ),
399
- containerPosition$: observer.containerPosition$,
400
- graphicTransform$: observer.graphicTransform$
401
- })
402
-
403
- observer.layout$.pipe(
404
- takeUntil(destroy$),
405
- ).subscribe(d => {
406
- rootRectSelection
407
- .attr('width', d.rootWidth)
408
- .attr('height', d.rootHeight)
409
- })
410
-
411
- const highlightTarget$ = observer.fullChartParams$.pipe(
412
- takeUntil(destroy$),
413
- map(d => d.highlightTarget),
414
- distinctUntilChanged()
415
- )
416
-
417
- // const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove')
418
- // .pipe(
419
- // takeUntil(destroy$),
420
- // debounceTime(10)
421
- // )
422
-
423
- // let r = 0
424
- // rootMousemove$.subscribe(d => {
425
- // r++
426
- // console.log('r:', r)
427
- // })
428
-
429
- const columnAmount$ = observer.containerPosition$.pipe(
430
- map(containerPosition => {
431
- const maxColumnIndex = containerPosition.reduce((acc, current) => {
432
- return current.columnIndex > acc ? current.columnIndex : acc
433
- }, 0)
434
- return maxColumnIndex + 1
435
- }),
436
- distinctUntilChanged()
437
- )
438
-
439
- const rowAmount$ = observer.containerPosition$.pipe(
440
- map(containerPosition => {
441
- const maxRowIndex = containerPosition.reduce((acc, current) => {
442
- return current.rowIndex > acc ? current.rowIndex : acc
443
- }, 0)
444
- return maxRowIndex + 1
445
- }),
446
- distinctUntilChanged()
447
- )
448
-
449
- const textReverseTransform$ = observer.containerPosition$.pipe(
450
- takeUntil(destroy$),
451
- switchMap(async (d) => d),
452
- map(containerPosition => {
453
- // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
454
- // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
455
- const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
456
- // 抵消最外層scale
457
- return `${containerScaleReverseValue}`
458
- }),
459
- distinctUntilChanged()
460
- )
461
-
462
- // const ordinalPosition$ = multiValueXYPositionObservable({
463
- // rootSelection,
464
- // fullDataFormatter$: observer.fullDataFormatter$,
465
- // filteredXYMinMaxData$: observer.filteredXYMinMaxData$,
466
- // containerPosition$: observer.containerPosition$,
467
- // layout$: observer.layout$
468
- // }).pipe(
469
- // takeUntil(destroy$)
470
- // )
471
-
472
- const ordinalPosition$ = ordinalPositionObservable({
473
- rootSelection,
474
- ordinalScaleDomain$: observer.ordinalScaleDomain$,
475
- ordinalScale$: observer.ordinalScale$,
476
- ordinalPadding$: observer.ordinalPadding$,
477
- containerSize$: observer.containerSize$,
478
- containerPosition$: observer.containerPosition$,
479
- layout$: observer.layout$,
480
- })
481
-
482
- const valueLabelData$ = combineLatest({
483
- valueLabels: observer.valueLabels$,
484
- fullParams: observer.fullParams$,
485
- }).pipe(
486
- takeUntil(destroy$),
487
- switchMap(async (d) => d),
488
- map(data => {
489
- return createValueLabelData(data.valueLabels, data.fullParams.labelTextFormat)
490
- }),
491
- shareReplay(1)
492
- )
493
-
494
- combineLatest({
495
- axesSelection: axesSelection$,
496
- // rootMousemove: rootMousemove$,
497
- layout: observer.layout$,
498
- ordinalPosition: ordinalPosition$,
499
- computedData: observer.computedData$,
500
- fullParams: observer.fullParams$,
501
- fullDataFormatter: observer.fullDataFormatter$,
502
- fullChartParams: observer.fullChartParams$,
503
- highlightTarget: highlightTarget$,
504
- textReverseTransform: textReverseTransform$,
505
- // CategoryDataMap: observer.CategoryDataMap$,
506
- textSizePx: observer.textSizePx$,
507
- columnAmount: columnAmount$,
508
- rowAmount: rowAmount$,
509
- valueLabelData: valueLabelData$,
510
- }).pipe(
511
- takeUntil(destroy$),
512
- switchMap(async d => d),
513
- ).subscribe(data => {
514
- // 依event的座標取得group資料
515
- const { x, xValue } = data.ordinalPosition
516
- // console.log({ x, xValue })
517
-
518
- const lineData = createLineData({
519
- axisX: x,
520
- // axisY: y,
521
- layout: data.layout,
522
- fullParams: data.fullParams,
523
- })
524
- renderLine({
525
- selection: data.axesSelection,
526
- pluginName: name,
527
- lineData,
528
- fullParams: data.fullParams,
529
- fullChartParams: data.fullChartParams
530
- })
531
- const labelData = createLabelData({
532
- valueLabelData: data.valueLabelData,
533
- axisX: x,
534
- // axisY: y,
535
- xValue,
536
- // yValue,
537
- fullParams: data.fullParams,
538
- textSizePx: data.textSizePx,
539
- layout: data.layout,
540
- // columnAmount: data.columnAmount,
541
- rowAmount: data.rowAmount
542
- })
543
- const labelSelection = renderLabel({
544
- selection: data.axesSelection,
545
- labelData,
546
- fullParams: data.fullParams,
547
- fullDataFormatter: data.fullDataFormatter,
548
- fullChartParams: data.fullChartParams,
549
- textReverseTransform: data.textReverseTransform,
550
- textSizePx: data.textSizePx
551
- })
552
-
553
- // label的事件
554
- labelSelection
555
- .on('mouseover', (event, datum) => {
556
- event.stopPropagation()
557
- // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
558
-
559
- isLabelMouseover = true
560
-
561
- subject.event$.next({
562
- type: 'multiValue',
563
- eventName: 'mouseover',
564
- pluginName,
565
- highlightTarget: data.highlightTarget,
566
- valueDetail: [],
567
- datum: null,
568
- category: [],
569
- categoryIndex: -1,
570
- categoryLabel: '',
571
- data: data.computedData,
572
- event,
573
- })
574
- })
575
- .on('mousemove', (event, datum) => {
576
- event.stopPropagation()
577
- // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
578
-
579
- subject.event$.next({
580
- type: 'multiValue',
581
- eventName: 'mousemove',
582
- pluginName,
583
- highlightTarget: data.highlightTarget,
584
- valueDetail: [],
585
- datum: null,
586
- category: [],
587
- categoryIndex: -1,
588
- categoryLabel: '',
589
- data: data.computedData,
590
- event,
591
- })
592
- })
593
- .on('mouseout', (event, datum) => {
594
- event.stopPropagation()
595
- // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
596
-
597
- isLabelMouseover = false
598
-
599
- subject.event$.next({
600
- type: 'multiValue',
601
- eventName: 'mouseout',
602
- pluginName,
603
- highlightTarget: data.highlightTarget,
604
- valueDetail: [],
605
- datum: null,
606
- category: [],
607
- categoryIndex: -1,
608
- categoryLabel: '',
609
- data: data.computedData,
610
- event,
611
- })
612
- })
613
- .on('click', (event, datum) => {
614
- event.stopPropagation()
615
- // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
616
-
617
- subject.event$.next({
618
- type: 'multiValue',
619
- eventName: 'click',
620
- pluginName,
621
- highlightTarget: data.highlightTarget,
622
- valueDetail: [],
623
- datum: null,
624
- category: [],
625
- categoryIndex: -1,
626
- categoryLabel: '',
627
- data: data.computedData,
628
- event,
629
- })
630
- })
631
-
632
- })
633
-
634
-
635
- const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
636
- takeUntil(destroy$),
637
- )
638
-
639
- combineLatest({
640
- rootRectMouseout: rootRectMouseout$,
641
- axesSelection: axesSelection$,
642
- }).pipe(
643
- takeUntil(destroy$),
644
- switchMap(async d => d)
645
- ).subscribe(data => {
646
- setTimeout(() => {
647
- // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
648
- if (isLabelMouseover == true) {
649
- return
650
- }
651
-
652
- removeLine(data.axesSelection)
653
- removeLabel(data.axesSelection)
654
- })
655
- })
656
-
657
- return () => {
658
- destroy$.next(undefined)
659
- rootRectSelection.remove()
660
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ // of,
4
+ iif,
5
+ debounceTime,
6
+ EMPTY,
7
+ combineLatest,
8
+ switchMap,
9
+ map,
10
+ filter,
11
+ first,
12
+ takeUntil,
13
+ distinctUntilChanged,
14
+ shareReplay,
15
+ Subject,
16
+ Observable } from 'rxjs'
17
+ import {
18
+ defineMultiValuePlugin } from '../../../lib/core'
19
+ import type { DefinePluginConfig } from '../../../lib/core-types'
20
+ import type {
21
+ ColorType,
22
+ TransformData,
23
+ DataFormatterMultiValue,
24
+ ChartParams,
25
+ Layout,
26
+ } from '../../../lib/core-types'
27
+ import { DEFAULT_ORDINAL_AUX_PARAMS } from '../defaults'
28
+ import { parseTickFormatValue } from '../../utils/d3Utils'
29
+ import { measureTextWidth } from '../../utils/commonUtils'
30
+ import { getColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
31
+ import { d3EventObservable } from '../../utils/observables'
32
+ import { ordinalPositionObservable } from '../multiValueObservables'
33
+ import type { OrdinalAuxParams } from '../../../lib/plugins-basic-types'
34
+ import { multiValueSelectionsObservable } from '../multiValueObservables'
35
+ import { renderTspansOnAxis } from '../../utils/d3Graphics'
36
+ import { LAYER_INDEX_OF_AUX } from '../../const'
37
+
38
+ interface LineDatum {
39
+ id: string
40
+ x1: number
41
+ x2: number
42
+ y1: number
43
+ y2: number
44
+ dashArray: string
45
+ colorType: ColorType
46
+ }
47
+
48
+ interface LabelDatum {
49
+ id: string
50
+ text: string
51
+ textArr: string[]
52
+ textWidth: number
53
+ textHeight: number
54
+ colorType: ColorType
55
+ textColorType: ColorType
56
+ x: number
57
+ y: number
58
+ rectWidth: number
59
+ rectHeight: number
60
+ rectX: number
61
+ rectY: number
62
+ textX: number
63
+ textY: number
64
+ }
65
+
66
+ interface ValueLabelData {
67
+ text: string
68
+ textArr: string[]
69
+ }
70
+
71
+ const pluginName = 'OrdinalAux'
72
+ const labelClassName = getClassName(pluginName, 'label-box')
73
+
74
+ const rectPaddingWidth = 6
75
+ const rectPaddingHeight = 3
76
+
77
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_ORDINAL_AUX_PARAMS> = {
78
+ name: pluginName,
79
+ defaultParams: DEFAULT_ORDINAL_AUX_PARAMS,
80
+ layerIndex: LAYER_INDEX_OF_AUX,
81
+ validator: (params, { validateColumns }) => {
82
+ const result = validateColumns(params, {
83
+ showLine: {
84
+ toBeTypes: ['boolean']
85
+ },
86
+ showLabel: {
87
+ toBeTypes: ['boolean']
88
+ },
89
+ lineDashArray: {
90
+ toBeTypes: ['string']
91
+ },
92
+ lineColorType: {
93
+ toBeOption: 'ColorType'
94
+ },
95
+ labelColorType: {
96
+ toBeOption: 'ColorType'
97
+ },
98
+ labelTextColorType: {
99
+ toBeOption: 'ColorType'
100
+ },
101
+ labelTextFormat: {
102
+ toBeTypes: ['Function']
103
+ },
104
+ labelPadding: {
105
+ toBeTypes: ['number']
106
+ },
107
+ })
108
+ return result
109
+ }
110
+ }
111
+
112
+ function createValueLabelData (groupLabels: string[], tickFormat: (text: any) => string): ValueLabelData[] {
113
+ return groupLabels.map((_text, i) => {
114
+ const text = parseTickFormatValue(_text, tickFormat)
115
+ const textArr = typeof text === 'string' ? text.split('\n') : [text]
116
+
117
+ return {
118
+ text,
119
+ textArr
120
+ }
121
+ })
122
+ }
123
+
124
+ function createLineData ({ axisX, layout, fullParams }: {
125
+ axisX: number
126
+ // axisY: number
127
+ layout: Layout
128
+ fullParams: OrdinalAuxParams
129
+ }): LineDatum[] {
130
+ if ((axisX >= 0 && axisX <= layout.width) === false) {
131
+ return []
132
+ }
133
+ return [
134
+ {
135
+ id: 'line-x',
136
+ x1: axisX,
137
+ x2: axisX,
138
+ y1: 0,
139
+ y2: layout.height,
140
+ dashArray: fullParams.lineDashArray ?? 'none',
141
+ colorType: fullParams.lineColorType
142
+ },
143
+ ]
144
+ }
145
+
146
+ function createLabelData ({ valueLabelData, axisX, xValue, fullParams, textSizePx, layout, rowAmount }: {
147
+ valueLabelData: ValueLabelData[]
148
+ axisX: number
149
+ // axisY: number
150
+ xValue: number
151
+ // yValue: number
152
+ fullParams: OrdinalAuxParams
153
+ textSizePx: number
154
+ layout: Layout
155
+ // columnAmount: number
156
+ rowAmount: number
157
+ }): LabelDatum[] {
158
+ if ((axisX >= 0 && axisX <= layout.width) === false) {
159
+ return []
160
+ }
161
+
162
+
163
+ // x
164
+ const xX = axisX
165
+ const xY = - fullParams.labelPadding * rowAmount // rowAmount 是為了把外部 container 的變形逆轉回來
166
+ // const xText = fullParams.labelTextFormat(xValue)
167
+ // const xTextArr = xText.split('\n')
168
+ const xText = valueLabelData[xValue] ? valueLabelData[xValue].text : ''
169
+ const xTextArr = valueLabelData[xValue] ? valueLabelData[xValue].textArr : []
170
+ const xMaxLengthText = xTextArr.reduce((acc, current) => current.length > acc.length ? current : acc, '')
171
+ const xTextWidth = measureTextWidth(xMaxLengthText, textSizePx)
172
+ const xTextHeight = textSizePx * xTextArr.length
173
+ const xRectWidth = xTextWidth + (rectPaddingWidth * 2)
174
+ const xRectHeight = xTextHeight + (rectPaddingHeight * 2)
175
+ const xRectX = - xRectWidth / 2
176
+ const xRectY = - rectPaddingHeight
177
+ const xTextX = xRectX + rectPaddingWidth
178
+ const xTextY = xRectY + rectPaddingHeight
179
+ return [
180
+ {
181
+ id: 'label-x',
182
+ x: xX,
183
+ y: xY - xRectHeight / 2,
184
+ text: xText,
185
+ textArr: xTextArr,
186
+ textWidth: xTextWidth,
187
+ textHeight: xTextHeight,
188
+ colorType: fullParams.labelColorType,
189
+ textColorType: fullParams.labelTextColorType,
190
+ rectWidth: xRectWidth,
191
+ rectHeight: xRectHeight,
192
+ rectX: xRectX,
193
+ rectY: xRectY,
194
+ textX: xTextX,
195
+ textY: xTextY
196
+ },
197
+ ]
198
+ }
199
+
200
+ function renderLine ({ selection, pluginName, lineData, fullParams, fullChartParams }: {
201
+ selection: d3.Selection<any, string, any, unknown>
202
+ pluginName: string
203
+ lineData: LineDatum[]
204
+ fullParams: OrdinalAuxParams
205
+ fullChartParams: ChartParams
206
+ }) {
207
+ const gClassName = getClassName(pluginName, 'auxline')
208
+ const auxLineSelection = selection
209
+ .selectAll<SVGLineElement, LineDatum>(`line.${gClassName}`)
210
+ .data(lineData)
211
+ .join(
212
+ enter => {
213
+ return enter
214
+ .append('line')
215
+ .classed(gClassName, true)
216
+ .style('stroke-width', 1)
217
+ .style('pointer-events', 'none')
218
+ .style('vector-effect', 'non-scaling-stroke')
219
+ .attr('x1', d => d.x1)
220
+ .attr('y1', d => d.y1)
221
+ .attr('x2', d => d.x2)
222
+ .attr('y2', d => d.y2)
223
+ },
224
+ update => {
225
+ const updateSelection = update
226
+ .transition()
227
+ .duration(50)
228
+ .attr('x1', d => d.x1)
229
+ .attr('y1', d => d.y1)
230
+ .attr('x2', d => d.x2)
231
+ .attr('y2', d => d.y2)
232
+ return updateSelection
233
+ },
234
+ exit => exit.remove()
235
+ )
236
+ .style('stroke', d => getColor(d.colorType, fullChartParams))
237
+ .style('stroke-dasharray', d => d.dashArray)
238
+
239
+ return auxLineSelection
240
+ }
241
+
242
+ function removeLine (selection: d3.Selection<any, string, any, unknown>) {
243
+ const update = selection
244
+ .selectAll<SVGLineElement, LineDatum>('line')
245
+ .data([])
246
+
247
+ update.exit().remove()
248
+ }
249
+
250
+ function renderLabel ({ selection, labelData, fullParams, fullDataFormatter, fullChartParams, textReverseTransform, textSizePx }: {
251
+ selection: d3.Selection<any, string, any, unknown>
252
+ labelData: LabelDatum[]
253
+ fullParams: OrdinalAuxParams
254
+ fullDataFormatter: DataFormatterMultiValue
255
+ fullChartParams: ChartParams
256
+ // gridAxesReverseTransformValue: string
257
+ textReverseTransform: string
258
+ textSizePx: number
259
+ }) {
260
+ // const rectHeight = textSizePx + 6
261
+
262
+ const axisLabelSelection = selection
263
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
264
+ .data(labelData)
265
+ .join(
266
+ enter => {
267
+ return enter
268
+ .append('g')
269
+ .classed(labelClassName, true)
270
+ .style('cursor', 'pointer')
271
+ .attr("transform", (d, i) => {
272
+ return `translate(${d.x}, ${d.y})`
273
+ })
274
+ },
275
+ update => {
276
+ const updateSelection = update
277
+ .transition()
278
+ .duration(50)
279
+ .attr("transform", (d, i) => {
280
+ return `translate(${d.x}, ${d.y})`
281
+ })
282
+ return updateSelection
283
+ },
284
+ exit => exit.remove()
285
+ )
286
+ .each((datum, i, n) => {
287
+ const gSelection = d3.select(n[i])
288
+
289
+ // -- rect --
290
+ const rect = gSelection
291
+ .selectAll<SVGRectElement, LabelDatum>('rect')
292
+ .data([datum])
293
+ .join(
294
+ enter => enter.append('rect')
295
+ .style('cursor', 'pointer')
296
+ .attr('rx', 5)
297
+ .attr('ry', 5),
298
+ update => update,
299
+ exit => exit.remove()
300
+ )
301
+ .attr('width', d => `${d.rectWidth}px`)
302
+ .attr('height', d => `${d.rectHeight}px`)
303
+ .attr('fill', d => getColor(d.colorType, fullChartParams))
304
+ .attr('x', d => d.rectX)
305
+ .attr('y', d => d.rectY)
306
+ .style('transform', textReverseTransform)
307
+
308
+ // -- text --
309
+ const text = gSelection
310
+ .selectAll<SVGTextElement, LabelDatum>('text')
311
+ .data([datum])
312
+ .join(
313
+ enter => enter.append('text')
314
+ .style('dominant-baseline', 'hanging')
315
+ .style('cursor', 'pointer')
316
+ .style('pointer-events', 'none'),
317
+ update => update,
318
+ exit => exit.remove()
319
+ )
320
+ .style('transform', textReverseTransform)
321
+ .attr('fill', d => getColor(d.textColorType, fullChartParams))
322
+ .attr('font-size', fullChartParams.styles.textSize)
323
+ .attr('x', d => d.textX)
324
+ .attr('y', d => d.textY)
325
+ .each((d, i, n) => {
326
+ renderTspansOnAxis(d3.select(n[i]), {
327
+ textArr: datum.textArr,
328
+ textSizePx,
329
+ groupAxisPosition: i === 0
330
+ ? 'bottom' // x axis
331
+ : 'left', // y axis
332
+ isContainerRotated: false
333
+ })
334
+ })
335
+
336
+ // -- 第二次文字寬度(因為原本計算的文字寬度有可能因為字體差異會有誤差) --
337
+ // 取得文字寬度
338
+ let textWidthArr: number[] = []
339
+ text.selectAll('tspan')
340
+ .each((d, i, n) => {
341
+ const tspan = d3.select(n[i])
342
+ const textNode: SVGTextElement = tspan.node() as SVGTextElement
343
+ if (textNode && textNode.getBBox()) {
344
+ textWidthArr.push(textNode.getBBox().width)
345
+ }
346
+ })
347
+ const maxTextWidth = Math.max(...textWidthArr)
348
+ // 依文字寬度設定矩形寬度
349
+ rect.attr('width', maxTextWidth + rectPaddingWidth * 2)
350
+
351
+ })
352
+
353
+ return axisLabelSelection
354
+ }
355
+
356
+ function removeLabel (selection: d3.Selection<any, string, any, unknown>) {
357
+ const gUpdate = selection
358
+ .selectAll<SVGGElement, LabelDatum>(`g.${labelClassName}`)
359
+ .data([])
360
+
361
+ gUpdate.exit().remove()
362
+ }
363
+
364
+
365
+ export const OrdinalAux = defineMultiValuePlugin(pluginConfig)(({ selection, rootSelection, name, subject, observer }) => {
366
+ const destroy$ = new Subject()
367
+
368
+ let isLabelMouseover: boolean = false
369
+
370
+ const rootRectSelection: d3.Selection<SVGRectElement, any, any, any> = rootSelection
371
+ .insert('rect', 'g')
372
+ .classed(getClassName(pluginName, 'rect'), true)
373
+ .attr('opacity', 0)
374
+
375
+ // const axisSelection: d3.Selection<SVGGElement, any, any, any> = selection
376
+ // .append('g')
377
+
378
+ const {
379
+ categorySelection$,
380
+ axesSelection$,
381
+ defsSelection$,
382
+ graphicGSelection$
383
+ } = multiValueSelectionsObservable({
384
+ selection,
385
+ pluginName,
386
+ clipPathID: 'test',
387
+ categoryLabels$: observer.isCategorySeprate$.pipe(
388
+ switchMap(isCategorySeprate => {
389
+ return iif(
390
+ () => isCategorySeprate,
391
+ observer.categoryLabels$,
392
+ // 如果沒分開的話只取一筆
393
+ observer.categoryLabels$.pipe(
394
+ map(d => [d[0]])
395
+ )
396
+ )
397
+ })
398
+ ),
399
+ containerPosition$: observer.containerPosition$,
400
+ graphicTransform$: observer.graphicTransform$
401
+ })
402
+
403
+ observer.layout$.pipe(
404
+ takeUntil(destroy$),
405
+ ).subscribe(d => {
406
+ rootRectSelection
407
+ .attr('width', d.rootWidth)
408
+ .attr('height', d.rootHeight)
409
+ })
410
+
411
+ const highlightTarget$ = observer.fullChartParams$.pipe(
412
+ takeUntil(destroy$),
413
+ map(d => d.highlightTarget),
414
+ distinctUntilChanged()
415
+ )
416
+
417
+ // const rootMousemove$: Observable<any> = d3EventObservable(rootSelection, 'mousemove')
418
+ // .pipe(
419
+ // takeUntil(destroy$),
420
+ // debounceTime(10)
421
+ // )
422
+
423
+ // let r = 0
424
+ // rootMousemove$.subscribe(d => {
425
+ // r++
426
+ // console.log('r:', r)
427
+ // })
428
+
429
+ const columnAmount$ = observer.containerPosition$.pipe(
430
+ map(containerPosition => {
431
+ const maxColumnIndex = containerPosition.reduce((acc, current) => {
432
+ return current.columnIndex > acc ? current.columnIndex : acc
433
+ }, 0)
434
+ return maxColumnIndex + 1
435
+ }),
436
+ distinctUntilChanged()
437
+ )
438
+
439
+ const rowAmount$ = observer.containerPosition$.pipe(
440
+ map(containerPosition => {
441
+ const maxRowIndex = containerPosition.reduce((acc, current) => {
442
+ return current.rowIndex > acc ? current.rowIndex : acc
443
+ }, 0)
444
+ return maxRowIndex + 1
445
+ }),
446
+ distinctUntilChanged()
447
+ )
448
+
449
+ const textReverseTransform$ = observer.containerPosition$.pipe(
450
+ takeUntil(destroy$),
451
+ switchMap(async (d) => d),
452
+ map(containerPosition => {
453
+ // const axesRotateXYReverseValue = `rotateX(${data.gridAxesReverseTransform.rotateX}deg) rotateY(${data.gridAxesReverseTransform.rotateY}deg)`
454
+ // const axesRotateReverseValue = `rotate(${data.gridAxesReverseTransform.rotate}deg)`
455
+ const containerScaleReverseValue = `scale(${1 / containerPosition[0].scale[0]}, ${1 / containerPosition[0].scale[1]})`
456
+ // 抵消最外層scale
457
+ return `${containerScaleReverseValue}`
458
+ }),
459
+ distinctUntilChanged()
460
+ )
461
+
462
+ // const ordinalPosition$ = multiValueXYPositionObservable({
463
+ // rootSelection,
464
+ // fullDataFormatter$: observer.fullDataFormatter$,
465
+ // filteredXYMinMaxData$: observer.filteredXYMinMaxData$,
466
+ // containerPosition$: observer.containerPosition$,
467
+ // layout$: observer.layout$
468
+ // }).pipe(
469
+ // takeUntil(destroy$)
470
+ // )
471
+
472
+ const ordinalPosition$ = ordinalPositionObservable({
473
+ rootSelection,
474
+ ordinalScaleDomain$: observer.ordinalScaleDomain$,
475
+ ordinalScale$: observer.ordinalScale$,
476
+ ordinalPadding$: observer.ordinalPadding$,
477
+ containerSize$: observer.containerSize$,
478
+ containerPosition$: observer.containerPosition$,
479
+ layout$: observer.layout$,
480
+ })
481
+
482
+ const valueLabelData$ = combineLatest({
483
+ valueLabels: observer.valueLabels$,
484
+ fullParams: observer.fullParams$,
485
+ }).pipe(
486
+ takeUntil(destroy$),
487
+ switchMap(async (d) => d),
488
+ map(data => {
489
+ return createValueLabelData(data.valueLabels, data.fullParams.labelTextFormat)
490
+ }),
491
+ shareReplay(1)
492
+ )
493
+
494
+ combineLatest({
495
+ axesSelection: axesSelection$,
496
+ // rootMousemove: rootMousemove$,
497
+ layout: observer.layout$,
498
+ ordinalPosition: ordinalPosition$,
499
+ computedData: observer.computedData$,
500
+ fullParams: observer.fullParams$,
501
+ fullDataFormatter: observer.fullDataFormatter$,
502
+ fullChartParams: observer.fullChartParams$,
503
+ highlightTarget: highlightTarget$,
504
+ textReverseTransform: textReverseTransform$,
505
+ // CategoryDataMap: observer.CategoryDataMap$,
506
+ textSizePx: observer.textSizePx$,
507
+ columnAmount: columnAmount$,
508
+ rowAmount: rowAmount$,
509
+ valueLabelData: valueLabelData$,
510
+ }).pipe(
511
+ takeUntil(destroy$),
512
+ switchMap(async d => d),
513
+ ).subscribe(data => {
514
+ // 依event的座標取得group資料
515
+ const { x, xValue } = data.ordinalPosition
516
+ // console.log({ x, xValue })
517
+
518
+ const lineData = createLineData({
519
+ axisX: x,
520
+ // axisY: y,
521
+ layout: data.layout,
522
+ fullParams: data.fullParams,
523
+ })
524
+ renderLine({
525
+ selection: data.axesSelection,
526
+ pluginName: name,
527
+ lineData,
528
+ fullParams: data.fullParams,
529
+ fullChartParams: data.fullChartParams
530
+ })
531
+ const labelData = createLabelData({
532
+ valueLabelData: data.valueLabelData,
533
+ axisX: x,
534
+ // axisY: y,
535
+ xValue,
536
+ // yValue,
537
+ fullParams: data.fullParams,
538
+ textSizePx: data.textSizePx,
539
+ layout: data.layout,
540
+ // columnAmount: data.columnAmount,
541
+ rowAmount: data.rowAmount
542
+ })
543
+ const labelSelection = renderLabel({
544
+ selection: data.axesSelection,
545
+ labelData,
546
+ fullParams: data.fullParams,
547
+ fullDataFormatter: data.fullDataFormatter,
548
+ fullChartParams: data.fullChartParams,
549
+ textReverseTransform: data.textReverseTransform,
550
+ textSizePx: data.textSizePx
551
+ })
552
+
553
+ // label的事件
554
+ labelSelection
555
+ .on('mouseover', (event, datum) => {
556
+ event.stopPropagation()
557
+ // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
558
+
559
+ isLabelMouseover = true
560
+
561
+ subject.event$.next({
562
+ type: 'multiValue',
563
+ eventName: 'mouseover',
564
+ pluginName,
565
+ highlightTarget: data.highlightTarget,
566
+ valueDetail: [],
567
+ datum: null,
568
+ category: [],
569
+ categoryIndex: -1,
570
+ categoryLabel: '',
571
+ data: data.computedData,
572
+ event,
573
+ })
574
+ })
575
+ .on('mousemove', (event, datum) => {
576
+ event.stopPropagation()
577
+ // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
578
+
579
+ subject.event$.next({
580
+ type: 'multiValue',
581
+ eventName: 'mousemove',
582
+ pluginName,
583
+ highlightTarget: data.highlightTarget,
584
+ valueDetail: [],
585
+ datum: null,
586
+ category: [],
587
+ categoryIndex: -1,
588
+ categoryLabel: '',
589
+ data: data.computedData,
590
+ event,
591
+ })
592
+ })
593
+ .on('mouseout', (event, datum) => {
594
+ event.stopPropagation()
595
+ // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
596
+
597
+ isLabelMouseover = false
598
+
599
+ subject.event$.next({
600
+ type: 'multiValue',
601
+ eventName: 'mouseout',
602
+ pluginName,
603
+ highlightTarget: data.highlightTarget,
604
+ valueDetail: [],
605
+ datum: null,
606
+ category: [],
607
+ categoryIndex: -1,
608
+ categoryLabel: '',
609
+ data: data.computedData,
610
+ event,
611
+ })
612
+ })
613
+ .on('click', (event, datum) => {
614
+ event.stopPropagation()
615
+ // const { groupIndex, groupLabel } = data.ordinalPositionFn(event)
616
+
617
+ subject.event$.next({
618
+ type: 'multiValue',
619
+ eventName: 'click',
620
+ pluginName,
621
+ highlightTarget: data.highlightTarget,
622
+ valueDetail: [],
623
+ datum: null,
624
+ category: [],
625
+ categoryIndex: -1,
626
+ categoryLabel: '',
627
+ data: data.computedData,
628
+ event,
629
+ })
630
+ })
631
+
632
+ })
633
+
634
+
635
+ const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
636
+ takeUntil(destroy$),
637
+ )
638
+
639
+ combineLatest({
640
+ rootRectMouseout: rootRectMouseout$,
641
+ axesSelection: axesSelection$,
642
+ }).pipe(
643
+ takeUntil(destroy$),
644
+ switchMap(async d => d)
645
+ ).subscribe(data => {
646
+ setTimeout(() => {
647
+ // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
648
+ if (isLabelMouseover == true) {
649
+ return
650
+ }
651
+
652
+ removeLine(data.axesSelection)
653
+ removeLabel(data.axesSelection)
654
+ })
655
+ })
656
+
657
+ return () => {
658
+ destroy$.next(undefined)
659
+ rootRectSelection.remove()
660
+ }
661
661
  })