@orbcharts/plugins-basic 3.0.5 → 3.0.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 (116) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +3414 -3390
  3. package/dist/orbcharts-plugins-basic.umd.js +35 -35
  4. package/lib/core-types.ts +7 -7
  5. package/lib/core.ts +6 -6
  6. package/lib/gridObservables.ts +6 -6
  7. package/lib/plugins-basic-types.ts +6 -6
  8. package/package.json +48 -48
  9. package/src/base/BaseBars.ts +765 -765
  10. package/src/base/BaseBarsTriangle.ts +676 -676
  11. package/src/base/BaseDots.ts +464 -464
  12. package/src/base/BaseGroupAxis.ts +691 -691
  13. package/src/base/BaseLegend.ts +684 -684
  14. package/src/base/BaseLineAreas.ts +629 -629
  15. package/src/base/BaseLines.ts +706 -706
  16. package/src/base/BaseOrdinalBubbles.ts +729 -729
  17. package/src/base/BaseRacingBars.ts +582 -582
  18. package/src/base/BaseRacingLabels.ts +404 -404
  19. package/src/base/BaseRacingValueLabels.ts +403 -403
  20. package/src/base/BaseStackedBars.ts +782 -782
  21. package/src/base/BaseTooltip.ts +408 -386
  22. package/src/base/BaseValueAxis.ts +600 -600
  23. package/src/base/BaseXAxis.ts +427 -427
  24. package/src/base/BaseXZoom.ts +241 -241
  25. package/src/base/BaseYAxis.ts +389 -389
  26. package/src/base/types.ts +2 -2
  27. package/src/const.ts +30 -30
  28. package/src/grid/defaults.ts +213 -213
  29. package/src/grid/gridObservables.ts +635 -635
  30. package/src/grid/index.ts +16 -16
  31. package/src/grid/plugins/Bars.ts +69 -69
  32. package/src/grid/plugins/BarsPN.ts +66 -66
  33. package/src/grid/plugins/BarsTriangle.ts +73 -73
  34. package/src/grid/plugins/Dots.ts +68 -68
  35. package/src/grid/plugins/GridLegend.ts +107 -107
  36. package/src/grid/plugins/GridTooltip.ts +66 -66
  37. package/src/grid/plugins/GroupAux.ts +1095 -1095
  38. package/src/grid/plugins/GroupAxis.ts +73 -73
  39. package/src/grid/plugins/GroupZoom.ts +218 -218
  40. package/src/grid/plugins/LineAreas.ts +65 -65
  41. package/src/grid/plugins/Lines.ts +59 -59
  42. package/src/grid/plugins/StackedBars.ts +64 -64
  43. package/src/grid/plugins/StackedValueAxis.ts +96 -96
  44. package/src/grid/plugins/ValueAxis.ts +94 -94
  45. package/src/index.ts +6 -6
  46. package/src/multiGrid/defaults.ts +244 -244
  47. package/src/multiGrid/index.ts +14 -14
  48. package/src/multiGrid/multiGridObservables.ts +50 -50
  49. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  50. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  51. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  52. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  53. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  54. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  55. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  56. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  57. package/src/multiGrid/plugins/MultiStackedBars.ts +106 -106
  58. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +134 -134
  59. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  60. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  61. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  62. package/src/multiValue/defaults.ts +523 -523
  63. package/src/multiValue/index.ts +16 -16
  64. package/src/multiValue/multiValueObservables.ts +781 -781
  65. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  66. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  67. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  68. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  69. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  70. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  71. package/src/multiValue/plugins/RacingBars.ts +375 -375
  72. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  73. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  74. package/src/multiValue/plugins/Scatter.ts +486 -486
  75. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  76. package/src/multiValue/plugins/XAxis.ts +107 -107
  77. package/src/multiValue/plugins/XYAux.ts +683 -683
  78. package/src/multiValue/plugins/XYAxes.ts +194 -194
  79. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  80. package/src/multiValue/plugins/XZoom.ts +40 -40
  81. package/src/noneData/defaults.ts +102 -102
  82. package/src/noneData/index.ts +3 -3
  83. package/src/noneData/plugins/Container.ts +27 -27
  84. package/src/noneData/plugins/Tooltip.ts +373 -373
  85. package/src/relationship/defaults.ts +221 -221
  86. package/src/relationship/index.ts +5 -5
  87. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  88. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  89. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  90. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  91. package/src/relationship/relationshipObservables.ts +49 -49
  92. package/src/series/defaults.ts +223 -222
  93. package/src/series/index.ts +9 -9
  94. package/src/series/plugins/Bubbles.ts +784 -766
  95. package/src/series/plugins/Pie.ts +622 -622
  96. package/src/series/plugins/PieEventTexts.ts +283 -283
  97. package/src/series/plugins/PieLabels.ts +639 -639
  98. package/src/series/plugins/Rose.ts +515 -515
  99. package/src/series/plugins/RoseLabels.ts +599 -599
  100. package/src/series/plugins/SeriesLegend.ts +107 -107
  101. package/src/series/plugins/SeriesTooltip.ts +66 -66
  102. package/src/series/seriesObservables.ts +168 -168
  103. package/src/series/seriesUtils.ts +51 -51
  104. package/src/tree/defaults.ts +102 -102
  105. package/src/tree/index.ts +4 -4
  106. package/src/tree/plugins/TreeLegend.ts +100 -100
  107. package/src/tree/plugins/TreeMap.ts +341 -341
  108. package/src/tree/plugins/TreeTooltip.ts +66 -66
  109. package/src/utils/commonUtils.ts +31 -31
  110. package/src/utils/d3Graphics.ts +176 -176
  111. package/src/utils/d3Utils.ts +92 -92
  112. package/src/utils/observables.ts +14 -14
  113. package/src/utils/orbchartsUtils.ts +129 -129
  114. package/tsconfig.base.json +13 -13
  115. package/tsconfig.json +2 -2
  116. package/vite.config.js +22 -22
