@orbcharts/plugins-basic 3.0.8 → 3.0.10

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 (120) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +6182 -6004
  3. package/dist/orbcharts-plugins-basic.umd.js +44 -44
  4. package/dist/src/series/defaults.d.ts +2 -1
  5. package/dist/src/series/index.d.ts +1 -0
  6. package/dist/src/series/plugins/Indicator.d.ts +3 -0
  7. package/lib/core-types.ts +7 -7
  8. package/lib/core.ts +6 -6
  9. package/lib/gridObservables.ts +6 -6
  10. package/lib/plugins-basic-types.ts +6 -6
  11. package/package.json +48 -48
  12. package/src/base/BaseBars.ts +765 -765
  13. package/src/base/BaseBarsTriangle.ts +676 -676
  14. package/src/base/BaseDots.ts +464 -464
  15. package/src/base/BaseGroupAxis.ts +691 -691
  16. package/src/base/BaseLegend.ts +684 -684
  17. package/src/base/BaseLineAreas.ts +629 -629
  18. package/src/base/BaseLines.ts +706 -706
  19. package/src/base/BaseOrdinalBubbles.ts +729 -729
  20. package/src/base/BaseRacingBars.ts +582 -582
  21. package/src/base/BaseRacingLabels.ts +404 -404
  22. package/src/base/BaseRacingValueLabels.ts +403 -403
  23. package/src/base/BaseStackedBars.ts +793 -793
  24. package/src/base/BaseTooltip.ts +408 -408
  25. package/src/base/BaseValueAxis.ts +600 -600
  26. package/src/base/BaseXAxis.ts +427 -427
  27. package/src/base/BaseXZoom.ts +241 -241
  28. package/src/base/BaseYAxis.ts +389 -389
  29. package/src/base/types.ts +2 -2
  30. package/src/const.ts +30 -30
  31. package/src/grid/defaults.ts +213 -213
  32. package/src/grid/gridObservables.ts +635 -635
  33. package/src/grid/index.ts +16 -16
  34. package/src/grid/plugins/Bars.ts +69 -69
  35. package/src/grid/plugins/BarsPN.ts +66 -66
  36. package/src/grid/plugins/BarsTriangle.ts +73 -73
  37. package/src/grid/plugins/Dots.ts +68 -68
  38. package/src/grid/plugins/GridLegend.ts +107 -107
  39. package/src/grid/plugins/GridTooltip.ts +66 -66
  40. package/src/grid/plugins/GroupAux.ts +1095 -1095
  41. package/src/grid/plugins/GroupAxis.ts +73 -73
  42. package/src/grid/plugins/GroupZoom.ts +218 -218
  43. package/src/grid/plugins/LineAreas.ts +65 -65
  44. package/src/grid/plugins/Lines.ts +59 -59
  45. package/src/grid/plugins/StackedBars.ts +66 -66
  46. package/src/grid/plugins/StackedValueAxis.ts +97 -97
  47. package/src/grid/plugins/ValueAxis.ts +94 -94
  48. package/src/index.ts +6 -6
  49. package/src/multiGrid/defaults.ts +244 -244
  50. package/src/multiGrid/index.ts +14 -14
  51. package/src/multiGrid/multiGridObservables.ts +50 -50
  52. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  53. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  54. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  55. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  56. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  57. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  58. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  59. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  60. package/src/multiGrid/plugins/MultiStackedBars.ts +109 -109
  61. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +135 -135
  62. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  63. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  64. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  65. package/src/multiValue/defaults.ts +523 -523
  66. package/src/multiValue/index.ts +16 -16
  67. package/src/multiValue/multiValueObservables.ts +781 -781
  68. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  69. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  70. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  71. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  72. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  73. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  74. package/src/multiValue/plugins/RacingBars.ts +375 -375
  75. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  76. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  77. package/src/multiValue/plugins/Scatter.ts +486 -486
  78. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  79. package/src/multiValue/plugins/XAxis.ts +107 -107
  80. package/src/multiValue/plugins/XYAux.ts +683 -683
  81. package/src/multiValue/plugins/XYAxes.ts +194 -194
  82. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  83. package/src/multiValue/plugins/XZoom.ts +40 -40
  84. package/src/noneData/defaults.ts +102 -102
  85. package/src/noneData/index.ts +3 -3
  86. package/src/noneData/plugins/Container.ts +27 -27
  87. package/src/noneData/plugins/Tooltip.ts +373 -373
  88. package/src/relationship/defaults.ts +221 -221
  89. package/src/relationship/index.ts +5 -5
  90. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  91. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  92. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  93. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  94. package/src/relationship/relationshipObservables.ts +49 -49
  95. package/src/series/defaults.ts +236 -224
  96. package/src/series/index.ts +10 -9
  97. package/src/series/plugins/Bubbles.ts +784 -784
  98. package/src/series/plugins/Indicator.ts +298 -0
  99. package/src/series/plugins/Pie.ts +622 -622
  100. package/src/series/plugins/PieEventTexts.ts +283 -283
  101. package/src/series/plugins/PieLabels.ts +639 -639
  102. package/src/series/plugins/Rose.ts +515 -515
  103. package/src/series/plugins/RoseLabels.ts +599 -599
  104. package/src/series/plugins/SeriesLegend.ts +107 -107
  105. package/src/series/plugins/SeriesTooltip.ts +66 -66
  106. package/src/series/seriesObservables.ts +168 -168
  107. package/src/series/seriesUtils.ts +51 -51
  108. package/src/tree/defaults.ts +102 -102
  109. package/src/tree/index.ts +4 -4
  110. package/src/tree/plugins/TreeLegend.ts +100 -100
  111. package/src/tree/plugins/TreeMap.ts +341 -341
  112. package/src/tree/plugins/TreeTooltip.ts +66 -66
  113. package/src/utils/commonUtils.ts +31 -31
  114. package/src/utils/d3Graphics.ts +176 -176
  115. package/src/utils/d3Utils.ts +92 -92
  116. package/src/utils/observables.ts +14 -14
  117. package/src/utils/orbchartsUtils.ts +129 -129
  118. package/tsconfig.base.json +13 -13
  119. package/tsconfig.json +2 -2
  120. package/vite.config.js +22 -22
