@orbcharts/plugins-basic 3.0.0-beta.6 → 3.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic/src/utils/d3Utils.d.ts +2 -2
  3. package/dist/orbcharts-plugins-basic.es.js +5001 -4981
  4. package/dist/orbcharts-plugins-basic.umd.js +28 -28
  5. package/lib/core-types.ts +7 -7
  6. package/lib/core.ts +6 -6
  7. package/lib/plugins-basic-types.ts +6 -6
  8. package/package.json +44 -44
  9. package/src/base/BaseBarStack.ts +782 -780
  10. package/src/base/BaseBars.ts +765 -765
  11. package/src/base/BaseBarsTriangle.ts +676 -674
  12. package/src/base/BaseDots.ts +464 -464
  13. package/src/base/BaseGroupAxis.ts +679 -679
  14. package/src/base/BaseLegend.ts +684 -684
  15. package/src/base/BaseLineAreas.ts +629 -629
  16. package/src/base/BaseLines.ts +706 -706
  17. package/src/base/BaseTooltip.ts +385 -385
  18. package/src/base/BaseValueAxis.ts +583 -583
  19. package/src/base/types.ts +2 -2
  20. package/src/const.ts +30 -30
  21. package/src/grid/defaults.ts +246 -244
  22. package/src/grid/gridObservables.ts +554 -554
  23. package/src/grid/index.ts +16 -16
  24. package/src/grid/plugins/BarStack.ts +64 -64
  25. package/src/grid/plugins/Bars.ts +69 -69
  26. package/src/grid/plugins/BarsPN.ts +66 -66
  27. package/src/grid/plugins/BarsTriangle.ts +73 -73
  28. package/src/grid/plugins/Dots.ts +68 -68
  29. package/src/grid/plugins/GridLegend.ts +107 -107
  30. package/src/grid/plugins/GridTooltip.ts +66 -66
  31. package/src/grid/plugins/GridZoom.ts +218 -218
  32. package/src/grid/plugins/GroupAux.ts +1103 -1103
  33. package/src/grid/plugins/GroupAxis.ts +97 -97
  34. package/src/grid/plugins/LineAreas.ts +65 -65
  35. package/src/grid/plugins/Lines.ts +59 -59
  36. package/src/grid/plugins/ValueAxis.ts +94 -94
  37. package/src/grid/plugins/ValueStackAxis.ts +96 -96
  38. package/src/index.ts +10 -10
  39. package/src/multiGrid/defaults.ts +224 -224
  40. package/src/multiGrid/index.ts +14 -14
  41. package/src/multiGrid/multiGridObservables.ts +49 -49
  42. package/src/multiGrid/plugins/MultiBarStack.ts +106 -106
  43. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  44. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  45. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  46. package/src/multiGrid/plugins/MultiGridLegend.ts +159 -159
  47. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  48. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  49. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  50. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  51. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  52. package/src/multiGrid/plugins/MultiValueStackAxis.ts +134 -134
  53. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  54. package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +299 -299
  55. package/src/multiValue/defaults.ts +166 -162
  56. package/src/multiValue/index.ts +8 -8
  57. package/src/multiValue/multiValueObservables.ts +297 -258
  58. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  59. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  60. package/src/multiValue/plugins/Scatter.ts +426 -426
  61. package/src/multiValue/plugins/ScatterBubbles.ts +554 -551
  62. package/src/multiValue/plugins/XYAux.ts +681 -681
  63. package/src/multiValue/plugins/XYAxes.ts +684 -676
  64. package/src/multiValue/plugins/XYZoom.ts +299 -299
  65. package/src/noneData/defaults.ts +102 -102
  66. package/src/noneData/index.ts +3 -3
  67. package/src/noneData/plugins/Container.ts +27 -27
  68. package/src/noneData/plugins/Tooltip.ts +373 -373
  69. package/src/series/defaults.ts +206 -206
  70. package/src/series/index.ts +9 -9
  71. package/src/series/plugins/Bubbles.ts +603 -603
  72. package/src/series/plugins/Pie.ts +623 -623
  73. package/src/series/plugins/PieEventTexts.ts +283 -283
  74. package/src/series/plugins/PieLabels.ts +640 -640
  75. package/src/series/plugins/Rose.ts +516 -516
  76. package/src/series/plugins/RoseLabels.ts +600 -600
  77. package/src/series/plugins/SeriesLegend.ts +107 -107
  78. package/src/series/plugins/SeriesTooltip.ts +66 -66
  79. package/src/series/seriesObservables.ts +145 -145
  80. package/src/series/seriesUtils.ts +51 -51
  81. package/src/tree/defaults.ts +78 -78
  82. package/src/tree/index.ts +4 -4
  83. package/src/tree/plugins/TreeLegend.ts +100 -100
  84. package/src/tree/plugins/TreeMap.ts +333 -333
  85. package/src/tree/plugins/TreeTooltip.ts +66 -66
  86. package/src/utils/commonUtils.ts +21 -21
  87. package/src/utils/d3Graphics.ts +174 -174
  88. package/src/utils/d3Utils.ts +74 -74
  89. package/src/utils/observables.ts +14 -14
  90. package/src/utils/orbchartsUtils.ts +116 -101
  91. package/tsconfig.base.json +13 -13
  92. package/tsconfig.json +2 -2
  93. package/vite.config.js +22 -22
