@orbcharts/core 3.0.0-alpha.42 → 3.0.0-alpha.43

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +1905 -1896
  3. package/dist/orbcharts-core.umd.js +2 -2
  4. package/dist/src/defaults.d.ts +4 -3
  5. package/dist/src/grid/computeGridData.d.ts +4 -11
  6. package/dist/src/grid/gridObservables.d.ts +15 -18
  7. package/dist/src/multiGrid/multiGridObservables.d.ts +4 -11
  8. package/dist/src/series/seriesObservables.d.ts +26 -1
  9. package/dist/src/types/ComputedData.d.ts +1 -0
  10. package/dist/src/types/ComputedDataGrid.d.ts +0 -3
  11. package/dist/src/types/ComputedDataSeries.d.ts +1 -2
  12. package/dist/src/types/ContextObserverGrid.d.ts +11 -4
  13. package/dist/src/types/ContextObserverMultiGrid.d.ts +8 -3
  14. package/dist/src/types/ContextObserverSeries.d.ts +18 -0
  15. package/dist/src/types/DataFormatter.d.ts +8 -5
  16. package/dist/src/types/DataFormatterGrid.d.ts +13 -16
  17. package/dist/src/types/DataFormatterMultiGrid.d.ts +6 -3
  18. package/dist/src/types/DataFormatterMultiValue.d.ts +3 -0
  19. package/dist/src/types/DataFormatterRelationship.d.ts +3 -0
  20. package/dist/src/types/DataFormatterSeries.d.ts +10 -3
  21. package/dist/src/utils/orbchartsUtils.d.ts +14 -13
  22. package/package.json +41 -41
  23. package/src/AbstractChart.ts +48 -48
  24. package/src/GridChart.ts +20 -20
  25. package/src/MultiGridChart.ts +20 -20
  26. package/src/MultiValueChart.ts +20 -20
  27. package/src/RelationshipChart.ts +20 -20
  28. package/src/SeriesChart.ts +20 -20
  29. package/src/TreeChart.ts +20 -20
  30. package/src/base/createBaseChart.ts +367 -367
  31. package/src/base/createBasePlugin.ts +89 -89
  32. package/src/defaults.ts +220 -248
  33. package/src/defineGridPlugin.ts +3 -3
  34. package/src/defineMultiGridPlugin.ts +3 -3
  35. package/src/defineMultiValuePlugin.ts +3 -3
  36. package/src/defineNoneDataPlugin.ts +4 -4
  37. package/src/defineRelationshipPlugin.ts +3 -3
  38. package/src/defineSeriesPlugin.ts +3 -3
  39. package/src/defineTreePlugin.ts +3 -3
  40. package/src/grid/computeGridData.ts +134 -205
  41. package/src/grid/createGridContextObserver.ts +147 -130
  42. package/src/grid/gridObservables.ts +573 -486
  43. package/src/index.ts +21 -21
  44. package/src/multiGrid/computeMultiGridData.ts +130 -173
  45. package/src/multiGrid/createMultiGridContextObserver.ts +40 -40
  46. package/src/multiGrid/multiGridObservables.ts +350 -285
  47. package/src/multiValue/computeMultiValueData.ts +143 -136
  48. package/src/multiValue/createMultiValueContextObserver.ts +12 -12
  49. package/src/relationship/computeRelationshipData.ts +118 -106
  50. package/src/relationship/createRelationshipContextObserver.ts +12 -12
  51. package/src/series/computeSeriesData.ts +90 -153
  52. package/src/series/createSeriesContextObserver.ts +93 -38
  53. package/src/series/seriesObservables.ts +176 -24
  54. package/src/tree/computeTreeData.ts +131 -128
  55. package/src/tree/createTreeContextObserver.ts +61 -61
  56. package/src/tree/treeObservables.ts +94 -94
  57. package/src/types/Chart.ts +48 -48
  58. package/src/types/ChartParams.ts +51 -51
  59. package/src/types/ComputedData.ts +83 -82
  60. package/src/types/ComputedDataGrid.ts +13 -13
  61. package/src/types/ComputedDataMultiGrid.ts +2 -2
  62. package/src/types/ComputedDataMultiValue.ts +9 -9
  63. package/src/types/ComputedDataRelationship.ts +19 -19
  64. package/src/types/ComputedDataSeries.ts +8 -8
  65. package/src/types/ComputedDataTree.ts +19 -19
  66. package/src/types/ContextObserver.ts +38 -38
  67. package/src/types/ContextObserverGrid.ts +41 -33
  68. package/src/types/ContextObserverMultiGrid.ts +16 -28
  69. package/src/types/ContextObserverMultiValue.ts +4 -4
  70. package/src/types/ContextObserverRelationship.ts +4 -4
  71. package/src/types/ContextObserverSeries.ts +29 -9
  72. package/src/types/ContextObserverTree.ts +11 -11
  73. package/src/types/ContextSubject.ts +18 -18
  74. package/src/types/Data.ts +45 -45
  75. package/src/types/DataFormatter.ts +74 -95
  76. package/src/types/DataFormatterGrid.ts +67 -55
  77. package/src/types/DataFormatterMultiGrid.ts +44 -42
  78. package/src/types/DataFormatterMultiValue.ts +23 -20
  79. package/src/types/DataFormatterRelationship.ts +25 -22
  80. package/src/types/DataFormatterSeries.ts +20 -30
  81. package/src/types/DataFormatterTree.ts +12 -12
  82. package/src/types/DataGrid.ts +11 -11
  83. package/src/types/DataMultiGrid.ts +6 -6
  84. package/src/types/DataMultiValue.ts +12 -12
  85. package/src/types/DataRelationship.ts +27 -27
  86. package/src/types/DataSeries.ts +11 -11
  87. package/src/types/DataTree.ts +20 -20
  88. package/src/types/Event.ts +153 -153
  89. package/src/types/Layout.ts +11 -11
  90. package/src/types/Padding.ts +5 -5
  91. package/src/types/Plugin.ts +60 -60
  92. package/src/types/TransformData.ts +7 -7
  93. package/src/types/index.ts +37 -37
  94. package/src/utils/commonUtils.ts +50 -50
  95. package/src/utils/d3Utils.ts +89 -89
  96. package/src/utils/index.ts +4 -4
  97. package/src/utils/observables.ts +201 -201
  98. package/src/utils/orbchartsUtils.ts +349 -253
  99. package/tsconfig.json +13 -13
  100. package/vite.config.js +44 -44
