@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.
- package/LICENSE +200 -200
- package/dist/orbcharts-core.es.js +4 -3
- package/dist/orbcharts-core.umd.js +1 -1
- package/lib/core-types.ts +7 -7
- package/package.json +42 -42
- package/src/AbstractChart.ts +57 -57
- package/src/GridChart.ts +24 -24
- package/src/MultiGridChart.ts +24 -24
- package/src/MultiValueChart.ts +24 -24
- package/src/RelationshipChart.ts +24 -24
- package/src/SeriesChart.ts +24 -24
- package/src/TreeChart.ts +24 -24
- package/src/base/createBaseChart.ts +505 -505
- package/src/base/createBasePlugin.ts +153 -153
- package/src/base/validators/chartOptionsValidator.ts +23 -23
- package/src/base/validators/chartParamsValidator.ts +133 -133
- package/src/base/validators/elementValidator.ts +13 -13
- package/src/base/validators/pluginsValidator.ts +14 -14
- package/src/defaults.ts +232 -232
- package/src/defineGridPlugin.ts +3 -3
- package/src/defineMultiGridPlugin.ts +3 -3
- package/src/defineMultiValuePlugin.ts +3 -3
- package/src/defineNoneDataPlugin.ts +4 -4
- package/src/defineRelationshipPlugin.ts +3 -3
- package/src/defineSeriesPlugin.ts +3 -3
- package/src/defineTreePlugin.ts +3 -3
- package/src/grid/computedDataFn.ts +129 -129
- package/src/grid/contextObserverCallback.ts +155 -155
- package/src/grid/dataFormatterValidator.ts +101 -101
- package/src/grid/dataValidator.ts +12 -12
- package/src/index.ts +20 -20
- package/src/multiGrid/computedDataFn.ts +123 -123
- package/src/multiGrid/contextObserverCallback.ts +41 -41
- package/src/multiGrid/dataFormatterValidator.ts +115 -115
- package/src/multiGrid/dataValidator.ts +12 -12
- package/src/multiValue/computedDataFn.ts +176 -176
- package/src/multiValue/contextObserverCallback.ts +12 -12
- package/src/multiValue/dataFormatterValidator.ts +9 -9
- package/src/multiValue/dataValidator.ts +9 -9
- package/src/relationship/computedDataFn.ts +125 -125
- package/src/relationship/contextObserverCallback.ts +12 -12
- package/src/relationship/dataFormatterValidator.ts +9 -9
- package/src/relationship/dataValidator.ts +9 -9
- package/src/series/computedDataFn.ts +88 -88
- package/src/series/contextObserverCallback.ts +100 -100
- package/src/series/dataFormatterValidator.ts +41 -41
- package/src/series/dataValidator.ts +12 -12
- package/src/tree/computedDataFn.ts +130 -130
- package/src/tree/contextObserverCallback.ts +61 -61
- package/src/tree/dataFormatterValidator.ts +13 -13
- package/src/tree/dataValidator.ts +13 -13
- package/src/utils/commonUtils.ts +55 -55
- package/src/utils/d3Utils.ts +108 -108
- package/src/utils/errorMessage.ts +42 -42
- package/src/utils/gridObservables.ts +614 -611
- package/src/utils/index.ts +9 -9
- package/src/utils/multiGridObservables.ts +366 -366
- package/src/utils/observables.ts +219 -219
- package/src/utils/orbchartsUtils.ts +352 -352
- package/src/utils/seriesObservables.ts +175 -175
- package/src/utils/treeObservables.ts +94 -94
- package/src/utils/validator.ts +125 -125
- package/tsconfig.base.json +13 -13
- package/tsconfig.json +2 -2
- package/vite-env.d.ts +6 -6
- package/vite.config.js +22 -22
@@ -1,352 +1,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
|
-
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
|
+
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
|
+
|