@orbcharts/core 3.0.0-beta.3 → 3.0.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +2623 -2338
  3. package/dist/orbcharts-core.umd.js +4 -4
  4. package/dist/src/utils/d3Scale.d.ts +28 -0
  5. package/dist/src/utils/gridObservables.d.ts +29 -19
  6. package/dist/src/utils/index.d.ts +1 -1
  7. package/dist/src/utils/multiGridObservables.d.ts +2 -2
  8. package/dist/src/utils/multiValueObservables.d.ts +73 -0
  9. package/dist/src/utils/orbchartsUtils.d.ts +19 -5
  10. package/dist/src/utils/seriesObservables.d.ts +4 -4
  11. package/dist/src/utils/treeObservables.d.ts +2 -5
  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 +505 -505
  22. package/src/base/createBasePlugin.ts +153 -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 +235 -232
  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 +176 -155
  37. package/src/grid/dataFormatterValidator.ts +101 -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 +41 -41
  42. package/src/multiGrid/dataFormatterValidator.ts +115 -115
  43. package/src/multiGrid/dataValidator.ts +12 -12
  44. package/src/multiValue/computedDataFn.ts +110 -176
  45. package/src/multiValue/contextObserverCallback.ts +160 -12
  46. package/src/multiValue/dataFormatterValidator.ts +9 -9
  47. package/src/multiValue/dataValidator.ts +9 -9
  48. package/src/relationship/computedDataFn.ts +125 -125
  49. package/src/relationship/contextObserverCallback.ts +12 -12
  50. package/src/relationship/dataFormatterValidator.ts +9 -9
  51. package/src/relationship/dataValidator.ts +9 -9
  52. package/src/series/computedDataFn.ts +88 -88
  53. package/src/series/contextObserverCallback.ts +100 -100
  54. package/src/series/dataFormatterValidator.ts +41 -41
  55. package/src/series/dataValidator.ts +12 -12
  56. package/src/tree/computedDataFn.ts +129 -130
  57. package/src/tree/contextObserverCallback.ts +58 -61
  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 -0
  62. package/src/utils/errorMessage.ts +42 -42
  63. package/src/utils/gridObservables.ts +671 -614
  64. package/src/utils/index.ts +9 -9
  65. package/src/utils/multiGridObservables.ts +392 -366
  66. package/src/utils/multiValueObservables.ts +643 -0
  67. package/src/utils/observables.ts +219 -219
  68. package/src/utils/orbchartsUtils.ts +377 -352
  69. package/src/utils/seriesObservables.ts +175 -175
  70. package/src/utils/treeObservables.ts +105 -94
  71. package/src/utils/validator.ts +126 -125
  72. package/tsconfig.base.json +13 -13
  73. package/tsconfig.json +2 -2
  74. package/vite-env.d.ts +6 -6
  75. package/vite.config.js +22 -22
  76. package/dist/src/utils/d3Utils.d.ts +0 -19
  77. package/src/utils/d3Utils.ts +0 -108
