@orbcharts/core 3.0.0-alpha.67 → 3.0.0-beta.1

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 (66) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +1337 -1328
  3. package/dist/orbcharts-core.umd.js +3 -3
  4. package/lib/core-types.ts +7 -7
  5. package/package.json +42 -42
  6. package/src/AbstractChart.ts +57 -57
  7. package/src/GridChart.ts +24 -24
  8. package/src/MultiGridChart.ts +24 -24
  9. package/src/MultiValueChart.ts +24 -24
  10. package/src/RelationshipChart.ts +24 -24
  11. package/src/SeriesChart.ts +24 -24
  12. package/src/TreeChart.ts +24 -24
  13. package/src/base/createBaseChart.ts +505 -500
  14. package/src/base/createBasePlugin.ts +153 -152
  15. package/src/base/validators/chartOptionsValidator.ts +23 -23
  16. package/src/base/validators/chartParamsValidator.ts +133 -133
  17. package/src/base/validators/elementValidator.ts +13 -13
  18. package/src/base/validators/pluginsValidator.ts +14 -14
  19. package/src/defaults.ts +232 -232
  20. package/src/defineGridPlugin.ts +3 -3
  21. package/src/defineMultiGridPlugin.ts +3 -3
  22. package/src/defineMultiValuePlugin.ts +3 -3
  23. package/src/defineNoneDataPlugin.ts +4 -4
  24. package/src/defineRelationshipPlugin.ts +3 -3
  25. package/src/defineSeriesPlugin.ts +3 -3
  26. package/src/defineTreePlugin.ts +3 -3
  27. package/src/grid/computedDataFn.ts +129 -129
  28. package/src/grid/contextObserverCallback.ts +155 -155
  29. package/src/grid/dataFormatterValidator.ts +101 -101
  30. package/src/grid/dataValidator.ts +12 -12
  31. package/src/index.ts +20 -20
  32. package/src/multiGrid/computedDataFn.ts +123 -123
  33. package/src/multiGrid/contextObserverCallback.ts +41 -41
  34. package/src/multiGrid/dataFormatterValidator.ts +115 -115
  35. package/src/multiGrid/dataValidator.ts +12 -12
  36. package/src/multiValue/computedDataFn.ts +176 -176
  37. package/src/multiValue/contextObserverCallback.ts +12 -12
  38. package/src/multiValue/dataFormatterValidator.ts +9 -9
  39. package/src/multiValue/dataValidator.ts +9 -9
  40. package/src/relationship/computedDataFn.ts +125 -125
  41. package/src/relationship/contextObserverCallback.ts +12 -12
  42. package/src/relationship/dataFormatterValidator.ts +9 -9
  43. package/src/relationship/dataValidator.ts +9 -9
  44. package/src/series/computedDataFn.ts +88 -88
  45. package/src/series/contextObserverCallback.ts +100 -100
  46. package/src/series/dataFormatterValidator.ts +41 -41
  47. package/src/series/dataValidator.ts +12 -12
  48. package/src/tree/computedDataFn.ts +130 -130
  49. package/src/tree/contextObserverCallback.ts +61 -61
  50. package/src/tree/dataFormatterValidator.ts +13 -13
  51. package/src/tree/dataValidator.ts +13 -13
  52. package/src/utils/commonUtils.ts +55 -55
  53. package/src/utils/d3Utils.ts +108 -108
  54. package/src/utils/errorMessage.ts +42 -42
  55. package/src/utils/gridObservables.ts +611 -611
  56. package/src/utils/index.ts +9 -9
  57. package/src/utils/multiGridObservables.ts +366 -366
  58. package/src/utils/observables.ts +219 -219
  59. package/src/utils/orbchartsUtils.ts +352 -352
  60. package/src/utils/seriesObservables.ts +175 -175
  61. package/src/utils/treeObservables.ts +94 -94
  62. package/src/utils/validator.ts +125 -125
  63. package/tsconfig.base.json +13 -13
  64. package/tsconfig.json +2 -2
  65. package/vite-env.d.ts +6 -6
  66. package/vite.config.js +22 -22
