@orbcharts/plugins-basic 3.0.0-beta.19 → 3.0.0-beta.20

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 (110) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +60 -22
  3. package/dist/orbcharts-plugins-basic.umd.js +86 -66
  4. package/lib/core-types.ts +7 -7
  5. package/lib/core.ts +6 -6
  6. package/lib/plugins-basic-types.ts +6 -6
  7. package/package.json +44 -44
  8. package/src/base/BaseBars.ts +765 -765
  9. package/src/base/BaseBarsTriangle.ts +676 -676
  10. package/src/base/BaseDots.ts +464 -464
  11. package/src/base/BaseGroupAxis.ts +691 -691
  12. package/src/base/BaseLegend.ts +684 -684
  13. package/src/base/BaseLineAreas.ts +629 -629
  14. package/src/base/BaseLines.ts +706 -706
  15. package/src/base/BaseRacingBars.ts +551 -551
  16. package/src/base/BaseRacingLabels.ts +396 -396
  17. package/src/base/BaseRacingValueLabels.ts +403 -403
  18. package/src/base/BaseStackedBars.ts +782 -782
  19. package/src/base/BaseTooltip.ts +386 -386
  20. package/src/base/BaseValueAxis.ts +600 -600
  21. package/src/base/BaseXAxis.ts +427 -427
  22. package/src/base/BaseYAxis.ts +389 -389
  23. package/src/base/types.ts +2 -2
  24. package/src/const.ts +30 -30
  25. package/src/grid/defaults.ts +213 -213
  26. package/src/grid/gridObservables.ts +612 -612
  27. package/src/grid/index.ts +16 -16
  28. package/src/grid/plugins/Bars.ts +69 -69
  29. package/src/grid/plugins/BarsPN.ts +66 -66
  30. package/src/grid/plugins/BarsTriangle.ts +73 -73
  31. package/src/grid/plugins/Dots.ts +68 -68
  32. package/src/grid/plugins/GridLegend.ts +107 -107
  33. package/src/grid/plugins/GridTooltip.ts +66 -66
  34. package/src/grid/plugins/GroupAux.ts +1120 -1120
  35. package/src/grid/plugins/GroupAxis.ts +73 -73
  36. package/src/grid/plugins/GroupZoom.ts +218 -218
  37. package/src/grid/plugins/LineAreas.ts +65 -65
  38. package/src/grid/plugins/Lines.ts +59 -59
  39. package/src/grid/plugins/StackedBars.ts +64 -64
  40. package/src/grid/plugins/StackedValueAxis.ts +96 -96
  41. package/src/grid/plugins/ValueAxis.ts +94 -94
  42. package/src/index.ts +6 -6
  43. package/src/multiGrid/defaults.ts +244 -244
  44. package/src/multiGrid/index.ts +14 -14
  45. package/src/multiGrid/multiGridObservables.ts +50 -50
  46. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  47. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  48. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  49. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  50. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  51. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  52. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  53. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  54. package/src/multiGrid/plugins/MultiStackedBars.ts +106 -106
  55. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +134 -134
  56. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  57. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  58. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  59. package/src/multiValue/defaults.ts +431 -391
  60. package/src/multiValue/index.ts +12 -12
  61. package/src/multiValue/multiValueObservables.ts +666 -666
  62. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  63. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  64. package/src/multiValue/plugins/RacingBars.ts +373 -373
  65. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  66. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  67. package/src/multiValue/plugins/RankingAxis_legacy.ts +109 -109
  68. package/src/multiValue/plugins/Scatter.ts +426 -426
  69. package/src/multiValue/plugins/ScatterBubbles.ts +554 -554
  70. package/src/multiValue/plugins/XAxis.ts +107 -107
  71. package/src/multiValue/plugins/XYAux.ts +682 -682
  72. package/src/multiValue/plugins/XYAxes.ts +194 -194
  73. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  74. package/src/multiValue/plugins/XZoom.ts +299 -299
  75. package/src/noneData/defaults.ts +102 -102
  76. package/src/noneData/index.ts +3 -3
  77. package/src/noneData/plugins/Container.ts +27 -27
  78. package/src/noneData/plugins/Tooltip.ts +373 -373
  79. package/src/relationship/defaults.ts +221 -221
  80. package/src/relationship/index.ts +5 -5
  81. package/src/relationship/plugins/ForceDirected.ts +1173 -1173
  82. package/src/relationship/plugins/ForceDirectedBubbles.ts +1411 -1411
  83. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  84. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  85. package/src/relationship/relationshipObservables.ts +49 -49
  86. package/src/series/defaults.ts +221 -221
  87. package/src/series/index.ts +9 -9
  88. package/src/series/plugins/Bubbles.ts +636 -636
  89. package/src/series/plugins/Pie.ts +623 -623
  90. package/src/series/plugins/PieEventTexts.ts +284 -284
  91. package/src/series/plugins/PieLabels.ts +640 -640
  92. package/src/series/plugins/Rose.ts +516 -516
  93. package/src/series/plugins/RoseLabels.ts +600 -600
  94. package/src/series/plugins/SeriesLegend.ts +107 -107
  95. package/src/series/plugins/SeriesTooltip.ts +66 -66
  96. package/src/series/seriesObservables.ts +145 -145
  97. package/src/series/seriesUtils.ts +51 -51
  98. package/src/tree/defaults.ts +102 -102
  99. package/src/tree/index.ts +4 -4
  100. package/src/tree/plugins/TreeLegend.ts +100 -100
  101. package/src/tree/plugins/TreeMap.ts +341 -341
  102. package/src/tree/plugins/TreeTooltip.ts +66 -66
  103. package/src/utils/commonUtils.ts +31 -31
  104. package/src/utils/d3Graphics.ts +176 -176
  105. package/src/utils/d3Utils.ts +92 -92
  106. package/src/utils/observables.ts +14 -14
  107. package/src/utils/orbchartsUtils.ts +129 -129
  108. package/tsconfig.base.json +13 -13
  109. package/tsconfig.json +2 -2
  110. package/vite.config.js +22 -22