@@ -1,352 +1,377 @@
1
- import * as d3 from 'd3'
2
- import type {
3
- ChartType,
4
- ChartParams,
5
- DatumValue,
6
- DataSeries,
7
- DataSeriesDatum,
8
- DataSeriesValue,
9
- DataGrid,
10
- DataGridDatum,
11
- DataGridValue,
12
- DataMultiGrid,
13
- DataMultiValue,
14
- DataMultiValueDatum,
15
- DataMultiValueValue,
16
- DataFormatterContainer,
17
- SeriesDirection,
18
- DataFormatterGridGrid,
19
- SeriesContainerPosition,
20
- GridContainerPosition,
21
- Layout
22
- } from '../../lib/core-types'
23
- import { isPlainObject } from './commonUtils'
24
-
25
- export function formatValueToLabel (value: any, valueFormatter: string | ((text: d3.NumberValue) => string)) {
26
- if (valueFormatter! instanceof Function == true) {
27
- return (valueFormatter as ((text: d3.NumberValue) => string))(value)
28
- }
29
- return d3.format(valueFormatter as string)!(value)
30
- }
31
-
32
- export function createDefaultDatumId (chartTypeOrPrefix: string, levelOneIndex: number, levelTwoIndex: number, levelThreeIndex?: number) {
33
- let text = `${chartTypeOrPrefix}_${levelOneIndex}_${levelTwoIndex}`
34
- if (levelThreeIndex != null) {
35
- text += `_${levelThreeIndex}`
36
- }
37
- return text
38
- }
39
-
40
- export function createDefaultSeriesLabel (chartTypeOrPrefix: string, seriesIndex: number) {
41
- return `${chartTypeOrPrefix}_series${seriesIndex}`
42
- }
43
-
44
- export function createDefaultGroupLabel (chartTypeOrPrefix: string, groupIndex: number) {
45
- return `${chartTypeOrPrefix}_group${groupIndex}`
46
- }
47
-
48
- export function createGridSeriesLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'grid' }: {
49
- transposedDataGrid: DataGridDatum[][],
50
- dataFormatterGrid: DataFormatterGridGrid
51
- chartType?: ChartType
52
- }) {
53
- const labels = dataFormatterGrid.seriesDirection === 'row'
54
- ? dataFormatterGrid.rowLabels
55
- : dataFormatterGrid.columnLabels
56
- return transposedDataGrid.map((_, rowIndex) => {
57
- return labels[rowIndex] != null
58
- ? labels[rowIndex]
59
- : createDefaultSeriesLabel(chartType, rowIndex)
60
- })
61
- }
62
-
63
- export function createMultiGridSeriesLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'multiGrid', gridIndex = 0 }: {
64
- transposedDataGrid: DataGridDatum[][],
65
- dataFormatterGrid: DataFormatterGridGrid
66
- chartType?: ChartType
67
- gridIndex?: number
68
- }) {
69
- const labels = dataFormatterGrid.seriesDirection === 'row'
70
- ? dataFormatterGrid.rowLabels
71
- : dataFormatterGrid.columnLabels
72
- return transposedDataGrid.map((_, rowIndex) => {
73
- return labels[rowIndex] != null
74
- ? labels[rowIndex]
75
- : createDefaultSeriesLabel(`${chartType}_grid${gridIndex}`, rowIndex)
76
- })
77
- }
78
-
79
- export function createGridGroupLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'grid' }: {
80
- transposedDataGrid: DataGridDatum[][],
81
- dataFormatterGrid: DataFormatterGridGrid
82
- chartType?: ChartType
83
- }) {
84
- if (transposedDataGrid[0] == null) {
85
- return []
86
- }
87
- const labels = dataFormatterGrid.seriesDirection === 'row'
88
- ? dataFormatterGrid.columnLabels
89
- : dataFormatterGrid.rowLabels
90
- return transposedDataGrid[0].map((_, columnLabels) => {
91
- return labels[columnLabels] != null
92
- ? labels[columnLabels]
93
- : createDefaultGroupLabel(chartType, columnLabels)
94
- })
95
- }
96
-
97
- export function createMultiGridGroupLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'multiGrid', gridIndex = 0 }: {
98
- transposedDataGrid: DataGridDatum[][],
99
- dataFormatterGrid: DataFormatterGridGrid
100
- chartType?: ChartType
101
- gridIndex?: number
102
- }) {
103
- if (transposedDataGrid[0] == null) {
104
- return []
105
- }
106
- const labels = dataFormatterGrid.seriesDirection === 'row'
107
- ? dataFormatterGrid.columnLabels
108
- : dataFormatterGrid.rowLabels
109
- return transposedDataGrid[0].map((_, columnLabels) => {
110
- return labels[columnLabels] != null
111
- ? labels[columnLabels]
112
- : createDefaultGroupLabel(`${chartType}_grid${gridIndex}`, columnLabels)
113
- })
114
- }
115
-
116
- // 取得最小及最大值 - 數字陣列
117
- export function getMinAndMax (data: number[]): [number, number] {
118
- const defaultMinAndMax: [number, number] = [0, 0] // default
119
- if (!data.length) {
120
- return defaultMinAndMax
121
- }
122
- const minAndMax: [number, number] = data.reduce((prev, current) => {
123
- // [min, max]
124
- return [
125
- current < prev[0] ? current : prev[0],
126
- current > prev[1] ? current : prev[1]
127
- ]
128
- }, [data[0], data[0]])
129
- return minAndMax
130
- }
131
-
132
- // 取得最小及最大值 - datum格式陣列資料
133
- export function getMinAndMaxValue (data: DatumValue[]): [number, number] {
134
- const arr = data
135
- .filter(d => d != null && d.value != null)
136
- .map(d => d.value )
137
- return getMinAndMax(arr)
138
- }
139
-
140
- // 取得最小及最大值 - Series Data
141
- export function getMinAndMaxSeries (data: DataSeries): [number, number] {
142
- const flatData: (DataSeriesValue | DataSeriesDatum)[] = data[0] && Array.isArray((data as (DataSeriesValue | DataSeriesDatum)[][])[0])
143
- ? data.flat()
144
- : data as (DataSeriesValue | DataSeriesDatum)[]
145
- const arr = flatData
146
- .filter(d => (d == null || (isPlainObject(d) && (d as DataSeriesDatum).value == null)) === false) // 過濾掉null &
147
- .map(d => typeof d === 'number' ? d : d.value )
148
- return getMinAndMax(arr)
149
- }
150
-
151
- // 取得最小及最大值 - Grid Data
152
- export function getMinAndMaxGrid (data: DataGrid): [number, number] {
153
- const flatData: (DataGridValue | DataGridDatum)[] = data.flat()
154
- const arr = flatData
155
- .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
156
- .map(d => typeof d === 'number' ? d : d.value )
157
- return getMinAndMax(arr)
158
- }
159
-
160
- // 取得最小及最大值 - MultiGrid Data
161
- export function getMinAndMaxMultiGrid (data: DataMultiGrid): [number, number] {
162
- const flatData: (DataGridValue | DataGridDatum)[] = data.flat().flat()
163
- const arr = flatData
164
- .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
165
- .map(d => typeof d === 'number' ? d : d.value )
166
- return getMinAndMax(arr)
167
- }
168
-
169
- // 取得最小及最大值 - MultiValue Data
170
- export function getMinAndMaxMultiValue (data: DataMultiValue, valueIndex: number = 2): [number, number] {
171
- const flatData: (DataMultiValueDatum | DataMultiValueValue)[] = data.flat().filter((d, i) => i == valueIndex)
172
- const arr = flatData
173
- .filter(d => (d == null || (isPlainObject(d) && (d as DataMultiValueDatum).value == null)) === false) // 過濾掉null
174
- .map(d => typeof d === 'number' ? d : d.value )
175
- return getMinAndMax(arr)
176
- }
177
-
178
- // @Q@ 待處理
179
- // // 取得最小及最大值 - Relationship Data
180
- // export function getMinAndMaxRelationship (data: DataRelationship, target: 'nodes' | 'edges' = 'nodes'): [number, number] {
181
-
182
- // }
183
-
184
- // @Q@ 待處理
185
- // // 取得最小及最大值 - Tree Data
186
- // export function getMinAndMaxTree (data: DataTree): [number, number] {
187
-
188
- // }
189
-
190
- // 轉置成seriesDirection為main的陣列格式
191
- export function transposeData<T> (seriesDirection: SeriesDirection, data: T[][]): T[][] {
192
- if (seriesDirection === 'row') {
193
- return Object.assign([], data)
194
- }
195
- // 取得原始陣列的維度
196
- const rows = data.length;
197
- const cols = data.reduce((prev, current) => {
198
- return Math.max(prev, current.length)
199
- }, 0)
200
-
201
- // 初始化轉換後的陣列
202
- const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
203
-
204
- // 遍歷原始陣列,進行轉換
205
- for (let i = 0; i < rows; i++) {
206
- for (let j = 0; j < cols; j++) {
207
- transposedArray[j][i] = data[i][j]
208
- }
209
- }
210
-
211
- return transposedArray
212
- }
213
-
214
-
215
- export function seriesColorPredicate (seriesIndex: number, chartParams: ChartParams) {
216
- return seriesIndex < chartParams.colors[chartParams.colorScheme].series.length
217
- ? chartParams.colors[chartParams.colorScheme].series[seriesIndex]
218
- : chartParams.colors[chartParams.colorScheme].series[
219
- seriesIndex % chartParams.colors[chartParams.colorScheme].series.length
220
- ]
221
- }
222
-
223
- // export function calcSeriesContainerPosition (layout: Layout, container: DataFormatterContainer, rowIndex: number, columnIndex: number) {
224
- // const { gap, rowAmount, columnAmount } = container
225
- // const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
226
- // const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
227
- // const x = columnIndex * width + (columnIndex * gap)
228
- // const y = rowIndex * height + (rowIndex * gap)
229
- // // const translate: [number, number] = [x, y]
230
-
231
- // return {
232
- // // translate,
233
- // startX: x,
234
- // startY: y,
235
- // centerX: x + width / 2,
236
- // centerY: y + height / 2,
237
- // width,
238
- // height
239
- // }
240
- // }
241
-
242
- // 計算預設欄列數量
243
- // 規則1.rowAmount*columnAmount要大於或等於amount,並且數字要盡可能小
244
- // 規則2.columnAmount要大於或等於rowAmount,並且數字要盡可能小
245
- function calcGridDimensions (amount: number): { rowAmount: number; columnAmount: number } {
246
- let rowAmount = Math.floor(Math.sqrt(amount))
247
- let columnAmount = Math.ceil(amount / rowAmount)
248
- while (rowAmount * columnAmount < amount) {
249
- columnAmount++
250
- }
251
- return { rowAmount, columnAmount }
252
- }
253
-
254
- export function calcSeriesContainerLayout (layout: Layout, container: DataFormatterContainer, amount: number): SeriesContainerPosition[] {
255
- const { gap } = container
256
- const { rowAmount, columnAmount } = (container.rowAmount * container.columnAmount) >= amount
257
- // 如果container設定的rowAmount和columnAmount的乘積大於或等於amount,則使用目前設定
258
- ? container
259
- // 否則計算一個合適的預設值
260
- : calcGridDimensions(amount)
261
-
262
- return new Array(amount).fill(null).map((_, index) => {
263
- const columnIndex = index % columnAmount
264
- const rowIndex = Math.floor(index / columnAmount)
265
-
266
- const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
267
- const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
268
- const x = columnIndex * width + (columnIndex * gap)
269
- const y = rowIndex * height + (rowIndex * gap)
270
- // const translate: [number, number] = [x, y]
271
-
272
- return {
273
- slotIndex: index,
274
- rowIndex,
275
- columnIndex,
276
- // translate,
277
- startX: x,
278
- startY: y,
279
- centerX: x + width / 2,
280
- centerY: y + height / 2,
281
- width,
282
- height
283
- }
284
- })
285
- }
286
-
287
- // export function calcGridContainerPosition (layout: Layout, container: DataFormatterContainer, rowIndex: number, columnIndex: number) {
288
- // const { gap, rowAmount, columnAmount } = container
289
- // const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
290
- // const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
291
- // const x = columnIndex * width + (columnIndex * gap)
292
- // const y = rowIndex * height + (rowIndex * gap)
293
- // const translate: [number, number] = [x, y]
294
- // const scale: [number, number] = [width / layout.width, height / layout.height]
295
-
296
- // return {
297
- // translate,
298
- // scale
299
- // }
300
- // }
301
-
302
- export function calcGridContainerLayout (layout: Layout, container: DataFormatterContainer, amount: number): GridContainerPosition[] {
303
- const { gap } = container
304
- const { rowAmount, columnAmount } = (container.rowAmount * container.columnAmount) >= amount
305
- // 如果container設定的rowAmount和columnAmount的乘積大於或等於amount,則使用目前設定
306
- ? container
307
- // 否則計算一個合適的預設值
308
- : calcGridDimensions(amount)
309
-
310
- return new Array(amount).fill(null).map((_, index) => {
311
- const columnIndex = index % columnAmount
312
- const rowIndex = Math.floor(index / columnAmount)
313
-
314
- const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
315
- const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
316
- const x = columnIndex * width + (columnIndex * gap)
317
- const y = rowIndex * height + (rowIndex * gap)
318
- const translate: [number, number] = [x, y]
319
- const scale: [number, number] = [width / layout.width, height / layout.height]
320
-
321
- return {
322
- slotIndex: index,
323
- rowIndex,
324
- columnIndex,
325
- translate,
326
- scale
327
- }
328
- })
329
- }
330
-
331
- // // multiGrid datum color
332
- // export function multiGridColorPredicate ({ seriesIndex, groupIndex, data, chartParams }: {
333
- // seriesIndex: number
334
- // groupIndex: number
335
- // data: ComputedDataMultiGrid
336
- // chartParams: ChartParams
337
- // }) {
338
- // // 累加前面的grid的seriesIndex
339
- // const accSeriesIndex = data.reduce((prev, current) => {
340
- // if (current[0] && current[0][0] && groupIndex > current[0][0].gridIndex) {
341
- // return prev + current[0].length
342
- // } else if (current[0] && current[0][0] && groupIndex == current[0][0].gridIndex) {
343
- // return prev + seriesIndex
344
- // } else {
345
- // return prev
346
- // }
347
- // }, 0)
348
-
349
- // return seriesColorPredicate(accSeriesIndex, chartParams)
350
- // }
351
-
352
-
1
+ import * as d3 from 'd3'
2
+ import type {
3
+ ChartType,
4
+ ChartParams,
5
+ DatumValue,
6
+ DataSeries,
7
+ DataSeriesDatum,
8
+ DataSeriesValue,
9
+ DataGrid,
10
+ DataGridDatum,
11
+ DataGridValue,
12
+ DataMultiGrid,
13
+ DataMultiValue,
14
+ DataMultiValueDatum,
15
+ DataMultiValueValue,
16
+ ComputedLayoutDatumMultiValue,
17
+ DataFormatterContainer,
18
+ SeriesDirection,
19
+ DataFormatterGridGrid,
20
+ ContainerPosition,
21
+ ContainerPositionScaled,
22
+ Layout
23
+ } from '../../lib/core-types'
24
+ import { isPlainObject } from './commonUtils'
25
+
26
+ export function formatValueToLabel (value: any, valueFormatter: string | ((text: d3.NumberValue) => string)) {
27
+ if (valueFormatter! instanceof Function == true) {
28
+ return (valueFormatter as ((text: d3.NumberValue) => string))(value)
29
+ }
30
+ return d3.format(valueFormatter as string)!(value)
31
+ }
32
+
33
+ export function createDefaultDatumId (chartTypeOrPrefix: string, levelOneIndex: number, levelTwoIndex?: number, levelThreeIndex?: number) {
34
+ let text = `${chartTypeOrPrefix}_${levelOneIndex}`
35
+ if (levelTwoIndex != null) {
36
+ text += `_${levelTwoIndex}`
37
+ }
38
+ if (levelThreeIndex != null) {
39
+ text += `_${levelThreeIndex}`
40
+ }
41
+ return text
42
+ }
43
+
44
+ export function createDefaultSeriesLabel (chartTypeOrPrefix: string, seriesIndex: number) {
45
+ return `${chartTypeOrPrefix}_series${seriesIndex}`
46
+ }
47
+
48
+ export function createDefaultGroupLabel (chartTypeOrPrefix: string, groupIndex: number) {
49
+ return `${chartTypeOrPrefix}_group${groupIndex}`
50
+ }
51
+
52
+ export function createDefaultCategoryLabel () {
53
+ // return `${chartTypeOrPrefix}_category`
54
+ return '' // 空值
55
+ }
56
+
57
+ export function createGridSeriesLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'grid' }: {
58
+ transposedDataGrid: DataGridDatum[][],
59
+ dataFormatterGrid: DataFormatterGridGrid
60
+ chartType?: ChartType
61
+ }) {
62
+ const labels = dataFormatterGrid.seriesDirection === 'row'
63
+ ? dataFormatterGrid.rowLabels
64
+ : dataFormatterGrid.columnLabels
65
+ return transposedDataGrid.map((_, rowIndex) => {
66
+ return labels[rowIndex] != null
67
+ ? labels[rowIndex]
68
+ : createDefaultSeriesLabel(chartType, rowIndex)
69
+ })
70
+ }
71
+
72
+ export function createMultiGridSeriesLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'multiGrid', gridIndex = 0 }: {
73
+ transposedDataGrid: DataGridDatum[][],
74
+ dataFormatterGrid: DataFormatterGridGrid
75
+ chartType?: ChartType
76
+ gridIndex?: number
77
+ }) {
78
+ const labels = dataFormatterGrid.seriesDirection === 'row'
79
+ ? dataFormatterGrid.rowLabels
80
+ : dataFormatterGrid.columnLabels
81
+ return transposedDataGrid.map((_, rowIndex) => {
82
+ return labels[rowIndex] != null
83
+ ? labels[rowIndex]
84
+ : createDefaultSeriesLabel(`${chartType}_grid${gridIndex}`, rowIndex)
85
+ })
86
+ }
87
+
88
+ export function createGridGroupLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'grid' }: {
89
+ transposedDataGrid: DataGridDatum[][],
90
+ dataFormatterGrid: DataFormatterGridGrid
91
+ chartType?: ChartType
92
+ }) {
93
+ if (transposedDataGrid[0] == null) {
94
+ return []
95
+ }
96
+ const labels = dataFormatterGrid.seriesDirection === 'row'
97
+ ? dataFormatterGrid.columnLabels
98
+ : dataFormatterGrid.rowLabels
99
+ return transposedDataGrid[0].map((_, columnLabels) => {
100
+ return labels[columnLabels] != null
101
+ ? labels[columnLabels]
102
+ : createDefaultGroupLabel(chartType, columnLabels)
103
+ })
104
+ }
105
+
106
+ export function createMultiGridGroupLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'multiGrid', gridIndex = 0 }: {
107
+ transposedDataGrid: DataGridDatum[][],
108
+ dataFormatterGrid: DataFormatterGridGrid
109
+ chartType?: ChartType
110
+ gridIndex?: number
111
+ }) {
112
+ if (transposedDataGrid[0] == null) {
113
+ return []
114
+ }
115
+ const labels = dataFormatterGrid.seriesDirection === 'row'
116
+ ? dataFormatterGrid.columnLabels
117
+ : dataFormatterGrid.rowLabels
118
+ return transposedDataGrid[0].map((_, columnLabels) => {
119
+ return labels[columnLabels] != null
120
+ ? labels[columnLabels]
121
+ : createDefaultGroupLabel(`${chartType}_grid${gridIndex}`, columnLabels)
122
+ })
123
+ }
124
+
125
+ // 取得最小及最大值 - 數字陣列
126
+ export function getMinAndMax (data: number[]): [number, number] {
127
+ const defaultMinAndMax: [number, number] = [0, 0] // default
128
+ if (!data.length) {
129
+ return defaultMinAndMax
130
+ }
131
+ const minAndMax: [number, number] = data.reduce((prev, current) => {
132
+ // [min, max]
133
+ return [
134
+ current < prev[0] ? current : prev[0],
135
+ current > prev[1] ? current : prev[1]
136
+ ]
137
+ }, [data[0], data[0]])
138
+ return minAndMax
139
+ }
140
+
141
+ // 取得最小及最大值 - datum格式陣列資料
142
+ export function getMinAndMaxValue (data: DatumValue[]): [number, number] {
143
+ const arr = data
144
+ .filter(d => d != null && d.value != null)
145
+ .map(d => d.value )
146
+ return getMinAndMax(arr)
147
+ }
148
+
149
+ // 取得最小及最大值 - Series Data
150
+ export function getMinAndMaxSeries (data: DataSeries): [number, number] {
151
+ const flatData: (DataSeriesValue | DataSeriesDatum)[] = data[0] && Array.isArray((data as (DataSeriesValue | DataSeriesDatum)[][])[0])
152
+ ? data.flat()
153
+ : data as (DataSeriesValue | DataSeriesDatum)[]
154
+ const arr = flatData
155
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataSeriesDatum).value == null)) === false) // 過濾掉null &
156
+ .map(d => typeof d === 'number' ? d : d.value )
157
+ return getMinAndMax(arr)
158
+ }
159
+
160
+ // 取得最小及最大值 - Grid Data
161
+ export function getMinAndMaxGrid (data: DataGrid): [number, number] {
162
+ const flatData: (DataGridValue | DataGridDatum)[] = data.flat()
163
+ const arr = flatData
164
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
165
+ .map(d => typeof d === 'number' ? d : d.value )
166
+ return getMinAndMax(arr)
167
+ }
168
+
169
+ // 取得最小及最大值 - MultiGrid Data
170
+ export function getMinAndMaxMultiGrid (data: DataMultiGrid): [number, number] {
171
+ const flatData: (DataGridValue | DataGridDatum)[] = data.flat().flat()
172
+ const arr = flatData
173
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
174
+ .map(d => typeof d === 'number' ? d : d.value )
175
+ return getMinAndMax(arr)
176
+ }
177
+
178
+ // 取得最小及最大值 - MultiValue Data
179
+ export function getMinAndMaxMultiValue (data: DataMultiValue, valueIndex: number): [number, number] {
180
+ const arr: number[] = data
181
+ .map(d => {
182
+ if (Array.isArray(d)) {
183
+ return d[valueIndex] ?? null
184
+ } else if (isPlainObject(d)) {
185
+ return (d as DataMultiValueDatum).value[valueIndex] ?? null
186
+ } else {
187
+ return null
188
+ }
189
+ })
190
+ .filter(d => d != null)
191
+ return getMinAndMax(arr)
192
+ }
193
+
194
+ export function getMinAndMaxMultiValueXY ({ data, minX, maxX, minY, maxY }: {
195
+ data: ComputedLayoutDatumMultiValue[][]
196
+ minX: number
197
+ maxX: number
198
+ minY: number
199
+ maxY: number
200
+ }) {
201
+ let filteredData: ComputedLayoutDatumMultiValue[][] = []
202
+ let minXDatum: ComputedLayoutDatumMultiValue | null = null
203
+ let maxXDatum: ComputedLayoutDatumMultiValue | null = null
204
+ let minYDatum: ComputedLayoutDatumMultiValue | null = null
205
+ let maxYDatum: ComputedLayoutDatumMultiValue | null = null
206
+
207
+ for (let categoryData of data) {
208
+ for (let datum of categoryData) {
209
+ if (datum.axisX >= minX && datum.axisX <= maxX && datum.axisY >= minY && datum.axisY <= maxY) {
210
+ filteredData.push(categoryData)
211
+ if (minXDatum == null || datum.axisX < minXDatum.axisX) {
212
+ minXDatum = datum
213
+ }
214
+ if (maxXDatum == null || datum.axisX > maxXDatum.axisX) {
215
+ maxXDatum = datum
216
+ }
217
+ if (minYDatum == null || datum.axisY < minYDatum.axisY) {
218
+ minYDatum = datum
219
+ }
220
+ if (maxYDatum == null || datum.axisY > maxYDatum.axisY) {
221
+ maxYDatum = datum
222
+ }
223
+ }
224
+ }
225
+ }
226
+
227
+ return {
228
+ minXDatum,
229
+ maxXDatum,
230
+ minYDatum,
231
+ maxYDatum,
232
+ filteredData
233
+ }
234
+ }
235
+
236
+ // @Q@ 待處理
237
+ // // 取得最小及最大值 - Relationship Data
238
+ // export function getMinAndMaxRelationship (data: DataRelationship, target: 'nodes' | 'edges' = 'nodes'): [number, number] {
239
+
240
+ // }
241
+
242
+ // @Q@ 待處理
243
+ // // 取得最小及最大值 - Tree Data
244
+ // export function getMinAndMaxTree (data: DataTree): [number, number] {
245
+
246
+ // }
247
+
248
+ // 轉置成seriesDirection為main的陣列格式
249
+ export function transposeData<T> (seriesDirection: SeriesDirection, data: T[][]): T[][] {
250
+ if (seriesDirection === 'row') {
251
+ return Object.assign([], data)
252
+ }
253
+ // 取得原始陣列的維度
254
+ const rows = data.length;
255
+ const cols = data.reduce((prev, current) => {
256
+ return Math.max(prev, current.length)
257
+ }, 0)
258
+
259
+ // 初始化轉換後的陣列
260
+ const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
261
+
262
+ // 遍歷原始陣列,進行轉換
263
+ for (let i = 0; i < rows; i++) {
264
+ for (let j = 0; j < cols; j++) {
265
+ transposedArray[j][i] = data[i][j]
266
+ }
267
+ }
268
+
269
+ return transposedArray
270
+ }
271
+
272
+
273
+ export function seriesColorPredicate (seriesIndex: number, chartParams: ChartParams) {
274
+ return seriesIndex < chartParams.colors[chartParams.colorScheme].series.length
275
+ ? chartParams.colors[chartParams.colorScheme].series[seriesIndex]
276
+ : chartParams.colors[chartParams.colorScheme].series[
277
+ seriesIndex % chartParams.colors[chartParams.colorScheme].series.length
278
+ ]
279
+ }
280
+
281
+
282
+ // 計算預設欄列數量
283
+ // 規則1.rowAmount*columnAmount要大於或等於amount,並且數字要盡可能小
284
+ // 規則2.columnAmount要大於或等於rowAmount,並且數字要盡可能小
285
+ function calcGridDimensions (amount: number): { rowAmount: number; columnAmount: number } {
286
+ let rowAmount = Math.floor(Math.sqrt(amount))
287
+ let columnAmount = Math.ceil(amount / rowAmount)
288
+ while (rowAmount * columnAmount < amount) {
289
+ columnAmount++
290
+ }
291
+ return { rowAmount, columnAmount }
292
+ }
293
+
294
+ export function calcSeriesContainerLayout (layout: Layout, container: DataFormatterContainer, amount: number): ContainerPosition[] {
295
+ const { gap } = container
296
+ const { rowAmount, columnAmount } = (container.rowAmount * container.columnAmount) >= amount
297
+ // 如果container設定的rowAmount和columnAmount的乘積大於或等於amount,則使用目前設定
298
+ ? container
299
+ // 否則計算一個合適的預設值
300
+ : calcGridDimensions(amount)
301
+
302
+ return new Array(amount).fill(null).map((_, index) => {
303
+ const columnIndex = index % columnAmount
304
+ const rowIndex = Math.floor(index / columnAmount)
305
+
306
+ const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
307
+ const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
308
+ const x = columnIndex * width + (columnIndex * gap)
309
+ const y = rowIndex * height + (rowIndex * gap)
310
+ // const translate: [number, number] = [x, y]
311
+
312
+ return {
313
+ slotIndex: index,
314
+ rowIndex,
315
+ columnIndex,
316
+ // translate,
317
+ startX: x,
318
+ startY: y,
319
+ centerX: x + width / 2,
320
+ centerY: y + height / 2,
321
+ width,
322
+ height
323
+ }
324
+ })
325
+ }
326
+
327
+ export function calcGridContainerLayout (layout: Layout, container: DataFormatterContainer, amount: number): ContainerPositionScaled[] {
328
+ const { gap } = container
329
+ const { rowAmount, columnAmount } = (container.rowAmount * container.columnAmount) >= amount
330
+ // 如果container設定的rowAmount和columnAmount的乘積大於或等於amount,則使用目前設定
331
+ ? container
332
+ // 否則計算一個合適的預設值
333
+ : calcGridDimensions(amount)
334
+
335
+ return new Array(amount).fill(null).map((_, index) => {
336
+ const columnIndex = index % columnAmount
337
+ const rowIndex = Math.floor(index / columnAmount)
338
+
339
+ const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
340
+ const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
341
+ const x = columnIndex * width + (columnIndex * gap)
342
+ const y = rowIndex * height + (rowIndex * gap)
343
+ const translate: [number, number] = [x, y]
344
+ const scale: [number, number] = [width / layout.width, height / layout.height]
345
+
346
+ return {
347
+ slotIndex: index,
348
+ rowIndex,
349
+ columnIndex,
350
+ translate,
351
+ scale
352
+ }
353
+ })
354
+ }
355
+
356
+ // // multiGrid datum color
357
+ // export function multiGridColorPredicate ({ seriesIndex, groupIndex, data, chartParams }: {
358
+ // seriesIndex: number
359
+ // groupIndex: number
360
+ // data: ComputedDataMultiGrid
361
+ // chartParams: ChartParams
362
+ // }) {
363
+ // // 累加前面的grid的seriesIndex
364
+ // const accSeriesIndex = data.reduce((prev, current) => {
365
+ // if (current[0] && current[0][0] && groupIndex > current[0][0].gridIndex) {
366
+ // return prev + current[0].length
367
+ // } else if (current[0] && current[0][0] && groupIndex == current[0][0].gridIndex) {
368
+ // return prev + seriesIndex
369
+ // } else {
370
+ // return prev
371
+ // }
372
+ // }, 0)
373
+
374
+ // return seriesColorPredicate(accSeriesIndex, chartParams)
375
+ // }
376
+
377
+