@@ -1,612 +1,612 @@
1
- import {
2
- combineLatest,
3
- distinctUntilChanged,
4
- iif,
5
- filter,
6
- map,
7
- merge,
8
- takeUntil,
9
- shareReplay,
10
- switchMap,
11
- Subject,
12
- Observable } from 'rxjs'
13
- import type {
14
- AxisPosition,
15
- ChartType,
16
- ChartParams,
17
- ComputedDataTypeMap,
18
- ComputedDatumTypeMap,
19
- ComputedDataGrid,
20
- DataTypeMap,
21
- DataGridDatum,
22
- ComputedDatumGrid,
23
- DataFormatterTypeMap,
24
- DataFormatterGrid,
25
- DataFormatterValueAxis,
26
- DataFormatterGroupAxis,
27
- ComputedLayoutDatumGrid,
28
- ComputedLayoutDataGrid,
29
- GridContainerPosition,
30
- HighlightTarget,
31
- Layout,
32
- TransformData } from '../../lib/core-types'
33
- import { getMinAndMaxGrid } from './orbchartsUtils'
34
- import { createAxisLinearScale, createAxisPointScale, createAxisQuantizeScale } from './d3Utils'
35
- import { calcGridContainerLayout } from './orbchartsUtils'
36
- import { getMinAndMaxValue } from './orbchartsUtils'
37
-
38
- export const gridComputedLayoutDataObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
39
- computedData$: Observable<ComputedDataTypeMap<'grid'>>
40
- fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
41
- layout$: Observable<Layout>
42
- }): Observable<ComputedLayoutDatumGrid[][]> => {
43
-
44
- // 未篩選group範圍前的group scale
45
- function createOriginGroupScale (computedData: ComputedDatumGrid[][], dataFormatter: DataFormatterGrid, layout: Layout) {
46
- const groupAxisWidth = (dataFormatter.grid.groupAxis.position === 'top' || dataFormatter.grid.groupAxis.position === 'bottom')
47
- ? layout.width
48
- : layout.height
49
- const groupEndIndex = computedData[0] ? computedData[0].length - 1 : 0
50
- const groupScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
51
- maxValue: groupEndIndex,
52
- minValue: 0,
53
- axisWidth: groupAxisWidth,
54
- scaleDomain: [0, groupEndIndex], // 不使用dataFormatter設定
55
- scaleRange: [0, 1] // 不使用dataFormatter設定
56
- })
57
- return groupScale
58
- }
59
-
60
- // 未篩選group範圍及visible前的value scale
61
- function createOriginValueScale (computedData: ComputedDatumGrid[][], dataFormatter: DataFormatterGrid, layout: Layout) {
62
- const valueAxisWidth = (dataFormatter.grid.valueAxis.position === 'left' || dataFormatter.grid.valueAxis.position === 'right')
63
- ? layout.height
64
- : layout.width
65
-
66
- const listData = computedData.flat()
67
- const [minValue, maxValue] = getMinAndMaxValue(listData)
68
-
69
- const valueScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
70
- maxValue,
71
- minValue,
72
- axisWidth: valueAxisWidth,
73
- scaleDomain: [minValue, maxValue], // 不使用dataFormatter設定
74
- scaleRange: [0, 1] // 不使用dataFormatter設定
75
- })
76
-
77
- return valueScale
78
- }
79
-
80
- return combineLatest({
81
- computedData: computedData$,
82
- fullDataFormatter: fullDataFormatter$,
83
- layout: layout$
84
- }).pipe(
85
- switchMap(async d => d),
86
- map(data => {
87
- const groupScale = createOriginGroupScale(data.computedData, data.fullDataFormatter, data.layout)
88
- const valueScale = createOriginValueScale(data.computedData, data.fullDataFormatter, data.layout)
89
- const zeroY = valueScale(0)
90
-
91
- return data.computedData.map((seriesData, seriesIndex) => {
92
- return seriesData.map((groupDatum, groupIndex) => {
93
- const axisX = groupScale(groupIndex)
94
- const axisY = valueScale(groupDatum.value ?? 0)
95
- return {
96
- ...groupDatum,
97
- axisX,
98
- axisY,
99
- axisYFromZero: axisY - zeroY
100
- }
101
- })
102
- })
103
- })
104
- )
105
- }
106
-
107
- export const gridAxesTransformObservable = ({ fullDataFormatter$, layout$ }: {
108
- fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
109
- layout$: Observable<Layout>
110
- }): Observable<TransformData> => {
111
- const destroy$ = new Subject()
112
-
113
- function calcAxesTransform ({ xAxis, yAxis, width, height }: {
114
- xAxis: DataFormatterGroupAxis | DataFormatterValueAxis,
115
- yAxis: DataFormatterValueAxis,
116
- width: number,
117
- height: number
118
- }): TransformData {
119
- if (!xAxis || !yAxis) {
120
- return {
121
- translate: [0, 0],
122
- scale: [1, 1],
123
- rotate: 0,
124
- rotateX: 0,
125
- rotateY: 0,
126
- value: ''
127
- }
128
- }
129
- // const width = size.width - fullChartParams.layout.left - fullChartParams.layout.right
130
- // const height = size.height - fullChartParams.layout.top - fullChartParams.layout.bottom
131
- let translateX = 0
132
- let translateY = 0
133
- let rotate = 0
134
- let rotateX = 0
135
- let rotateY = 0
136
- if (xAxis.position === 'bottom') {
137
- if (yAxis.position === 'left') {
138
- rotateX = 180
139
- translateY = height
140
- } else if (yAxis.position === 'right') {
141
- rotateX = 180
142
- rotateY = 180
143
- translateX = width
144
- translateY = height
145
- } else {
146
- // 預設
147
- rotateX = 180
148
- translateY = height
149
- }
150
- } else if (xAxis.position === 'top') {
151
- if (yAxis.position === 'left') {
152
- } else if (yAxis.position === 'right') {
153
- rotateY = 180
154
- translateX = width
155
- } else {
156
- // 預設
157
- rotateX = 180
158
- translateY = height
159
- }
160
- } else if (xAxis.position === 'left') {
161
- if (yAxis.position === 'bottom') {
162
- rotate = -90
163
- translateY = height
164
- } else if (yAxis.position === 'top') {
165
- rotate = -90
166
- rotateY = 180
167
- } else {
168
- // 預設
169
- rotateX = 180
170
- translateY = height
171
- }
172
- } else if (xAxis.position === 'right') {
173
- if (yAxis.position === 'bottom') {
174
- rotate = -90
175
- rotateX = 180
176
- translateY = height
177
- translateX = width
178
- } else if (yAxis.position === 'top') {
179
- rotate = -90
180
- rotateX = 180
181
- rotateY = 180
182
- translateX = width
183
- } else {
184
- // 預設
185
- rotateX = 180
186
- translateY = height
187
- }
188
- } else {
189
- // 預設
190
- rotateX = 180
191
- translateY = height
192
- }
193
- // selection.style('transform', `translate(${translateX}px, ${translateY}px) rotate(${rotate}deg) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`)
194
-
195
- return {
196
- translate: [translateX, translateY],
197
- scale: [1, 1],
198
- rotate,
199
- rotateX,
200
- rotateY,
201
- value: `translate(${translateX}px, ${translateY}px) rotate(${rotate}deg) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`
202
- }
203
- }
204
-
205
- return new Observable(subscriber => {
206
- combineLatest({
207
- fullDataFormatter: fullDataFormatter$,
208
- layout: layout$
209
- }).pipe(
210
- takeUntil(destroy$),
211
- switchMap(async (d) => d),
212
- ).subscribe(data => {
213
- const axesTransformData = calcAxesTransform({
214
- xAxis: data.fullDataFormatter.grid.groupAxis,
215
- yAxis: data.fullDataFormatter.grid.valueAxis,
216
- width: data.layout.width,
217
- height: data.layout.height
218
- })
219
-
220
- subscriber.next(axesTransformData)
221
- })
222
-
223
- return function unscbscribe () {
224
- destroy$.next(undefined)
225
- }
226
- })
227
- }
228
-
229
-
230
- export const gridAxesReverseTransformObservable = ({ gridAxesTransform$ }: {
231
- gridAxesTransform$: Observable<TransformData>
232
- }): Observable<TransformData> => {
233
- return gridAxesTransform$.pipe(
234
- map(d => {
235
- // const translate: [number, number] = [d.translate[0] * -1, d.translate[1] * -1]
236
- const translate: [number, number] = [0, 0] // 無需逆轉
237
- const scale: [number, number] = [1 / d.scale[0], 1 / d.scale[1]]
238
- const rotate = d.rotate * -1
239
- const rotateX = d.rotateX * -1
240
- const rotateY = d.rotateY * -1
241
- return {
242
- translate,
243
- scale,
244
- rotate,
245
- rotateX,
246
- rotateY,
247
- value: `translate(${translate[0]}px, ${translate[1]}px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) rotate(${rotate}deg)`
248
- }
249
- }),
250
- )
251
- }
252
-
253
- export const gridGraphicTransformObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
254
- computedData$: Observable<ComputedDataTypeMap<'grid'>>
255
- fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
256
- layout$: Observable<Layout>
257
- }): Observable<TransformData> => {
258
- const destroy$ = new Subject()
259
-
260
- function calcGridDataAreaTransform ({ data, groupAxis, valueAxis, width, height }: {
261
- data: ComputedDataTypeMap<'grid'>
262
- groupAxis: DataFormatterGroupAxis
263
- valueAxis: DataFormatterValueAxis
264
- width: number
265
- height: number
266
- }): TransformData {
267
- let translateX = 0
268
- let translateY = 0
269
- let scaleX = 0
270
- let scaleY = 0
271
-
272
- // -- groupScale --
273
- const groupAxisWidth = (groupAxis.position === 'top' || groupAxis.position === 'bottom')
274
- ? width
275
- : height
276
- const groupMin = 0
277
- const groupMax = data[0] ? data[0].length - 1 : 0
278
- // const groupScaleDomainMin = groupAxis.scaleDomain[0] === 'min'
279
- // ? groupMin - groupAxis.scalePadding
280
- // : groupAxis.scaleDomain[0] as number - groupAxis.scalePadding
281
- const groupScaleDomainMin = groupAxis.scaleDomain[0] - groupAxis.scalePadding
282
- const groupScaleDomainMax = groupAxis.scaleDomain[1] === 'max'
283
- ? groupMax + groupAxis.scalePadding
284
- : groupAxis.scaleDomain[1] as number + groupAxis.scalePadding
285
-
286
- const groupScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
287
- maxValue: groupMax,
288
- minValue: groupMin,
289
- axisWidth: groupAxisWidth,
290
- // scaleDomain: groupAxis.scaleDomain,
291
- scaleDomain: [groupScaleDomainMin, groupScaleDomainMax],
292
- scaleRange: [0, 1]
293
- })
294
-
295
- // -- translateX, scaleX --
296
- const rangeMinX = groupScale(groupMin)
297
- const rangeMaxX = groupScale(groupMax)
298
- if (groupMin == groupMax) {
299
- // 當group只有一個
300
- translateX = 0
301
- scaleX = 1
302
- } else {
303
- translateX = rangeMinX
304
- const gWidth = rangeMaxX - rangeMinX
305
- scaleX = gWidth / groupAxisWidth
306
- }
307
-
308
- // -- valueScale --
309
- const filteredData = data.map((d, i) => {
310
- return d.filter((_d, _i) => {
311
- return _i >= groupScaleDomainMin && _i <= groupScaleDomainMax && _d.visible == true
312
- })
313
- })
314
-
315
- const filteredMinAndMax = getMinAndMaxGrid(filteredData)
316
- if (filteredMinAndMax[0] === filteredMinAndMax[1]) {
317
- filteredMinAndMax[0] = filteredMinAndMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
318
- }
319
-
320
- const valueAxisWidth = (valueAxis.position === 'left' || valueAxis.position === 'right')
321
- ? height
322
- : width
323
-
324
- const valueScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
325
- maxValue: filteredMinAndMax[1],
326
- minValue: filteredMinAndMax[0],
327
- axisWidth: valueAxisWidth,
328
- scaleDomain: valueAxis.scaleDomain,
329
- scaleRange: valueAxis.scaleRange
330
- })
331
-
332
- // -- translateY, scaleY --
333
- const minAndMax = getMinAndMaxGrid(data)
334
- if (minAndMax[0] === minAndMax[1]) {
335
- minAndMax[0] = minAndMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
336
- }
337
- const rangeMinY = valueScale(minAndMax[0])
338
- const rangeMaxY = valueScale(minAndMax[1])
339
- translateY = rangeMinY
340
- const gHeight = rangeMaxY - rangeMinY
341
- scaleY = gHeight / valueAxisWidth
342
-
343
- return {
344
- translate: [translateX, translateY],
345
- scale: [scaleX, scaleY],
346
- rotate: 0,
347
- rotateX: 0,
348
- rotateY: 0,
349
- value: `translate(${translateX}px, ${translateY}px) scale(${scaleX}, ${scaleY})`
350
- }
351
- }
352
-
353
- return new Observable(subscriber => {
354
- combineLatest({
355
- computedData: computedData$,
356
- fullDataFormatter: fullDataFormatter$,
357
- layout: layout$
358
- }).pipe(
359
- takeUntil(destroy$),
360
- switchMap(async (d) => d),
361
- ).subscribe(data => {
362
- const dataAreaTransformData = calcGridDataAreaTransform ({
363
- data: data.computedData,
364
- groupAxis: data.fullDataFormatter.grid.groupAxis,
365
- valueAxis: data.fullDataFormatter.grid.valueAxis,
366
- width: data.layout.width,
367
- height: data.layout.height
368
- })
369
-
370
- subscriber.next(dataAreaTransformData)
371
- })
372
-
373
- return function unscbscribe () {
374
- destroy$.next(undefined)
375
- }
376
- })
377
- }
378
-
379
- export const gridGraphicReverseScaleObservable = ({ gridContainerPosition$, gridAxesTransform$, gridGraphicTransform$ }: {
380
- gridContainerPosition$: Observable<GridContainerPosition[]>
381
- gridAxesTransform$: Observable<TransformData>
382
- gridGraphicTransform$: Observable<TransformData>
383
- }): Observable<[number, number][]> => {
384
- return combineLatest({
385
- gridContainerPosition: gridContainerPosition$,
386
- gridAxesTransform: gridAxesTransform$,
387
- gridGraphicTransform: gridGraphicTransform$,
388
- }).pipe(
389
- switchMap(async (d) => d),
390
- map(data => {
391
- if (data.gridAxesTransform.rotate == 0 || data.gridAxesTransform.rotate == 180) {
392
- return data.gridContainerPosition.map((series, seriesIndex) => {
393
- return [
394
- 1 / data.gridGraphicTransform.scale[0] / data.gridContainerPosition[seriesIndex].scale[0],
395
- 1 / data.gridGraphicTransform.scale[1] / data.gridContainerPosition[seriesIndex].scale[1],
396
- ]
397
- })
398
- } else {
399
- return data.gridContainerPosition.map((series, seriesIndex) => {
400
- // 由於有垂直的旋轉,所以外層 (container) x和y的scale要互換
401
- return [
402
- 1 / data.gridGraphicTransform.scale[0] / data.gridContainerPosition[seriesIndex].scale[1],
403
- 1 / data.gridGraphicTransform.scale[1] / data.gridContainerPosition[seriesIndex].scale[0],
404
- ]
405
- })
406
- }
407
- }),
408
- )
409
- }
410
-
411
- export const gridAxesSizeObservable = ({ fullDataFormatter$, layout$ }: {
412
- fullDataFormatter$: Observable<DataFormatterGrid>
413
- layout$: Observable<Layout>
414
- }): Observable<{
415
- width: number;
416
- height: number;
417
- }> => {
418
- const destroy$ = new Subject()
419
-
420
- function calcAxesSize ({ xAxisPosition, yAxisPosition, width, height }: {
421
- xAxisPosition: AxisPosition
422
- yAxisPosition: AxisPosition
423
- width: number
424
- height: number
425
- }) {
426
- if ((xAxisPosition === 'bottom' || xAxisPosition === 'top') && (yAxisPosition === 'left' || yAxisPosition === 'right')) {
427
- return { width, height }
428
- } else if ((xAxisPosition === 'left' || xAxisPosition === 'right') && (yAxisPosition === 'bottom' || yAxisPosition === 'top')) {
429
- return {
430
- width: height,
431
- height: width
432
- }
433
- } else {
434
- // default
435
- return { width, height }
436
- }
437
- }
438
-
439
- return new Observable(subscriber => {
440
- combineLatest({
441
- fullDataFormatter: fullDataFormatter$,
442
- layout: layout$
443
- }).pipe(
444
- takeUntil(destroy$),
445
- switchMap(async (d) => d),
446
- ).subscribe(data => {
447
-
448
- const axisSize = calcAxesSize({
449
- xAxisPosition: data.fullDataFormatter.grid.groupAxis.position,
450
- yAxisPosition: data.fullDataFormatter.grid.valueAxis.position,
451
- width: data.layout.width,
452
- height: data.layout.height,
453
- })
454
-
455
- subscriber.next(axisSize)
456
-
457
- return function unsubscribe () {
458
- destroy$.next(undefined)
459
- }
460
- })
461
- })
462
- }
463
-
464
- // export const gridHighlightObservable = ({ computedData$, fullChartParams$, event$ }: {
465
- // computedData$: Observable<ComputedDataTypeMap<'grid'>>
466
- // fullChartParams$: Observable<ChartParams>
467
- // event$: Subject<any>
468
- // }): Observable<string[]> => {
469
- // const datumList$ = computedData$.pipe(
470
- // map(d => d.flat())
471
- // )
472
- // return highlightObservable ({ datumList$, fullChartParams$, event$ })
473
- // }
474
-
475
- export const gridSeriesLabelsObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'grid'>> }) => {
476
- return computedData$.pipe(
477
- map(data => {
478
- return data
479
- .filter(series => series.length)
480
- .map(series => {
481
- return series[0].seriesLabel
482
- })
483
- }),
484
- distinctUntilChanged((a, b) => {
485
- return JSON.stringify(a).length === JSON.stringify(b).length
486
- }),
487
- )
488
- }
489
-
490
- export const gridVisibleComputedDataObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'grid'>> }) => {
491
- return computedData$.pipe(
492
- map(data => {
493
- const visibleComputedData = data
494
- .map(d => {
495
- return d.filter(_d => {
496
- return _d.visible == true
497
- })
498
- })
499
- .filter(d => d.length)
500
- return visibleComputedData
501
- })
502
- )
503
- }
504
-
505
- export const gridVisibleComputedLayoutDataObservable = ({ computedLayoutData$ }: { computedLayoutData$: Observable<ComputedLayoutDataGrid> }) => {
506
- return computedLayoutData$.pipe(
507
- map(data => {
508
- const visibleComputedData = data
509
- .map(d => {
510
- return d.filter(_d => {
511
- return _d.visible == true
512
- })
513
- })
514
- .filter(d => d.length)
515
- return visibleComputedData
516
- })
517
- )
518
- }
519
-
520
- // 所有container位置(對應series)
521
- export const gridContainerPositionObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
522
- computedData$: Observable<ComputedDataTypeMap<'grid'>>
523
- fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
524
- layout$: Observable<Layout>
525
- }): Observable<GridContainerPosition[]> => {
526
-
527
- const gridContainerPosition$ = combineLatest({
528
- computedData: computedData$,
529
- fullDataFormatter: fullDataFormatter$,
530
- layout: layout$,
531
- }).pipe(
532
- switchMap(async (d) => d),
533
- map(data => {
534
-
535
- if (data.fullDataFormatter.grid.separateSeries) {
536
- // -- 依slotIndexes計算 --
537
- return calcGridContainerLayout(data.layout, data.fullDataFormatter.container, data.computedData.length)
538
- // return data.computedData.map((seriesData, seriesIndex) => {
539
- // const columnIndex = seriesIndex % data.fullDataFormatter.container.columnAmount
540
- // const rowIndex = Math.floor(seriesIndex / data.fullDataFormatter.container.columnAmount)
541
- // const { translate, scale } = calcGridContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
542
- // return {
543
- // slotIndex: seriesIndex,
544
- // rowIndex,
545
- // columnIndex,
546
- // translate,
547
- // scale,
548
- // }
549
- // })
550
- } else {
551
- // -- 無拆分 --
552
- const gridContainerPositionArr = calcGridContainerLayout(data.layout, data.fullDataFormatter.container, 1)
553
- return data.computedData.map((d, i) => gridContainerPositionArr[0]) // 每個series相同位置
554
- // const columnIndex = 0
555
- // const rowIndex = 0
556
- // return data.computedData.map((seriesData, seriesIndex) => {
557
- // const { translate, scale } = calcGridContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
558
- // return {
559
- // slotIndex: 0,
560
- // rowIndex,
561
- // columnIndex,
562
- // translate,
563
- // scale,
564
- // }
565
- // })
566
- }
567
- })
568
- )
569
-
570
- return gridContainerPosition$
571
- }
572
-
573
- // 將原本的value全部替換成加總後的value
574
- export const computedStackedDataObservables = ({ isSeriesSeprate$, computedData$ }: {
575
- isSeriesSeprate$: Observable<boolean>
576
- computedData$: Observable<ComputedDataGrid>
577
- }): Observable<ComputedDataGrid> => {
578
- const stackedData$: Observable<ComputedDataGrid> = computedData$.pipe(
579
- map(data => {
580
- // 將同一group的value加總起來
581
- const stackedValue = new Array(data[0] ? data[0].length : 0)
582
- .fill(null)
583
- .map((_, i) => {
584
- return data.reduce((prev, current) => {
585
- if (current && current[i]) {
586
- const currentValue = current[i].value == null || current[i].visible == false
587
- ? 0
588
- : current[i].value!
589
- return prev + currentValue
590
- }
591
- return prev
592
- }, 0)
593
- })
594
- // 將原本的value全部替換成加總後的value
595
- const computedData = data.map((series, seriesIndex) => {
596
- return series.map((d, i) => {
597
- return {
598
- ...d,
599
- value: stackedValue[i],
600
- }
601
- })
602
- })
603
- return computedData
604
- }),
605
- )
606
-
607
- return isSeriesSeprate$.pipe(
608
- switchMap(isSeriesSeprate => {
609
- return iif(() => isSeriesSeprate, computedData$, stackedData$)
610
- })
611
- )
1
+ import {
2
+ combineLatest,
3
+ distinctUntilChanged,
4
+ iif,
5
+ filter,
6
+ map,
7
+ merge,
8
+ takeUntil,
9
+ shareReplay,
10
+ switchMap,
11
+ Subject,
12
+ Observable } from 'rxjs'
13
+ import type {
14
+ AxisPosition,
15
+ ChartType,
16
+ ChartParams,
17
+ ComputedDataTypeMap,
18
+ ComputedDatumTypeMap,
19
+ ComputedDataGrid,
20
+ DataTypeMap,
21
+ DataGridDatum,
22
+ ComputedDatumGrid,
23
+ DataFormatterTypeMap,
24
+ DataFormatterGrid,
25
+ DataFormatterValueAxis,
26
+ DataFormatterGroupAxis,
27
+ ComputedLayoutDatumGrid,
28
+ ComputedLayoutDataGrid,
29
+ GridContainerPosition,
30
+ HighlightTarget,
31
+ Layout,
32
+ TransformData } from '../../lib/core-types'
33
+ import { getMinAndMaxGrid } from './orbchartsUtils'
34
+ import { createAxisLinearScale, createAxisPointScale, createAxisQuantizeScale } from './d3Utils'
35
+ import { calcGridContainerLayout } from './orbchartsUtils'
36
+ import { getMinAndMaxValue } from './orbchartsUtils'
37
+
38
+ export const gridComputedLayoutDataObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
39
+ computedData$: Observable<ComputedDataTypeMap<'grid'>>
40
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
41
+ layout$: Observable<Layout>
42
+ }): Observable<ComputedLayoutDatumGrid[][]> => {
43
+
44
+ // 未篩選group範圍前的group scale
45
+ function createOriginGroupScale (computedData: ComputedDatumGrid[][], dataFormatter: DataFormatterGrid, layout: Layout) {
46
+ const groupAxisWidth = (dataFormatter.grid.groupAxis.position === 'top' || dataFormatter.grid.groupAxis.position === 'bottom')
47
+ ? layout.width
48
+ : layout.height
49
+ const groupEndIndex = computedData[0] ? computedData[0].length - 1 : 0
50
+ const groupScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
51
+ maxValue: groupEndIndex,
52
+ minValue: 0,
53
+ axisWidth: groupAxisWidth,
54
+ scaleDomain: [0, groupEndIndex], // 不使用dataFormatter設定
55
+ scaleRange: [0, 1] // 不使用dataFormatter設定
56
+ })
57
+ return groupScale
58
+ }
59
+
60
+ // 未篩選group範圍及visible前的value scale
61
+ function createOriginValueScale (computedData: ComputedDatumGrid[][], dataFormatter: DataFormatterGrid, layout: Layout) {
62
+ const valueAxisWidth = (dataFormatter.grid.valueAxis.position === 'left' || dataFormatter.grid.valueAxis.position === 'right')
63
+ ? layout.height
64
+ : layout.width
65
+
66
+ const listData = computedData.flat()
67
+ const [minValue, maxValue] = getMinAndMaxValue(listData)
68
+
69
+ const valueScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
70
+ maxValue,
71
+ minValue,
72
+ axisWidth: valueAxisWidth,
73
+ scaleDomain: [minValue, maxValue], // 不使用dataFormatter設定
74
+ scaleRange: [0, 1] // 不使用dataFormatter設定
75
+ })
76
+
77
+ return valueScale
78
+ }
79
+
80
+ return combineLatest({
81
+ computedData: computedData$,
82
+ fullDataFormatter: fullDataFormatter$,
83
+ layout: layout$
84
+ }).pipe(
85
+ switchMap(async d => d),
86
+ map(data => {
87
+ const groupScale = createOriginGroupScale(data.computedData, data.fullDataFormatter, data.layout)
88
+ const valueScale = createOriginValueScale(data.computedData, data.fullDataFormatter, data.layout)
89
+ const zeroY = valueScale(0)
90
+
91
+ return data.computedData.map((seriesData, seriesIndex) => {
92
+ return seriesData.map((groupDatum, groupIndex) => {
93
+ const axisX = groupScale(groupIndex)
94
+ const axisY = valueScale(groupDatum.value ?? 0)
95
+ return {
96
+ ...groupDatum,
97
+ axisX,
98
+ axisY,
99
+ axisYFromZero: axisY - zeroY
100
+ }
101
+ })
102
+ })
103
+ })
104
+ )
105
+ }
106
+
107
+ export const gridAxesTransformObservable = ({ fullDataFormatter$, layout$ }: {
108
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
109
+ layout$: Observable<Layout>
110
+ }): Observable<TransformData> => {
111
+ const destroy$ = new Subject()
112
+
113
+ function calcAxesTransform ({ xAxis, yAxis, width, height }: {
114
+ xAxis: DataFormatterGroupAxis | DataFormatterValueAxis,
115
+ yAxis: DataFormatterValueAxis,
116
+ width: number,
117
+ height: number
118
+ }): TransformData {
119
+ if (!xAxis || !yAxis) {
120
+ return {
121
+ translate: [0, 0],
122
+ scale: [1, 1],
123
+ rotate: 0,
124
+ rotateX: 0,
125
+ rotateY: 0,
126
+ value: ''
127
+ }
128
+ }
129
+ // const width = size.width - fullChartParams.layout.left - fullChartParams.layout.right
130
+ // const height = size.height - fullChartParams.layout.top - fullChartParams.layout.bottom
131
+ let translateX = 0
132
+ let translateY = 0
133
+ let rotate = 0
134
+ let rotateX = 0
135
+ let rotateY = 0
136
+ if (xAxis.position === 'bottom') {
137
+ if (yAxis.position === 'left') {
138
+ rotateX = 180
139
+ translateY = height
140
+ } else if (yAxis.position === 'right') {
141
+ rotateX = 180
142
+ rotateY = 180
143
+ translateX = width
144
+ translateY = height
145
+ } else {
146
+ // 預設
147
+ rotateX = 180
148
+ translateY = height
149
+ }
150
+ } else if (xAxis.position === 'top') {
151
+ if (yAxis.position === 'left') {
152
+ } else if (yAxis.position === 'right') {
153
+ rotateY = 180
154
+ translateX = width
155
+ } else {
156
+ // 預設
157
+ rotateX = 180
158
+ translateY = height
159
+ }
160
+ } else if (xAxis.position === 'left') {
161
+ if (yAxis.position === 'bottom') {
162
+ rotate = -90
163
+ translateY = height
164
+ } else if (yAxis.position === 'top') {
165
+ rotate = -90
166
+ rotateY = 180
167
+ } else {
168
+ // 預設
169
+ rotateX = 180
170
+ translateY = height
171
+ }
172
+ } else if (xAxis.position === 'right') {
173
+ if (yAxis.position === 'bottom') {
174
+ rotate = -90
175
+ rotateX = 180
176
+ translateY = height
177
+ translateX = width
178
+ } else if (yAxis.position === 'top') {
179
+ rotate = -90
180
+ rotateX = 180
181
+ rotateY = 180
182
+ translateX = width
183
+ } else {
184
+ // 預設
185
+ rotateX = 180
186
+ translateY = height
187
+ }
188
+ } else {
189
+ // 預設
190
+ rotateX = 180
191
+ translateY = height
192
+ }
193
+ // selection.style('transform', `translate(${translateX}px, ${translateY}px) rotate(${rotate}deg) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`)
194
+
195
+ return {
196
+ translate: [translateX, translateY],
197
+ scale: [1, 1],
198
+ rotate,
199
+ rotateX,
200
+ rotateY,
201
+ value: `translate(${translateX}px, ${translateY}px) rotate(${rotate}deg) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`
202
+ }
203
+ }
204
+
205
+ return new Observable(subscriber => {
206
+ combineLatest({
207
+ fullDataFormatter: fullDataFormatter$,
208
+ layout: layout$
209
+ }).pipe(
210
+ takeUntil(destroy$),
211
+ switchMap(async (d) => d),
212
+ ).subscribe(data => {
213
+ const axesTransformData = calcAxesTransform({
214
+ xAxis: data.fullDataFormatter.grid.groupAxis,
215
+ yAxis: data.fullDataFormatter.grid.valueAxis,
216
+ width: data.layout.width,
217
+ height: data.layout.height
218
+ })
219
+
220
+ subscriber.next(axesTransformData)
221
+ })
222
+
223
+ return function unscbscribe () {
224
+ destroy$.next(undefined)
225
+ }
226
+ })
227
+ }
228
+
229
+
230
+ export const gridAxesReverseTransformObservable = ({ gridAxesTransform$ }: {
231
+ gridAxesTransform$: Observable<TransformData>
232
+ }): Observable<TransformData> => {
233
+ return gridAxesTransform$.pipe(
234
+ map(d => {
235
+ // const translate: [number, number] = [d.translate[0] * -1, d.translate[1] * -1]
236
+ const translate: [number, number] = [0, 0] // 無需逆轉
237
+ const scale: [number, number] = [1 / d.scale[0], 1 / d.scale[1]]
238
+ const rotate = d.rotate * -1
239
+ const rotateX = d.rotateX * -1
240
+ const rotateY = d.rotateY * -1
241
+ return {
242
+ translate,
243
+ scale,
244
+ rotate,
245
+ rotateX,
246
+ rotateY,
247
+ value: `translate(${translate[0]}px, ${translate[1]}px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) rotate(${rotate}deg)`
248
+ }
249
+ }),
250
+ )
251
+ }
252
+
253
+ export const gridGraphicTransformObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
254
+ computedData$: Observable<ComputedDataTypeMap<'grid'>>
255
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
256
+ layout$: Observable<Layout>
257
+ }): Observable<TransformData> => {
258
+ const destroy$ = new Subject()
259
+
260
+ function calcGridDataAreaTransform ({ data, groupAxis, valueAxis, width, height }: {
261
+ data: ComputedDataTypeMap<'grid'>
262
+ groupAxis: DataFormatterGroupAxis
263
+ valueAxis: DataFormatterValueAxis
264
+ width: number
265
+ height: number
266
+ }): TransformData {
267
+ let translateX = 0
268
+ let translateY = 0
269
+ let scaleX = 0
270
+ let scaleY = 0
271
+
272
+ // -- groupScale --
273
+ const groupAxisWidth = (groupAxis.position === 'top' || groupAxis.position === 'bottom')
274
+ ? width
275
+ : height
276
+ const groupMin = 0
277
+ const groupMax = data[0] ? data[0].length - 1 : 0
278
+ // const groupScaleDomainMin = groupAxis.scaleDomain[0] === 'min'
279
+ // ? groupMin - groupAxis.scalePadding
280
+ // : groupAxis.scaleDomain[0] as number - groupAxis.scalePadding
281
+ const groupScaleDomainMin = groupAxis.scaleDomain[0] - groupAxis.scalePadding
282
+ const groupScaleDomainMax = groupAxis.scaleDomain[1] === 'max'
283
+ ? groupMax + groupAxis.scalePadding
284
+ : groupAxis.scaleDomain[1] as number + groupAxis.scalePadding
285
+
286
+ const groupScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
287
+ maxValue: groupMax,
288
+ minValue: groupMin,
289
+ axisWidth: groupAxisWidth,
290
+ // scaleDomain: groupAxis.scaleDomain,
291
+ scaleDomain: [groupScaleDomainMin, groupScaleDomainMax],
292
+ scaleRange: [0, 1]
293
+ })
294
+
295
+ // -- translateX, scaleX --
296
+ const rangeMinX = groupScale(groupMin)
297
+ const rangeMaxX = groupScale(groupMax)
298
+ if (groupMin == groupMax) {
299
+ // 當group只有一個
300
+ translateX = 0
301
+ scaleX = 1
302
+ } else {
303
+ translateX = rangeMinX
304
+ const gWidth = rangeMaxX - rangeMinX
305
+ scaleX = gWidth / groupAxisWidth
306
+ }
307
+
308
+ // -- valueScale --
309
+ const filteredData = data.map((d, i) => {
310
+ return d.filter((_d, _i) => {
311
+ return _i >= groupScaleDomainMin && _i <= groupScaleDomainMax && _d.visible == true
312
+ })
313
+ })
314
+
315
+ const filteredMinAndMax = getMinAndMaxGrid(filteredData)
316
+ if (filteredMinAndMax[0] === filteredMinAndMax[1]) {
317
+ filteredMinAndMax[0] = filteredMinAndMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
318
+ }
319
+
320
+ const valueAxisWidth = (valueAxis.position === 'left' || valueAxis.position === 'right')
321
+ ? height
322
+ : width
323
+
324
+ const valueScale: d3.ScaleLinear<number, number> = createAxisLinearScale({
325
+ maxValue: filteredMinAndMax[1],
326
+ minValue: filteredMinAndMax[0],
327
+ axisWidth: valueAxisWidth,
328
+ scaleDomain: valueAxis.scaleDomain,
329
+ scaleRange: valueAxis.scaleRange
330
+ })
331
+
332
+ // -- translateY, scaleY --
333
+ const minAndMax = getMinAndMaxGrid(data)
334
+ if (minAndMax[0] === minAndMax[1]) {
335
+ minAndMax[0] = minAndMax[1] - 1 // 避免最大及最小值相同造成無法計算scale
336
+ }
337
+ const rangeMinY = valueScale(minAndMax[0])
338
+ const rangeMaxY = valueScale(minAndMax[1])
339
+ translateY = rangeMinY
340
+ const gHeight = rangeMaxY - rangeMinY
341
+ scaleY = gHeight / valueAxisWidth
342
+
343
+ return {
344
+ translate: [translateX, translateY],
345
+ scale: [scaleX, scaleY],
346
+ rotate: 0,
347
+ rotateX: 0,
348
+ rotateY: 0,
349
+ value: `translate(${translateX}px, ${translateY}px) scale(${scaleX}, ${scaleY})`
350
+ }
351
+ }
352
+
353
+ return new Observable(subscriber => {
354
+ combineLatest({
355
+ computedData: computedData$,
356
+ fullDataFormatter: fullDataFormatter$,
357
+ layout: layout$
358
+ }).pipe(
359
+ takeUntil(destroy$),
360
+ switchMap(async (d) => d),
361
+ ).subscribe(data => {
362
+ const dataAreaTransformData = calcGridDataAreaTransform ({
363
+ data: data.computedData,
364
+ groupAxis: data.fullDataFormatter.grid.groupAxis,
365
+ valueAxis: data.fullDataFormatter.grid.valueAxis,
366
+ width: data.layout.width,
367
+ height: data.layout.height
368
+ })
369
+
370
+ subscriber.next(dataAreaTransformData)
371
+ })
372
+
373
+ return function unscbscribe () {
374
+ destroy$.next(undefined)
375
+ }
376
+ })
377
+ }
378
+
379
+ export const gridGraphicReverseScaleObservable = ({ gridContainerPosition$, gridAxesTransform$, gridGraphicTransform$ }: {
380
+ gridContainerPosition$: Observable<GridContainerPosition[]>
381
+ gridAxesTransform$: Observable<TransformData>
382
+ gridGraphicTransform$: Observable<TransformData>
383
+ }): Observable<[number, number][]> => {
384
+ return combineLatest({
385
+ gridContainerPosition: gridContainerPosition$,
386
+ gridAxesTransform: gridAxesTransform$,
387
+ gridGraphicTransform: gridGraphicTransform$,
388
+ }).pipe(
389
+ switchMap(async (d) => d),
390
+ map(data => {
391
+ if (data.gridAxesTransform.rotate == 0 || data.gridAxesTransform.rotate == 180) {
392
+ return data.gridContainerPosition.map((series, seriesIndex) => {
393
+ return [
394
+ 1 / data.gridGraphicTransform.scale[0] / data.gridContainerPosition[seriesIndex].scale[0],
395
+ 1 / data.gridGraphicTransform.scale[1] / data.gridContainerPosition[seriesIndex].scale[1],
396
+ ]
397
+ })
398
+ } else {
399
+ return data.gridContainerPosition.map((series, seriesIndex) => {
400
+ // 由於有垂直的旋轉,所以外層 (container) x和y的scale要互換
401
+ return [
402
+ 1 / data.gridGraphicTransform.scale[0] / data.gridContainerPosition[seriesIndex].scale[1],
403
+ 1 / data.gridGraphicTransform.scale[1] / data.gridContainerPosition[seriesIndex].scale[0],
404
+ ]
405
+ })
406
+ }
407
+ }),
408
+ )
409
+ }
410
+
411
+ export const gridAxesSizeObservable = ({ fullDataFormatter$, layout$ }: {
412
+ fullDataFormatter$: Observable<DataFormatterGrid>
413
+ layout$: Observable<Layout>
414
+ }): Observable<{
415
+ width: number;
416
+ height: number;
417
+ }> => {
418
+ const destroy$ = new Subject()
419
+
420
+ function calcAxesSize ({ xAxisPosition, yAxisPosition, width, height }: {
421
+ xAxisPosition: AxisPosition
422
+ yAxisPosition: AxisPosition
423
+ width: number
424
+ height: number
425
+ }) {
426
+ if ((xAxisPosition === 'bottom' || xAxisPosition === 'top') && (yAxisPosition === 'left' || yAxisPosition === 'right')) {
427
+ return { width, height }
428
+ } else if ((xAxisPosition === 'left' || xAxisPosition === 'right') && (yAxisPosition === 'bottom' || yAxisPosition === 'top')) {
429
+ return {
430
+ width: height,
431
+ height: width
432
+ }
433
+ } else {
434
+ // default
435
+ return { width, height }
436
+ }
437
+ }
438
+
439
+ return new Observable(subscriber => {
440
+ combineLatest({
441
+ fullDataFormatter: fullDataFormatter$,
442
+ layout: layout$
443
+ }).pipe(
444
+ takeUntil(destroy$),
445
+ switchMap(async (d) => d),
446
+ ).subscribe(data => {
447
+
448
+ const axisSize = calcAxesSize({
449
+ xAxisPosition: data.fullDataFormatter.grid.groupAxis.position,
450
+ yAxisPosition: data.fullDataFormatter.grid.valueAxis.position,
451
+ width: data.layout.width,
452
+ height: data.layout.height,
453
+ })
454
+
455
+ subscriber.next(axisSize)
456
+
457
+ return function unsubscribe () {
458
+ destroy$.next(undefined)
459
+ }
460
+ })
461
+ })
462
+ }
463
+
464
+ // export const gridHighlightObservable = ({ computedData$, fullChartParams$, event$ }: {
465
+ // computedData$: Observable<ComputedDataTypeMap<'grid'>>
466
+ // fullChartParams$: Observable<ChartParams>
467
+ // event$: Subject<any>
468
+ // }): Observable<string[]> => {
469
+ // const datumList$ = computedData$.pipe(
470
+ // map(d => d.flat())
471
+ // )
472
+ // return highlightObservable ({ datumList$, fullChartParams$, event$ })
473
+ // }
474
+
475
+ export const gridSeriesLabelsObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'grid'>> }) => {
476
+ return computedData$.pipe(
477
+ map(data => {
478
+ return data
479
+ .filter(series => series.length)
480
+ .map(series => {
481
+ return series[0].seriesLabel
482
+ })
483
+ }),
484
+ distinctUntilChanged((a, b) => {
485
+ return JSON.stringify(a).length === JSON.stringify(b).length
486
+ }),
487
+ )
488
+ }
489
+
490
+ export const gridVisibleComputedDataObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'grid'>> }) => {
491
+ return computedData$.pipe(
492
+ map(data => {
493
+ const visibleComputedData = data
494
+ .map(d => {
495
+ return d.filter(_d => {
496
+ return _d.visible == true
497
+ })
498
+ })
499
+ .filter(d => d.length)
500
+ return visibleComputedData
501
+ })
502
+ )
503
+ }
504
+
505
+ export const gridVisibleComputedLayoutDataObservable = ({ computedLayoutData$ }: { computedLayoutData$: Observable<ComputedLayoutDataGrid> }) => {
506
+ return computedLayoutData$.pipe(
507
+ map(data => {
508
+ const visibleComputedData = data
509
+ .map(d => {
510
+ return d.filter(_d => {
511
+ return _d.visible == true
512
+ })
513
+ })
514
+ .filter(d => d.length)
515
+ return visibleComputedData
516
+ })
517
+ )
518
+ }
519
+
520
+ // 所有container位置(對應series)
521
+ export const gridContainerPositionObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
522
+ computedData$: Observable<ComputedDataTypeMap<'grid'>>
523
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'grid'>>
524
+ layout$: Observable<Layout>
525
+ }): Observable<GridContainerPosition[]> => {
526
+
527
+ const gridContainerPosition$ = combineLatest({
528
+ computedData: computedData$,
529
+ fullDataFormatter: fullDataFormatter$,
530
+ layout: layout$,
531
+ }).pipe(
532
+ switchMap(async (d) => d),
533
+ map(data => {
534
+
535
+ if (data.fullDataFormatter.grid.separateSeries) {
536
+ // -- 依slotIndexes計算 --
537
+ return calcGridContainerLayout(data.layout, data.fullDataFormatter.container, data.computedData.length)
538
+ // return data.computedData.map((seriesData, seriesIndex) => {
539
+ // const columnIndex = seriesIndex % data.fullDataFormatter.container.columnAmount
540
+ // const rowIndex = Math.floor(seriesIndex / data.fullDataFormatter.container.columnAmount)
541
+ // const { translate, scale } = calcGridContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
542
+ // return {
543
+ // slotIndex: seriesIndex,
544
+ // rowIndex,
545
+ // columnIndex,
546
+ // translate,
547
+ // scale,
548
+ // }
549
+ // })
550
+ } else {
551
+ // -- 無拆分 --
552
+ const gridContainerPositionArr = calcGridContainerLayout(data.layout, data.fullDataFormatter.container, 1)
553
+ return data.computedData.map((d, i) => gridContainerPositionArr[0]) // 每個series相同位置
554
+ // const columnIndex = 0
555
+ // const rowIndex = 0
556
+ // return data.computedData.map((seriesData, seriesIndex) => {
557
+ // const { translate, scale } = calcGridContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
558
+ // return {
559
+ // slotIndex: 0,
560
+ // rowIndex,
561
+ // columnIndex,
562
+ // translate,
563
+ // scale,
564
+ // }
565
+ // })
566
+ }
567
+ })
568
+ )
569
+
570
+ return gridContainerPosition$
571
+ }
572
+
573
+ // 將原本的value全部替換成加總後的value
574
+ export const computedStackedDataObservables = ({ isSeriesSeprate$, computedData$ }: {
575
+ isSeriesSeprate$: Observable<boolean>
576
+ computedData$: Observable<ComputedDataGrid>
577
+ }): Observable<ComputedDataGrid> => {
578
+ const stackedData$: Observable<ComputedDataGrid> = computedData$.pipe(
579
+ map(data => {
580
+ // 將同一group的value加總起來
581
+ const stackedValue = new Array(data[0] ? data[0].length : 0)
582
+ .fill(null)
583
+ .map((_, i) => {
584
+ return data.reduce((prev, current) => {
585
+ if (current && current[i]) {
586
+ const currentValue = current[i].value == null || current[i].visible == false
587
+ ? 0
588
+ : current[i].value!
589
+ return prev + currentValue
590
+ }
591
+ return prev
592
+ }, 0)
593
+ })
594
+ // 將原本的value全部替換成加總後的value
595
+ const computedData = data.map((series, seriesIndex) => {
596
+ return series.map((d, i) => {
597
+ return {
598
+ ...d,
599
+ value: stackedValue[i],
600
+ }
601
+ })
602
+ })
603
+ return computedData
604
+ }),
605
+ )
606
+
607
+ return isSeriesSeprate$.pipe(
608
+ switchMap(isSeriesSeprate => {
609
+ return iif(() => isSeriesSeprate, computedData$, stackedData$)
610
+ })
611
+ )
612
612
  }