@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,781 +1,783 @@
1
- import * as d3 from 'd3'
2
- import {
3
- iif,
4
- combineLatest,
5
- map,
6
- switchMap,
7
- takeUntil,
8
- distinctUntilChanged,
9
- Observable,
10
- Subject } from 'rxjs'
11
- import type { BasePluginFn } from './types'
12
- import type {
13
- ComputedDatumGrid,
14
- ComputedDataGrid,
15
- ComputedLayoutDatumGrid,
16
- ComputedLayoutDataGrid,
17
- DataFormatterGrid,
18
- EventGrid,
19
- ChartParams,
20
- ContainerPositionScaled,
21
- Layout,
22
- TransformData } from '../../lib/core-types'
23
- import type { BaseBarStackParams } from '../../lib/plugins-basic-types'
24
- import { getD3TransitionEase } from '../utils/d3Utils'
25
- import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
- import { gridSelectionsObservable } from '../grid/gridObservables'
27
- import { shareReplay } from 'rxjs/operators'
28
-
29
- // export interface BaseBarStackParams {
30
- // barWidth: number
31
- // barGroupPadding: number
32
- // barRadius: number | boolean
33
- // }
34
-
35
- interface BaseBarStackContext {
36
- selection: d3.Selection<any, unknown, any, unknown>
37
- computedData$: Observable<ComputedDataGrid>
38
- computedLayoutData$: Observable<ComputedLayoutDataGrid>
39
- visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
- visibleComputedLayoutData$: Observable<ComputedLayoutDataGrid>
41
- seriesLabels$: Observable<string[]>
42
- SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
- GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
- fullParams$: Observable<BaseBarStackParams>
45
- fullDataFormatter$: Observable<DataFormatterGrid>
46
- fullChartParams$: Observable<ChartParams>
47
- gridAxesTransform$: Observable<TransformData>
48
- gridGraphicTransform$: Observable<TransformData>
49
- gridGraphicReverseScale$: Observable<[number, number][]>
50
- gridAxesSize$: Observable<{
51
- width: number;
52
- height: number;
53
- }>
54
- gridHighlight$: Observable<ComputedDatumGrid[]>
55
- gridContainerPosition$: Observable<ContainerPositionScaled[]>
56
- isSeriesSeprate$: Observable<boolean>
57
- event$: Subject<EventGrid>
58
- }
59
-
60
-
61
- interface GraphicDatum extends ComputedLayoutDatumGrid {
62
- _barStartY: number // bar的起點y座標
63
- _barHeight: number // bar的高度
64
- }
65
-
66
- interface RenderBarParams {
67
- graphicGSelection: d3.Selection<SVGGElement, unknown, any, any>
68
- rectClassName: string
69
- barData: GraphicDatum[][]
70
- zeroY: number
71
- groupLabels: string[]
72
- // barScale: d3.ScalePoint<string>
73
- params: BaseBarStackParams
74
- chartParams: ChartParams
75
- barWidth: number
76
- transformedBarRadius: [number, number][]
77
- delayGroup: number
78
- transitionItem: number
79
- isSeriesSeprate: boolean
80
- }
81
-
82
- type ClipPathDatum = {
83
- id: string;
84
- // x: number;
85
- // y: number;
86
- width: number;
87
- height: number;
88
- }
89
-
90
- // const pluginName = 'BarStack'
91
- // const rectClassName = getClassName(pluginName, 'rect')
92
- // group的delay在動畫中的佔比(剩餘部份的時間為圖形本身的動畫時間,因為delay時間和最後一個group的動畫時間加總為1)
93
- const groupDelayProportionOfDuration = 0.3
94
-
95
- function calcBarWidth ({ axisWidth, groupAmount, barGroupPadding = 0 }: {
96
- axisWidth: number
97
- groupAmount: number
98
- barGroupPadding: number
99
- }) {
100
- const eachGroupWidth = axisWidth / (groupAmount - 1)
101
- const width = eachGroupWidth - barGroupPadding
102
- return width > 1 ? width : 1
103
-
104
- }
105
-
106
- // function makeBarScale (barWidth: number, seriesLabels: string[], params: BarStackParams) {
107
- // const barHalfWidth = barWidth! / 2
108
- // const barGroupWidth = barWidth * seriesLabels.length + params.barPadding! * seriesLabels.length
109
- // return d3.scalePoint()
110
- // .domain(seriesLabels)
111
- // .range([-barGroupWidth / 2 + barHalfWidth, barGroupWidth / 2 - barHalfWidth])
112
- // }
113
-
114
- function calcDelayGroup (barGroupAmount: number, totalDuration: number) {
115
- if (barGroupAmount <= 1) {
116
- // 一筆內計算會出錯所以不算
117
- return 0
118
- }
119
- return totalDuration / (barGroupAmount - 1) * groupDelayProportionOfDuration // 依group數量計算
120
- }
121
-
122
- function calctransitionItem (barGroupAmount: number, totalDuration: number) {
123
- if (barGroupAmount <= 1) {
124
- // 一筆內不會有delay
125
- return totalDuration
126
- }
127
- return totalDuration * (1 - groupDelayProportionOfDuration) // delay後剩餘的時間
128
- }
129
-
130
- function renderRectBars ({ graphicGSelection, rectClassName, barData, zeroY, groupLabels, params, chartParams, barWidth, transformedBarRadius, delayGroup, transitionItem, isSeriesSeprate }: RenderBarParams) {
131
-
132
- const barHalfWidth = barWidth! / 2
133
-
134
- graphicGSelection
135
- .each((seriesData, seriesIndex, g) => {
136
- d3.select(g[seriesIndex])
137
- .selectAll<SVGGElement, ComputedDatumGrid>(`rect.${rectClassName}`)
138
- .data(barData[seriesIndex] ?? [], d => d.id)
139
- .join(
140
- enter => {
141
- // console.log('enter')
142
- return enter
143
- .append('rect')
144
- .classed(rectClassName, true)
145
- .attr('cursor', 'pointer')
146
- .attr('height', d => 0)
147
- },
148
- update => update,
149
- exit => exit.remove()
150
- )
151
- .attr('transform', (d, i) => `translate(${(d ? d.axisX : 0) - barHalfWidth}, ${0})`)
152
- .attr('fill', d => d.color)
153
- .attr('y', d => zeroY)
154
- .attr('x', d =>0)
155
- .attr('width', barWidth!)
156
- .attr('rx', transformedBarRadius[seriesIndex][0] ?? 1)
157
- .attr('ry', transformedBarRadius[seriesIndex][1] ?? 1)
158
- .transition()
159
- .duration(transitionItem)
160
- .ease(getD3TransitionEase(chartParams.transitionEase))
161
- .delay((d, i) => d.groupIndex * delayGroup)
162
- .attr('y', d => d._barStartY)
163
- .attr('height', d => Math.abs(d._barHeight))
164
- })
165
-
166
- // const barGroup = graphicGSelection
167
- // .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${gClassName}`)
168
- // .data(data, (d, i) => groupLabels[i])
169
- // .join(
170
- // enter => {
171
- // return enter
172
- // .append('g')
173
- // .classed(gClassName, true)
174
- // .attr('cursor', 'pointer')
175
- // },
176
- // update => update,
177
- // exit => exit.remove()
178
- // )
179
- // .attr('transform', (d, i) => `translate(${d[0] ? d[0].axisX : 0}, ${0})`)
180
- // .each((d, i, g) => {
181
- // const bars = d3.select(g[i])
182
- // .selectAll<SVGGElement, ComputedDatumGrid>('g')
183
- // .data(d, _d => _d.id)
184
- // .join(
185
- // enter => {
186
- // return enter
187
- // .append('g')
188
- // .classed(gContentClassName, true)
189
- // },
190
- // update => update,
191
- // exit => exit.remove()
192
- // )
193
- // .each((_d, _i, _g) => {
194
- // const rect = d3.select(_g[_i])
195
- // .selectAll<SVGRectElement, ComputedDatumGrid>('rect')
196
- // .data([_d], _d => _d.id)
197
- // .join(
198
- // enter => {
199
- // return enter
200
- // .append('rect')
201
- // .attr('y', d => zeroY)
202
- // .attr('height', d => 0)
203
- // },
204
- // update => update,
205
- // exit => exit.remove()
206
- // )
207
- // .attr('rx', transformedBarRadius[0])
208
- // .attr('ry', transformedBarRadius[1])
209
- // .attr('fill', d => d.color)
210
- // .attr('transform', `translate(${-barHalfWidth}, 0)`)
211
- // .attr('x', d => 0)
212
- // .attr('width', barWidth!)
213
- // .transition()
214
- // .duration(transitionItem)
215
- // .ease(getD3TransitionEase(chartParams.transitionEase))
216
- // .delay((d, i) => d.groupIndex * delayGroup)
217
- // .attr('y', d => d._barStartY)
218
- // .attr('height', d => Math.abs(d._barHeight))
219
- // })
220
-
221
- // })
222
-
223
- const graphicBarSelection: d3.Selection<SVGRectElement, ComputedDatumGrid, SVGGElement, unknown> = graphicGSelection.selectAll(`rect.${rectClassName}`)
224
-
225
-
226
- return graphicBarSelection
227
- }
228
-
229
- function renderClipPath ({ defsSelection, clipPathData }: {
230
- defsSelection: d3.Selection<SVGDefsElement, any, any, any>
231
- clipPathData: ClipPathDatum[]
232
- }) {
233
- const clipPath = defsSelection
234
- .selectAll<SVGClipPathElement, Layout>('clipPath')
235
- .data(clipPathData)
236
- .join(
237
- enter => {
238
- return enter
239
- .append('clipPath')
240
- },
241
- update => update,
242
- exit => exit.remove()
243
- )
244
- .attr('id', d => d.id)
245
- .each((d, i, g) => {
246
- const rect = d3.select(g[i])
247
- .selectAll<SVGRectElement, typeof d>('rect')
248
- .data([d])
249
- .join(
250
- enter => {
251
- return enter
252
- .append('rect')
253
- },
254
- update => update,
255
- exit => exit.remove()
256
- )
257
- .attr('x', 0)
258
- .attr('y', 0)
259
- .attr('width', _d => _d.width)
260
- .attr('height', _d => _d.height)
261
- })
262
- }
263
-
264
- function highlight ({ selection, ids, fullChartParams }: {
265
- selection: d3.Selection<any, ComputedDatumGrid, any, any>
266
- ids: string[]
267
- fullChartParams: ChartParams
268
- }) {
269
- selection.interrupt('highlight')
270
-
271
- if (!ids.length) {
272
- // remove highlight
273
- selection
274
- .transition('highlight')
275
- .duration(200)
276
- .style('opacity', 1)
277
- return
278
- }
279
-
280
- selection
281
- .each((d, i, n) => {
282
- if (ids.includes(d.id)) {
283
- d3.select(n[i])
284
- .style('opacity', 1)
285
- } else {
286
- d3.select(n[i])
287
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
288
- }
289
- })
290
- }
291
-
292
-
293
- export const createBaseBarStack: BasePluginFn<BaseBarStackContext> = (pluginName: string, {
294
- selection,
295
- computedData$,
296
- computedLayoutData$,
297
- visibleComputedData$,
298
- visibleComputedLayoutData$,
299
- seriesLabels$,
300
- SeriesDataMap$,
301
- GroupDataMap$,
302
- fullParams$,
303
- fullDataFormatter$,
304
- fullChartParams$,
305
- gridAxesTransform$,
306
- gridGraphicTransform$,
307
- gridGraphicReverseScale$,
308
- gridAxesSize$,
309
- gridHighlight$,
310
- gridContainerPosition$,
311
- isSeriesSeprate$,
312
- event$
313
- }) => {
314
-
315
- const destroy$ = new Subject()
316
-
317
- const clipPathID = getUniID(pluginName, 'clipPath-box')
318
- const rectClassName = getClassName(pluginName, 'rect')
319
-
320
- const {
321
- seriesSelection$,
322
- axesSelection$,
323
- defsSelection$,
324
- graphicGSelection$
325
- } = gridSelectionsObservable({
326
- selection,
327
- pluginName,
328
- clipPathID,
329
- seriesLabels$,
330
- gridContainerPosition$,
331
- gridAxesTransform$,
332
- gridGraphicTransform$
333
- })
334
-
335
-
336
- const zeroY$ = visibleComputedLayoutData$.pipe(
337
- takeUntil(destroy$),
338
- map(d => d[0] && d[0][0]
339
- ? d[0][0].axisY - d[0][0].axisYFromZero
340
- : 0),
341
- distinctUntilChanged()
342
- )
343
-
344
- const barWidth$ = combineLatest({
345
- computedData: computedData$,
346
- // visibleComputedData: visibleComputedData$,
347
- params: fullParams$,
348
- axisSize: gridAxesSize$,
349
- isSeriesSeprate: isSeriesSeprate$
350
- }).pipe(
351
- takeUntil(destroy$),
352
- switchMap(async d => d),
353
- map(data => {
354
- const barWidth = data.params.barWidth
355
- ? data.params.barWidth
356
- : calcBarWidth({
357
- axisWidth: data.axisSize.width,
358
- groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
359
- barGroupPadding: data.params.barGroupPadding
360
- })
361
- return barWidth
362
- }),
363
- distinctUntilChanged()
364
- )
365
-
366
- // 圓角的值 [rx, ry]
367
- const transformedBarRadius$: Observable<[number, number][]> = combineLatest({
368
- computedData: computedData$,
369
- // gridGraphicTransform: gridGraphicTransform$,
370
- barWidth: barWidth$,
371
- params: fullParams$,
372
- gridGraphicReverseScale: gridGraphicReverseScale$
373
- }).pipe(
374
- takeUntil(destroy$),
375
- switchMap(async data => data),
376
- map(data => {
377
- const barHalfWidth = data.barWidth! / 2
378
- const radius = data.params.barRadius === true ? barHalfWidth
379
- : data.params.barRadius === false ? 0
380
- : typeof data.params.barRadius == 'number' ? data.params.barRadius
381
- : 0
382
-
383
- return data.computedData.map((series, seriesIndex) => {
384
- const gridGraphicReverseScale = data.gridGraphicReverseScale[seriesIndex] ?? data.gridGraphicReverseScale[0]
385
-
386
- const transformedRx = radius * gridGraphicReverseScale[0]
387
- const transformedRy = radius * gridGraphicReverseScale[1]
388
-
389
- return [transformedRx, transformedRy]
390
- })
391
- })
392
- )
393
-
394
- // const seriesLabels$ = visibleComputedData$.pipe(
395
- // takeUntil(destroy$),
396
- // map(data => {
397
- // const SeriesLabelSet: Set<string> = new Set()
398
- // data.forEach(d => {
399
- // d.forEach(_d => {
400
- // SeriesLabelSet.add(_d.seriesLabel)
401
- // })
402
- // })
403
- // return Array.from(SeriesLabelSet)
404
- // })
405
- // )
406
-
407
- const groupLabels$ = visibleComputedData$.pipe(
408
- takeUntil(destroy$),
409
- map(data => {
410
- const GroupLabelSet: Set<string> = new Set()
411
- data.forEach(d => {
412
- d.forEach(_d => {
413
- GroupLabelSet.add(_d.groupLabel)
414
- })
415
- })
416
- return Array.from(GroupLabelSet)
417
- }),
418
- shareReplay(1)
419
- )
420
-
421
- const transitionDuration$ = fullChartParams$.pipe(
422
- takeUntil(destroy$),
423
- map(d => d.transitionDuration),
424
- distinctUntilChanged()
425
- )
426
-
427
- const delayGroup$ = new Observable<number>(subscriber => {
428
- combineLatest({
429
- groupLabels: groupLabels$,
430
- transitionDuration: transitionDuration$,
431
- }).pipe(
432
- switchMap(async d => d)
433
- ).subscribe(data => {
434
- const delay = calcDelayGroup(data.groupLabels.length, data.transitionDuration)
435
- subscriber.next(delay)
436
- })
437
- }).pipe(
438
- takeUntil(destroy$),
439
- distinctUntilChanged()
440
- )
441
-
442
- const transitionItem$ = new Observable<number>(subscriber => {
443
- combineLatest({
444
- groupLabels: groupLabels$,
445
- transitionDuration: transitionDuration$
446
- }).pipe(
447
- switchMap(async d => d)
448
- ).subscribe(data => {
449
- const transition = calctransitionItem(data.groupLabels.length, data.transitionDuration)
450
- subscriber.next(transition)
451
- })
452
- }).pipe(
453
- takeUntil(destroy$),
454
- distinctUntilChanged()
455
- )
456
-
457
- // const transposedVisibleData$: Observable<ComputedDataGrid> = visibleComputedData$.pipe(
458
- // takeUntil(destroy$),
459
- // map(data => {
460
- // console.log('visibleComputedData', data)
461
- // // 取得原始陣列的維度
462
- // const rows = data.length;
463
- // const cols = data.reduce((prev, current) => {
464
- // return Math.max(prev, current.length)
465
- // }, 0)
466
-
467
- // // 初始化轉換後的陣列
468
- // const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
469
-
470
- // // 遍歷原始陣列,進行轉換
471
- // for (let i = 0; i < rows; i++) {
472
- // for (let j = 0; j < cols; j++) {
473
- // transposedArray[j][i] = data[i][j]
474
- // }
475
- // }
476
- // return transposedArray
477
- // })
478
- // )
479
-
480
- const groupScaleDomain$ = combineLatest({
481
- computedData: computedData$,
482
- dataFormatter: fullDataFormatter$,
483
- }).pipe(
484
- takeUntil(destroy$),
485
- switchMap(async d => d),
486
- map(data => {
487
- const groupMin = 0
488
- const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
489
- // const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
490
- // ? groupMin // - data.dataFormatter.grid.groupAxis.scalePadding
491
- // : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number // - data.dataFormatter.grid.groupAxis.scalePadding
492
- const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0]
493
- const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
494
- ? groupMax // + data.dataFormatter.grid.groupAxis.scalePadding
495
- : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number // + data.dataFormatter.grid.groupAxis.scalePadding
496
-
497
- return [groupScaleDomainMin, groupScaleDomainMax]
498
- })
499
- )
500
-
501
- // 堆疊後的高度對應圖軸的比例(最大值/最大值所在group的總和)
502
- const yRatio$ = combineLatest({
503
- visibleComputedLayoutData: visibleComputedLayoutData$,
504
- groupScaleDomain: groupScaleDomain$
505
- }).pipe(
506
- takeUntil(destroy$),
507
- switchMap(async d => d),
508
- map(data => {
509
- const groupScaleDomainMin = data.groupScaleDomain[0]
510
- const groupScaleDomainMax = data.groupScaleDomain[1]
511
- // 只選取group篩選範圍內的資料
512
- const filteredData = data.visibleComputedLayoutData
513
- .map(d => {
514
- return d.filter((_d, i) => {
515
- return _d.groupIndex >= groupScaleDomainMin && _d.groupIndex <= groupScaleDomainMax
516
- })
517
- })
518
-
519
- const filteredDataList = filteredData.flat()
520
- if (filteredDataList.length <= 1) {
521
- return 1
522
- }
523
-
524
- const maxValueDatum = filteredDataList.reduce((max, current) => {
525
- return current.value > max.value ? current : max;
526
- }, filteredDataList[0])
527
- const maxValueGroupIndex = maxValueDatum.groupIndex
528
- const maxValueGroupSum = filteredDataList
529
- .filter(d => d.groupIndex === maxValueGroupIndex)
530
- .reduce((sum, current) => {
531
- return sum + current.value
532
- }, 0)
533
- return maxValueDatum.value / maxValueGroupSum
534
- })
535
- )
536
-
537
- const stackedData$ = combineLatest({
538
- computedLayoutData: computedLayoutData$,
539
- yRatio: yRatio$,
540
- zeroY: zeroY$
541
- }).pipe(
542
- takeUntil(destroy$),
543
- map(data => {
544
- let accYArr: number[] = data.computedLayoutData[0]
545
- ? data.computedLayoutData[0].map(() => data.zeroY)
546
- : []
547
- return data.computedLayoutData.map((series, seriesIndex) => {
548
- return series.map((datum, groupIndex) => {
549
- const _barStartY = accYArr[groupIndex] // 前一次的累加高度
550
- let _barHeight = 0
551
- if (datum.visible) {
552
- _barHeight = datum.axisYFromZero * data.yRatio
553
- accYArr[groupIndex] = accYArr[groupIndex] + _barHeight // 累加高度
554
- }
555
- return <GraphicDatum>{
556
- ...datum,
557
- _barStartY,
558
- _barHeight
559
- }
560
- })
561
- })
562
- // return data.computedData.map(d => {
563
- // let accY = data.zeroY
564
- // return d.map(_d => {
565
- // const _barStartY = accY
566
- // const _barHeight = _d.axisYFromZero * data.yRatio
567
- // accY = accY + _barHeight
568
- // return <GraphicDatum>{
569
- // ..._d,
570
- // _barStartY,
571
- // _barHeight
572
- // }
573
- // })
574
- // })
575
- })
576
- )
577
-
578
- const noneStackedData$ = combineLatest({
579
- computedLayoutData: computedLayoutData$,
580
- // yRatio: yRatio$,
581
- zeroY: zeroY$
582
- }).pipe(
583
- takeUntil(destroy$),
584
- map(data => {
585
- return data.computedLayoutData.map((series, seriesIndex) => {
586
- return series.map((datum, groupIndex) => {
587
- return <GraphicDatum>{
588
- ...datum,
589
- _barStartY: data.zeroY,
590
- _barHeight: datum.axisYFromZero
591
- }
592
- })
593
- })
594
- })
595
- )
596
-
597
- const graphicData$ = isSeriesSeprate$.pipe(
598
- switchMap(isSeriesSeprate => {
599
- return iif(() => isSeriesSeprate, noneStackedData$, stackedData$)
600
- })
601
- )
602
-
603
- combineLatest({
604
- defsSelection: defsSelection$,
605
- gridAxesSize: gridAxesSize$,
606
- }).pipe(
607
- takeUntil(destroy$),
608
- switchMap(async d => d)
609
- ).subscribe(data => {
610
- const clipPathData = [{
611
- id: clipPathID,
612
- width: data.gridAxesSize.width,
613
- height: data.gridAxesSize.height
614
- }]
615
- renderClipPath({
616
- defsSelection: data.defsSelection,
617
- clipPathData
618
- })
619
- })
620
-
621
- const highlightTarget$ = fullChartParams$.pipe(
622
- takeUntil(destroy$),
623
- map(d => d.highlightTarget),
624
- distinctUntilChanged()
625
- )
626
-
627
- const barSelection$ = combineLatest({
628
- graphicGSelection: graphicGSelection$,
629
- graphicData: graphicData$,
630
- zeroY: zeroY$,
631
- groupLabels: groupLabels$,
632
- // barScale: barScale$,
633
- params: fullParams$,
634
- chartParams: fullChartParams$,
635
- highlightTarget: highlightTarget$,
636
- barWidth: barWidth$,
637
- transformedBarRadius: transformedBarRadius$,
638
- delayGroup: delayGroup$,
639
- transitionItem: transitionItem$,
640
- isSeriesSeprate: isSeriesSeprate$
641
- }).pipe(
642
- takeUntil(destroy$),
643
- switchMap(async (d) => d),
644
- map(data => {
645
- return renderRectBars({
646
- graphicGSelection: data.graphicGSelection,
647
- rectClassName,
648
- barData: data.graphicData,
649
- zeroY: data.zeroY,
650
- groupLabels: data.groupLabels,
651
- // barScale: data.barScale,
652
- params: data.params,
653
- chartParams: data.chartParams,
654
- barWidth: data.barWidth,
655
- transformedBarRadius: data.transformedBarRadius,
656
- delayGroup: data.delayGroup,
657
- transitionItem: data.transitionItem,
658
- isSeriesSeprate: data.isSeriesSeprate
659
- })
660
- })
661
- )
662
-
663
- combineLatest({
664
- barSelection: barSelection$,
665
- computedData: computedData$,
666
- highlightTarget: highlightTarget$,
667
- SeriesDataMap: SeriesDataMap$,
668
- GroupDataMap: GroupDataMap$,
669
- }).subscribe(data => {
670
-
671
- data.barSelection!
672
- .on('mouseover', (event, datum) => {
673
- event.stopPropagation()
674
-
675
- event$.next({
676
- type: 'grid',
677
- eventName: 'mouseover',
678
- pluginName,
679
- highlightTarget: data.highlightTarget,
680
- datum,
681
- gridIndex: datum.gridIndex,
682
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
683
- seriesIndex: datum.seriesIndex,
684
- seriesLabel: datum.seriesLabel,
685
- groups: data.GroupDataMap.get(datum.groupLabel)!,
686
- groupIndex: datum.groupIndex,
687
- groupLabel: datum.groupLabel,
688
- event,
689
- data: data.computedData
690
- })
691
- })
692
- .on('mousemove', (event, datum) => {
693
- event.stopPropagation()
694
-
695
- event$.next({
696
- type: 'grid',
697
- eventName: 'mousemove',
698
- pluginName,
699
- highlightTarget: data.highlightTarget,
700
- datum,
701
- gridIndex: datum.gridIndex,
702
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
703
- seriesIndex: datum.seriesIndex,
704
- seriesLabel: datum.seriesLabel,
705
- groups: data.GroupDataMap.get(datum.groupLabel)!,
706
- groupIndex: datum.groupIndex,
707
- groupLabel: datum.groupLabel,
708
- event,
709
- data: data.computedData
710
- })
711
- })
712
- .on('mouseout', (event, datum) => {
713
- event.stopPropagation()
714
-
715
- event$.next({
716
- type: 'grid',
717
- eventName: 'mouseout',
718
- pluginName,
719
- highlightTarget: data.highlightTarget,
720
- datum,
721
- gridIndex: datum.gridIndex,
722
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
723
- seriesIndex: datum.seriesIndex,
724
- seriesLabel: datum.seriesLabel,
725
- groups: data.GroupDataMap.get(datum.groupLabel)!,
726
- groupIndex: datum.groupIndex,
727
- groupLabel: datum.groupLabel,
728
- event,
729
- data: data.computedData
730
- })
731
- })
732
- .on('click', (event, datum) => {
733
- event.stopPropagation()
734
-
735
- event$.next({
736
- type: 'grid',
737
- eventName: 'click',
738
- pluginName,
739
- highlightTarget: data.highlightTarget,
740
- datum,
741
- gridIndex: datum.gridIndex,
742
- series: data.SeriesDataMap.get(datum.seriesLabel)!,
743
- seriesIndex: datum.seriesIndex,
744
- seriesLabel: datum.seriesLabel,
745
- groups: data.GroupDataMap.get(datum.groupLabel)!,
746
- groupIndex: datum.groupIndex,
747
- groupLabel: datum.groupLabel,
748
- event,
749
- data: data.computedData
750
- })
751
- })
752
-
753
- })
754
-
755
- // const datumList$ = computedData$.pipe(
756
- // takeUntil(destroy$),
757
- // map(d => d.flat())
758
- // )
759
- // const highlight$ = highlightObservable({ datumList$, chartParams$, event$: store.event$ })
760
-
761
- combineLatest({
762
- barSelection: barSelection$,
763
- highlight: gridHighlight$.pipe(
764
- map(data => data.map(d => d.id))
765
- ),
766
- fullChartParams: fullChartParams$
767
- }).pipe(
768
- takeUntil(destroy$),
769
- switchMap(async d => d)
770
- ).subscribe(data => {
771
- highlight({
772
- selection: data.barSelection,
773
- ids: data.highlight,
774
- fullChartParams: data.fullChartParams
775
- })
776
- })
777
-
778
- return () => {
779
- destroy$.next(undefined)
780
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ iif,
4
+ combineLatest,
5
+ map,
6
+ switchMap,
7
+ takeUntil,
8
+ distinctUntilChanged,
9
+ Observable,
10
+ Subject } from 'rxjs'
11
+ import type { BasePluginFn } from './types'
12
+ import type {
13
+ ComputedDatumGrid,
14
+ ComputedDataGrid,
15
+ ComputedLayoutDatumGrid,
16
+ ComputedLayoutDataGrid,
17
+ DataFormatterGrid,
18
+ EventGrid,
19
+ ChartParams,
20
+ ContainerPositionScaled,
21
+ Layout,
22
+ TransformData } from '../../lib/core-types'
23
+ import type { BaseBarStackParams } from '../../lib/plugins-basic-types'
24
+ import { getD3TransitionEase } from '../utils/d3Utils'
25
+ import { getClassName, getUniID } from '../utils/orbchartsUtils'
26
+ import { gridSelectionsObservable } from '../grid/gridObservables'
27
+ import { shareReplay } from 'rxjs/operators'
28
+
29
+ // export interface BaseBarStackParams {
30
+ // barWidth: number
31
+ // barGroupPadding: number
32
+ // barRadius: number | boolean
33
+ // }
34
+
35
+ interface BaseBarStackContext {
36
+ selection: d3.Selection<any, unknown, any, unknown>
37
+ computedData$: Observable<ComputedDataGrid>
38
+ computedLayoutData$: Observable<ComputedLayoutDataGrid>
39
+ visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
+ visibleComputedLayoutData$: Observable<ComputedLayoutDataGrid>
41
+ seriesLabels$: Observable<string[]>
42
+ SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
+ GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
+ fullParams$: Observable<BaseBarStackParams>
45
+ fullDataFormatter$: Observable<DataFormatterGrid>
46
+ fullChartParams$: Observable<ChartParams>
47
+ gridAxesTransform$: Observable<TransformData>
48
+ gridGraphicTransform$: Observable<TransformData>
49
+ gridGraphicReverseScale$: Observable<[number, number][]>
50
+ gridAxesSize$: Observable<{
51
+ width: number;
52
+ height: number;
53
+ }>
54
+ gridHighlight$: Observable<ComputedDatumGrid[]>
55
+ gridContainerPosition$: Observable<ContainerPositionScaled[]>
56
+ isSeriesSeprate$: Observable<boolean>
57
+ event$: Subject<EventGrid>
58
+ }
59
+
60
+
61
+ interface GraphicDatum extends ComputedLayoutDatumGrid {
62
+ _barStartY: number // bar的起點y座標
63
+ _barHeight: number // bar的高度
64
+ }
65
+
66
+ interface RenderBarParams {
67
+ graphicGSelection: d3.Selection<SVGGElement, unknown, any, any>
68
+ rectClassName: string
69
+ barData: GraphicDatum[][]
70
+ zeroY: number
71
+ groupLabels: string[]
72
+ // barScale: d3.ScalePoint<string>
73
+ params: BaseBarStackParams
74
+ chartParams: ChartParams
75
+ barWidth: number
76
+ transformedBarRadius: [number, number][]
77
+ delayGroup: number
78
+ transitionItem: number
79
+ isSeriesSeprate: boolean
80
+ }
81
+
82
+ type ClipPathDatum = {
83
+ id: string;
84
+ // x: number;
85
+ // y: number;
86
+ width: number;
87
+ height: number;
88
+ }
89
+
90
+ // const pluginName = 'BarStack'
91
+ // const rectClassName = getClassName(pluginName, 'rect')
92
+ // group的delay在動畫中的佔比(剩餘部份的時間為圖形本身的動畫時間,因為delay時間和最後一個group的動畫時間加總為1)
93
+ const groupDelayProportionOfDuration = 0.3
94
+
95
+ function calcBarWidth ({ axisWidth, groupAmount, barGroupPadding = 0 }: {
96
+ axisWidth: number
97
+ groupAmount: number
98
+ barGroupPadding: number
99
+ }) {
100
+ const eachGroupWidth = groupAmount > 1 // 等於 1 時會算出 Infinity
101
+ ? axisWidth / (groupAmount - 1) // -1是因為要扣掉兩側的padding
102
+ : axisWidth
103
+ const width = eachGroupWidth - barGroupPadding
104
+ return width > 1 ? width : 1
105
+
106
+ }
107
+
108
+ // function makeBarScale (barWidth: number, seriesLabels: string[], params: BarStackParams) {
109
+ // const barHalfWidth = barWidth! / 2
110
+ // const barGroupWidth = barWidth * seriesLabels.length + params.barPadding! * seriesLabels.length
111
+ // return d3.scalePoint()
112
+ // .domain(seriesLabels)
113
+ // .range([-barGroupWidth / 2 + barHalfWidth, barGroupWidth / 2 - barHalfWidth])
114
+ // }
115
+
116
+ function calcDelayGroup (barGroupAmount: number, totalDuration: number) {
117
+ if (barGroupAmount <= 1) {
118
+ // 一筆內計算會出錯所以不算
119
+ return 0
120
+ }
121
+ return totalDuration / (barGroupAmount - 1) * groupDelayProportionOfDuration // 依group數量計算
122
+ }
123
+
124
+ function calctransitionItem (barGroupAmount: number, totalDuration: number) {
125
+ if (barGroupAmount <= 1) {
126
+ // 一筆內不會有delay
127
+ return totalDuration
128
+ }
129
+ return totalDuration * (1 - groupDelayProportionOfDuration) // delay後剩餘的時間
130
+ }
131
+
132
+ function renderRectBars ({ graphicGSelection, rectClassName, barData, zeroY, groupLabels, params, chartParams, barWidth, transformedBarRadius, delayGroup, transitionItem, isSeriesSeprate }: RenderBarParams) {
133
+
134
+ const barHalfWidth = barWidth! / 2
135
+
136
+ graphicGSelection
137
+ .each((seriesData, seriesIndex, g) => {
138
+ d3.select(g[seriesIndex])
139
+ .selectAll<SVGGElement, ComputedDatumGrid>(`rect.${rectClassName}`)
140
+ .data(barData[seriesIndex] ?? [], d => d.id)
141
+ .join(
142
+ enter => {
143
+ // console.log('enter')
144
+ return enter
145
+ .append('rect')
146
+ .classed(rectClassName, true)
147
+ .attr('cursor', 'pointer')
148
+ .attr('height', d => 1)
149
+ },
150
+ update => update,
151
+ exit => exit.remove()
152
+ )
153
+ .attr('transform', (d, i) => `translate(${(d ? d.axisX : 0) - barHalfWidth}, ${0})`)
154
+ .attr('fill', d => d.color)
155
+ .attr('y', d => zeroY)
156
+ .attr('x', d =>0)
157
+ .attr('width', barWidth!)
158
+ .attr('rx', transformedBarRadius[seriesIndex][0] ?? 1)
159
+ .attr('ry', transformedBarRadius[seriesIndex][1] ?? 1)
160
+ .transition()
161
+ .duration(transitionItem)
162
+ .ease(getD3TransitionEase(chartParams.transitionEase))
163
+ .delay((d, i) => d.groupIndex * delayGroup)
164
+ .attr('y', d => d._barStartY)
165
+ .attr('height', d => Math.abs(d._barHeight) || 1) // 無值還是給一個 1 的高度
166
+ })
167
+
168
+ // const barGroup = graphicGSelection
169
+ // .selectAll<SVGGElement, ComputedDatumGrid[]>(`g.${gClassName}`)
170
+ // .data(data, (d, i) => groupLabels[i])
171
+ // .join(
172
+ // enter => {
173
+ // return enter
174
+ // .append('g')
175
+ // .classed(gClassName, true)
176
+ // .attr('cursor', 'pointer')
177
+ // },
178
+ // update => update,
179
+ // exit => exit.remove()
180
+ // )
181
+ // .attr('transform', (d, i) => `translate(${d[0] ? d[0].axisX : 0}, ${0})`)
182
+ // .each((d, i, g) => {
183
+ // const bars = d3.select(g[i])
184
+ // .selectAll<SVGGElement, ComputedDatumGrid>('g')
185
+ // .data(d, _d => _d.id)
186
+ // .join(
187
+ // enter => {
188
+ // return enter
189
+ // .append('g')
190
+ // .classed(gContentClassName, true)
191
+ // },
192
+ // update => update,
193
+ // exit => exit.remove()
194
+ // )
195
+ // .each((_d, _i, _g) => {
196
+ // const rect = d3.select(_g[_i])
197
+ // .selectAll<SVGRectElement, ComputedDatumGrid>('rect')
198
+ // .data([_d], _d => _d.id)
199
+ // .join(
200
+ // enter => {
201
+ // return enter
202
+ // .append('rect')
203
+ // .attr('y', d => zeroY)
204
+ // .attr('height', d => 0)
205
+ // },
206
+ // update => update,
207
+ // exit => exit.remove()
208
+ // )
209
+ // .attr('rx', transformedBarRadius[0])
210
+ // .attr('ry', transformedBarRadius[1])
211
+ // .attr('fill', d => d.color)
212
+ // .attr('transform', `translate(${-barHalfWidth}, 0)`)
213
+ // .attr('x', d => 0)
214
+ // .attr('width', barWidth!)
215
+ // .transition()
216
+ // .duration(transitionItem)
217
+ // .ease(getD3TransitionEase(chartParams.transitionEase))
218
+ // .delay((d, i) => d.groupIndex * delayGroup)
219
+ // .attr('y', d => d._barStartY)
220
+ // .attr('height', d => Math.abs(d._barHeight))
221
+ // })
222
+
223
+ // })
224
+
225
+ const graphicBarSelection: d3.Selection<SVGRectElement, ComputedDatumGrid, SVGGElement, unknown> = graphicGSelection.selectAll(`rect.${rectClassName}`)
226
+
227
+
228
+ return graphicBarSelection
229
+ }
230
+
231
+ function renderClipPath ({ defsSelection, clipPathData }: {
232
+ defsSelection: d3.Selection<SVGDefsElement, any, any, any>
233
+ clipPathData: ClipPathDatum[]
234
+ }) {
235
+ const clipPath = defsSelection
236
+ .selectAll<SVGClipPathElement, Layout>('clipPath')
237
+ .data(clipPathData)
238
+ .join(
239
+ enter => {
240
+ return enter
241
+ .append('clipPath')
242
+ },
243
+ update => update,
244
+ exit => exit.remove()
245
+ )
246
+ .attr('id', d => d.id)
247
+ .each((d, i, g) => {
248
+ const rect = d3.select(g[i])
249
+ .selectAll<SVGRectElement, typeof d>('rect')
250
+ .data([d])
251
+ .join(
252
+ enter => {
253
+ return enter
254
+ .append('rect')
255
+ },
256
+ update => update,
257
+ exit => exit.remove()
258
+ )
259
+ .attr('x', 0)
260
+ .attr('y', 0)
261
+ .attr('width', _d => _d.width)
262
+ .attr('height', _d => _d.height)
263
+ })
264
+ }
265
+
266
+ function highlight ({ selection, ids, fullChartParams }: {
267
+ selection: d3.Selection<any, ComputedDatumGrid, any, any>
268
+ ids: string[]
269
+ fullChartParams: ChartParams
270
+ }) {
271
+ selection.interrupt('highlight')
272
+
273
+ if (!ids.length) {
274
+ // remove highlight
275
+ selection
276
+ .transition('highlight')
277
+ .duration(200)
278
+ .style('opacity', 1)
279
+ return
280
+ }
281
+
282
+ selection
283
+ .each((d, i, n) => {
284
+ if (ids.includes(d.id)) {
285
+ d3.select(n[i])
286
+ .style('opacity', 1)
287
+ } else {
288
+ d3.select(n[i])
289
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
290
+ }
291
+ })
292
+ }
293
+
294
+
295
+ export const createBaseBarStack: BasePluginFn<BaseBarStackContext> = (pluginName: string, {
296
+ selection,
297
+ computedData$,
298
+ computedLayoutData$,
299
+ visibleComputedData$,
300
+ visibleComputedLayoutData$,
301
+ seriesLabels$,
302
+ SeriesDataMap$,
303
+ GroupDataMap$,
304
+ fullParams$,
305
+ fullDataFormatter$,
306
+ fullChartParams$,
307
+ gridAxesTransform$,
308
+ gridGraphicTransform$,
309
+ gridGraphicReverseScale$,
310
+ gridAxesSize$,
311
+ gridHighlight$,
312
+ gridContainerPosition$,
313
+ isSeriesSeprate$,
314
+ event$
315
+ }) => {
316
+
317
+ const destroy$ = new Subject()
318
+
319
+ const clipPathID = getUniID(pluginName, 'clipPath-box')
320
+ const rectClassName = getClassName(pluginName, 'rect')
321
+
322
+ const {
323
+ seriesSelection$,
324
+ axesSelection$,
325
+ defsSelection$,
326
+ graphicGSelection$
327
+ } = gridSelectionsObservable({
328
+ selection,
329
+ pluginName,
330
+ clipPathID,
331
+ seriesLabels$,
332
+ gridContainerPosition$,
333
+ gridAxesTransform$,
334
+ gridGraphicTransform$
335
+ })
336
+
337
+
338
+ const zeroY$ = visibleComputedLayoutData$.pipe(
339
+ takeUntil(destroy$),
340
+ map(d => d[0] && d[0][0]
341
+ ? d[0][0].axisY - d[0][0].axisYFromZero
342
+ : 0),
343
+ distinctUntilChanged()
344
+ )
345
+
346
+ const barWidth$ = combineLatest({
347
+ computedData: computedData$,
348
+ // visibleComputedData: visibleComputedData$,
349
+ params: fullParams$,
350
+ axisSize: gridAxesSize$,
351
+ isSeriesSeprate: isSeriesSeprate$
352
+ }).pipe(
353
+ takeUntil(destroy$),
354
+ switchMap(async d => d),
355
+ map(data => {
356
+ const barWidth = data.params.barWidth
357
+ ? data.params.barWidth
358
+ : calcBarWidth({
359
+ axisWidth: data.axisSize.width,
360
+ groupAmount: data.computedData[0] ? data.computedData[0].length : 0,
361
+ barGroupPadding: data.params.barGroupPadding
362
+ })
363
+ return barWidth
364
+ }),
365
+ distinctUntilChanged()
366
+ )
367
+
368
+ // 圓角的值 [rx, ry]
369
+ const transformedBarRadius$: Observable<[number, number][]> = combineLatest({
370
+ computedData: computedData$,
371
+ // gridGraphicTransform: gridGraphicTransform$,
372
+ barWidth: barWidth$,
373
+ params: fullParams$,
374
+ gridGraphicReverseScale: gridGraphicReverseScale$
375
+ }).pipe(
376
+ takeUntil(destroy$),
377
+ switchMap(async data => data),
378
+ map(data => {
379
+ const barHalfWidth = data.barWidth! / 2
380
+ const radius = data.params.barRadius === true ? barHalfWidth
381
+ : data.params.barRadius === false ? 0
382
+ : typeof data.params.barRadius == 'number' ? data.params.barRadius
383
+ : 0
384
+
385
+ return data.computedData.map((series, seriesIndex) => {
386
+ const gridGraphicReverseScale = data.gridGraphicReverseScale[seriesIndex] ?? data.gridGraphicReverseScale[0]
387
+
388
+ const transformedRx = radius * gridGraphicReverseScale[0]
389
+ const transformedRy = radius * gridGraphicReverseScale[1]
390
+
391
+ return [transformedRx, transformedRy]
392
+ })
393
+ })
394
+ )
395
+
396
+ // const seriesLabels$ = visibleComputedData$.pipe(
397
+ // takeUntil(destroy$),
398
+ // map(data => {
399
+ // const SeriesLabelSet: Set<string> = new Set()
400
+ // data.forEach(d => {
401
+ // d.forEach(_d => {
402
+ // SeriesLabelSet.add(_d.seriesLabel)
403
+ // })
404
+ // })
405
+ // return Array.from(SeriesLabelSet)
406
+ // })
407
+ // )
408
+
409
+ const groupLabels$ = visibleComputedData$.pipe(
410
+ takeUntil(destroy$),
411
+ map(data => {
412
+ const GroupLabelSet: Set<string> = new Set()
413
+ data.forEach(d => {
414
+ d.forEach(_d => {
415
+ GroupLabelSet.add(_d.groupLabel)
416
+ })
417
+ })
418
+ return Array.from(GroupLabelSet)
419
+ }),
420
+ shareReplay(1)
421
+ )
422
+
423
+ const transitionDuration$ = fullChartParams$.pipe(
424
+ takeUntil(destroy$),
425
+ map(d => d.transitionDuration),
426
+ distinctUntilChanged()
427
+ )
428
+
429
+ const delayGroup$ = new Observable<number>(subscriber => {
430
+ combineLatest({
431
+ groupLabels: groupLabels$,
432
+ transitionDuration: transitionDuration$,
433
+ }).pipe(
434
+ switchMap(async d => d)
435
+ ).subscribe(data => {
436
+ const delay = calcDelayGroup(data.groupLabels.length, data.transitionDuration)
437
+ subscriber.next(delay)
438
+ })
439
+ }).pipe(
440
+ takeUntil(destroy$),
441
+ distinctUntilChanged()
442
+ )
443
+
444
+ const transitionItem$ = new Observable<number>(subscriber => {
445
+ combineLatest({
446
+ groupLabels: groupLabels$,
447
+ transitionDuration: transitionDuration$
448
+ }).pipe(
449
+ switchMap(async d => d)
450
+ ).subscribe(data => {
451
+ const transition = calctransitionItem(data.groupLabels.length, data.transitionDuration)
452
+ subscriber.next(transition)
453
+ })
454
+ }).pipe(
455
+ takeUntil(destroy$),
456
+ distinctUntilChanged()
457
+ )
458
+
459
+ // const transposedVisibleData$: Observable<ComputedDataGrid> = visibleComputedData$.pipe(
460
+ // takeUntil(destroy$),
461
+ // map(data => {
462
+ // console.log('visibleComputedData', data)
463
+ // // 取得原始陣列的維度
464
+ // const rows = data.length;
465
+ // const cols = data.reduce((prev, current) => {
466
+ // return Math.max(prev, current.length)
467
+ // }, 0)
468
+
469
+ // // 初始化轉換後的陣列
470
+ // const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
471
+
472
+ // // 遍歷原始陣列,進行轉換
473
+ // for (let i = 0; i < rows; i++) {
474
+ // for (let j = 0; j < cols; j++) {
475
+ // transposedArray[j][i] = data[i][j]
476
+ // }
477
+ // }
478
+ // return transposedArray
479
+ // })
480
+ // )
481
+
482
+ const groupScaleDomain$ = combineLatest({
483
+ computedData: computedData$,
484
+ dataFormatter: fullDataFormatter$,
485
+ }).pipe(
486
+ takeUntil(destroy$),
487
+ switchMap(async d => d),
488
+ map(data => {
489
+ const groupMin = 0
490
+ const groupMax = data.computedData[0] ? data.computedData[0].length - 1 : 0
491
+ // const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0] === 'auto'
492
+ // ? groupMin // - data.dataFormatter.grid.groupAxis.scalePadding
493
+ // : data.dataFormatter.grid.groupAxis.scaleDomain[0] as number // - data.dataFormatter.grid.groupAxis.scalePadding
494
+ const groupScaleDomainMin = data.dataFormatter.grid.groupAxis.scaleDomain[0]
495
+ const groupScaleDomainMax = data.dataFormatter.grid.groupAxis.scaleDomain[1] === 'max'
496
+ ? groupMax // + data.dataFormatter.grid.groupAxis.scalePadding
497
+ : data.dataFormatter.grid.groupAxis.scaleDomain[1] as number // + data.dataFormatter.grid.groupAxis.scalePadding
498
+
499
+ return [groupScaleDomainMin, groupScaleDomainMax]
500
+ })
501
+ )
502
+
503
+ // 堆疊後的高度對應圖軸的比例(最大值/最大值所在group的總和)
504
+ const yRatio$ = combineLatest({
505
+ visibleComputedLayoutData: visibleComputedLayoutData$,
506
+ groupScaleDomain: groupScaleDomain$
507
+ }).pipe(
508
+ takeUntil(destroy$),
509
+ switchMap(async d => d),
510
+ map(data => {
511
+ const groupScaleDomainMin = data.groupScaleDomain[0]
512
+ const groupScaleDomainMax = data.groupScaleDomain[1]
513
+ // 只選取group篩選範圍內的資料
514
+ const filteredData = data.visibleComputedLayoutData
515
+ .map(d => {
516
+ return d.filter((_d, i) => {
517
+ return _d.groupIndex >= groupScaleDomainMin && _d.groupIndex <= groupScaleDomainMax
518
+ })
519
+ })
520
+
521
+ const filteredDataList = filteredData.flat()
522
+ if (filteredDataList.length <= 1) {
523
+ return 1
524
+ }
525
+
526
+ const maxValueDatum = filteredDataList.reduce((max, current) => {
527
+ return current.value > max.value ? current : max;
528
+ }, filteredDataList[0])
529
+ const maxValueGroupIndex = maxValueDatum.groupIndex
530
+ const maxValueGroupSum = filteredDataList
531
+ .filter(d => d.groupIndex === maxValueGroupIndex)
532
+ .reduce((sum, current) => {
533
+ return sum + current.value
534
+ }, 0)
535
+ return maxValueDatum.value / maxValueGroupSum
536
+ })
537
+ )
538
+
539
+ const stackedData$ = combineLatest({
540
+ computedLayoutData: computedLayoutData$,
541
+ yRatio: yRatio$,
542
+ zeroY: zeroY$
543
+ }).pipe(
544
+ takeUntil(destroy$),
545
+ map(data => {
546
+ let accYArr: number[] = data.computedLayoutData[0]
547
+ ? data.computedLayoutData[0].map(() => data.zeroY)
548
+ : []
549
+ return data.computedLayoutData.map((series, seriesIndex) => {
550
+ return series.map((datum, groupIndex) => {
551
+ const _barStartY = accYArr[groupIndex] // 前一次的累加高度
552
+ let _barHeight = 0
553
+ if (datum.visible) {
554
+ _barHeight = datum.axisYFromZero * data.yRatio
555
+ accYArr[groupIndex] = accYArr[groupIndex] + _barHeight // 累加高度
556
+ }
557
+ return <GraphicDatum>{
558
+ ...datum,
559
+ _barStartY,
560
+ _barHeight
561
+ }
562
+ })
563
+ })
564
+ // return data.computedData.map(d => {
565
+ // let accY = data.zeroY
566
+ // return d.map(_d => {
567
+ // const _barStartY = accY
568
+ // const _barHeight = _d.axisYFromZero * data.yRatio
569
+ // accY = accY + _barHeight
570
+ // return <GraphicDatum>{
571
+ // ..._d,
572
+ // _barStartY,
573
+ // _barHeight
574
+ // }
575
+ // })
576
+ // })
577
+ })
578
+ )
579
+
580
+ const noneStackedData$ = combineLatest({
581
+ computedLayoutData: computedLayoutData$,
582
+ // yRatio: yRatio$,
583
+ zeroY: zeroY$
584
+ }).pipe(
585
+ takeUntil(destroy$),
586
+ map(data => {
587
+ return data.computedLayoutData.map((series, seriesIndex) => {
588
+ return series.map((datum, groupIndex) => {
589
+ return <GraphicDatum>{
590
+ ...datum,
591
+ _barStartY: data.zeroY,
592
+ _barHeight: datum.axisYFromZero
593
+ }
594
+ })
595
+ })
596
+ })
597
+ )
598
+
599
+ const graphicData$ = isSeriesSeprate$.pipe(
600
+ switchMap(isSeriesSeprate => {
601
+ return iif(() => isSeriesSeprate, noneStackedData$, stackedData$)
602
+ })
603
+ )
604
+
605
+ combineLatest({
606
+ defsSelection: defsSelection$,
607
+ gridAxesSize: gridAxesSize$,
608
+ }).pipe(
609
+ takeUntil(destroy$),
610
+ switchMap(async d => d)
611
+ ).subscribe(data => {
612
+ const clipPathData = [{
613
+ id: clipPathID,
614
+ width: data.gridAxesSize.width,
615
+ height: data.gridAxesSize.height
616
+ }]
617
+ renderClipPath({
618
+ defsSelection: data.defsSelection,
619
+ clipPathData
620
+ })
621
+ })
622
+
623
+ const highlightTarget$ = fullChartParams$.pipe(
624
+ takeUntil(destroy$),
625
+ map(d => d.highlightTarget),
626
+ distinctUntilChanged()
627
+ )
628
+
629
+ const barSelection$ = combineLatest({
630
+ graphicGSelection: graphicGSelection$,
631
+ graphicData: graphicData$,
632
+ zeroY: zeroY$,
633
+ groupLabels: groupLabels$,
634
+ // barScale: barScale$,
635
+ params: fullParams$,
636
+ chartParams: fullChartParams$,
637
+ highlightTarget: highlightTarget$,
638
+ barWidth: barWidth$,
639
+ transformedBarRadius: transformedBarRadius$,
640
+ delayGroup: delayGroup$,
641
+ transitionItem: transitionItem$,
642
+ isSeriesSeprate: isSeriesSeprate$
643
+ }).pipe(
644
+ takeUntil(destroy$),
645
+ switchMap(async (d) => d),
646
+ map(data => {
647
+ return renderRectBars({
648
+ graphicGSelection: data.graphicGSelection,
649
+ rectClassName,
650
+ barData: data.graphicData,
651
+ zeroY: data.zeroY,
652
+ groupLabels: data.groupLabels,
653
+ // barScale: data.barScale,
654
+ params: data.params,
655
+ chartParams: data.chartParams,
656
+ barWidth: data.barWidth,
657
+ transformedBarRadius: data.transformedBarRadius,
658
+ delayGroup: data.delayGroup,
659
+ transitionItem: data.transitionItem,
660
+ isSeriesSeprate: data.isSeriesSeprate
661
+ })
662
+ })
663
+ )
664
+
665
+ combineLatest({
666
+ barSelection: barSelection$,
667
+ computedData: computedData$,
668
+ highlightTarget: highlightTarget$,
669
+ SeriesDataMap: SeriesDataMap$,
670
+ GroupDataMap: GroupDataMap$,
671
+ }).subscribe(data => {
672
+
673
+ data.barSelection!
674
+ .on('mouseover', (event, datum) => {
675
+ event.stopPropagation()
676
+
677
+ event$.next({
678
+ type: 'grid',
679
+ eventName: 'mouseover',
680
+ pluginName,
681
+ highlightTarget: data.highlightTarget,
682
+ datum,
683
+ gridIndex: datum.gridIndex,
684
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
685
+ seriesIndex: datum.seriesIndex,
686
+ seriesLabel: datum.seriesLabel,
687
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
688
+ groupIndex: datum.groupIndex,
689
+ groupLabel: datum.groupLabel,
690
+ event,
691
+ data: data.computedData
692
+ })
693
+ })
694
+ .on('mousemove', (event, datum) => {
695
+ event.stopPropagation()
696
+
697
+ event$.next({
698
+ type: 'grid',
699
+ eventName: 'mousemove',
700
+ pluginName,
701
+ highlightTarget: data.highlightTarget,
702
+ datum,
703
+ gridIndex: datum.gridIndex,
704
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
705
+ seriesIndex: datum.seriesIndex,
706
+ seriesLabel: datum.seriesLabel,
707
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
708
+ groupIndex: datum.groupIndex,
709
+ groupLabel: datum.groupLabel,
710
+ event,
711
+ data: data.computedData
712
+ })
713
+ })
714
+ .on('mouseout', (event, datum) => {
715
+ event.stopPropagation()
716
+
717
+ event$.next({
718
+ type: 'grid',
719
+ eventName: 'mouseout',
720
+ pluginName,
721
+ highlightTarget: data.highlightTarget,
722
+ datum,
723
+ gridIndex: datum.gridIndex,
724
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
725
+ seriesIndex: datum.seriesIndex,
726
+ seriesLabel: datum.seriesLabel,
727
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
728
+ groupIndex: datum.groupIndex,
729
+ groupLabel: datum.groupLabel,
730
+ event,
731
+ data: data.computedData
732
+ })
733
+ })
734
+ .on('click', (event, datum) => {
735
+ event.stopPropagation()
736
+
737
+ event$.next({
738
+ type: 'grid',
739
+ eventName: 'click',
740
+ pluginName,
741
+ highlightTarget: data.highlightTarget,
742
+ datum,
743
+ gridIndex: datum.gridIndex,
744
+ series: data.SeriesDataMap.get(datum.seriesLabel)!,
745
+ seriesIndex: datum.seriesIndex,
746
+ seriesLabel: datum.seriesLabel,
747
+ groups: data.GroupDataMap.get(datum.groupLabel)!,
748
+ groupIndex: datum.groupIndex,
749
+ groupLabel: datum.groupLabel,
750
+ event,
751
+ data: data.computedData
752
+ })
753
+ })
754
+
755
+ })
756
+
757
+ // const datumList$ = computedData$.pipe(
758
+ // takeUntil(destroy$),
759
+ // map(d => d.flat())
760
+ // )
761
+ // const highlight$ = highlightObservable({ datumList$, chartParams$, event$: store.event$ })
762
+
763
+ combineLatest({
764
+ barSelection: barSelection$,
765
+ highlight: gridHighlight$.pipe(
766
+ map(data => data.map(d => d.id))
767
+ ),
768
+ fullChartParams: fullChartParams$
769
+ }).pipe(
770
+ takeUntil(destroy$),
771
+ switchMap(async d => d)
772
+ ).subscribe(data => {
773
+ highlight({
774
+ selection: data.barSelection,
775
+ ids: data.highlight,
776
+ fullChartParams: data.fullChartParams
777
+ })
778
+ })
779
+
780
+ return () => {
781
+ destroy$.next(undefined)
782
+ }
781
783
  }