@orbcharts/plugins-basic 3.0.0-beta.3 → 3.0.0-beta.4

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 (80) hide show
  1. package/LICENSE +200 -200
  2. package/lib/core-types.ts +7 -7
  3. package/lib/core.ts +6 -6
  4. package/lib/plugins-basic-types.ts +6 -6
  5. package/package.json +44 -44
  6. package/src/base/BaseBarStack.ts +780 -780
  7. package/src/base/BaseBars.ts +765 -765
  8. package/src/base/BaseBarsTriangle.ts +674 -674
  9. package/src/base/BaseDots.ts +515 -515
  10. package/src/base/BaseGroupAxis.ts +679 -679
  11. package/src/base/BaseLegend.ts +680 -680
  12. package/src/base/BaseLineAreas.ts +629 -629
  13. package/src/base/BaseLines.ts +706 -706
  14. package/src/base/BaseTooltip.ts +385 -385
  15. package/src/base/BaseValueAxis.ts +580 -580
  16. package/src/base/types.ts +2 -2
  17. package/src/const.ts +30 -30
  18. package/src/grid/defaults.ts +244 -244
  19. package/src/grid/gridObservables.ts +545 -545
  20. package/src/grid/index.ts +16 -16
  21. package/src/grid/plugins/BarStack.ts +64 -64
  22. package/src/grid/plugins/Bars.ts +69 -69
  23. package/src/grid/plugins/BarsPN.ts +66 -66
  24. package/src/grid/plugins/BarsTriangle.ts +73 -73
  25. package/src/grid/plugins/Dots.ts +68 -68
  26. package/src/grid/plugins/GridLegend.ts +107 -107
  27. package/src/grid/plugins/GridTooltip.ts +66 -66
  28. package/src/grid/plugins/GridZoom.ts +218 -218
  29. package/src/grid/plugins/GroupAux.ts +1098 -1098
  30. package/src/grid/plugins/GroupAxis.ts +97 -97
  31. package/src/grid/plugins/LineAreas.ts +65 -65
  32. package/src/grid/plugins/Lines.ts +59 -59
  33. package/src/grid/plugins/ValueAxis.ts +93 -93
  34. package/src/grid/plugins/ValueStackAxis.ts +95 -95
  35. package/src/index.ts +9 -9
  36. package/src/multiGrid/defaults.ts +224 -224
  37. package/src/multiGrid/index.ts +14 -14
  38. package/src/multiGrid/multiGridObservables.ts +49 -49
  39. package/src/multiGrid/plugins/MultiBarStack.ts +106 -106
  40. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  41. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  42. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  43. package/src/multiGrid/plugins/MultiGridLegend.ts +159 -159
  44. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  45. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  46. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  47. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  48. package/src/multiGrid/plugins/MultiValueAxis.ts +133 -133
  49. package/src/multiGrid/plugins/MultiValueStackAxis.ts +133 -133
  50. package/src/multiGrid/plugins/OverlappingValueAxes.ts +299 -299
  51. package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +298 -298
  52. package/src/noneData/defaults.ts +102 -102
  53. package/src/noneData/index.ts +3 -3
  54. package/src/noneData/plugins/Container.ts +27 -27
  55. package/src/noneData/plugins/Tooltip.ts +373 -373
  56. package/src/series/defaults.ts +206 -206
  57. package/src/series/index.ts +9 -9
  58. package/src/series/plugins/Bubbles.ts +603 -603
  59. package/src/series/plugins/Pie.ts +623 -623
  60. package/src/series/plugins/PieEventTexts.ts +283 -283
  61. package/src/series/plugins/PieLabels.ts +640 -640
  62. package/src/series/plugins/Rose.ts +516 -516
  63. package/src/series/plugins/RoseLabels.ts +600 -600
  64. package/src/series/plugins/SeriesLegend.ts +107 -107
  65. package/src/series/plugins/SeriesTooltip.ts +66 -66
  66. package/src/series/seriesObservables.ts +145 -145
  67. package/src/series/seriesUtils.ts +51 -51
  68. package/src/tree/defaults.ts +78 -78
  69. package/src/tree/index.ts +4 -4
  70. package/src/tree/plugins/TreeLegend.ts +107 -107
  71. package/src/tree/plugins/TreeMap.ts +333 -333
  72. package/src/tree/plugins/TreeTooltip.ts +66 -66
  73. package/src/utils/commonUtils.ts +21 -21
  74. package/src/utils/d3Graphics.ts +174 -174
  75. package/src/utils/d3Utils.ts +73 -73
  76. package/src/utils/observables.ts +14 -14
  77. package/src/utils/orbchartsUtils.ts +100 -100
  78. package/tsconfig.base.json +13 -13
  79. package/tsconfig.json +2 -2
  80. package/vite.config.js +22 -22
@@ -1,781 +1,781 @@
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
- GridContainerPosition,
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<GridContainerPosition[]>
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
+ GridContainerPosition,
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<GridContainerPosition[]>
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
+ }
781
781
  }