@@ -1,486 +1,486 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- map,
5
- switchMap,
6
- takeUntil,
7
- distinctUntilChanged,
8
- Observable,
9
- Subject
10
- } from 'rxjs'
11
- import type {
12
- ComputedDatumMultiValue,
13
- ComputedDataMultiValue,
14
- ComputedXYDataMultiValue,
15
- DefinePluginConfig,
16
- EventMultiValue,
17
- ChartParams,
18
- ContainerPositionScaled,
19
- Layout,
20
- TransformData,
21
- ColorType
22
- } from '../../../lib/core-types'
23
- import {
24
- defineMultiValuePlugin
25
- } from '../../../lib/core'
26
- import type { ScatterParams } from '../../../lib/plugins-basic-types'
27
- import { DEFAULT_SCATTER_PARAMS } from '../defaults'
28
- import { LAYER_INDEX_OF_GRAPHIC_COVER } from '../../const'
29
- import { getDatumColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
30
- import { multiValueSelectionsObservable } from '../multiValueObservables'
31
-
32
- type ClipPathDatum = {
33
- id: string;
34
- // x: number;
35
- // y: number;
36
- width: number;
37
- height: number;
38
- }
39
-
40
- const pluginName = 'Scatter'
41
-
42
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_SCATTER_PARAMS> = {
43
- name: pluginName,
44
- defaultParams: DEFAULT_SCATTER_PARAMS,
45
- layerIndex: LAYER_INDEX_OF_GRAPHIC_COVER,
46
- validator: (params, { validateColumns }) => {
47
- const result = validateColumns(params, {
48
- radius: {
49
- toBeTypes: ['number']
50
- },
51
- fillColorType: {
52
- toBeOption: 'ColorType',
53
- },
54
- strokeColorType: {
55
- toBeOption: 'ColorType',
56
- },
57
- strokeWidth: {
58
- toBeTypes: ['number']
59
- },
60
- // strokeWidthWhileHighlight: {
61
- // toBeTypes: ['number']
62
- // },
63
- // onlyShowHighlighted: {
64
- // toBeTypes: ['boolean']
65
- // }
66
- })
67
- return result
68
- }
69
- }
70
-
71
- function renderDots ({ graphicGSelection, circleGClassName, circleClassName, visibleComputedXYData, fullParams, fullChartParams, graphicReverseScale }: {
72
- graphicGSelection: d3.Selection<SVGGElement, any, any, any>
73
- circleGClassName: string
74
- circleClassName: string
75
- visibleComputedXYData: ComputedXYDataMultiValue
76
- fullParams: ScatterParams
77
- fullChartParams: ChartParams
78
- graphicReverseScale: [number, number][]
79
- }) {
80
- const createEnterDuration = (enter: d3.Selection<d3.EnterElement, ComputedDatumMultiValue, SVGGElement, any>) => {
81
- const enterSize = enter.size()
82
- const eachDuration = fullChartParams.transitionDuration / enterSize
83
- return eachDuration
84
- }
85
- // enterDuration
86
- let enterDuration = 0
87
-
88
- graphicGSelection
89
- .each((categoryData, categoryIndex, g) => {
90
- d3.select(g[categoryIndex])
91
- .selectAll<SVGGElement, ComputedDatumMultiValue>('g')
92
- .data(visibleComputedXYData[categoryIndex], d => d.id)
93
- .join(
94
- enter => {
95
- // enterDuration
96
- enterDuration = createEnterDuration(enter)
97
-
98
- return enter
99
- .append('g')
100
- .classed(circleGClassName, true)
101
- },
102
- update => update,
103
- exit => exit.remove()
104
- )
105
- .attr('transform', d => `translate(${d.axisX}, ${d.axisY})`)
106
- .each((d, i, g) => {
107
- const circle = d3.select(g[i])
108
- .selectAll('circle')
109
- .data([d])
110
- .join(
111
- enter => {
112
- return enter
113
- .append('circle')
114
- .style('cursor', 'pointer')
115
- .style('vector-effect', 'non-scaling-stroke')
116
- .classed(circleClassName, true)
117
- .attr('opacity', 0)
118
- .transition()
119
- .delay((_d, _i) => {
120
- return i * enterDuration
121
- })
122
- .attr('opacity', 1)
123
- },
124
- update => {
125
- return update
126
- .transition()
127
- .duration(50)
128
- // .attr('cx', d => d.axisX)
129
- // .attr('cy', d => d.axisY)
130
- .attr('opacity', 1)
131
- },
132
- exit => exit.remove()
133
- )
134
- .attr('r', fullParams.radius)
135
- .attr('fill', (d, i) => getDatumColor({ datum: d, colorType: fullParams.fillColorType, fullChartParams }))
136
- .attr('stroke', (d, i) => getDatumColor({ datum: d, colorType: fullParams.strokeColorType, fullChartParams }))
137
- .attr('stroke-width', fullParams.strokeWidth)
138
- .attr('transform', `scale(${graphicReverseScale[categoryIndex][0] ?? 1}, ${graphicReverseScale[categoryIndex][1] ?? 1})`)
139
- })
140
- })
141
-
142
- const graphicCircleSelection: d3.Selection<SVGRectElement, ComputedDatumMultiValue, SVGGElement, unknown> = graphicGSelection.selectAll(`circle.${circleClassName}`)
143
-
144
- return graphicCircleSelection
145
- }
146
-
147
-
148
- function highlightDots ({ selection, ids, fullChartParams }: {
149
- selection: d3.Selection<SVGGElement, ComputedDatumMultiValue, any, any>
150
- ids: string[]
151
- fullChartParams: ChartParams
152
- }) {
153
- selection.interrupt('highlight')
154
- if (!ids.length) {
155
- // remove highlight
156
- selection
157
- .transition('highlight')
158
- .duration(200)
159
- .style('opacity', 1)
160
- // selection
161
- // .attr('stroke-width', fullParams.strokeWidth)
162
-
163
- return
164
- }
165
-
166
- selection
167
- .each((d, i, n) => {
168
- if (ids.includes(d.id)) {
169
- const dot = d3.select(n[i])
170
- dot
171
- .style('opacity', 1)
172
- .transition('highlight')
173
- .duration(200)
174
- // dot
175
- // .attr('stroke-width', fullParams.strokeWidthWhileHighlight)
176
- } else {
177
- const dot = d3.select(n[i])
178
- dot
179
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
180
- .transition('highlight')
181
- .duration(200)
182
- // dot
183
- // .attr('stroke-width', fullParams.strokeWidth)
184
-
185
- }
186
- })
187
- }
188
-
189
- function renderClipPath ({ defsSelection, clipPathData }: {
190
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
191
- clipPathData: ClipPathDatum[]
192
- }) {
193
- const clipPath = defsSelection
194
- .selectAll<SVGClipPathElement, Layout>('clipPath')
195
- .data(clipPathData)
196
- .join(
197
- enter => {
198
- return enter
199
- .append('clipPath')
200
- },
201
- update => update,
202
- exit => exit.remove()
203
- )
204
- .attr('id', d => d.id)
205
- .each((d, i, g) => {
206
- const rect = d3.select(g[i])
207
- .selectAll<SVGRectElement, typeof d>('rect')
208
- .data([d])
209
- .join('rect')
210
- .attr('x', 0)
211
- .attr('y', 0)
212
- .attr('width', _d => _d.width)
213
- .attr('height', _d => _d.height)
214
- })
215
-
216
- }
217
-
218
-
219
- export const Scatter = defineMultiValuePlugin(pluginConfig)(({ selection, name, subject, observer }) => {
220
-
221
- const destroy$ = new Subject()
222
-
223
- const clipPathID = getUniID(pluginName, 'clipPath-box')
224
- const circleGClassName = getClassName(pluginName, 'circleG')
225
- const circleClassName = getClassName(pluginName, 'circle')
226
-
227
- const {
228
- categorySelection$,
229
- axesSelection$,
230
- defsSelection$,
231
- graphicGSelection$
232
- } = multiValueSelectionsObservable({
233
- selection,
234
- pluginName,
235
- clipPathID,
236
- categoryLabels$: observer.categoryLabels$,
237
- containerPosition$: observer.containerPosition$,
238
- graphicTransform$: observer.graphicTransform$
239
- })
240
-
241
- const graphicReverseScale$: Observable<[number, number][]> = combineLatest({
242
- computedData: observer.computedData$,
243
- graphicReverseScale: observer.graphicReverseScale$
244
- }).pipe(
245
- takeUntil(destroy$),
246
- switchMap(async data => data),
247
- map(data => {
248
- return data.computedData.map((series, categoryIndex) => {
249
- return data.graphicReverseScale[categoryIndex]
250
- })
251
- })
252
- )
253
-
254
- const clipPathSubscription = combineLatest({
255
- defsSelection: defsSelection$,
256
- layout: observer.layout$,
257
- }).pipe(
258
- takeUntil(destroy$),
259
- switchMap(async (d) => d),
260
- ).subscribe(data => {
261
- // 外層的遮罩
262
- const clipPathData = [{
263
- id: clipPathID,
264
- width: data.layout.width,
265
- height: data.layout.height
266
- }]
267
- renderClipPath({
268
- defsSelection: data.defsSelection,
269
- clipPathData,
270
- })
271
- })
272
-
273
- const graphicSelection$ = combineLatest({
274
- graphicGSelection: graphicGSelection$,
275
- visibleComputedXYData: observer.visibleComputedXYData$,
276
- graphicReverseScale: graphicReverseScale$,
277
- fullChartParams: observer.fullChartParams$,
278
- fullParams: observer.fullParams$,
279
- }).pipe(
280
- takeUntil(destroy$),
281
- switchMap(async (d) => d),
282
- map(data => {
283
- return renderDots({
284
- graphicGSelection: data.graphicGSelection,
285
- circleGClassName,
286
- circleClassName,
287
- visibleComputedXYData: data.visibleComputedXYData,
288
- fullParams: data.fullParams,
289
- fullChartParams: data.fullChartParams,
290
- graphicReverseScale: data.graphicReverseScale
291
- })
292
- })
293
- )
294
-
295
- const valueLabels$ = observer.fullDataFormatter$.pipe(
296
- takeUntil(destroy$),
297
- map(dataFormatter => {
298
- return [
299
- dataFormatter.valueLabels[0] ?? 'X',
300
- dataFormatter.valueLabels[1] ?? 'Y'
301
- ]
302
- }),
303
- distinctUntilChanged((a, b) => a[0] === b[0] && a[1] === b[1])
304
- )
305
-
306
- const highlightTarget$ = observer.fullChartParams$.pipe(
307
- takeUntil(destroy$),
308
- map(d => d.highlightTarget),
309
- distinctUntilChanged()
310
- )
311
-
312
- combineLatest({
313
- graphicSelection: graphicSelection$,
314
- computedData: observer.computedData$,
315
- CategoryDataMap: observer.CategoryDataMap$,
316
- highlightTarget: highlightTarget$,
317
- valueLabels: valueLabels$
318
- }).pipe(
319
- takeUntil(destroy$),
320
- switchMap(async (d) => d),
321
- ).subscribe(data => {
322
-
323
- data.graphicSelection
324
- .on('mouseover', (event, datum) => {
325
- // event.stopPropagation()
326
- // console.log({
327
- // type: 'multiValue',
328
- // eventName: 'mouseover',
329
- // pluginName,
330
- // highlightTarget: data.highlightTarget,
331
- // datum,
332
- // category: data.CategoryDataMap.get(datum.categoryLabel)!,
333
- // categoryIndex: datum.categoryIndex,
334
- // categoryLabel: datum.categoryLabel,
335
- // data: data.computedData,
336
- // event,
337
- // })
338
- subject.event$.next({
339
- type: 'multiValue',
340
- eventName: 'mouseover',
341
- pluginName,
342
- highlightTarget: data.highlightTarget,
343
- valueDetail: [
344
- {
345
- value: datum.value[0],
346
- valueIndex: 0,
347
- valueLabel: data.valueLabels[0]
348
- },
349
- {
350
- value: datum.value[1],
351
- valueIndex: 1,
352
- valueLabel: data.valueLabels[1]
353
- }
354
- ],
355
- datum,
356
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
357
- categoryIndex: datum.categoryIndex,
358
- categoryLabel: datum.categoryLabel,
359
- data: data.computedData,
360
- event,
361
- })
362
- })
363
- .on('mousemove', (event, datum) => {
364
- // event.stopPropagation()
365
-
366
- subject.event$.next({
367
- type: 'multiValue',
368
- eventName: 'mousemove',
369
- pluginName,
370
- highlightTarget: data.highlightTarget,
371
- valueDetail: [
372
- {
373
- value: datum.value[0],
374
- valueIndex: 0,
375
- valueLabel: data.valueLabels[0]
376
- },
377
- {
378
- value: datum.value[1],
379
- valueIndex: 1,
380
- valueLabel: data.valueLabels[1]
381
- }
382
- ],
383
- datum,
384
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
385
- categoryIndex: datum.categoryIndex,
386
- categoryLabel: datum.categoryLabel,
387
- data: data.computedData,
388
- event,
389
- })
390
- })
391
- .on('mouseout', (event, datum) => {
392
- // event.stopPropagation()
393
-
394
- subject.event$.next({
395
- type: 'multiValue',
396
- eventName: 'mouseout',
397
- pluginName,
398
- highlightTarget: data.highlightTarget,
399
- valueDetail: [
400
- {
401
- value: datum.value[0],
402
- valueIndex: 0,
403
- valueLabel: data.valueLabels[0]
404
- },
405
- {
406
- value: datum.value[1],
407
- valueIndex: 1,
408
- valueLabel: data.valueLabels[1]
409
- }
410
- ],
411
- datum,
412
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
413
- categoryIndex: datum.categoryIndex,
414
- categoryLabel: datum.categoryLabel,
415
- data: data.computedData,
416
- event,
417
- })
418
- })
419
- .on('click', (event, datum) => {
420
- // event.stopPropagation()
421
-
422
- subject.event$.next({
423
- type: 'multiValue',
424
- eventName: 'click',
425
- pluginName,
426
- highlightTarget: data.highlightTarget,
427
- valueDetail: [
428
- {
429
- value: datum.value[0],
430
- valueIndex: 0,
431
- valueLabel: data.valueLabels[0]
432
- },
433
- {
434
- value: datum.value[1],
435
- valueIndex: 1,
436
- valueLabel: data.valueLabels[1]
437
- }
438
- ],
439
- datum,
440
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
441
- categoryIndex: datum.categoryIndex,
442
- categoryLabel: datum.categoryLabel,
443
- data: data.computedData,
444
- event,
445
- })
446
- })
447
-
448
- })
449
-
450
- combineLatest({
451
- graphicSelection: graphicSelection$,
452
- highlight: observer.highlight$.pipe(
453
- map(data => data.map(d => d.id))
454
- ),
455
- fullChartParams: observer.fullChartParams$
456
- }).pipe(
457
- takeUntil(destroy$),
458
- switchMap(async d => d)
459
- ).subscribe(data => {
460
- highlightDots({
461
- selection: data.graphicSelection,
462
- ids: data.highlight,
463
- fullChartParams: data.fullChartParams
464
- })
465
- })
466
-
467
- // graphicGSelection$.subscribe(data => {
468
- // console.log('graphicGSelection$', data)
469
- // })
470
-
471
- // observer.visibleComputedXYData$.subscribe(data => {
472
- // console.log('visibleComputedXYData$', data)
473
- // })
474
-
475
- // observer.fullChartParams$.subscribe(data => {
476
- // console.log('fullChartParams$', data)
477
- // })
478
-
479
- // observer.fullParams$.subscribe(data => {
480
- // console.log('fullParams$', data)
481
- // })
482
-
483
- return () => {
484
- destroy$.next(undefined)
485
- }
486
- })
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ map,
5
+ switchMap,
6
+ takeUntil,
7
+ distinctUntilChanged,
8
+ Observable,
9
+ Subject
10
+ } from 'rxjs'
11
+ import type {
12
+ ComputedDatumMultiValue,
13
+ ComputedDataMultiValue,
14
+ ComputedXYDataMultiValue,
15
+ DefinePluginConfig,
16
+ EventMultiValue,
17
+ ChartParams,
18
+ ContainerPositionScaled,
19
+ Layout,
20
+ TransformData,
21
+ ColorType
22
+ } from '../../../lib/core-types'
23
+ import {
24
+ defineMultiValuePlugin
25
+ } from '../../../lib/core'
26
+ import type { ScatterParams } from '../../../lib/plugins-basic-types'
27
+ import { DEFAULT_SCATTER_PARAMS } from '../defaults'
28
+ import { LAYER_INDEX_OF_GRAPHIC_COVER } from '../../const'
29
+ import { getDatumColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
30
+ import { multiValueSelectionsObservable } from '../multiValueObservables'
31
+
32
+ type ClipPathDatum = {
33
+ id: string;
34
+ // x: number;
35
+ // y: number;
36
+ width: number;
37
+ height: number;
38
+ }
39
+
40
+ const pluginName = 'Scatter'
41
+
42
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_SCATTER_PARAMS> = {
43
+ name: pluginName,
44
+ defaultParams: DEFAULT_SCATTER_PARAMS,
45
+ layerIndex: LAYER_INDEX_OF_GRAPHIC_COVER,
46
+ validator: (params, { validateColumns }) => {
47
+ const result = validateColumns(params, {
48
+ radius: {
49
+ toBeTypes: ['number']
50
+ },
51
+ fillColorType: {
52
+ toBeOption: 'ColorType',
53
+ },
54
+ strokeColorType: {
55
+ toBeOption: 'ColorType',
56
+ },
57
+ strokeWidth: {
58
+ toBeTypes: ['number']
59
+ },
60
+ // strokeWidthWhileHighlight: {
61
+ // toBeTypes: ['number']
62
+ // },
63
+ // onlyShowHighlighted: {
64
+ // toBeTypes: ['boolean']
65
+ // }
66
+ })
67
+ return result
68
+ }
69
+ }
70
+
71
+ function renderDots ({ graphicGSelection, circleGClassName, circleClassName, visibleComputedXYData, fullParams, fullChartParams, graphicReverseScale }: {
72
+ graphicGSelection: d3.Selection<SVGGElement, any, any, any>
73
+ circleGClassName: string
74
+ circleClassName: string
75
+ visibleComputedXYData: ComputedXYDataMultiValue
76
+ fullParams: ScatterParams
77
+ fullChartParams: ChartParams
78
+ graphicReverseScale: [number, number][]
79
+ }) {
80
+ const createEnterDuration = (enter: d3.Selection<d3.EnterElement, ComputedDatumMultiValue, SVGGElement, any>) => {
81
+ const enterSize = enter.size()
82
+ const eachDuration = fullChartParams.transitionDuration / enterSize
83
+ return eachDuration
84
+ }
85
+ // enterDuration
86
+ let enterDuration = 0
87
+
88
+ graphicGSelection
89
+ .each((categoryData, categoryIndex, g) => {
90
+ d3.select(g[categoryIndex])
91
+ .selectAll<SVGGElement, ComputedDatumMultiValue>('g')
92
+ .data(visibleComputedXYData[categoryIndex], d => d.id)
93
+ .join(
94
+ enter => {
95
+ // enterDuration
96
+ enterDuration = createEnterDuration(enter)
97
+
98
+ return enter
99
+ .append('g')
100
+ .classed(circleGClassName, true)
101
+ },
102
+ update => update,
103
+ exit => exit.remove()
104
+ )
105
+ .attr('transform', d => `translate(${d.axisX}, ${d.axisY})`)
106
+ .each((d, i, g) => {
107
+ const circle = d3.select(g[i])
108
+ .selectAll('circle')
109
+ .data([d])
110
+ .join(
111
+ enter => {
112
+ return enter
113
+ .append('circle')
114
+ .style('cursor', 'pointer')
115
+ .style('vector-effect', 'non-scaling-stroke')
116
+ .classed(circleClassName, true)
117
+ .attr('opacity', 0)
118
+ .transition()
119
+ .delay((_d, _i) => {
120
+ return i * enterDuration
121
+ })
122
+ .attr('opacity', 1)
123
+ },
124
+ update => {
125
+ return update
126
+ .transition()
127
+ .duration(50)
128
+ // .attr('cx', d => d.axisX)
129
+ // .attr('cy', d => d.axisY)
130
+ .attr('opacity', 1)
131
+ },
132
+ exit => exit.remove()
133
+ )
134
+ .attr('r', fullParams.radius)
135
+ .attr('fill', (d, i) => getDatumColor({ datum: d, colorType: fullParams.fillColorType, fullChartParams }))
136
+ .attr('stroke', (d, i) => getDatumColor({ datum: d, colorType: fullParams.strokeColorType, fullChartParams }))
137
+ .attr('stroke-width', fullParams.strokeWidth)
138
+ .attr('transform', `scale(${graphicReverseScale[categoryIndex][0] ?? 1}, ${graphicReverseScale[categoryIndex][1] ?? 1})`)
139
+ })
140
+ })
141
+
142
+ const graphicCircleSelection: d3.Selection<SVGRectElement, ComputedDatumMultiValue, SVGGElement, unknown> = graphicGSelection.selectAll(`circle.${circleClassName}`)
143
+
144
+ return graphicCircleSelection
145
+ }
146
+
147
+
148
+ function highlightDots ({ selection, ids, fullChartParams }: {
149
+ selection: d3.Selection<SVGGElement, ComputedDatumMultiValue, any, any>
150
+ ids: string[]
151
+ fullChartParams: ChartParams
152
+ }) {
153
+ selection.interrupt('highlight')
154
+ if (!ids.length) {
155
+ // remove highlight
156
+ selection
157
+ .transition('highlight')
158
+ .duration(200)
159
+ .style('opacity', 1)
160
+ // selection
161
+ // .attr('stroke-width', fullParams.strokeWidth)
162
+
163
+ return
164
+ }
165
+
166
+ selection
167
+ .each((d, i, n) => {
168
+ if (ids.includes(d.id)) {
169
+ const dot = d3.select(n[i])
170
+ dot
171
+ .style('opacity', 1)
172
+ .transition('highlight')
173
+ .duration(200)
174
+ // dot
175
+ // .attr('stroke-width', fullParams.strokeWidthWhileHighlight)
176
+ } else {
177
+ const dot = d3.select(n[i])
178
+ dot
179
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
180
+ .transition('highlight')
181
+ .duration(200)
182
+ // dot
183
+ // .attr('stroke-width', fullParams.strokeWidth)
184
+
185
+ }
186
+ })
187
+ }
188
+
189
+ function renderClipPath ({ defsSelection, clipPathData }: {
190
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
191
+ clipPathData: ClipPathDatum[]
192
+ }) {
193
+ const clipPath = defsSelection
194
+ .selectAll<SVGClipPathElement, Layout>('clipPath')
195
+ .data(clipPathData)
196
+ .join(
197
+ enter => {
198
+ return enter
199
+ .append('clipPath')
200
+ },
201
+ update => update,
202
+ exit => exit.remove()
203
+ )
204
+ .attr('id', d => d.id)
205
+ .each((d, i, g) => {
206
+ const rect = d3.select(g[i])
207
+ .selectAll<SVGRectElement, typeof d>('rect')
208
+ .data([d])
209
+ .join('rect')
210
+ .attr('x', 0)
211
+ .attr('y', 0)
212
+ .attr('width', _d => _d.width)
213
+ .attr('height', _d => _d.height)
214
+ })
215
+
216
+ }
217
+
218
+
219
+ export const Scatter = defineMultiValuePlugin(pluginConfig)(({ selection, name, subject, observer }) => {
220
+
221
+ const destroy$ = new Subject()
222
+
223
+ const clipPathID = getUniID(pluginName, 'clipPath-box')
224
+ const circleGClassName = getClassName(pluginName, 'circleG')
225
+ const circleClassName = getClassName(pluginName, 'circle')
226
+
227
+ const {
228
+ categorySelection$,
229
+ axesSelection$,
230
+ defsSelection$,
231
+ graphicGSelection$
232
+ } = multiValueSelectionsObservable({
233
+ selection,
234
+ pluginName,
235
+ clipPathID,
236
+ categoryLabels$: observer.categoryLabels$,
237
+ containerPosition$: observer.containerPosition$,
238
+ graphicTransform$: observer.graphicTransform$
239
+ })
240
+
241
+ const graphicReverseScale$: Observable<[number, number][]> = combineLatest({
242
+ computedData: observer.computedData$,
243
+ graphicReverseScale: observer.graphicReverseScale$
244
+ }).pipe(
245
+ takeUntil(destroy$),
246
+ switchMap(async data => data),
247
+ map(data => {
248
+ return data.computedData.map((series, categoryIndex) => {
249
+ return data.graphicReverseScale[categoryIndex]
250
+ })
251
+ })
252
+ )
253
+
254
+ const clipPathSubscription = combineLatest({
255
+ defsSelection: defsSelection$,
256
+ layout: observer.layout$,
257
+ }).pipe(
258
+ takeUntil(destroy$),
259
+ switchMap(async (d) => d),
260
+ ).subscribe(data => {
261
+ // 外層的遮罩
262
+ const clipPathData = [{
263
+ id: clipPathID,
264
+ width: data.layout.width,
265
+ height: data.layout.height
266
+ }]
267
+ renderClipPath({
268
+ defsSelection: data.defsSelection,
269
+ clipPathData,
270
+ })
271
+ })
272
+
273
+ const graphicSelection$ = combineLatest({
274
+ graphicGSelection: graphicGSelection$,
275
+ visibleComputedXYData: observer.visibleComputedXYData$,
276
+ graphicReverseScale: graphicReverseScale$,
277
+ fullChartParams: observer.fullChartParams$,
278
+ fullParams: observer.fullParams$,
279
+ }).pipe(
280
+ takeUntil(destroy$),
281
+ switchMap(async (d) => d),
282
+ map(data => {
283
+ return renderDots({
284
+ graphicGSelection: data.graphicGSelection,
285
+ circleGClassName,
286
+ circleClassName,
287
+ visibleComputedXYData: data.visibleComputedXYData,
288
+ fullParams: data.fullParams,
289
+ fullChartParams: data.fullChartParams,
290
+ graphicReverseScale: data.graphicReverseScale
291
+ })
292
+ })
293
+ )
294
+
295
+ const valueLabels$ = observer.fullDataFormatter$.pipe(
296
+ takeUntil(destroy$),
297
+ map(dataFormatter => {
298
+ return [
299
+ dataFormatter.valueLabels[0] ?? 'X',
300
+ dataFormatter.valueLabels[1] ?? 'Y'
301
+ ]
302
+ }),
303
+ distinctUntilChanged((a, b) => a[0] === b[0] && a[1] === b[1])
304
+ )
305
+
306
+ const highlightTarget$ = observer.fullChartParams$.pipe(
307
+ takeUntil(destroy$),
308
+ map(d => d.highlightTarget),
309
+ distinctUntilChanged()
310
+ )
311
+
312
+ combineLatest({
313
+ graphicSelection: graphicSelection$,
314
+ computedData: observer.computedData$,
315
+ CategoryDataMap: observer.CategoryDataMap$,
316
+ highlightTarget: highlightTarget$,
317
+ valueLabels: valueLabels$
318
+ }).pipe(
319
+ takeUntil(destroy$),
320
+ switchMap(async (d) => d),
321
+ ).subscribe(data => {
322
+
323
+ data.graphicSelection
324
+ .on('mouseover', (event, datum) => {
325
+ // event.stopPropagation()
326
+ // console.log({
327
+ // type: 'multiValue',
328
+ // eventName: 'mouseover',
329
+ // pluginName,
330
+ // highlightTarget: data.highlightTarget,
331
+ // datum,
332
+ // category: data.CategoryDataMap.get(datum.categoryLabel)!,
333
+ // categoryIndex: datum.categoryIndex,
334
+ // categoryLabel: datum.categoryLabel,
335
+ // data: data.computedData,
336
+ // event,
337
+ // })
338
+ subject.event$.next({
339
+ type: 'multiValue',
340
+ eventName: 'mouseover',
341
+ pluginName,
342
+ highlightTarget: data.highlightTarget,
343
+ valueDetail: [
344
+ {
345
+ value: datum.value[0],
346
+ valueIndex: 0,
347
+ valueLabel: data.valueLabels[0]
348
+ },
349
+ {
350
+ value: datum.value[1],
351
+ valueIndex: 1,
352
+ valueLabel: data.valueLabels[1]
353
+ }
354
+ ],
355
+ datum,
356
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
357
+ categoryIndex: datum.categoryIndex,
358
+ categoryLabel: datum.categoryLabel,
359
+ data: data.computedData,
360
+ event,
361
+ })
362
+ })
363
+ .on('mousemove', (event, datum) => {
364
+ // event.stopPropagation()
365
+
366
+ subject.event$.next({
367
+ type: 'multiValue',
368
+ eventName: 'mousemove',
369
+ pluginName,
370
+ highlightTarget: data.highlightTarget,
371
+ valueDetail: [
372
+ {
373
+ value: datum.value[0],
374
+ valueIndex: 0,
375
+ valueLabel: data.valueLabels[0]
376
+ },
377
+ {
378
+ value: datum.value[1],
379
+ valueIndex: 1,
380
+ valueLabel: data.valueLabels[1]
381
+ }
382
+ ],
383
+ datum,
384
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
385
+ categoryIndex: datum.categoryIndex,
386
+ categoryLabel: datum.categoryLabel,
387
+ data: data.computedData,
388
+ event,
389
+ })
390
+ })
391
+ .on('mouseout', (event, datum) => {
392
+ // event.stopPropagation()
393
+
394
+ subject.event$.next({
395
+ type: 'multiValue',
396
+ eventName: 'mouseout',
397
+ pluginName,
398
+ highlightTarget: data.highlightTarget,
399
+ valueDetail: [
400
+ {
401
+ value: datum.value[0],
402
+ valueIndex: 0,
403
+ valueLabel: data.valueLabels[0]
404
+ },
405
+ {
406
+ value: datum.value[1],
407
+ valueIndex: 1,
408
+ valueLabel: data.valueLabels[1]
409
+ }
410
+ ],
411
+ datum,
412
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
413
+ categoryIndex: datum.categoryIndex,
414
+ categoryLabel: datum.categoryLabel,
415
+ data: data.computedData,
416
+ event,
417
+ })
418
+ })
419
+ .on('click', (event, datum) => {
420
+ // event.stopPropagation()
421
+
422
+ subject.event$.next({
423
+ type: 'multiValue',
424
+ eventName: 'click',
425
+ pluginName,
426
+ highlightTarget: data.highlightTarget,
427
+ valueDetail: [
428
+ {
429
+ value: datum.value[0],
430
+ valueIndex: 0,
431
+ valueLabel: data.valueLabels[0]
432
+ },
433
+ {
434
+ value: datum.value[1],
435
+ valueIndex: 1,
436
+ valueLabel: data.valueLabels[1]
437
+ }
438
+ ],
439
+ datum,
440
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
441
+ categoryIndex: datum.categoryIndex,
442
+ categoryLabel: datum.categoryLabel,
443
+ data: data.computedData,
444
+ event,
445
+ })
446
+ })
447
+
448
+ })
449
+
450
+ combineLatest({
451
+ graphicSelection: graphicSelection$,
452
+ highlight: observer.highlight$.pipe(
453
+ map(data => data.map(d => d.id))
454
+ ),
455
+ fullChartParams: observer.fullChartParams$
456
+ }).pipe(
457
+ takeUntil(destroy$),
458
+ switchMap(async d => d)
459
+ ).subscribe(data => {
460
+ highlightDots({
461
+ selection: data.graphicSelection,
462
+ ids: data.highlight,
463
+ fullChartParams: data.fullChartParams
464
+ })
465
+ })
466
+
467
+ // graphicGSelection$.subscribe(data => {
468
+ // console.log('graphicGSelection$', data)
469
+ // })
470
+
471
+ // observer.visibleComputedXYData$.subscribe(data => {
472
+ // console.log('visibleComputedXYData$', data)
473
+ // })
474
+
475
+ // observer.fullChartParams$.subscribe(data => {
476
+ // console.log('fullChartParams$', data)
477
+ // })
478
+
479
+ // observer.fullParams$.subscribe(data => {
480
+ // console.log('fullParams$', data)
481
+ // })
482
+
483
+ return () => {
484
+ destroy$.next(undefined)
485
+ }
486
+ })