@orbcharts/core 3.0.0-alpha.60 → 3.0.0-alpha.62

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. package/LICENSE +200 -200
  2. package/dist/lib/core-types.d.ts +1 -0
  3. package/dist/orbcharts-core.es.js +3056 -2609
  4. package/dist/orbcharts-core.umd.js +6 -2
  5. package/dist/src/AbstractChart.d.ts +5 -3
  6. package/dist/src/GridChart.d.ts +1 -1
  7. package/dist/src/MultiGridChart.d.ts +1 -1
  8. package/dist/src/MultiValueChart.d.ts +1 -1
  9. package/dist/src/RelationshipChart.d.ts +1 -1
  10. package/dist/src/SeriesChart.d.ts +1 -1
  11. package/dist/src/TreeChart.d.ts +1 -1
  12. package/dist/src/base/createBaseChart.d.ts +1 -1
  13. package/dist/src/base/createBasePlugin.d.ts +1 -1
  14. package/dist/src/base/validators/chartOptionsValidator.d.ts +3 -0
  15. package/dist/src/base/validators/chartParamsValidator.d.ts +3 -0
  16. package/dist/src/base/validators/elementValidator.d.ts +3 -0
  17. package/dist/src/base/validators/pluginsValidator.d.ts +3 -0
  18. package/dist/src/defaults.d.ts +1 -16
  19. package/dist/src/defineGridPlugin.d.ts +1 -1
  20. package/dist/src/defineMultiGridPlugin.d.ts +1 -1
  21. package/dist/src/defineMultiValuePlugin.d.ts +1 -1
  22. package/dist/src/defineNoneDataPlugin.d.ts +1 -1
  23. package/dist/src/defineRelationshipPlugin.d.ts +1 -1
  24. package/dist/src/defineSeriesPlugin.d.ts +1 -1
  25. package/dist/src/defineTreePlugin.d.ts +1 -1
  26. package/dist/src/grid/computedDataFn.d.ts +4 -0
  27. package/dist/src/grid/contextObserverCallback.d.ts +3 -0
  28. package/dist/src/grid/dataFormatterValidator.d.ts +3 -0
  29. package/dist/src/grid/dataValidator.d.ts +3 -0
  30. package/dist/src/index.d.ts +1 -1
  31. package/dist/src/multiGrid/computedDataFn.d.ts +3 -0
  32. package/dist/src/multiGrid/contextObserverCallback.d.ts +3 -0
  33. package/dist/src/multiGrid/dataFormatterValidator.d.ts +3 -0
  34. package/dist/src/multiGrid/dataValidator.d.ts +3 -0
  35. package/dist/src/multiValue/computedDataFn.d.ts +3 -0
  36. package/dist/src/multiValue/contextObserverCallback.d.ts +3 -0
  37. package/dist/src/multiValue/dataFormatterValidator.d.ts +3 -0
  38. package/dist/src/multiValue/dataValidator.d.ts +3 -0
  39. package/dist/src/relationship/computedDataFn.d.ts +3 -0
  40. package/dist/src/relationship/contextObserverCallback.d.ts +3 -0
  41. package/dist/src/relationship/dataFormatterValidator.d.ts +3 -0
  42. package/dist/src/relationship/dataValidator.d.ts +3 -0
  43. package/dist/src/series/computedDataFn.d.ts +3 -0
  44. package/dist/src/series/contextObserverCallback.d.ts +3 -0
  45. package/dist/src/series/dataFormatterValidator.d.ts +3 -0
  46. package/dist/src/series/dataValidator.d.ts +3 -0
  47. package/dist/src/tree/computedDataFn.d.ts +3 -0
  48. package/dist/src/tree/contextObserverCallback.d.ts +3 -0
  49. package/dist/src/tree/dataFormatterValidator.d.ts +3 -0
  50. package/dist/src/tree/dataValidator.d.ts +3 -0
  51. package/dist/src/utils/commonUtils.d.ts +1 -0
  52. package/dist/src/utils/errorMessage.d.ts +14 -0
  53. package/dist/src/{grid → utils}/gridObservables.d.ts +2 -2
  54. package/dist/src/utils/index.d.ts +7 -3
  55. package/dist/src/{multiGrid → utils}/multiGridObservables.d.ts +1 -1
  56. package/dist/src/utils/observables.d.ts +2 -1
  57. package/dist/src/utils/orbchartsUtils.d.ts +1 -12
  58. package/dist/src/{series → utils}/seriesObservables.d.ts +3 -3
  59. package/dist/src/{tree → utils}/treeObservables.d.ts +1 -1
  60. package/dist/src/utils/validator.d.ts +3 -0
  61. package/lib/core-types.ts +7 -0
  62. package/package.json +42 -41
  63. package/src/AbstractChart.ts +57 -48
  64. package/src/GridChart.ts +24 -20
  65. package/src/MultiGridChart.ts +24 -20
  66. package/src/MultiValueChart.ts +24 -20
  67. package/src/RelationshipChart.ts +24 -20
  68. package/src/SeriesChart.ts +24 -20
  69. package/src/TreeChart.ts +24 -20
  70. package/src/base/createBaseChart.ts +500 -386
  71. package/src/base/createBasePlugin.ts +152 -95
  72. package/src/base/validators/chartOptionsValidator.ts +24 -0
  73. package/src/base/validators/chartParamsValidator.ts +134 -0
  74. package/src/base/validators/elementValidator.ts +14 -0
  75. package/src/base/validators/pluginsValidator.ts +15 -0
  76. package/src/defaults.ts +232 -226
  77. package/src/defineGridPlugin.ts +3 -3
  78. package/src/defineMultiGridPlugin.ts +3 -3
  79. package/src/defineMultiValuePlugin.ts +3 -3
  80. package/src/defineNoneDataPlugin.ts +4 -4
  81. package/src/defineRelationshipPlugin.ts +3 -3
  82. package/src/defineSeriesPlugin.ts +3 -3
  83. package/src/defineTreePlugin.ts +3 -3
  84. package/src/grid/{computeGridData.ts → computedDataFn.ts} +129 -134
  85. package/src/grid/{createGridContextObserver.ts → contextObserverCallback.ts} +155 -155
  86. package/src/grid/dataFormatterValidator.ts +9 -0
  87. package/src/grid/dataValidator.ts +9 -0
  88. package/src/index.ts +20 -21
  89. package/src/multiGrid/{computeMultiGridData.ts → computedDataFn.ts} +123 -130
  90. package/src/multiGrid/{createMultiGridContextObserver.ts → contextObserverCallback.ts} +41 -41
  91. package/src/multiGrid/dataFormatterValidator.ts +9 -0
  92. package/src/multiGrid/dataValidator.ts +9 -0
  93. package/src/multiValue/{computeMultiValueData.ts → computedDataFn.ts} +176 -179
  94. package/src/multiValue/{createMultiValueContextObserver.ts → contextObserverCallback.ts} +12 -12
  95. package/src/multiValue/dataFormatterValidator.ts +9 -0
  96. package/src/multiValue/dataValidator.ts +9 -0
  97. package/src/relationship/{computeRelationshipData.ts → computedDataFn.ts} +125 -118
  98. package/src/relationship/{createRelationshipContextObserver.ts → contextObserverCallback.ts} +12 -12
  99. package/src/relationship/dataFormatterValidator.ts +9 -0
  100. package/src/relationship/dataValidator.ts +9 -0
  101. package/src/series/{computeSeriesData.ts → computedDataFn.ts} +88 -90
  102. package/src/series/{createSeriesContextObserver.ts → contextObserverCallback.ts} +100 -93
  103. package/src/series/dataFormatterValidator.ts +9 -0
  104. package/src/series/dataValidator.ts +9 -0
  105. package/src/tree/{computeTreeData.ts → computedDataFn.ts} +130 -132
  106. package/src/tree/{createTreeContextObserver.ts → contextObserverCallback.ts} +61 -61
  107. package/src/tree/dataFormatterValidator.ts +9 -0
  108. package/src/tree/dataValidator.ts +9 -0
  109. package/src/utils/commonUtils.ts +54 -50
  110. package/src/utils/d3Utils.ts +108 -108
  111. package/src/utils/errorMessage.ts +43 -0
  112. package/src/{grid → utils}/gridObservables.ts +611 -614
  113. package/src/utils/index.ts +10 -4
  114. package/src/{multiGrid → utils}/multiGridObservables.ts +366 -365
  115. package/src/utils/observables.ts +218 -202
  116. package/src/utils/orbchartsUtils.ts +352 -349
  117. package/src/{series → utils}/seriesObservables.ts +175 -175
  118. package/src/{tree → utils}/treeObservables.ts +94 -94
  119. package/src/utils/validator.ts +126 -0
  120. package/tsconfig.base.json +13 -13
  121. package/tsconfig.json +2 -2
  122. package/vite-env.d.ts +7 -0
  123. package/vite.config.js +22 -22
  124. package/dist/src/grid/computeGridData.d.ts +0 -6
  125. package/dist/src/grid/createGridContextObserver.d.ts +0 -3
  126. package/dist/src/multiGrid/computeMultiGridData.d.ts +0 -3
  127. package/dist/src/multiGrid/createMultiGridContextObserver.d.ts +0 -3
  128. package/dist/src/multiValue/computeMultiValueData.d.ts +0 -3
  129. package/dist/src/multiValue/createMultiValueContextObserver.d.ts +0 -3
  130. package/dist/src/relationship/computeRelationshipData.d.ts +0 -3
  131. package/dist/src/relationship/createRelationshipContextObserver.d.ts +0 -3
  132. package/dist/src/series/computeSeriesData.d.ts +0 -3
  133. package/dist/src/series/createSeriesContextObserver.d.ts +0 -3
  134. package/dist/src/tree/computeTreeData.d.ts +0 -3
  135. package/dist/src/tree/createTreeContextObserver.d.ts +0 -3
  136. package/dist/src/types/Axis.d.ts +0 -1
  137. package/dist/src/types/Chart.d.ts +0 -45
  138. package/dist/src/types/ChartParams.d.ts +0 -36
  139. package/dist/src/types/ComputedData.d.ts +0 -42
  140. package/dist/src/types/ComputedDataGrid.d.ts +0 -5
  141. package/dist/src/types/ComputedDataMultiGrid.d.ts +0 -3
  142. package/dist/src/types/ComputedDataMultiValue.d.ts +0 -6
  143. package/dist/src/types/ComputedDataRelationship.d.ts +0 -18
  144. package/dist/src/types/ComputedDataSeries.d.ts +0 -5
  145. package/dist/src/types/ComputedDataTree.d.ts +0 -7
  146. package/dist/src/types/ContextObserver.d.ts +0 -28
  147. package/dist/src/types/ContextObserverGrid.d.ts +0 -41
  148. package/dist/src/types/ContextObserverMultiGrid.d.ts +0 -15
  149. package/dist/src/types/ContextObserverMultiValue.d.ts +0 -4
  150. package/dist/src/types/ContextObserverRelationship.d.ts +0 -4
  151. package/dist/src/types/ContextObserverSeries.d.ts +0 -27
  152. package/dist/src/types/ContextObserverTree.d.ts +0 -11
  153. package/dist/src/types/ContextSubject.d.ts +0 -15
  154. package/dist/src/types/Data.d.ts +0 -19
  155. package/dist/src/types/DataFormatter.d.ts +0 -41
  156. package/dist/src/types/DataFormatterGrid.d.ts +0 -34
  157. package/dist/src/types/DataFormatterMultiGrid.d.ts +0 -20
  158. package/dist/src/types/DataFormatterMultiValue.d.ts +0 -18
  159. package/dist/src/types/DataFormatterRelationship.d.ts +0 -10
  160. package/dist/src/types/DataFormatterSeries.d.ts +0 -19
  161. package/dist/src/types/DataFormatterTree.d.ts +0 -7
  162. package/dist/src/types/DataGrid.d.ts +0 -6
  163. package/dist/src/types/DataMultiGrid.d.ts +0 -6
  164. package/dist/src/types/DataMultiValue.d.ts +0 -7
  165. package/dist/src/types/DataRelationship.d.ts +0 -21
  166. package/dist/src/types/DataSeries.d.ts +0 -6
  167. package/dist/src/types/DataTree.d.ts +0 -15
  168. package/dist/src/types/Event.d.ts +0 -56
  169. package/dist/src/types/Layout.d.ts +0 -8
  170. package/dist/src/types/Padding.d.ts +0 -6
  171. package/dist/src/types/Plugin.d.ts +0 -37
  172. package/dist/src/types/TransformData.d.ts +0 -8
  173. package/dist/src/types/index.d.ts +0 -37
  174. package/src/types/Axis.ts +0 -1
  175. package/src/types/Chart.ts +0 -54
  176. package/src/types/ChartParams.ts +0 -51
  177. package/src/types/ComputedData.ts +0 -84
  178. package/src/types/ComputedDataGrid.ts +0 -14
  179. package/src/types/ComputedDataMultiGrid.ts +0 -3
  180. package/src/types/ComputedDataMultiValue.ts +0 -9
  181. package/src/types/ComputedDataRelationship.ts +0 -20
  182. package/src/types/ComputedDataSeries.ts +0 -8
  183. package/src/types/ComputedDataTree.ts +0 -20
  184. package/src/types/ContextObserver.ts +0 -38
  185. package/src/types/ContextObserverGrid.ts +0 -43
  186. package/src/types/ContextObserverMultiGrid.ts +0 -17
  187. package/src/types/ContextObserverMultiValue.ts +0 -5
  188. package/src/types/ContextObserverRelationship.ts +0 -5
  189. package/src/types/ContextObserverSeries.ts +0 -29
  190. package/src/types/ContextObserverTree.ts +0 -11
  191. package/src/types/ContextSubject.ts +0 -18
  192. package/src/types/Data.ts +0 -45
  193. package/src/types/DataFormatter.ts +0 -74
  194. package/src/types/DataFormatterGrid.ts +0 -68
  195. package/src/types/DataFormatterMultiGrid.ts +0 -45
  196. package/src/types/DataFormatterMultiValue.ts +0 -24
  197. package/src/types/DataFormatterRelationship.ts +0 -26
  198. package/src/types/DataFormatterSeries.ts +0 -20
  199. package/src/types/DataFormatterTree.ts +0 -12
  200. package/src/types/DataGrid.ts +0 -11
  201. package/src/types/DataMultiGrid.ts +0 -7
  202. package/src/types/DataMultiValue.ts +0 -12
  203. package/src/types/DataRelationship.ts +0 -28
  204. package/src/types/DataSeries.ts +0 -11
  205. package/src/types/DataTree.ts +0 -20
  206. package/src/types/Event.ts +0 -153
  207. package/src/types/Layout.ts +0 -12
  208. package/src/types/Padding.ts +0 -6
  209. package/src/types/Plugin.ts +0 -60
  210. package/src/types/TransformData.ts +0 -8
  211. package/src/types/index.ts +0 -37
  212. /package/dist/src/{multiValue → utils}/multiValueObservables.d.ts +0 -0
  213. /package/dist/src/{relationship → utils}/relationshipObservables.d.ts +0 -0
  214. /package/src/{multiValue → utils}/multiValueObservables.ts +0 -0
  215. /package/src/{relationship → utils}/relationshipObservables.ts +0 -0
@@ -1,349 +1,352 @@
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
-
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
+