@orbcharts/core 3.0.0-beta.1 → 3.0.0-beta.2

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