@@ -1,551 +1,554 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- map,
5
- switchMap,
6
- takeUntil,
7
- distinctUntilChanged,
8
- Observable,
9
- Subject,
10
- share,
11
- shareReplay
12
- } from 'rxjs'
13
- import type {
14
- ComputedDatumMultiValue,
15
- ComputedDataMultiValue,
16
- ComputedLayoutDatumMultiValue,
17
- ComputedLayoutDataMultiValue,
18
- DefinePluginConfig,
19
- EventMultiValue,
20
- ChartParams,
21
- ContainerPositionScaled,
22
- Layout,
23
- TransformData,
24
- ColorType
25
- } from '../../../lib/core-types'
26
- import {
27
- defineMultiValuePlugin,
28
- getMinAndMax
29
- } from '../../../lib/core'
30
- import type { ScatterBubblesParams } from '../../../lib/plugins-basic-types'
31
- import { DEFAULT_SCATTER_BUBBLES_PARAMS } from '../defaults'
32
- import { LAYER_INDEX_OF_GRAPHIC_COVER } from '../../const'
33
- import { getDatumColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
34
- import { multiValueSelectionsObservable } from '../multiValueObservables'
35
-
36
- type ClipPathDatum = {
37
- id: string;
38
- // x: number;
39
- // y: number;
40
- width: number;
41
- height: number;
42
- }
43
-
44
- interface BubbleDatum extends ComputedLayoutDatumMultiValue {
45
- r: number
46
- opacity: number
47
- }
48
-
49
- const pluginName = 'ScatterBubbles'
50
-
51
- // 調整係數 - 因為圓和圓之間的空隙造成聚合起來的大圓會略大,所以稍作微調
52
- const adjustmentFactor = 0.9
53
-
54
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_SCATTER_BUBBLES_PARAMS> = {
55
- name: pluginName,
56
- defaultParams: DEFAULT_SCATTER_BUBBLES_PARAMS,
57
- layerIndex: LAYER_INDEX_OF_GRAPHIC_COVER,
58
- validator: (params, { validateColumns }) => {
59
- const result = validateColumns(params, {
60
- // radius: {
61
- // toBeTypes: ['number']
62
- // },
63
- fillColorType: {
64
- toBeOption: 'ColorType',
65
- },
66
- strokeColorType: {
67
- toBeOption: 'ColorType',
68
- },
69
- strokeWidth: {
70
- toBeTypes: ['number']
71
- },
72
- valueLinearOpacity: {
73
- toBeTypes: ['number[]']
74
- },
75
- arcScaleType: {
76
- toBe: 'ArcScaleType',
77
- test: (value) => {
78
- return value === 'area' || value === 'radius'
79
- }
80
- },
81
- sizeAdjust: {
82
- toBeTypes: ['number']
83
- }
84
- })
85
- return result
86
- }
87
- }
88
-
89
- function renderDots ({ graphicGSelection, circleGClassName, circleClassName, bubbleData, fullParams, fullChartParams, graphicReverseScale }: {
90
- graphicGSelection: d3.Selection<SVGGElement, any, any, any>
91
- circleGClassName: string
92
- circleClassName: string
93
- // visibleComputedLayoutData: ComputedLayoutDataMultiValue
94
- bubbleData: BubbleDatum[][]
95
- fullParams: ScatterBubblesParams
96
- fullChartParams: ChartParams
97
- graphicReverseScale: [number, number][]
98
- }) {
99
- const createEnterDuration = (enter: d3.Selection<d3.EnterElement, ComputedDatumMultiValue, SVGGElement, any>) => {
100
- const enterSize = enter.size()
101
- const eachDuration = fullChartParams.transitionDuration / enterSize
102
- return eachDuration
103
- }
104
- // enterDuration
105
- let enterDuration = 0
106
-
107
- graphicGSelection
108
- .each((categoryData, categoryIndex, g) => {
109
- d3.select(g[categoryIndex])
110
- .selectAll<SVGGElement, ComputedDatumMultiValue>('g')
111
- .data(bubbleData[categoryIndex], d => d.id)
112
- .join(
113
- enter => {
114
- // enterDuration
115
- enterDuration = createEnterDuration(enter)
116
-
117
- return enter
118
- .append('g')
119
- .classed(circleGClassName, true)
120
- },
121
- update => update,
122
- exit => exit.remove()
123
- )
124
- .attr('transform', d => `translate(${d.axisX}, ${d.axisY})`)
125
- .each((d, i, g) => {
126
- const circle = d3.select(g[i])
127
- .selectAll('circle')
128
- .data([d])
129
- .join(
130
- enter => {
131
- return enter
132
- .append('circle')
133
- .style('cursor', 'pointer')
134
- .style('vector-effect', 'non-scaling-stroke')
135
- .classed(circleClassName, true)
136
- .attr('opacity', 0)
137
- .transition()
138
- .delay((_d, _i) => {
139
- return i * enterDuration
140
- })
141
- .attr('opacity', 0.8)
142
- },
143
- update => {
144
- return update
145
- .transition()
146
- .duration(50)
147
- // .attr('cx', d => d.axisX)
148
- // .attr('cy', d => d.axisY)
149
- .attr('opacity', 0.8)
150
- },
151
- exit => exit.remove()
152
- )
153
- .attr('r', d => d.r)
154
- .attr('fill', (d, i) => getDatumColor({ datum: d, colorType: fullParams.fillColorType, fullChartParams }))
155
- .attr('stroke', (d, i) => getDatumColor({ datum: d, colorType: fullParams.strokeColorType, fullChartParams }))
156
- .attr('stroke-width', fullParams.strokeWidth)
157
- .attr('transform', `scale(${graphicReverseScale[categoryIndex][0] ?? 1}, ${graphicReverseScale[categoryIndex][1] ?? 1})`)
158
- })
159
- })
160
-
161
- const graphicCircleSelection: d3.Selection<SVGRectElement, BubbleDatum, SVGGElement, unknown> = graphicGSelection.selectAll(`circle.${circleClassName}`)
162
-
163
- return graphicCircleSelection
164
- }
165
-
166
-
167
- function highlightBubbles ({ selection, ids, fullChartParams }: {
168
- selection: d3.Selection<SVGGElement, BubbleDatum, any, any>
169
- ids: string[]
170
- fullChartParams: ChartParams
171
- }) {
172
- selection.interrupt('highlight')
173
- if (!ids.length) {
174
- // remove highlight
175
- selection
176
- .transition('highlight')
177
- .duration(200)
178
- .style('opacity', d => d.opacity)
179
- // selection
180
- // .attr('stroke-width', fullParams.strokeWidth)
181
-
182
- return
183
- }
184
-
185
- selection
186
- .each((d, i, n) => {
187
- if (ids.includes(d.id)) {
188
- const dot = d3.select<SVGGElement, BubbleDatum>(n[i])
189
- dot
190
- .style('opacity', d => d.opacity)
191
- .transition('highlight')
192
- .duration(200)
193
- // dot
194
- // .attr('stroke-width', fullParams.strokeWidthWhileHighlight)
195
- } else {
196
- const dot = d3.select(n[i])
197
- dot
198
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
199
- .transition('highlight')
200
- .duration(200)
201
- // dot
202
- // .attr('stroke-width', fullParams.strokeWidth)
203
-
204
- }
205
- })
206
- }
207
-
208
- function renderClipPath ({ defsSelection, clipPathData }: {
209
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
210
- clipPathData: ClipPathDatum[]
211
- }) {
212
- const clipPath = defsSelection
213
- .selectAll<SVGClipPathElement, Layout>('clipPath')
214
- .data(clipPathData)
215
- .join(
216
- enter => {
217
- return enter
218
- .append('clipPath')
219
- },
220
- update => update,
221
- exit => exit.remove()
222
- )
223
- .attr('id', d => d.id)
224
- .each((d, i, g) => {
225
- const rect = d3.select(g[i])
226
- .selectAll<SVGRectElement, typeof d>('rect')
227
- .data([d])
228
- .join('rect')
229
- .attr('x', 0)
230
- .attr('y', 0)
231
- .attr('width', _d => _d.width)
232
- .attr('height', _d => _d.height)
233
- })
234
-
235
- }
236
-
237
-
238
- export const ScatterBubbles = defineMultiValuePlugin(pluginConfig)(({ selection, name, subject, observer }) => {
239
-
240
- const destroy$ = new Subject()
241
-
242
- const clipPathID = getUniID(pluginName, 'clipPath-box')
243
- const circleGClassName = getClassName(pluginName, 'circleG')
244
- const circleClassName = getClassName(pluginName, 'circle')
245
-
246
- const {
247
- categorySelection$,
248
- axesSelection$,
249
- defsSelection$,
250
- graphicGSelection$
251
- } = multiValueSelectionsObservable({
252
- selection,
253
- pluginName,
254
- clipPathID,
255
- categoryLabels$: observer.categoryLabels$,
256
- multiValueContainerPosition$: observer.multiValueContainerPosition$,
257
- multiValueGraphicTransform$: observer.multiValueGraphicTransform$
258
- })
259
-
260
- const graphicReverseScale$: Observable<[number, number][]> = combineLatest({
261
- computedData: observer.computedData$,
262
- multiValueGraphicReverseScale: observer.multiValueGraphicReverseScale$
263
- }).pipe(
264
- takeUntil(destroy$),
265
- switchMap(async data => data),
266
- map(data => {
267
- return data.computedData.map((series, categoryIndex) => {
268
- return data.multiValueGraphicReverseScale[categoryIndex]
269
- })
270
- })
271
- )
272
-
273
- const clipPathSubscription = combineLatest({
274
- defsSelection: defsSelection$,
275
- layout: observer.layout$,
276
- }).pipe(
277
- takeUntil(destroy$),
278
- switchMap(async (d) => d),
279
- ).subscribe(data => {
280
- // 外層的遮罩
281
- const clipPathData = [{
282
- id: clipPathID,
283
- width: data.layout.width,
284
- height: data.layout.height
285
- }]
286
- renderClipPath({
287
- defsSelection: data.defsSelection,
288
- clipPathData,
289
- })
290
- })
291
-
292
- const filteredValueList$ = observer.filteredMinMaxXYData$.pipe(
293
- takeUntil(destroy$),
294
- map(data => data.datumList.flat().map(d => d.value[2] ?? 0)),
295
- shareReplay(1)
296
- )
297
-
298
- const filteredMinMaxValue$ = observer.filteredMinMaxXYData$.pipe(
299
- takeUntil(destroy$),
300
- map(data => {
301
- return getMinAndMax(data.datumList.flat().map(d => d.value[2] ?? 0))
302
- })
303
- )
304
-
305
- const opacityScale$ = combineLatest({
306
- filteredMinMaxValue: filteredMinMaxValue$,
307
- fullParams: observer.fullParams$
308
- }).pipe(
309
- takeUntil(destroy$),
310
- switchMap(async (d) => d),
311
- map(data => {
312
- return d3.scaleLinear()
313
- .domain(data.filteredMinMaxValue)
314
- .range(data.fullParams.valueLinearOpacity)
315
- })
316
- )
317
-
318
- // 虛擬大圓(所有小圓聚合起來的大圓)的半徑
319
- const totalR$ = combineLatest({
320
- layout: observer.layout$,
321
- fullParams: observer.fullParams$
322
- }).pipe(
323
- takeUntil(destroy$),
324
- map(data => {
325
- // 場景最短邊
326
- const fullRadius = Math.min(...[data.layout.width, data.layout.height]) / 2
327
- return fullRadius * data.fullParams.sizeAdjust
328
- })
329
- )
330
-
331
- const totalValue$ = filteredValueList$.pipe(
332
- takeUntil(destroy$),
333
- map(data => {
334
- return data.reduce((acc, current) => acc + current, 0)
335
- })
336
- )
337
-
338
- const radiusScale$ = combineLatest({
339
- totalR: totalR$,
340
- totalValue: totalValue$,
341
- fullParams: observer.fullParams$
342
- }).pipe(
343
- takeUntil(destroy$),
344
- switchMap(async (d) => d),
345
- map(data => {
346
- return d3.scalePow()
347
- .domain([0, data.totalValue])
348
- .range([0, data.totalR])
349
- .exponent(data.fullParams.arcScaleType === 'area'
350
- ? 0.5 // 數值映射面積(0.5為取平方根)
351
- : 1 // 數值映射半徑
352
- )
353
- })
354
- )
355
-
356
- const scaleFactor$ = combineLatest({
357
- radiusScale: radiusScale$,
358
- fullParams: observer.fullParams$,
359
- totalR: totalR$,
360
- filteredValueList: filteredValueList$
361
- }).pipe(
362
- takeUntil(destroy$),
363
- switchMap(async (d) => d),
364
- map(data => {
365
- return data.fullParams.arcScaleType === 'area'
366
- ? 1
367
- // 當數值映射半徑時,多個小圓的總面積會小於大圓的面積,所以要計算縮放比例
368
- : (() => {
369
- const totalArea = data.totalR * data.totalR * Math.PI
370
- return Math.sqrt(totalArea / d3.sum(data.filteredValueList, d => Math.PI * Math.pow(data.radiusScale(d), 2)))
371
- })()
372
- })
373
- )
374
-
375
- const bubbleData$ = combineLatest({
376
- computedLayoutData: observer.computedLayoutData$,
377
- opacityScale: opacityScale$,
378
- radiusScale: radiusScale$,
379
- scaleFactor: scaleFactor$,
380
- fullParams: observer.fullParams$
381
- }).pipe(
382
- takeUntil(destroy$),
383
- switchMap(async (d) => d),
384
- map(data => {
385
- return data.computedLayoutData.map(category => {
386
- return category.map(_d => {
387
- const d: BubbleDatum = _d as BubbleDatum
388
- d.r = data.radiusScale(d.value[2]) * data.scaleFactor * adjustmentFactor
389
- d.opacity = data.opacityScale(d.value[2])
390
- return d
391
- })
392
- })
393
- })
394
- )
395
-
396
-
397
- const graphicSelection$ = combineLatest({
398
- graphicGSelection: graphicGSelection$,
399
- // visibleComputedLayoutData: observer.visibleComputedLayoutData$,
400
- bubbleData: bubbleData$,
401
- graphicReverseScale: graphicReverseScale$,
402
- fullChartParams: observer.fullChartParams$,
403
- fullParams: observer.fullParams$,
404
- }).pipe(
405
- takeUntil(destroy$),
406
- switchMap(async (d) => d),
407
- map(data => {
408
- return renderDots({
409
- graphicGSelection: data.graphicGSelection,
410
- circleGClassName,
411
- circleClassName,
412
- bubbleData: data.bubbleData,
413
- fullParams: data.fullParams,
414
- fullChartParams: data.fullChartParams,
415
- graphicReverseScale: data.graphicReverseScale
416
- })
417
- })
418
- )
419
-
420
- const highlightTarget$ = observer.fullChartParams$.pipe(
421
- takeUntil(destroy$),
422
- map(d => d.highlightTarget),
423
- distinctUntilChanged()
424
- )
425
-
426
- combineLatest({
427
- graphicSelection: graphicSelection$,
428
- computedData: observer.computedData$,
429
- CategoryDataMap: observer.CategoryDataMap$,
430
- highlightTarget: highlightTarget$
431
- }).pipe(
432
- takeUntil(destroy$),
433
- switchMap(async (d) => d),
434
- ).subscribe(data => {
435
-
436
- data.graphicSelection
437
- .on('mouseover', (event, datum) => {
438
- // event.stopPropagation()
439
- // console.log({
440
- // type: 'multiValue',
441
- // eventName: 'mouseover',
442
- // pluginName,
443
- // highlightTarget: data.highlightTarget,
444
- // datum,
445
- // category: data.CategoryDataMap.get(datum.categoryLabel)!,
446
- // categoryIndex: datum.categoryIndex,
447
- // categoryLabel: datum.categoryLabel,
448
- // data: data.computedData,
449
- // event,
450
- // })
451
- subject.event$.next({
452
- type: 'multiValue',
453
- eventName: 'mouseover',
454
- pluginName,
455
- highlightTarget: data.highlightTarget,
456
- datum,
457
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
458
- categoryIndex: datum.categoryIndex,
459
- categoryLabel: datum.categoryLabel,
460
- data: data.computedData,
461
- event,
462
- })
463
- })
464
- .on('mousemove', (event, datum) => {
465
- // event.stopPropagation()
466
-
467
- subject.event$.next({
468
- type: 'multiValue',
469
- eventName: 'mousemove',
470
- pluginName,
471
- highlightTarget: data.highlightTarget,
472
- datum,
473
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
474
- categoryIndex: datum.categoryIndex,
475
- categoryLabel: datum.categoryLabel,
476
- data: data.computedData,
477
- event,
478
- })
479
- })
480
- .on('mouseout', (event, datum) => {
481
- // event.stopPropagation()
482
-
483
- subject.event$.next({
484
- type: 'multiValue',
485
- eventName: 'mouseout',
486
- pluginName,
487
- highlightTarget: data.highlightTarget,
488
- datum,
489
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
490
- categoryIndex: datum.categoryIndex,
491
- categoryLabel: datum.categoryLabel,
492
- data: data.computedData,
493
- event,
494
- })
495
- })
496
- .on('click', (event, datum) => {
497
- // event.stopPropagation()
498
-
499
- subject.event$.next({
500
- type: 'multiValue',
501
- eventName: 'click',
502
- pluginName,
503
- highlightTarget: data.highlightTarget,
504
- datum,
505
- category: data.CategoryDataMap.get(datum.categoryLabel)!,
506
- categoryIndex: datum.categoryIndex,
507
- categoryLabel: datum.categoryLabel,
508
- data: data.computedData,
509
- event,
510
- })
511
- })
512
-
513
- })
514
-
515
- combineLatest({
516
- graphicSelection: graphicSelection$,
517
- highlight: observer.multiValueHighlight$.pipe(
518
- map(data => data.map(d => d.id))
519
- ),
520
- fullChartParams: observer.fullChartParams$
521
- }).pipe(
522
- takeUntil(destroy$),
523
- switchMap(async d => d)
524
- ).subscribe(data => {
525
- highlightBubbles({
526
- selection: data.graphicSelection,
527
- ids: data.highlight,
528
- fullChartParams: data.fullChartParams
529
- })
530
- })
531
-
532
- // graphicGSelection$.subscribe(data => {
533
- // console.log('graphicGSelection$', data)
534
- // })
535
-
536
- // observer.visibleComputedLayoutData$.subscribe(data => {
537
- // console.log('visibleComputedLayoutData$', data)
538
- // })
539
-
540
- // observer.fullChartParams$.subscribe(data => {
541
- // console.log('fullChartParams$', data)
542
- // })
543
-
544
- // observer.fullParams$.subscribe(data => {
545
- // console.log('fullParams$', data)
546
- // })
547
-
548
- return () => {
549
- destroy$.next(undefined)
550
- }
551
- })
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ map,
5
+ filter,
6
+ switchMap,
7
+ takeUntil,
8
+ distinctUntilChanged,
9
+ Observable,
10
+ Subject,
11
+ share,
12
+ shareReplay
13
+ } from 'rxjs'
14
+ import type {
15
+ ComputedDatumMultiValue,
16
+ ComputedDataMultiValue,
17
+ ComputedLayoutDatumMultiValue,
18
+ ComputedLayoutDataMultiValue,
19
+ DefinePluginConfig,
20
+ EventMultiValue,
21
+ ChartParams,
22
+ ContainerPositionScaled,
23
+ Layout,
24
+ TransformData,
25
+ ColorType
26
+ } from '../../../lib/core-types'
27
+ import {
28
+ defineMultiValuePlugin,
29
+ getMinAndMax
30
+ } from '../../../lib/core'
31
+ import type { ScatterBubblesParams } from '../../../lib/plugins-basic-types'
32
+ import { DEFAULT_SCATTER_BUBBLES_PARAMS } from '../defaults'
33
+ import { LAYER_INDEX_OF_GRAPHIC_COVER } from '../../const'
34
+ import { getDatumColor, getClassName, getUniID } from '../../utils/orbchartsUtils'
35
+ import { multiValueSelectionsObservable } from '../multiValueObservables'
36
+
37
+ type ClipPathDatum = {
38
+ id: string;
39
+ // x: number;
40
+ // y: number;
41
+ width: number;
42
+ height: number;
43
+ }
44
+
45
+ interface BubbleDatum extends ComputedLayoutDatumMultiValue {
46
+ r: number
47
+ opacity: number
48
+ }
49
+
50
+ const pluginName = 'ScatterBubbles'
51
+
52
+ // 調整係數 - 因為圓和圓之間的空隙造成聚合起來的大圓會略大,所以稍作微調
53
+ const adjustmentFactor = 0.9
54
+
55
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_SCATTER_BUBBLES_PARAMS> = {
56
+ name: pluginName,
57
+ defaultParams: DEFAULT_SCATTER_BUBBLES_PARAMS,
58
+ layerIndex: LAYER_INDEX_OF_GRAPHIC_COVER,
59
+ validator: (params, { validateColumns }) => {
60
+ const result = validateColumns(params, {
61
+ // radius: {
62
+ // toBeTypes: ['number']
63
+ // },
64
+ fillColorType: {
65
+ toBeOption: 'ColorType',
66
+ },
67
+ strokeColorType: {
68
+ toBeOption: 'ColorType',
69
+ },
70
+ strokeWidth: {
71
+ toBeTypes: ['number']
72
+ },
73
+ valueLinearOpacity: {
74
+ toBeTypes: ['number[]']
75
+ },
76
+ arcScaleType: {
77
+ toBe: 'ArcScaleType',
78
+ test: (value) => {
79
+ return value === 'area' || value === 'radius'
80
+ }
81
+ },
82
+ sizeAdjust: {
83
+ toBeTypes: ['number']
84
+ }
85
+ })
86
+ return result
87
+ }
88
+ }
89
+
90
+ function renderDots ({ graphicGSelection, circleGClassName, circleClassName, bubbleData, fullParams, fullChartParams, graphicReverseScale }: {
91
+ graphicGSelection: d3.Selection<SVGGElement, any, any, any>
92
+ circleGClassName: string
93
+ circleClassName: string
94
+ // visibleComputedLayoutData: ComputedLayoutDataMultiValue
95
+ bubbleData: BubbleDatum[][]
96
+ fullParams: ScatterBubblesParams
97
+ fullChartParams: ChartParams
98
+ graphicReverseScale: [number, number][]
99
+ }) {
100
+ const createEnterDuration = (enter: d3.Selection<d3.EnterElement, ComputedDatumMultiValue, SVGGElement, any>) => {
101
+ const enterSize = enter.size()
102
+ const eachDuration = fullChartParams.transitionDuration / enterSize
103
+ return eachDuration
104
+ }
105
+ // enterDuration
106
+ let enterDuration = 0
107
+
108
+ graphicGSelection
109
+ .each((categoryData, categoryIndex, g) => {
110
+ d3.select(g[categoryIndex])
111
+ .selectAll<SVGGElement, ComputedDatumMultiValue>('g')
112
+ .data(bubbleData[categoryIndex], d => d.id)
113
+ .join(
114
+ enter => {
115
+ // enterDuration
116
+ enterDuration = createEnterDuration(enter)
117
+
118
+ return enter
119
+ .append('g')
120
+ .classed(circleGClassName, true)
121
+ },
122
+ update => update,
123
+ exit => exit.remove()
124
+ )
125
+ .attr('transform', d => `translate(${d.axisX}, ${d.axisY})`)
126
+ .each((d, i, g) => {
127
+ const circle = d3.select(g[i])
128
+ .selectAll('circle')
129
+ .data([d])
130
+ .join(
131
+ enter => {
132
+ return enter
133
+ .append('circle')
134
+ .style('cursor', 'pointer')
135
+ .style('vector-effect', 'non-scaling-stroke')
136
+ .classed(circleClassName, true)
137
+ .attr('opacity', 0)
138
+ .transition()
139
+ .delay((_d, _i) => {
140
+ return i * enterDuration
141
+ })
142
+ .attr('opacity', 0.8)
143
+ },
144
+ update => {
145
+ return update
146
+ .transition()
147
+ .duration(50)
148
+ // .attr('cx', d => d.axisX)
149
+ // .attr('cy', d => d.axisY)
150
+ .attr('opacity', 0.8)
151
+ },
152
+ exit => exit.remove()
153
+ )
154
+ .attr('r', d => d.r)
155
+ .attr('fill', (d, i) => getDatumColor({ datum: d, colorType: fullParams.fillColorType, fullChartParams }))
156
+ .attr('stroke', (d, i) => getDatumColor({ datum: d, colorType: fullParams.strokeColorType, fullChartParams }))
157
+ .attr('stroke-width', fullParams.strokeWidth)
158
+ .attr('transform', `scale(${graphicReverseScale[categoryIndex][0] ?? 1}, ${graphicReverseScale[categoryIndex][1] ?? 1})`)
159
+ })
160
+ })
161
+
162
+ const graphicCircleSelection: d3.Selection<SVGRectElement, BubbleDatum, SVGGElement, unknown> = graphicGSelection.selectAll(`circle.${circleClassName}`)
163
+
164
+ return graphicCircleSelection
165
+ }
166
+
167
+
168
+ function highlightBubbles ({ selection, ids, fullChartParams }: {
169
+ selection: d3.Selection<SVGGElement, BubbleDatum, any, any>
170
+ ids: string[]
171
+ fullChartParams: ChartParams
172
+ }) {
173
+ selection.interrupt('highlight')
174
+ if (!ids.length) {
175
+ // remove highlight
176
+ selection
177
+ .transition('highlight')
178
+ .duration(200)
179
+ .style('opacity', d => d.opacity)
180
+ // selection
181
+ // .attr('stroke-width', fullParams.strokeWidth)
182
+
183
+ return
184
+ }
185
+
186
+ selection
187
+ .each((d, i, n) => {
188
+ if (ids.includes(d.id)) {
189
+ const dot = d3.select<SVGGElement, BubbleDatum>(n[i])
190
+ dot
191
+ .style('opacity', d => d.opacity)
192
+ .transition('highlight')
193
+ .duration(200)
194
+ // dot
195
+ // .attr('stroke-width', fullParams.strokeWidthWhileHighlight)
196
+ } else {
197
+ const dot = d3.select(n[i])
198
+ dot
199
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
200
+ .transition('highlight')
201
+ .duration(200)
202
+ // dot
203
+ // .attr('stroke-width', fullParams.strokeWidth)
204
+
205
+ }
206
+ })
207
+ }
208
+
209
+ function renderClipPath ({ defsSelection, clipPathData }: {
210
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
211
+ clipPathData: ClipPathDatum[]
212
+ }) {
213
+ const clipPath = defsSelection
214
+ .selectAll<SVGClipPathElement, Layout>('clipPath')
215
+ .data(clipPathData)
216
+ .join(
217
+ enter => {
218
+ return enter
219
+ .append('clipPath')
220
+ },
221
+ update => update,
222
+ exit => exit.remove()
223
+ )
224
+ .attr('id', d => d.id)
225
+ .each((d, i, g) => {
226
+ const rect = d3.select(g[i])
227
+ .selectAll<SVGRectElement, typeof d>('rect')
228
+ .data([d])
229
+ .join('rect')
230
+ .attr('x', 0)
231
+ .attr('y', 0)
232
+ .attr('width', _d => _d.width)
233
+ .attr('height', _d => _d.height)
234
+ })
235
+
236
+ }
237
+
238
+
239
+ export const ScatterBubbles = defineMultiValuePlugin(pluginConfig)(({ selection, name, subject, observer }) => {
240
+
241
+ const destroy$ = new Subject()
242
+
243
+ const clipPathID = getUniID(pluginName, 'clipPath-box')
244
+ const circleGClassName = getClassName(pluginName, 'circleG')
245
+ const circleClassName = getClassName(pluginName, 'circle')
246
+
247
+ const {
248
+ categorySelection$,
249
+ axesSelection$,
250
+ defsSelection$,
251
+ graphicGSelection$
252
+ } = multiValueSelectionsObservable({
253
+ selection,
254
+ pluginName,
255
+ clipPathID,
256
+ categoryLabels$: observer.categoryLabels$,
257
+ multiValueContainerPosition$: observer.multiValueContainerPosition$,
258
+ multiValueGraphicTransform$: observer.multiValueGraphicTransform$
259
+ })
260
+
261
+ const graphicReverseScale$: Observable<[number, number][]> = combineLatest({
262
+ computedData: observer.computedData$,
263
+ multiValueGraphicReverseScale: observer.multiValueGraphicReverseScale$
264
+ }).pipe(
265
+ takeUntil(destroy$),
266
+ switchMap(async data => data),
267
+ map(data => {
268
+ return data.computedData.map((series, categoryIndex) => {
269
+ return data.multiValueGraphicReverseScale[categoryIndex]
270
+ })
271
+ })
272
+ )
273
+
274
+ const clipPathSubscription = combineLatest({
275
+ defsSelection: defsSelection$,
276
+ layout: observer.layout$,
277
+ }).pipe(
278
+ takeUntil(destroy$),
279
+ switchMap(async (d) => d),
280
+ ).subscribe(data => {
281
+ // 外層的遮罩
282
+ const clipPathData = [{
283
+ id: clipPathID,
284
+ width: data.layout.width,
285
+ height: data.layout.height
286
+ }]
287
+ renderClipPath({
288
+ defsSelection: data.defsSelection,
289
+ clipPathData,
290
+ })
291
+ })
292
+
293
+ const filteredValueList$ = observer.filteredMinMaxXYData$.pipe(
294
+ takeUntil(destroy$),
295
+ map(data => data.datumList.flat().map(d => d.value[2] ?? 0)),
296
+ shareReplay(1)
297
+ )
298
+
299
+ const filteredMinMaxValue$ = observer.filteredMinMaxXYData$.pipe(
300
+ takeUntil(destroy$),
301
+ map(data => {
302
+ return getMinAndMax(data.datumList.flat().map(d => d.value[2] ?? 0))
303
+ })
304
+ )
305
+
306
+ const opacityScale$ = combineLatest({
307
+ filteredMinMaxValue: filteredMinMaxValue$,
308
+ fullParams: observer.fullParams$
309
+ }).pipe(
310
+ takeUntil(destroy$),
311
+ switchMap(async (d) => d),
312
+ map(data => {
313
+ return d3.scaleLinear()
314
+ .domain(data.filteredMinMaxValue)
315
+ .range(data.fullParams.valueLinearOpacity)
316
+ })
317
+ )
318
+
319
+ // 虛擬大圓(所有小圓聚合起來的大圓)的半徑
320
+ const totalR$ = combineLatest({
321
+ layout: observer.layout$,
322
+ fullParams: observer.fullParams$
323
+ }).pipe(
324
+ takeUntil(destroy$),
325
+ map(data => {
326
+ // 場景最短邊
327
+ const fullRadius = Math.min(...[data.layout.width, data.layout.height]) / 2
328
+ return fullRadius * data.fullParams.sizeAdjust
329
+ })
330
+ )
331
+
332
+ const totalValue$ = filteredValueList$.pipe(
333
+ takeUntil(destroy$),
334
+ map(data => {
335
+ return data.reduce((acc, current) => acc + current, 0)
336
+ }),
337
+ filter(data => data > 0) // 避免後續計算scale的時候發生問題
338
+ )
339
+
340
+ const radiusScale$ = combineLatest({
341
+ totalR: totalR$,
342
+ totalValue: totalValue$,
343
+ fullParams: observer.fullParams$
344
+ }).pipe(
345
+ takeUntil(destroy$),
346
+ switchMap(async (d) => d),
347
+ map(data => {
348
+ // console.log({ totalR: data.totalR, totalValue: data.totalValue })
349
+ return d3.scalePow()
350
+ .domain([0, data.totalValue])
351
+ .range([0, data.totalR])
352
+ .exponent(data.fullParams.arcScaleType === 'area'
353
+ ? 0.5 // 數值映射面積(0.5為取平方根)
354
+ : 1 // 數值映射半徑
355
+ )
356
+ })
357
+ )
358
+
359
+ const scaleFactor$ = combineLatest({
360
+ radiusScale: radiusScale$,
361
+ fullParams: observer.fullParams$,
362
+ totalR: totalR$,
363
+ filteredValueList: filteredValueList$
364
+ }).pipe(
365
+ takeUntil(destroy$),
366
+ switchMap(async (d) => d),
367
+ map(data => {
368
+ return data.fullParams.arcScaleType === 'area'
369
+ ? 1
370
+ // 當數值映射半徑時,多個小圓的總面積會小於大圓的面積,所以要計算縮放比例
371
+ : (() => {
372
+ const totalArea = data.totalR * data.totalR * Math.PI
373
+ return Math.sqrt(totalArea / d3.sum(data.filteredValueList, d => Math.PI * Math.pow(data.radiusScale(d), 2)))
374
+ })()
375
+ })
376
+ )
377
+
378
+ const bubbleData$ = combineLatest({
379
+ computedLayoutData: observer.computedLayoutData$,
380
+ opacityScale: opacityScale$,
381
+ radiusScale: radiusScale$,
382
+ scaleFactor: scaleFactor$,
383
+ fullParams: observer.fullParams$
384
+ }).pipe(
385
+ takeUntil(destroy$),
386
+ switchMap(async (d) => d),
387
+ map(data => {
388
+ return data.computedLayoutData.map(category => {
389
+ return category.map(_d => {
390
+ const d: BubbleDatum = _d as BubbleDatum
391
+ d.r = data.radiusScale(d.value[2]) * data.scaleFactor * adjustmentFactor
392
+ d.opacity = data.opacityScale(d.value[2])
393
+ return d
394
+ })
395
+ })
396
+ })
397
+ )
398
+
399
+
400
+ const graphicSelection$ = combineLatest({
401
+ graphicGSelection: graphicGSelection$,
402
+ // visibleComputedLayoutData: observer.visibleComputedLayoutData$,
403
+ bubbleData: bubbleData$,
404
+ graphicReverseScale: graphicReverseScale$,
405
+ fullChartParams: observer.fullChartParams$,
406
+ fullParams: observer.fullParams$,
407
+ }).pipe(
408
+ takeUntil(destroy$),
409
+ switchMap(async (d) => d),
410
+ map(data => {
411
+ return renderDots({
412
+ graphicGSelection: data.graphicGSelection,
413
+ circleGClassName,
414
+ circleClassName,
415
+ bubbleData: data.bubbleData,
416
+ fullParams: data.fullParams,
417
+ fullChartParams: data.fullChartParams,
418
+ graphicReverseScale: data.graphicReverseScale
419
+ })
420
+ })
421
+ )
422
+
423
+ const highlightTarget$ = observer.fullChartParams$.pipe(
424
+ takeUntil(destroy$),
425
+ map(d => d.highlightTarget),
426
+ distinctUntilChanged()
427
+ )
428
+
429
+ combineLatest({
430
+ graphicSelection: graphicSelection$,
431
+ computedData: observer.computedData$,
432
+ CategoryDataMap: observer.CategoryDataMap$,
433
+ highlightTarget: highlightTarget$
434
+ }).pipe(
435
+ takeUntil(destroy$),
436
+ switchMap(async (d) => d),
437
+ ).subscribe(data => {
438
+
439
+ data.graphicSelection
440
+ .on('mouseover', (event, datum) => {
441
+ // event.stopPropagation()
442
+ // console.log({
443
+ // type: 'multiValue',
444
+ // eventName: 'mouseover',
445
+ // pluginName,
446
+ // highlightTarget: data.highlightTarget,
447
+ // datum,
448
+ // category: data.CategoryDataMap.get(datum.categoryLabel)!,
449
+ // categoryIndex: datum.categoryIndex,
450
+ // categoryLabel: datum.categoryLabel,
451
+ // data: data.computedData,
452
+ // event,
453
+ // })
454
+ subject.event$.next({
455
+ type: 'multiValue',
456
+ eventName: 'mouseover',
457
+ pluginName,
458
+ highlightTarget: data.highlightTarget,
459
+ datum,
460
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
461
+ categoryIndex: datum.categoryIndex,
462
+ categoryLabel: datum.categoryLabel,
463
+ data: data.computedData,
464
+ event,
465
+ })
466
+ })
467
+ .on('mousemove', (event, datum) => {
468
+ // event.stopPropagation()
469
+
470
+ subject.event$.next({
471
+ type: 'multiValue',
472
+ eventName: 'mousemove',
473
+ pluginName,
474
+ highlightTarget: data.highlightTarget,
475
+ datum,
476
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
477
+ categoryIndex: datum.categoryIndex,
478
+ categoryLabel: datum.categoryLabel,
479
+ data: data.computedData,
480
+ event,
481
+ })
482
+ })
483
+ .on('mouseout', (event, datum) => {
484
+ // event.stopPropagation()
485
+
486
+ subject.event$.next({
487
+ type: 'multiValue',
488
+ eventName: 'mouseout',
489
+ pluginName,
490
+ highlightTarget: data.highlightTarget,
491
+ datum,
492
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
493
+ categoryIndex: datum.categoryIndex,
494
+ categoryLabel: datum.categoryLabel,
495
+ data: data.computedData,
496
+ event,
497
+ })
498
+ })
499
+ .on('click', (event, datum) => {
500
+ // event.stopPropagation()
501
+
502
+ subject.event$.next({
503
+ type: 'multiValue',
504
+ eventName: 'click',
505
+ pluginName,
506
+ highlightTarget: data.highlightTarget,
507
+ datum,
508
+ category: data.CategoryDataMap.get(datum.categoryLabel)!,
509
+ categoryIndex: datum.categoryIndex,
510
+ categoryLabel: datum.categoryLabel,
511
+ data: data.computedData,
512
+ event,
513
+ })
514
+ })
515
+
516
+ })
517
+
518
+ combineLatest({
519
+ graphicSelection: graphicSelection$,
520
+ highlight: observer.multiValueHighlight$.pipe(
521
+ map(data => data.map(d => d.id))
522
+ ),
523
+ fullChartParams: observer.fullChartParams$
524
+ }).pipe(
525
+ takeUntil(destroy$),
526
+ switchMap(async d => d)
527
+ ).subscribe(data => {
528
+ highlightBubbles({
529
+ selection: data.graphicSelection,
530
+ ids: data.highlight,
531
+ fullChartParams: data.fullChartParams
532
+ })
533
+ })
534
+
535
+ // graphicGSelection$.subscribe(data => {
536
+ // console.log('graphicGSelection$', data)
537
+ // })
538
+
539
+ // observer.visibleComputedLayoutData$.subscribe(data => {
540
+ // console.log('visibleComputedLayoutData$', data)
541
+ // })
542
+
543
+ // observer.fullChartParams$.subscribe(data => {
544
+ // console.log('fullChartParams$', data)
545
+ // })
546
+
547
+ // observer.fullParams$.subscribe(data => {
548
+ // console.log('fullParams$', data)
549
+ // })
550
+
551
+ return () => {
552
+ destroy$.next(undefined)
553
+ }
554
+ })