@@ -1,683 +1,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
- layout$: observer.layout$
519
- }).pipe(
520
- takeUntil(destroy$)
521
- )
522
-
523
- combineLatest({
524
- axesSelection: axesSelection$,
525
- // rootMousemove: rootMousemove$,
526
- layout: observer.layout$,
527
- xyPosition: xyPosition$,
528
- // computedData: observer.computedData$,
529
- fullParams: observer.fullParams$,
530
- fullDataFormatter: observer.fullDataFormatter$,
531
- fullChartParams: observer.fullChartParams$,
532
- // highlightTarget: highlightTarget$,
533
- textReverseTransform: textReverseTransform$,
534
- // CategoryDataMap: observer.CategoryDataMap$,
535
- textSizePx: observer.textSizePx$,
536
- columnAmount: columnAmount$,
537
- rowAmount: rowAmount$
538
- }).pipe(
539
- takeUntil(destroy$),
540
- switchMap(async d => d),
541
- ).subscribe(data => {
542
- // 依event的座標取得group資料
543
- const { x, y, xValue, yValue } = data.xyPosition
544
-
545
- const lineData = createLineData({
546
- axisX: x,
547
- axisY: y,
548
- layout: data.layout,
549
- fullParams: data.fullParams,
550
- })
551
- renderLine({
552
- selection: data.axesSelection,
553
- pluginName: name,
554
- lineData,
555
- fullParams: data.fullParams,
556
- fullChartParams: data.fullChartParams
557
- })
558
- const labelData = createLabelData({
559
- axisX: x,
560
- axisY: y,
561
- xValue,
562
- yValue,
563
- fullParams: data.fullParams,
564
- textSizePx: data.textSizePx,
565
- layout: data.layout,
566
- columnAmount: data.columnAmount,
567
- rowAmount: data.rowAmount
568
- })
569
- const labelSelection = renderLabel({
570
- selection: data.axesSelection,
571
- labelData,
572
- fullParams: data.fullParams,
573
- fullDataFormatter: data.fullDataFormatter,
574
- fullChartParams: data.fullChartParams,
575
- textReverseTransform: data.textReverseTransform,
576
- textSizePx: data.textSizePx
577
- })
578
-
579
- // label的事件
580
- // labelSelection
581
- // .on('mouseover', (event, datum) => {
582
- // event.stopPropagation()
583
- // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
584
-
585
- // isLabelMouseover = true
586
-
587
- // subject.event$.next({
588
- // type: 'multiValue',
589
- // eventName: 'mouseover',
590
- // pluginName,
591
- // highlightTarget: data.highlightTarget,
592
- // datum,
593
- // category: [],
594
- // categoryIndex: -1,
595
- // categoryLabel: '',
596
- // data: data.computedData,
597
- // event,
598
- // })
599
- // })
600
- // .on('mousemove', (event, datum) => {
601
- // event.stopPropagation()
602
- // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
603
-
604
- // subject.event$.next({
605
- // type: 'multiValue',
606
- // eventName: 'mousemove',
607
- // pluginName,
608
- // highlightTarget: data.highlightTarget,
609
- // datum,
610
- // category: [],
611
- // categoryIndex: -1,
612
- // categoryLabel: '',
613
- // data: data.computedData,
614
- // event,
615
- // })
616
- // })
617
- // .on('mouseout', (event, datum) => {
618
- // event.stopPropagation()
619
- // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
620
-
621
- // isLabelMouseover = false
622
-
623
- // subject.event$.next({
624
- // type: 'multiValue',
625
- // eventName: 'mouseout',
626
- // pluginName,
627
- // highlightTarget: data.highlightTarget,
628
- // datum,
629
- // category: [],
630
- // categoryIndex: -1,
631
- // categoryLabel: '',
632
- // data: data.computedData,
633
- // event,
634
- // })
635
- // })
636
- // .on('click', (event, datum) => {
637
- // event.stopPropagation()
638
- // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
639
-
640
- // subject.event$.next({
641
- // type: 'multiValue',
642
- // eventName: 'click',
643
- // pluginName,
644
- // highlightTarget: data.highlightTarget,
645
- // datum,
646
- // category: [],
647
- // categoryIndex: -1,
648
- // categoryLabel: '',
649
- // data: data.computedData,
650
- // event,
651
- // })
652
- // })
653
-
654
- })
655
-
656
-
657
- const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
658
- takeUntil(destroy$),
659
- )
660
-
661
- combineLatest({
662
- rootRectMouseout: rootRectMouseout$,
663
- axesSelection: axesSelection$,
664
- }).pipe(
665
- takeUntil(destroy$),
666
- switchMap(async d => d)
667
- ).subscribe(data => {
668
- setTimeout(() => {
669
- // // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
670
- // if (isLabelMouseover == true) {
671
- // return
672
- // }
673
-
674
- removeLine(data.axesSelection)
675
- removeLabel(data.axesSelection)
676
- })
677
- })
678
-
679
- return () => {
680
- destroy$.next(undefined)
681
- rootRectSelection.remove()
682
- }
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
+ layout$: observer.layout$
519
+ }).pipe(
520
+ takeUntil(destroy$)
521
+ )
522
+
523
+ combineLatest({
524
+ axesSelection: axesSelection$,
525
+ // rootMousemove: rootMousemove$,
526
+ layout: observer.layout$,
527
+ xyPosition: xyPosition$,
528
+ // computedData: observer.computedData$,
529
+ fullParams: observer.fullParams$,
530
+ fullDataFormatter: observer.fullDataFormatter$,
531
+ fullChartParams: observer.fullChartParams$,
532
+ // highlightTarget: highlightTarget$,
533
+ textReverseTransform: textReverseTransform$,
534
+ // CategoryDataMap: observer.CategoryDataMap$,
535
+ textSizePx: observer.textSizePx$,
536
+ columnAmount: columnAmount$,
537
+ rowAmount: rowAmount$
538
+ }).pipe(
539
+ takeUntil(destroy$),
540
+ switchMap(async d => d),
541
+ ).subscribe(data => {
542
+ // 依event的座標取得group資料
543
+ const { x, y, xValue, yValue } = data.xyPosition
544
+
545
+ const lineData = createLineData({
546
+ axisX: x,
547
+ axisY: y,
548
+ layout: data.layout,
549
+ fullParams: data.fullParams,
550
+ })
551
+ renderLine({
552
+ selection: data.axesSelection,
553
+ pluginName: name,
554
+ lineData,
555
+ fullParams: data.fullParams,
556
+ fullChartParams: data.fullChartParams
557
+ })
558
+ const labelData = createLabelData({
559
+ axisX: x,
560
+ axisY: y,
561
+ xValue,
562
+ yValue,
563
+ fullParams: data.fullParams,
564
+ textSizePx: data.textSizePx,
565
+ layout: data.layout,
566
+ columnAmount: data.columnAmount,
567
+ rowAmount: data.rowAmount
568
+ })
569
+ const labelSelection = renderLabel({
570
+ selection: data.axesSelection,
571
+ labelData,
572
+ fullParams: data.fullParams,
573
+ fullDataFormatter: data.fullDataFormatter,
574
+ fullChartParams: data.fullChartParams,
575
+ textReverseTransform: data.textReverseTransform,
576
+ textSizePx: data.textSizePx
577
+ })
578
+
579
+ // label的事件
580
+ // labelSelection
581
+ // .on('mouseover', (event, datum) => {
582
+ // event.stopPropagation()
583
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
584
+
585
+ // isLabelMouseover = true
586
+
587
+ // subject.event$.next({
588
+ // type: 'multiValue',
589
+ // eventName: 'mouseover',
590
+ // pluginName,
591
+ // highlightTarget: data.highlightTarget,
592
+ // datum,
593
+ // category: [],
594
+ // categoryIndex: -1,
595
+ // categoryLabel: '',
596
+ // data: data.computedData,
597
+ // event,
598
+ // })
599
+ // })
600
+ // .on('mousemove', (event, datum) => {
601
+ // event.stopPropagation()
602
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
603
+
604
+ // subject.event$.next({
605
+ // type: 'multiValue',
606
+ // eventName: 'mousemove',
607
+ // pluginName,
608
+ // highlightTarget: data.highlightTarget,
609
+ // datum,
610
+ // category: [],
611
+ // categoryIndex: -1,
612
+ // categoryLabel: '',
613
+ // data: data.computedData,
614
+ // event,
615
+ // })
616
+ // })
617
+ // .on('mouseout', (event, datum) => {
618
+ // event.stopPropagation()
619
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
620
+
621
+ // isLabelMouseover = false
622
+
623
+ // subject.event$.next({
624
+ // type: 'multiValue',
625
+ // eventName: 'mouseout',
626
+ // pluginName,
627
+ // highlightTarget: data.highlightTarget,
628
+ // datum,
629
+ // category: [],
630
+ // categoryIndex: -1,
631
+ // categoryLabel: '',
632
+ // data: data.computedData,
633
+ // event,
634
+ // })
635
+ // })
636
+ // .on('click', (event, datum) => {
637
+ // event.stopPropagation()
638
+ // // const { groupIndex, groupLabel } = data.xyPositionFn(event)
639
+
640
+ // subject.event$.next({
641
+ // type: 'multiValue',
642
+ // eventName: 'click',
643
+ // pluginName,
644
+ // highlightTarget: data.highlightTarget,
645
+ // datum,
646
+ // category: [],
647
+ // categoryIndex: -1,
648
+ // categoryLabel: '',
649
+ // data: data.computedData,
650
+ // event,
651
+ // })
652
+ // })
653
+
654
+ })
655
+
656
+
657
+ const rootRectMouseout$ = d3EventObservable(rootRectSelection, 'mouseout').pipe(
658
+ takeUntil(destroy$),
659
+ )
660
+
661
+ combineLatest({
662
+ rootRectMouseout: rootRectMouseout$,
663
+ axesSelection: axesSelection$,
664
+ }).pipe(
665
+ takeUntil(destroy$),
666
+ switchMap(async d => d)
667
+ ).subscribe(data => {
668
+ setTimeout(() => {
669
+ // // @Q@ workaround - 不知為何和 label 會有衝突,當滑鼠移動到 label 上時,會觸發 mouseout 事件
670
+ // if (isLabelMouseover == true) {
671
+ // return
672
+ // }
673
+
674
+ removeLine(data.axesSelection)
675
+ removeLabel(data.axesSelection)
676
+ })
677
+ })
678
+
679
+ return () => {
680
+ destroy$.next(undefined)
681
+ rootRectSelection.remove()
682
+ }
683
683
  })