@@ -1,253 +1,349 @@
1
- import * as d3 from 'd3'
2
- import type { ChartType } from '../types/Chart'
3
- import type { ChartParams } from '../types/ChartParams'
4
- import type { DatumBase, DatumValue } from '../types/Data'
5
- import type { DataSeries, DataSeriesDatum, DataSeriesValue } from '../types/DataSeries'
6
- import type { DataGrid, DataGridDatum, DataGridValue } from '../types/DataGrid'
7
- import type { DataMultiGrid } from '../types/DataMultiGrid'
8
- import type { DataMultiValue, DataMultiValueDatum, DataMultiValueValue } from '../types/DataMultiValue'
9
- import type { SeriesDirection, DataFormatterGrid, DataFormatterGridContainer } from '../types/DataFormatterGrid'
10
- import type { DataFormatterMultiGrid } from '../types/DataFormatterMultiGrid'
11
- import type { ComputedDatumSeriesValue } from '../types/ComputedData'
12
- import type { ComputedDatumSeries } from '../types/ComputedDataSeries'
13
- import type { ComputedDatumGrid, ComputedDataGrid } from '../types/ComputedDataGrid'
14
- import type { ComputedDataMultiGrid } from '../types/ComputedDataMultiGrid'
15
- import type { Layout } from '../types/Layout'
16
- // import type { ComputedDatumMultiGrid } from '../types/ComputedDataMultiGrid'
17
- import { isPlainObject } from './commonUtils'
18
-
19
- export function formatValueToLabel (value: any, valueFormatter: string | ((text: d3.NumberValue) => string)) {
20
- if (valueFormatter! instanceof Function == true) {
21
- return (valueFormatter as ((text: d3.NumberValue) => string))(value)
22
- }
23
- return d3.format(valueFormatter as string)!(value)
24
- }
25
-
26
- export function createDefaultDatumId (chartTypeOrPrefix: string, levelOneIndex: number, levelTwoIndex: number, levelThreeIndex?: number) {
27
- let text = `${chartTypeOrPrefix}_${levelOneIndex}_${levelTwoIndex}`
28
- if (levelThreeIndex != null) {
29
- text += `_${levelThreeIndex}`
30
- }
31
- return text
32
- }
33
-
34
- export function createDefaultSeriesLabel (chartTypeOrPrefix: string, seriesIndex: number) {
35
- return `${chartTypeOrPrefix}_series${seriesIndex}`
36
- }
37
-
38
- export function createDefaultGroupLabel (chartTypeOrPrefix: string, groupIndex: number) {
39
- return `${chartTypeOrPrefix}_group${groupIndex}`
40
- }
41
-
42
- export function createGridSeriesLabels ({ transposedDataGrid, dataFormatter, chartType = 'grid' }: {
43
- transposedDataGrid: DataGridDatum[][],
44
- dataFormatter: DataFormatterGrid
45
- chartType?: ChartType
46
- }) {
47
- const labels = dataFormatter.grid.gridData.seriesDirection === 'row'
48
- ? dataFormatter.grid.gridData.rowLabels
49
- : dataFormatter.grid.gridData.columnLabels
50
- return transposedDataGrid.map((_, rowIndex) => {
51
- return labels[rowIndex] != null
52
- ? labels[rowIndex]
53
- : createDefaultSeriesLabel(chartType, rowIndex)
54
- })
55
- }
56
-
57
- export function createMultiGridSeriesLabels ({ transposedDataGrid, dataFormatter, chartType = 'multiGrid', gridIndex = 0 }: {
58
- transposedDataGrid: DataGridDatum[][],
59
- dataFormatter: DataFormatterGrid
60
- chartType?: ChartType
61
- gridIndex?: number
62
- }) {
63
- const labels = dataFormatter.grid.gridData.seriesDirection === 'row'
64
- ? dataFormatter.grid.gridData.rowLabels
65
- : dataFormatter.grid.gridData.columnLabels
66
- return transposedDataGrid.map((_, rowIndex) => {
67
- return labels[rowIndex] != null
68
- ? labels[rowIndex]
69
- : createDefaultSeriesLabel(`${chartType}_grid${gridIndex}`, rowIndex)
70
- })
71
- }
72
-
73
- export function createGridGroupLabels ({ transposedDataGrid, dataFormatter, chartType = 'grid' }: {
74
- transposedDataGrid: DataGridDatum[][],
75
- dataFormatter: DataFormatterGrid
76
- chartType?: ChartType
77
- }) {
78
- if (transposedDataGrid[0] == null) {
79
- return []
80
- }
81
- const labels = dataFormatter.grid.gridData.seriesDirection === 'row'
82
- ? dataFormatter.grid.gridData.columnLabels
83
- : dataFormatter.grid.gridData.rowLabels
84
- return transposedDataGrid[0].map((_, columnLabels) => {
85
- return labels[columnLabels] != null
86
- ? labels[columnLabels]
87
- : createDefaultGroupLabel(chartType, columnLabels)
88
- })
89
- }
90
-
91
- export function createMultiGridGroupLabels ({ transposedDataGrid, dataFormatter, chartType = 'multiGrid', gridIndex = 0 }: {
92
- transposedDataGrid: DataGridDatum[][],
93
- dataFormatter: DataFormatterGrid
94
- chartType?: ChartType
95
- gridIndex?: number
96
- }) {
97
- if (transposedDataGrid[0] == null) {
98
- return []
99
- }
100
- const labels = dataFormatter.grid.gridData.seriesDirection === 'row'
101
- ? dataFormatter.grid.gridData.columnLabels
102
- : dataFormatter.grid.gridData.rowLabels
103
- return transposedDataGrid[0].map((_, columnLabels) => {
104
- return labels[columnLabels] != null
105
- ? labels[columnLabels]
106
- : createDefaultGroupLabel(`${chartType}_grid${gridIndex}`, columnLabels)
107
- })
108
- }
109
-
110
- // 取得最小及最大值 - 數字陣列
111
- export function getMinAndMax (data: number[]): [number, number] {
112
- const defaultMinAndMax: [number, number] = [0, 0] // default
113
- if (!data.length) {
114
- return defaultMinAndMax
115
- }
116
- const minAndMax: [number, number] = data.reduce((prev, current) => {
117
- // [min, max]
118
- return [
119
- current < prev[0] ? current : prev[0],
120
- current > prev[1] ? current : prev[1]
121
- ]
122
- }, [data[0], data[0]])
123
- return minAndMax
124
- }
125
-
126
- // 取得最小及最大值 - datum格式陣列資料
127
- export function getMinAndMaxValue (data: DatumValue[]): [number, number] {
128
- const arr = data
129
- .filter(d => d != null && d.value != null)
130
- .map(d => d.value )
131
- return getMinAndMax(arr)
132
- }
133
-
134
- // 取得最小及最大值 - Series Data
135
- export function getMinAndMaxSeries (data: DataSeries): [number, number] {
136
- const flatData: (DataSeriesValue | DataSeriesDatum)[] = data[0] && Array.isArray((data as (DataSeriesValue | DataSeriesDatum)[][])[0])
137
- ? data.flat()
138
- : data as (DataSeriesValue | DataSeriesDatum)[]
139
- const arr = flatData
140
- .filter(d => (d == null || (isPlainObject(d) && (d as DataSeriesDatum).value == null)) === false) // 過濾掉null &
141
- .map(d => typeof d === 'number' ? d : d.value )
142
- return getMinAndMax(arr)
143
- }
144
-
145
- // 取得最小及最大值 - Grid Data
146
- export function getMinAndMaxGrid (data: DataGrid): [number, number] {
147
- const flatData: (DataGridValue | DataGridDatum)[] = data.flat()
148
- const arr = flatData
149
- .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
150
- .map(d => typeof d === 'number' ? d : d.value )
151
- return getMinAndMax(arr)
152
- }
153
-
154
- // 取得最小及最大值 - MultiGrid Data
155
- export function getMinAndMaxMultiGrid (data: DataMultiGrid): [number, number] {
156
- const flatData: (DataGridValue | DataGridDatum)[] = data.flat().flat()
157
- const arr = flatData
158
- .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
159
- .map(d => typeof d === 'number' ? d : d.value )
160
- return getMinAndMax(arr)
161
- }
162
-
163
- // 取得最小及最大值 - MultiValue Data
164
- export function getMinAndMaxMultiValue (data: DataMultiValue, valueIndex: number = 2): [number, number] {
165
- const flatData: (DataMultiValueDatum | DataMultiValueValue)[] = data.flat().filter((d, i) => i == valueIndex)
166
- const arr = flatData
167
- .filter(d => (d == null || (isPlainObject(d) && (d as DataMultiValueDatum).value == null)) === false) // 過濾掉null
168
- .map(d => typeof d === 'number' ? d : d.value )
169
- return getMinAndMax(arr)
170
- }
171
-
172
- // @Q@ 待處理
173
- // // 取得最小及最大值 - Relationship Data
174
- // export function getMinAndMaxRelationship (data: DataRelationship, target: 'nodes' | 'edges' = 'nodes'): [number, number] {
175
-
176
- // }
177
-
178
- // @Q@ 待處理
179
- // // 取得最小及最大值 - Tree Data
180
- // export function getMinAndMaxTree (data: DataTree): [number, number] {
181
-
182
- // }
183
-
184
- // 轉置成seriesDirection為main的陣列格式
185
- export function transposeData<T> (seriesDirection: SeriesDirection, data: T[][]): T[][] {
186
- if (seriesDirection === 'row') {
187
- return Object.assign([], data)
188
- }
189
- // 取得原始陣列的維度
190
- const rows = data.length;
191
- const cols = data.reduce((prev, current) => {
192
- return Math.max(prev, current.length)
193
- }, 0)
194
-
195
- // 初始化轉換後的陣列
196
- const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
197
-
198
- // 遍歷原始陣列,進行轉換
199
- for (let i = 0; i < rows; i++) {
200
- for (let j = 0; j < cols; j++) {
201
- transposedArray[j][i] = data[i][j]
202
- }
203
- }
204
-
205
- return transposedArray
206
- }
207
-
208
-
209
- export function seriesColorPredicate (seriesIndex: number, chartParams: ChartParams) {
210
- return seriesIndex < chartParams.colors[chartParams.colorScheme].series.length
211
- ? chartParams.colors[chartParams.colorScheme].series[seriesIndex]
212
- : chartParams.colors[chartParams.colorScheme].series[
213
- seriesIndex % chartParams.colors[chartParams.colorScheme].series.length
214
- ]
215
- }
216
-
217
- export function calcGridContainerPosition (layout: Layout, container: DataFormatterGridContainer, rowIndex: number, columnIndex: number) {
218
- const { gap, rowAmount, columnAmount } = container
219
- const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
220
- const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
221
- const x = columnIndex * width + (columnIndex * gap)
222
- const y = rowIndex * height + (rowIndex * gap)
223
- const translate: [number, number] = [x, y]
224
- const scale: [number, number] = [width / layout.width, height / layout.height]
225
-
226
- return {
227
- translate,
228
- scale
229
- }
230
- }
231
-
232
- // // multiGrid datum color
233
- // export function multiGridColorPredicate ({ seriesIndex, groupIndex, data, chartParams }: {
234
- // seriesIndex: number
235
- // groupIndex: number
236
- // data: ComputedDataMultiGrid
237
- // chartParams: ChartParams
238
- // }) {
239
- // // 累加前面的grid的seriesIndex
240
- // const accSeriesIndex = data.reduce((prev, current) => {
241
- // if (current[0] && current[0][0] && groupIndex > current[0][0].gridIndex) {
242
- // return prev + current[0].length
243
- // } else if (current[0] && current[0][0] && groupIndex == current[0][0].gridIndex) {
244
- // return prev + seriesIndex
245
- // } else {
246
- // return prev
247
- // }
248
- // }, 0)
249
-
250
- // return seriesColorPredicate(accSeriesIndex, chartParams)
251
- // }
252
-
253
-
1
+ import * as d3 from 'd3'
2
+ import type { ChartType } from '../types/Chart'
3
+ import type { ChartParams } from '../types/ChartParams'
4
+ import type { DatumBase, DatumValue } from '../types/Data'
5
+ import type { DataSeries, DataSeriesDatum, DataSeriesValue } from '../types/DataSeries'
6
+ import type { DataGrid, DataGridDatum, DataGridValue } from '../types/DataGrid'
7
+ import type { DataMultiGrid } from '../types/DataMultiGrid'
8
+ import type { DataMultiValue, DataMultiValueDatum, DataMultiValueValue } from '../types/DataMultiValue'
9
+ import type { DataFormatterContainer } from '../types/DataFormatter'
10
+ import type { SeriesDirection, DataFormatterGrid, DataFormatterGridGrid } from '../types/DataFormatterGrid'
11
+ import type { DataFormatterMultiGrid } from '../types/DataFormatterMultiGrid'
12
+ import type { ComputedDatumSeriesValue } from '../types/ComputedData'
13
+ import type { ComputedDatumSeries } from '../types/ComputedDataSeries'
14
+ import type { ComputedDatumGrid, ComputedDataGrid } from '../types/ComputedDataGrid'
15
+ import type { ComputedDataMultiGrid } from '../types/ComputedDataMultiGrid'
16
+ import type { SeriesContainerPosition } from '../types/ContextObserverSeries'
17
+ import type { GridContainerPosition } from '../types/ContextObserverGrid'
18
+ import type { Layout } from '../types/Layout'
19
+ // import type { ComputedDatumMultiGrid } from '../types/ComputedDataMultiGrid'
20
+ import { isPlainObject } from './commonUtils'
21
+
22
+ export function formatValueToLabel (value: any, valueFormatter: string | ((text: d3.NumberValue) => string)) {
23
+ if (valueFormatter! instanceof Function == true) {
24
+ return (valueFormatter as ((text: d3.NumberValue) => string))(value)
25
+ }
26
+ return d3.format(valueFormatter as string)!(value)
27
+ }
28
+
29
+ export function createDefaultDatumId (chartTypeOrPrefix: string, levelOneIndex: number, levelTwoIndex: number, levelThreeIndex?: number) {
30
+ let text = `${chartTypeOrPrefix}_${levelOneIndex}_${levelTwoIndex}`
31
+ if (levelThreeIndex != null) {
32
+ text += `_${levelThreeIndex}`
33
+ }
34
+ return text
35
+ }
36
+
37
+ export function createDefaultSeriesLabel (chartTypeOrPrefix: string, seriesIndex: number) {
38
+ return `${chartTypeOrPrefix}_series${seriesIndex}`
39
+ }
40
+
41
+ export function createDefaultGroupLabel (chartTypeOrPrefix: string, groupIndex: number) {
42
+ return `${chartTypeOrPrefix}_group${groupIndex}`
43
+ }
44
+
45
+ export function createGridSeriesLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'grid' }: {
46
+ transposedDataGrid: DataGridDatum[][],
47
+ dataFormatterGrid: DataFormatterGridGrid
48
+ chartType?: ChartType
49
+ }) {
50
+ const labels = dataFormatterGrid.seriesDirection === 'row'
51
+ ? dataFormatterGrid.rowLabels
52
+ : dataFormatterGrid.columnLabels
53
+ return transposedDataGrid.map((_, rowIndex) => {
54
+ return labels[rowIndex] != null
55
+ ? labels[rowIndex]
56
+ : createDefaultSeriesLabel(chartType, rowIndex)
57
+ })
58
+ }
59
+
60
+ export function createMultiGridSeriesLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'multiGrid', gridIndex = 0 }: {
61
+ transposedDataGrid: DataGridDatum[][],
62
+ dataFormatterGrid: DataFormatterGridGrid
63
+ chartType?: ChartType
64
+ gridIndex?: number
65
+ }) {
66
+ const labels = dataFormatterGrid.seriesDirection === 'row'
67
+ ? dataFormatterGrid.rowLabels
68
+ : dataFormatterGrid.columnLabels
69
+ return transposedDataGrid.map((_, rowIndex) => {
70
+ return labels[rowIndex] != null
71
+ ? labels[rowIndex]
72
+ : createDefaultSeriesLabel(`${chartType}_grid${gridIndex}`, rowIndex)
73
+ })
74
+ }
75
+
76
+ export function createGridGroupLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'grid' }: {
77
+ transposedDataGrid: DataGridDatum[][],
78
+ dataFormatterGrid: DataFormatterGridGrid
79
+ chartType?: ChartType
80
+ }) {
81
+ if (transposedDataGrid[0] == null) {
82
+ return []
83
+ }
84
+ const labels = dataFormatterGrid.seriesDirection === 'row'
85
+ ? dataFormatterGrid.columnLabels
86
+ : dataFormatterGrid.rowLabels
87
+ return transposedDataGrid[0].map((_, columnLabels) => {
88
+ return labels[columnLabels] != null
89
+ ? labels[columnLabels]
90
+ : createDefaultGroupLabel(chartType, columnLabels)
91
+ })
92
+ }
93
+
94
+ export function createMultiGridGroupLabels ({ transposedDataGrid, dataFormatterGrid, chartType = 'multiGrid', gridIndex = 0 }: {
95
+ transposedDataGrid: DataGridDatum[][],
96
+ dataFormatterGrid: DataFormatterGridGrid
97
+ chartType?: ChartType
98
+ gridIndex?: number
99
+ }) {
100
+ if (transposedDataGrid[0] == null) {
101
+ return []
102
+ }
103
+ const labels = dataFormatterGrid.seriesDirection === 'row'
104
+ ? dataFormatterGrid.columnLabels
105
+ : dataFormatterGrid.rowLabels
106
+ return transposedDataGrid[0].map((_, columnLabels) => {
107
+ return labels[columnLabels] != null
108
+ ? labels[columnLabels]
109
+ : createDefaultGroupLabel(`${chartType}_grid${gridIndex}`, columnLabels)
110
+ })
111
+ }
112
+
113
+ // 取得最小及最大值 - 數字陣列
114
+ export function getMinAndMax (data: number[]): [number, number] {
115
+ const defaultMinAndMax: [number, number] = [0, 0] // default
116
+ if (!data.length) {
117
+ return defaultMinAndMax
118
+ }
119
+ const minAndMax: [number, number] = data.reduce((prev, current) => {
120
+ // [min, max]
121
+ return [
122
+ current < prev[0] ? current : prev[0],
123
+ current > prev[1] ? current : prev[1]
124
+ ]
125
+ }, [data[0], data[0]])
126
+ return minAndMax
127
+ }
128
+
129
+ // 取得最小及最大值 - datum格式陣列資料
130
+ export function getMinAndMaxValue (data: DatumValue[]): [number, number] {
131
+ const arr = data
132
+ .filter(d => d != null && d.value != null)
133
+ .map(d => d.value )
134
+ return getMinAndMax(arr)
135
+ }
136
+
137
+ // 取得最小及最大值 - Series Data
138
+ export function getMinAndMaxSeries (data: DataSeries): [number, number] {
139
+ const flatData: (DataSeriesValue | DataSeriesDatum)[] = data[0] && Array.isArray((data as (DataSeriesValue | DataSeriesDatum)[][])[0])
140
+ ? data.flat()
141
+ : data as (DataSeriesValue | DataSeriesDatum)[]
142
+ const arr = flatData
143
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataSeriesDatum).value == null)) === false) // 過濾掉null &
144
+ .map(d => typeof d === 'number' ? d : d.value )
145
+ return getMinAndMax(arr)
146
+ }
147
+
148
+ // 取得最小及最大值 - Grid Data
149
+ export function getMinAndMaxGrid (data: DataGrid): [number, number] {
150
+ const flatData: (DataGridValue | DataGridDatum)[] = data.flat()
151
+ const arr = flatData
152
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
153
+ .map(d => typeof d === 'number' ? d : d.value )
154
+ return getMinAndMax(arr)
155
+ }
156
+
157
+ // 取得最小及最大值 - MultiGrid Data
158
+ export function getMinAndMaxMultiGrid (data: DataMultiGrid): [number, number] {
159
+ const flatData: (DataGridValue | DataGridDatum)[] = data.flat().flat()
160
+ const arr = flatData
161
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataGridDatum).value == null)) === false) // 過濾掉null
162
+ .map(d => typeof d === 'number' ? d : d.value )
163
+ return getMinAndMax(arr)
164
+ }
165
+
166
+ // 取得最小及最大值 - MultiValue Data
167
+ export function getMinAndMaxMultiValue (data: DataMultiValue, valueIndex: number = 2): [number, number] {
168
+ const flatData: (DataMultiValueDatum | DataMultiValueValue)[] = data.flat().filter((d, i) => i == valueIndex)
169
+ const arr = flatData
170
+ .filter(d => (d == null || (isPlainObject(d) && (d as DataMultiValueDatum).value == null)) === false) // 過濾掉null
171
+ .map(d => typeof d === 'number' ? d : d.value )
172
+ return getMinAndMax(arr)
173
+ }
174
+
175
+ // @Q@ 待處理
176
+ // // 取得最小及最大值 - Relationship Data
177
+ // export function getMinAndMaxRelationship (data: DataRelationship, target: 'nodes' | 'edges' = 'nodes'): [number, number] {
178
+
179
+ // }
180
+
181
+ // @Q@ 待處理
182
+ // // 取得最小及最大值 - Tree Data
183
+ // export function getMinAndMaxTree (data: DataTree): [number, number] {
184
+
185
+ // }
186
+
187
+ // 轉置成seriesDirection為main的陣列格式
188
+ export function transposeData<T> (seriesDirection: SeriesDirection, data: T[][]): T[][] {
189
+ if (seriesDirection === 'row') {
190
+ return Object.assign([], data)
191
+ }
192
+ // 取得原始陣列的維度
193
+ const rows = data.length;
194
+ const cols = data.reduce((prev, current) => {
195
+ return Math.max(prev, current.length)
196
+ }, 0)
197
+
198
+ // 初始化轉換後的陣列
199
+ const transposedArray = new Array(cols).fill(null).map(() => new Array(rows).fill(null))
200
+
201
+ // 遍歷原始陣列,進行轉換
202
+ for (let i = 0; i < rows; i++) {
203
+ for (let j = 0; j < cols; j++) {
204
+ transposedArray[j][i] = data[i][j]
205
+ }
206
+ }
207
+
208
+ return transposedArray
209
+ }
210
+
211
+
212
+ export function seriesColorPredicate (seriesIndex: number, chartParams: ChartParams) {
213
+ return seriesIndex < chartParams.colors[chartParams.colorScheme].series.length
214
+ ? chartParams.colors[chartParams.colorScheme].series[seriesIndex]
215
+ : chartParams.colors[chartParams.colorScheme].series[
216
+ seriesIndex % chartParams.colors[chartParams.colorScheme].series.length
217
+ ]
218
+ }
219
+
220
+ // export function calcSeriesContainerPosition (layout: Layout, container: DataFormatterContainer, rowIndex: number, columnIndex: number) {
221
+ // const { gap, rowAmount, columnAmount } = container
222
+ // const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
223
+ // const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
224
+ // const x = columnIndex * width + (columnIndex * gap)
225
+ // const y = rowIndex * height + (rowIndex * gap)
226
+ // // const translate: [number, number] = [x, y]
227
+
228
+ // return {
229
+ // // translate,
230
+ // startX: x,
231
+ // startY: y,
232
+ // centerX: x + width / 2,
233
+ // centerY: y + height / 2,
234
+ // width,
235
+ // height
236
+ // }
237
+ // }
238
+
239
+ // 計算預設欄列數量
240
+ // 規則1.rowAmount*columnAmount要大於或等於amount,並且數字要盡可能小
241
+ // 規則2.columnAmount要大於或等於rowAmount,並且數字要盡可能小
242
+ function calcGridDimensions (amount: number): { rowAmount: number; columnAmount: number } {
243
+ let rowAmount = Math.floor(Math.sqrt(amount))
244
+ let columnAmount = Math.ceil(amount / rowAmount)
245
+ while (rowAmount * columnAmount < amount) {
246
+ columnAmount++
247
+ }
248
+ return { rowAmount, columnAmount }
249
+ }
250
+
251
+ export function calcSeriesContainerLayout (layout: Layout, container: DataFormatterContainer, amount: number): SeriesContainerPosition[] {
252
+ const { gap } = container
253
+ const { rowAmount, columnAmount } = (container.rowAmount * container.columnAmount) >= amount
254
+ // 如果container設定的rowAmount和columnAmount的乘積大於或等於amount,則使用目前設定
255
+ ? container
256
+ // 否則計算一個合適的預設值
257
+ : calcGridDimensions(amount)
258
+
259
+ return new Array(amount).fill(null).map((_, index) => {
260
+ const columnIndex = index % columnAmount
261
+ const rowIndex = Math.floor(index / columnAmount)
262
+
263
+ const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
264
+ const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
265
+ const x = columnIndex * width + (columnIndex * gap)
266
+ const y = rowIndex * height + (rowIndex * gap)
267
+ // const translate: [number, number] = [x, y]
268
+
269
+ return {
270
+ slotIndex: index,
271
+ rowIndex,
272
+ columnIndex,
273
+ // translate,
274
+ startX: x,
275
+ startY: y,
276
+ centerX: x + width / 2,
277
+ centerY: y + height / 2,
278
+ width,
279
+ height
280
+ }
281
+ })
282
+ }
283
+
284
+ // export function calcGridContainerPosition (layout: Layout, container: DataFormatterContainer, rowIndex: number, columnIndex: number) {
285
+ // const { gap, rowAmount, columnAmount } = container
286
+ // const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
287
+ // const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
288
+ // const x = columnIndex * width + (columnIndex * gap)
289
+ // const y = rowIndex * height + (rowIndex * gap)
290
+ // const translate: [number, number] = [x, y]
291
+ // const scale: [number, number] = [width / layout.width, height / layout.height]
292
+
293
+ // return {
294
+ // translate,
295
+ // scale
296
+ // }
297
+ // }
298
+
299
+ export function calcGridContainerLayout (layout: Layout, container: DataFormatterContainer, amount: number): GridContainerPosition[] {
300
+ const { gap } = container
301
+ const { rowAmount, columnAmount } = (container.rowAmount * container.columnAmount) >= amount
302
+ // 如果container設定的rowAmount和columnAmount的乘積大於或等於amount,則使用目前設定
303
+ ? container
304
+ // 否則計算一個合適的預設值
305
+ : calcGridDimensions(amount)
306
+
307
+ return new Array(amount).fill(null).map((_, index) => {
308
+ const columnIndex = index % columnAmount
309
+ const rowIndex = Math.floor(index / columnAmount)
310
+
311
+ const width = (layout.width - (gap * (columnAmount - 1))) / columnAmount
312
+ const height = (layout.height - (gap * (rowAmount - 1))) / rowAmount
313
+ const x = columnIndex * width + (columnIndex * gap)
314
+ const y = rowIndex * height + (rowIndex * gap)
315
+ const translate: [number, number] = [x, y]
316
+ const scale: [number, number] = [width / layout.width, height / layout.height]
317
+
318
+ return {
319
+ slotIndex: index,
320
+ rowIndex,
321
+ columnIndex,
322
+ translate,
323
+ scale
324
+ }
325
+ })
326
+ }
327
+
328
+ // // multiGrid datum color
329
+ // export function multiGridColorPredicate ({ seriesIndex, groupIndex, data, chartParams }: {
330
+ // seriesIndex: number
331
+ // groupIndex: number
332
+ // data: ComputedDataMultiGrid
333
+ // chartParams: ChartParams
334
+ // }) {
335
+ // // 累加前面的grid的seriesIndex
336
+ // const accSeriesIndex = data.reduce((prev, current) => {
337
+ // if (current[0] && current[0][0] && groupIndex > current[0][0].gridIndex) {
338
+ // return prev + current[0].length
339
+ // } else if (current[0] && current[0][0] && groupIndex == current[0][0].gridIndex) {
340
+ // return prev + seriesIndex
341
+ // } else {
342
+ // return prev
343
+ // }
344
+ // }, 0)
345
+
346
+ // return seriesColorPredicate(accSeriesIndex, chartParams)
347
+ // }
348
+
349
+
package/tsconfig.json CHANGED
@@ -1,14 +1,14 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "./dist/",
4
- "sourceMap": true,
5
- "noImplicitAny": true,
6
- "module": "es6",
7
- "target": "es5",
8
- "jsx": "react",
9
- "allowJs": true,
10
- "moduleResolution": "node",
11
- "allowSyntheticDefaultImports" : true,
12
- "esModuleInterop" : true
13
- }
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist/",
4
+ "sourceMap": true,
5
+ "noImplicitAny": true,
6
+ "module": "es6",
7
+ "target": "es5",
8
+ "jsx": "react",
9
+ "allowJs": true,
10
+ "moduleResolution": "node",
11
+ "allowSyntheticDefaultImports" : true,
12
+ "esModuleInterop" : true
13
+ }
14
14
  }