@orbcharts/core 3.0.0-beta.8 → 3.0.0

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