@orbcharts/plugins-basic 3.0.7 → 3.0.9

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