@@ -1,783 +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
- ComputedAxesDataGrid,
17
- DataFormatterGrid,
18
- EventGrid,
19
- ChartParams,
20
- ContainerPositionScaled,
21
- Layout,
22
- TransformData } from '../../lib/core-types'
23
- import type { BaseStackedBarsParams } 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 BaseStackedBarsParams {
30
- // barWidth: number
31
- // barGroupPadding: number
32
- // barRadius: number | boolean
33
- // }
34
-
35
- interface BaseStackedBarsContext {
36
- selection: d3.Selection<any, unknown, any, unknown>
37
- computedData$: Observable<ComputedDataGrid>
38
- computedAxesData$: Observable<ComputedAxesDataGrid>
39
- visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
- visibleComputedAxesData$: Observable<ComputedAxesDataGrid>
41
- seriesLabels$: Observable<string[]>
42
- SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
- GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
- fullParams$: Observable<BaseStackedBarsParams>
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: BaseStackedBarsParams
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 = 'StackedBars'
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: StackedBarsParams) {
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 createBaseStackedBars: BasePluginFn<BaseStackedBarsContext> = (pluginName: string, {
296
- selection,
297
- computedData$,
298
- computedAxesData$,
299
- visibleComputedData$,
300
- visibleComputedAxesData$,
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$ = visibleComputedAxesData$.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.groupAxis.scaleDomain[0] === 'auto'
492
- // ? groupMin // - data.dataFormatter.groupAxis.scalePadding
493
- // : data.dataFormatter.groupAxis.scaleDomain[0] as number // - data.dataFormatter.groupAxis.scalePadding
494
- const groupScaleDomainMin = data.dataFormatter.groupAxis.scaleDomain[0]
495
- const groupScaleDomainMax = data.dataFormatter.groupAxis.scaleDomain[1] === 'max'
496
- ? groupMax // + data.dataFormatter.groupAxis.scalePadding
497
- : data.dataFormatter.groupAxis.scaleDomain[1] as number // + data.dataFormatter.groupAxis.scalePadding
498
-
499
- return [groupScaleDomainMin, groupScaleDomainMax]
500
- })
501
- )
502
-
503
- // 堆疊後的高度對應圖軸的比例(最大值/最大值所在group的總和)
504
- const yRatio$ = combineLatest({
505
- visibleComputedAxesData: visibleComputedAxesData$,
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.visibleComputedAxesData
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
- computedAxesData: computedAxesData$,
541
- yRatio: yRatio$,
542
- zeroY: zeroY$
543
- }).pipe(
544
- takeUntil(destroy$),
545
- map(data => {
546
- let accYArr: number[] = data.computedAxesData[0]
547
- ? data.computedAxesData[0].map(() => data.zeroY)
548
- : []
549
- return data.computedAxesData.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
- computedAxesData: computedAxesData$,
582
- // yRatio: yRatio$,
583
- zeroY: zeroY$
584
- }).pipe(
585
- takeUntil(destroy$),
586
- map(data => {
587
- return data.computedAxesData.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
- group: 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
- group: 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
- group: 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
- group: 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
- }
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
+ ComputedAxesDataGrid,
17
+ DataFormatterGrid,
18
+ EventGrid,
19
+ ChartParams,
20
+ ContainerPositionScaled,
21
+ Layout,
22
+ TransformData } from '../../lib/core-types'
23
+ import type { BaseStackedBarsParams } 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 BaseStackedBarsParams {
30
+ // barWidth: number
31
+ // barGroupPadding: number
32
+ // barRadius: number | boolean
33
+ // }
34
+
35
+ interface BaseStackedBarsContext {
36
+ selection: d3.Selection<any, unknown, any, unknown>
37
+ computedData$: Observable<ComputedDataGrid>
38
+ computedAxesData$: Observable<ComputedAxesDataGrid>
39
+ visibleComputedData$: Observable<ComputedDatumGrid[][]>
40
+ visibleComputedAxesData$: Observable<ComputedAxesDataGrid>
41
+ seriesLabels$: Observable<string[]>
42
+ SeriesDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
43
+ GroupDataMap$: Observable<Map<string, ComputedDatumGrid[]>>
44
+ fullParams$: Observable<BaseStackedBarsParams>
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: BaseStackedBarsParams
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 = 'StackedBars'
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: StackedBarsParams) {
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 createBaseStackedBars: BasePluginFn<BaseStackedBarsContext> = (pluginName: string, {
296
+ selection,
297
+ computedData$,
298
+ computedAxesData$,
299
+ visibleComputedData$,
300
+ visibleComputedAxesData$,
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$ = visibleComputedAxesData$.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.groupAxis.scaleDomain[0] === 'auto'
492
+ // ? groupMin // - data.dataFormatter.groupAxis.scalePadding
493
+ // : data.dataFormatter.groupAxis.scaleDomain[0] as number // - data.dataFormatter.groupAxis.scalePadding
494
+ const groupScaleDomainMin = data.dataFormatter.groupAxis.scaleDomain[0]
495
+ const groupScaleDomainMax = data.dataFormatter.groupAxis.scaleDomain[1] === 'max'
496
+ ? groupMax // + data.dataFormatter.groupAxis.scalePadding
497
+ : data.dataFormatter.groupAxis.scaleDomain[1] as number // + data.dataFormatter.groupAxis.scalePadding
498
+
499
+ return [groupScaleDomainMin, groupScaleDomainMax]
500
+ })
501
+ )
502
+
503
+ // 堆疊後的高度對應圖軸的比例(最大值/最大值所在group的總和)
504
+ const yRatio$ = combineLatest({
505
+ visibleComputedAxesData: visibleComputedAxesData$,
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.visibleComputedAxesData
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
+ computedAxesData: computedAxesData$,
541
+ yRatio: yRatio$,
542
+ zeroY: zeroY$
543
+ }).pipe(
544
+ takeUntil(destroy$),
545
+ map(data => {
546
+ let accYArr: number[] = data.computedAxesData[0]
547
+ ? data.computedAxesData[0].map(() => data.zeroY)
548
+ : []
549
+ return data.computedAxesData.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
+ computedAxesData: computedAxesData$,
582
+ // yRatio: yRatio$,
583
+ zeroY: zeroY$
584
+ }).pipe(
585
+ takeUntil(destroy$),
586
+ map(data => {
587
+ return data.computedAxesData.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
+ group: 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
+ group: 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
+ group: 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
+ group: 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
+ }
783
783
  }