@orbcharts/core 3.0.6 → 4.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +2826 -6582
  3. package/dist/orbcharts-core.umd.js +6 -6
  4. package/dist/src/OrbCharts.d.ts +18 -0
  5. package/dist/src/chart/createChart.d.ts +3 -0
  6. package/dist/src/chart/createGraphData.d.ts +3 -0
  7. package/dist/src/chart/createGridData.d.ts +3 -0
  8. package/dist/src/chart/createMultivariateData.d.ts +3 -0
  9. package/dist/src/chart/createSeriesData.d.ts +3 -0
  10. package/dist/src/chart/createTreeData.d.ts +3 -0
  11. package/dist/src/chart/defaults.d.ts +5 -0
  12. package/dist/src/defineCanvasLayer.d.ts +16 -0
  13. package/dist/src/defineCanvasPlugin.d.ts +22 -0
  14. package/dist/src/defineSVGLayer.d.ts +16 -0
  15. package/dist/src/defineSVGPlugin.d.ts +22 -0
  16. package/dist/src/index.d.ts +6 -14
  17. package/dist/src/layer/createLayer.d.ts +3 -0
  18. package/dist/src/plugin/createPlugin.d.ts +3 -0
  19. package/dist/src/test/createGraphData.test.d.ts +1 -0
  20. package/dist/src/test/createTreeData.test.d.ts +1 -0
  21. package/dist/src/test/simple-graph-test.d.ts +74 -0
  22. package/dist/src/test/simple-tree-test.d.ts +13 -0
  23. package/dist/src/types/Chart.d.ts +39 -0
  24. package/dist/src/types/ChartContext.d.ts +27 -0
  25. package/dist/src/types/Common.d.ts +3 -0
  26. package/dist/src/types/Encoding.d.ts +33 -0
  27. package/dist/src/types/Event.d.ts +12 -0
  28. package/dist/src/types/Layers.d.ts +55 -0
  29. package/dist/src/types/ModelData.d.ts +70 -0
  30. package/dist/src/types/Plugin.d.ts +39 -0
  31. package/dist/src/types/RawData.d.ts +18 -0
  32. package/dist/src/types/RenderData.d.ts +4 -0
  33. package/dist/src/types/Theme.d.ts +17 -0
  34. package/dist/src/types/Validator.d.ts +20 -0
  35. package/dist/src/types/index.d.ts +12 -0
  36. package/dist/src/utils/aggregateUtils.d.ts +37 -0
  37. package/dist/src/utils/colorUtils.d.ts +22 -0
  38. package/dist/src/utils/commonUtils.d.ts +3 -5
  39. package/dist/src/utils/dom-lifecycle.d.ts +37 -0
  40. package/dist/src/utils/dom.d.ts +6 -0
  41. package/dist/src/utils/index.d.ts +5 -1
  42. package/dist/src/utils/observables.d.ts +1 -25
  43. package/dist/src/utils/orbchartsUtils.d.ts +2 -53
  44. package/dist/src/utils/validator.d.ts +2 -2
  45. package/dist/test/aggregateTest.d.ts +1 -0
  46. package/package.json +57 -46
  47. package/src/OrbCharts.ts +35 -0
  48. package/src/chart/createChart.ts +997 -0
  49. package/src/chart/createGraphData.ts +391 -0
  50. package/src/chart/createGridData.ts +247 -0
  51. package/src/chart/createMultivariateData.ts +181 -0
  52. package/src/chart/createSeriesData.ts +297 -0
  53. package/src/chart/createTreeData.ts +344 -0
  54. package/src/chart/defaults.ts +120 -0
  55. package/src/defineCanvasLayer.ts +24 -0
  56. package/src/defineCanvasPlugin.ts +39 -0
  57. package/src/defineSVGLayer.ts +24 -0
  58. package/src/defineSVGPlugin.ts +39 -0
  59. package/src/index.ts +8 -20
  60. package/src/layer/createLayer.ts +138 -0
  61. package/src/plugin/createPlugin.ts +470 -0
  62. package/src/test/createGraphData.test.ts +103 -0
  63. package/src/test/createTreeData.test.ts +97 -0
  64. package/src/test/simple-graph-test.js +51 -0
  65. package/src/test/simple-tree-test.js +58 -0
  66. package/src/types/Chart.ts +62 -0
  67. package/src/types/ChartContext.ts +42 -0
  68. package/src/types/Common.ts +5 -0
  69. package/src/types/Encoding.ts +43 -0
  70. package/src/types/Event.ts +26 -0
  71. package/src/types/Layers.ts +93 -0
  72. package/src/types/ModelData.ts +95 -0
  73. package/src/types/Plugin.ts +98 -0
  74. package/src/types/RawData.ts +67 -0
  75. package/src/types/RenderData.ts +16 -0
  76. package/src/types/Theme.ts +21 -0
  77. package/src/types/Validator.ts +36 -0
  78. package/src/types/index.ts +12 -0
  79. package/src/utils/aggregateUtils.ts +99 -0
  80. package/src/utils/colorUtils.ts +63 -0
  81. package/src/utils/commonUtils.ts +56 -55
  82. package/src/utils/dom-lifecycle.ts +164 -0
  83. package/src/utils/dom.ts +55 -0
  84. package/src/utils/errorMessage.ts +40 -40
  85. package/src/utils/index.ts +8 -4
  86. package/src/utils/observables.ts +16 -308
  87. package/src/utils/orbchartsUtils.ts +9 -396
  88. package/src/utils/validator.ts +127 -126
  89. package/dist/lib/core-types.d.ts +0 -1
  90. package/dist/src/AbstractChart.d.ts +0 -19
  91. package/dist/src/GridChart.d.ts +0 -6
  92. package/dist/src/MultiGridChart.d.ts +0 -6
  93. package/dist/src/MultiValueChart.d.ts +0 -6
  94. package/dist/src/RelationshipChart.d.ts +0 -6
  95. package/dist/src/SeriesChart.d.ts +0 -6
  96. package/dist/src/TreeChart.d.ts +0 -6
  97. package/dist/src/base/createBaseChart.d.ts +0 -3
  98. package/dist/src/base/createBasePlugin.d.ts +0 -3
  99. package/dist/src/base/validators/chartOptionsValidator.d.ts +0 -3
  100. package/dist/src/base/validators/chartParamsValidator.d.ts +0 -3
  101. package/dist/src/base/validators/elementValidator.d.ts +0 -3
  102. package/dist/src/base/validators/pluginsValidator.d.ts +0 -3
  103. package/dist/src/defaults.d.ts +0 -25
  104. package/dist/src/defineGridPlugin.d.ts +0 -1
  105. package/dist/src/defineMultiGridPlugin.d.ts +0 -1
  106. package/dist/src/defineMultiValuePlugin.d.ts +0 -1
  107. package/dist/src/defineNoneDataPlugin.d.ts +0 -1
  108. package/dist/src/defineRelationshipPlugin.d.ts +0 -1
  109. package/dist/src/defineSeriesPlugin.d.ts +0 -1
  110. package/dist/src/defineTreePlugin.d.ts +0 -1
  111. package/dist/src/grid/computedDataFn.d.ts +0 -4
  112. package/dist/src/grid/contextObserverCallback.d.ts +0 -3
  113. package/dist/src/grid/dataFormatterValidator.d.ts +0 -3
  114. package/dist/src/grid/dataValidator.d.ts +0 -3
  115. package/dist/src/grid/gridObservables.d.ts +0 -64
  116. package/dist/src/multiGrid/computedDataFn.d.ts +0 -3
  117. package/dist/src/multiGrid/contextObserverCallback.d.ts +0 -3
  118. package/dist/src/multiGrid/dataFormatterValidator.d.ts +0 -3
  119. package/dist/src/multiGrid/dataValidator.d.ts +0 -3
  120. package/dist/src/multiGrid/multiGridObservables.d.ts +0 -16
  121. package/dist/src/multiValue/computedDataFn.d.ts +0 -3
  122. package/dist/src/multiValue/contextObserverCallback.d.ts +0 -3
  123. package/dist/src/multiValue/dataFormatterValidator.d.ts +0 -3
  124. package/dist/src/multiValue/dataValidator.d.ts +0 -3
  125. package/dist/src/multiValue/multiValueObservables.d.ts +0 -130
  126. package/dist/src/relationship/computedDataFn.d.ts +0 -3
  127. package/dist/src/relationship/contextObserverCallback.d.ts +0 -3
  128. package/dist/src/relationship/dataFormatterValidator.d.ts +0 -3
  129. package/dist/src/relationship/dataValidator.d.ts +0 -3
  130. package/dist/src/relationship/relationshipObservables.d.ts +0 -13
  131. package/dist/src/series/computedDataFn.d.ts +0 -3
  132. package/dist/src/series/contextObserverCallback.d.ts +0 -3
  133. package/dist/src/series/dataFormatterValidator.d.ts +0 -3
  134. package/dist/src/series/dataValidator.d.ts +0 -3
  135. package/dist/src/series/seriesObservables.d.ts +0 -37
  136. package/dist/src/tree/computedDataFn.d.ts +0 -3
  137. package/dist/src/tree/contextObserverCallback.d.ts +0 -3
  138. package/dist/src/tree/dataFormatterValidator.d.ts +0 -3
  139. package/dist/src/tree/dataValidator.d.ts +0 -3
  140. package/dist/src/tree/treeObservables.d.ts +0 -10
  141. package/dist/src/utils/d3Scale.d.ts +0 -28
  142. package/lib/core-types.ts +0 -7
  143. package/src/AbstractChart.ts +0 -57
  144. package/src/GridChart.ts +0 -25
  145. package/src/MultiGridChart.ts +0 -25
  146. package/src/MultiValueChart.ts +0 -25
  147. package/src/RelationshipChart.ts +0 -25
  148. package/src/SeriesChart.ts +0 -25
  149. package/src/TreeChart.ts +0 -25
  150. package/src/base/createBaseChart.ts +0 -524
  151. package/src/base/createBasePlugin.ts +0 -154
  152. package/src/base/validators/chartOptionsValidator.ts +0 -24
  153. package/src/base/validators/chartParamsValidator.ts +0 -134
  154. package/src/base/validators/elementValidator.ts +0 -14
  155. package/src/base/validators/pluginsValidator.ts +0 -15
  156. package/src/defaults.ts +0 -284
  157. package/src/defineGridPlugin.ts +0 -3
  158. package/src/defineMultiGridPlugin.ts +0 -3
  159. package/src/defineMultiValuePlugin.ts +0 -3
  160. package/src/defineNoneDataPlugin.ts +0 -4
  161. package/src/defineRelationshipPlugin.ts +0 -3
  162. package/src/defineSeriesPlugin.ts +0 -3
  163. package/src/defineTreePlugin.ts +0 -3
  164. package/src/grid/computedDataFn.ts +0 -129
  165. package/src/grid/contextObserverCallback.ts +0 -209
  166. package/src/grid/dataFormatterValidator.ts +0 -126
  167. package/src/grid/dataValidator.ts +0 -13
  168. package/src/grid/gridObservables.ts +0 -699
  169. package/src/multiGrid/computedDataFn.ts +0 -123
  170. package/src/multiGrid/contextObserverCallback.ts +0 -109
  171. package/src/multiGrid/dataFormatterValidator.ts +0 -121
  172. package/src/multiGrid/dataValidator.ts +0 -13
  173. package/src/multiGrid/multiGridObservables.ts +0 -367
  174. package/src/multiValue/computedDataFn.ts +0 -113
  175. package/src/multiValue/contextObserverCallback.ts +0 -328
  176. package/src/multiValue/dataFormatterValidator.ts +0 -95
  177. package/src/multiValue/dataValidator.ts +0 -13
  178. package/src/multiValue/multiValueObservables.ts +0 -865
  179. package/src/relationship/computedDataFn.ts +0 -159
  180. package/src/relationship/contextObserverCallback.ts +0 -80
  181. package/src/relationship/dataFormatterValidator.ts +0 -14
  182. package/src/relationship/dataValidator.ts +0 -14
  183. package/src/relationship/relationshipObservables.ts +0 -85
  184. package/src/series/computedDataFn.ts +0 -88
  185. package/src/series/contextObserverCallback.ts +0 -132
  186. package/src/series/dataFormatterValidator.ts +0 -47
  187. package/src/series/dataValidator.ts +0 -13
  188. package/src/series/seriesObservables.ts +0 -210
  189. package/src/tree/computedDataFn.ts +0 -129
  190. package/src/tree/contextObserverCallback.ts +0 -58
  191. package/src/tree/dataFormatterValidator.ts +0 -14
  192. package/src/tree/dataValidator.ts +0 -14
  193. package/src/tree/treeObservables.ts +0 -106
  194. package/src/utils/d3Scale.ts +0 -198
  195. package/tsconfig.base.json +0 -14
  196. package/tsconfig.json +0 -3
  197. package/vite-env.d.ts +0 -7
  198. package/vite.config.js +0 -23
@@ -1,524 +0,0 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- iif,
5
- of,
6
- EMPTY,
7
- Subject,
8
- BehaviorSubject,
9
- Observable,
10
- first,
11
- takeUntil,
12
- catchError,
13
- throwError } from 'rxjs'
14
- import {
15
- map,
16
- mergeWith,
17
- concatMap,
18
- switchMap,
19
- switchAll,
20
- throttleTime,
21
- debounceTime,
22
- distinctUntilChanged,
23
- share,
24
- shareReplay,
25
- filter,
26
- take,
27
- startWith,
28
- scan,
29
- } from 'rxjs/operators'
30
- import type {
31
- CreateBaseChart,
32
- CreateChart,
33
- ComputedDataFn,
34
- ChartEntity,
35
- ChartType,
36
- ChartParams,
37
- ChartParamsPartial,
38
- ContextSubject,
39
- ComputedDataTypeMap,
40
- ContextObserverCallback,
41
- ChartOptionsPartial,
42
- DataTypeMap,
43
- DataFormatterTypeMap,
44
- DataFormatterPartialTypeMap,
45
- DataFormatterBase,
46
- DataFormatterContext,
47
- DataFormatterValidator,
48
- DataValidator,
49
- Layout,
50
- PluginEntity,
51
- PluginContext,
52
- Preset,
53
- PresetPartial,
54
- ContextObserverTypeMap,
55
- ValidatorResult,
56
- } from '../../lib/core-types'
57
- import { mergeOptionsWithDefault, resizeObservable } from '../utils'
58
- import { createValidatorErrorMessage, createValidatorWarningMessage, createUnexpectedErrorMessage, createOrbChartsErrorMessage } from '../utils/errorMessage'
59
- import { chartOptionsValidator } from './validators/chartOptionsValidator'
60
- import { elementValidator } from './validators/elementValidator'
61
- import { chartParamsValidator } from './validators/chartParamsValidator'
62
- import { pluginsValidator } from './validators/pluginsValidator'
63
- import {
64
- DEFAULT_CHART_OPTIONS,
65
- DEFAULT_PADDING,
66
- DEFAULT_CHART_PARAMS,
67
- DEFAULT_CHART_WIDTH,
68
- DEFAULT_CHART_HEIGHT } from '../defaults'
69
-
70
- // 判斷dataFormatter是否需要size參數
71
- // const isAxesTypeMap: {[key in ChartType]: Boolean} = {
72
- // series: false,
73
- // grid: true,
74
- // multiGrid: true,
75
- // multiValue: true,
76
- // tree: false,
77
- // relationship: false
78
- // }
79
-
80
-
81
- function mergeDataFormatter <T>(dataFormatter: any, defaultDataFormatter: T, chartType: ChartType): T {
82
- const mergedData = mergeOptionsWithDefault(dataFormatter, defaultDataFormatter)
83
-
84
- if (chartType === 'multiGrid' && (dataFormatter as DataFormatterPartialTypeMap<'multiGrid'>).gridList != null) {
85
- // multiGrid欄位為陣列,需要各別來merge預設值
86
- (mergedData as DataFormatterTypeMap<'multiGrid'>).gridList = (dataFormatter as DataFormatterPartialTypeMap<'multiGrid'>).gridList.map((d, i) => {
87
- const defaultGrid = (defaultDataFormatter as DataFormatterTypeMap<'multiGrid'>).gridList[i] || (defaultDataFormatter as DataFormatterTypeMap<'multiGrid'>).gridList[0]
88
- return mergeOptionsWithDefault(d, defaultGrid)
89
- })
90
- }
91
- return mergedData
92
- }
93
-
94
- export const createBaseChart: CreateBaseChart = <T extends ChartType>({
95
- defaultDataFormatter,
96
- dataFormatterValidator,
97
- computedDataFn,
98
- dataValidator,
99
- contextObserverCallback
100
- }: {
101
- defaultDataFormatter: DataFormatterTypeMap<T>
102
- dataFormatterValidator: DataFormatterValidator<T>
103
- computedDataFn: ComputedDataFn<T>
104
- dataValidator: DataValidator<T>
105
- contextObserverCallback: ContextObserverCallback<T>
106
- }): CreateChart<T> => {
107
- const destroy$ = new Subject()
108
-
109
- const chartType: ChartType = (defaultDataFormatter as unknown as DataFormatterBase<any>).type
110
-
111
- // 建立chart實例
112
- return function createChart (element: HTMLElement | Element, options?: ChartOptionsPartial<T>): ChartEntity<T> {
113
- try {
114
- const { status, columnName, expectToBe } = chartOptionsValidator(options)
115
- if (status === 'error') {
116
- throw new Error(createValidatorErrorMessage({
117
- columnName,
118
- expectToBe,
119
- from: 'Chart.constructor'
120
- }))
121
- } else if (status === 'warning') {
122
- console.warn(createValidatorWarningMessage({
123
- columnName,
124
- expectToBe,
125
- from: 'Chart.constructor'
126
- }))
127
- } else {
128
- const { status, columnName, expectToBe } = elementValidator(element)
129
- if (status === 'error') {
130
- throw new Error(createValidatorErrorMessage({
131
- columnName,
132
- expectToBe,
133
- from: 'Chart.constructor'
134
- }))
135
- } else if (status === 'warning') {
136
- console.warn(createValidatorWarningMessage({
137
- columnName,
138
- expectToBe,
139
- from: 'Chart.constructor'
140
- }))
141
- }
142
- }
143
- } catch (e) {
144
- throw new Error(e)
145
- }
146
-
147
-
148
- // -- selections --
149
- // svg selection
150
- d3.select(element).selectAll('svg').remove()
151
- const svgSelection = d3.select(element).append('svg')
152
- svgSelection
153
- .attr('xmlns:xlink', 'http://www.w3.org/1999/xlink')
154
- .attr('xmls', 'http://www.w3.org/2000/svg')
155
- .attr('version', '1.1')
156
- .style('position', 'absolute')
157
- // .style('width', '100%')
158
- // .style('height', '100%')
159
- .classed('orbcharts__root', true)
160
- // 傳入操作的 selection
161
- const selectionLayout = svgSelection.append('g')
162
- selectionLayout.classed('orbcharts__layout', true)
163
- const selectionPlugins = selectionLayout.append('g')
164
- selectionPlugins.classed('orbcharts__plugins', true)
165
-
166
- // chartSubject
167
- const chartSubject: ContextSubject<T> = {
168
- data$: new Subject(),
169
- dataFormatter$: new Subject(),
170
- plugins$: new Subject(),
171
- chartParams$: new Subject(),
172
- event$: new Subject()
173
- }
174
-
175
- // options
176
- const mergedPresetWithDefault: Preset<T, any> = ((options) => {
177
- const _options = options ? options : DEFAULT_CHART_OPTIONS as ChartOptionsPartial<T>
178
- const preset = _options.preset ? _options.preset : {} as PresetPartial<T, any>
179
-
180
- return {
181
- name: preset.name ?? '',
182
- description: preset.description ?? '',
183
- descriptionZh: preset.descriptionZh ?? '',
184
- chartParams: preset.chartParams
185
- ? mergeOptionsWithDefault(preset.chartParams, DEFAULT_CHART_PARAMS)
186
- : DEFAULT_CHART_PARAMS,
187
- dataFormatter: preset.dataFormatter
188
- // ? mergeOptionsWithDefault(preset.dataFormatter, defaultDataFormatter)
189
- ? mergeDataFormatter(preset.dataFormatter, defaultDataFormatter, chartType)
190
- : defaultDataFormatter,
191
- pluginParams: preset.pluginParams
192
- ? preset.pluginParams
193
- : {}
194
- }
195
- })(options)
196
-
197
- const sharedData$ = chartSubject.data$.pipe(shareReplay(1))
198
- const shareAndMergedDataFormatter$ = chartSubject.dataFormatter$
199
- .pipe(
200
- takeUntil(destroy$),
201
- startWith({} as DataFormatterPartialTypeMap<T>),
202
- map((dataFormatter) => {
203
- try {
204
- // 檢查 dataFormatter$ 資料格式是否正確
205
- const { status, columnName, expectToBe } = dataFormatterValidator(dataFormatter)
206
- if (status === 'error') {
207
- throw new Error(createValidatorErrorMessage({
208
- columnName,
209
- expectToBe,
210
- from: 'Chart.dataFormatter$'
211
- }))
212
- } else if (status === 'warning') {
213
- console.warn(createValidatorWarningMessage({
214
- columnName,
215
- expectToBe,
216
- from: 'Chart.dataFormatter$'
217
- }))
218
- }
219
- } catch (e) {
220
- // 不中斷資料流
221
- console.error(createOrbChartsErrorMessage(e))
222
- }
223
- try {
224
- return mergeDataFormatter(dataFormatter, mergedPresetWithDefault.dataFormatter, chartType)
225
- } catch (e) {
226
- throw new Error(createUnexpectedErrorMessage({
227
- from: 'Chart.dataFormatter$',
228
- systemMessage: e
229
- }))
230
- }
231
- }),
232
- catchError((e) => {
233
- console.error(createOrbChartsErrorMessage(e))
234
- return EMPTY
235
- }),
236
- shareReplay(1)
237
- )
238
- const shareAndMergedChartParams$ = chartSubject.chartParams$
239
- .pipe(
240
- takeUntil(destroy$),
241
- startWith({}),
242
- map((d) => {
243
- try {
244
- // 檢查 chartParams$ 資料格式是否正確
245
- const { status, columnName, expectToBe } = chartParamsValidator(chartType, d)
246
- if (status === 'error') {
247
- throw new Error(createValidatorErrorMessage({
248
- columnName,
249
- expectToBe,
250
- from: 'Chart.chartParams$'
251
- }))
252
- } else if (status === 'warning') {
253
- console.warn(createValidatorWarningMessage({
254
- columnName,
255
- expectToBe,
256
- from: 'Chart.chartParams$'
257
- }))
258
- }
259
- } catch (e) {
260
- // 不中斷資料流
261
- console.error(createOrbChartsErrorMessage(e))
262
- }
263
- try {
264
- return mergeOptionsWithDefault(d, mergedPresetWithDefault.chartParams)
265
- } catch (e) {
266
- throw new Error(createUnexpectedErrorMessage({
267
- from: 'Chart.chartParams$',
268
- systemMessage: e
269
- }))
270
- }
271
- }),
272
- catchError((e) => {
273
- console.error(createOrbChartsErrorMessage(e))
274
- return EMPTY
275
- }),
276
- shareReplay(1)
277
- )
278
-
279
- // -- size --
280
- // padding
281
- const mergedPadding$ = shareAndMergedChartParams$
282
- .pipe(
283
- takeUntil(destroy$),
284
- startWith({}),
285
- map((d: any) => {
286
- return mergeOptionsWithDefault(d.padding ?? {}, DEFAULT_PADDING)
287
- })
288
- )
289
- mergedPadding$
290
- .pipe(
291
- takeUntil(destroy$),
292
- first()
293
- )
294
- .subscribe(d => {
295
- selectionLayout
296
- .attr('transform', `translate(${d.left}, ${d.top})`)
297
- })
298
- mergedPadding$.subscribe(size => {
299
- selectionLayout
300
- .transition()
301
- .attr('transform', `translate(${size.left}, ${size.top})`)
302
- })
303
-
304
- // 監聽外層的element尺寸
305
- const rootSize$: Observable<{ width: number; height: number }> = of({
306
- width: options?.width ?? DEFAULT_CHART_OPTIONS.width,
307
- height: options?.height ?? DEFAULT_CHART_OPTIONS.height
308
- }).pipe(
309
- switchMap(size => {
310
- return iif(
311
- () => size.width === 'auto' || size.height === 'auto',
312
- // 有 'auto' 的話就監聽element的尺寸
313
- resizeObservable(element).pipe(
314
- map((d) => {
315
- return {
316
- width: size.width === 'auto' ? d.width : size.width,
317
- height: size.height === 'auto' ? d.height : size.height
318
- }
319
- })
320
- ),
321
- of(size as { width: number; height: number })
322
- )
323
- }),
324
- takeUntil(destroy$),
325
- share()
326
- )
327
- const rootSizeFiltered$ = of().pipe(
328
- mergeWith(
329
- rootSize$.pipe(
330
- debounceTime(250)
331
- ),
332
- rootSize$.pipe(
333
- throttleTime(250)
334
- )
335
- ),
336
- distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)),
337
- share()
338
- )
339
- const rootSizeSubscription = rootSizeFiltered$.subscribe()
340
-
341
- // layout
342
- const layout$: Observable<Layout> = combineLatest({
343
- rootSize: rootSizeFiltered$,
344
- mergedPadding: mergedPadding$
345
- }).pipe(
346
- takeUntil(destroy$),
347
- switchMap(async (d) => {
348
- const rootWidth = d.rootSize.width > 0
349
- ? d.rootSize.width
350
- : DEFAULT_CHART_WIDTH
351
- const rootHeight = d.rootSize.height > 0
352
- ? d.rootSize.height
353
- : DEFAULT_CHART_HEIGHT
354
- return {
355
- width: rootWidth - d.mergedPadding.left - d.mergedPadding.right,
356
- height: rootHeight - d.mergedPadding.top - d.mergedPadding.bottom,
357
- top: d.mergedPadding.top,
358
- right: d.mergedPadding.right,
359
- bottom: d.mergedPadding.bottom,
360
- left: d.mergedPadding.left,
361
- rootWidth,
362
- rootHeight
363
- }
364
- }),
365
- shareReplay(1)
366
- )
367
- layout$.subscribe(d => {
368
- svgSelection
369
- .attr('width', d.rootWidth)
370
- .attr('height', d.rootHeight)
371
- })
372
-
373
- // -- computedData --
374
- const computedData$: Observable<ComputedDataTypeMap<T>> = combineLatest({
375
- data: sharedData$,
376
- dataFormatter: shareAndMergedDataFormatter$,
377
- chartParams: shareAndMergedChartParams$,
378
- // layout: iif(() => isAxesTypeMap[chartType] === true, layout$, of(undefined))
379
- }).pipe(
380
- takeUntil(destroy$),
381
- switchMap(async d => d),
382
- switchMap((d) => {
383
- return of(d)
384
- .pipe(
385
- map(_d => {
386
- try {
387
- // 檢查 data$ 資料格式是否正確
388
- const { status, columnName, expectToBe } = dataValidator(_d.data)
389
- if (status === 'error') {
390
- throw new Error(createValidatorErrorMessage({
391
- columnName,
392
- expectToBe,
393
- from: 'Chart.data$'
394
- }))
395
- } else if (status === 'warning') {
396
- console.warn(createValidatorWarningMessage({
397
- columnName,
398
- expectToBe,
399
- from: 'Chart.data$'
400
- }))
401
- }
402
- } catch (e) {
403
- // 不中斷資料流
404
- console.error(createOrbChartsErrorMessage(e))
405
- }
406
- try {
407
- return computedDataFn({ data: _d.data, dataFormatter: _d.dataFormatter, chartParams: _d.chartParams })
408
- } catch (e) {
409
- throw new Error(createUnexpectedErrorMessage({
410
- from: 'Chart.data$',
411
- systemMessage: e
412
- }))
413
- }
414
- }),
415
- catchError((e) => {
416
- console.error(createOrbChartsErrorMessage(e))
417
- return EMPTY
418
- })
419
- )
420
- }),
421
- shareReplay(1)
422
- )
423
-
424
- // subscribe - computedData組合了所有的chart參數,所以訂閱computedData可以一次訂閱所有的資料流
425
- computedData$.subscribe()
426
-
427
- // -- plugins --
428
- const pluginEntityMap: any = {} // 用於destroy
429
- chartSubject.plugins$.subscribe(plugins => {
430
- try {
431
- // 檢查 plugins$ 資料格式是否正確
432
- const { status, columnName, expectToBe } = pluginsValidator(chartType, plugins)
433
- if (status === 'error') {
434
- throw new Error(createValidatorErrorMessage({
435
- columnName,
436
- expectToBe,
437
- from: 'Chart.plugins$'
438
- }))
439
- } else if (status === 'warning') {
440
- console.warn(createValidatorWarningMessage({
441
- columnName,
442
- expectToBe,
443
- from: 'Chart.plugins$'
444
- }))
445
- }
446
- } catch (e) {
447
- // plugin驗證失敗就不執行
448
- console.error(createOrbChartsErrorMessage(e))
449
- return
450
- }
451
-
452
- selectionPlugins
453
- .selectAll<SVGGElement, PluginEntity<T, any, any>>('g.orbcharts__plugin')
454
- .data(plugins, d => d.name as string)
455
- .join(
456
- enter => {
457
- return enter
458
- .append('g')
459
- .attr('class', plugin => {
460
- return `orbcharts__plugin orbcharts__${plugin.name}`
461
- })
462
- .each((plugin, i, n) => {
463
- const _pluginObserverBase = {
464
- fullParams$: new Observable(),
465
- fullChartParams$: shareAndMergedChartParams$,
466
- fullDataFormatter$: shareAndMergedDataFormatter$,
467
- computedData$,
468
- layout$
469
- }
470
- const pluginObserver: ContextObserverTypeMap<T, typeof plugin.defaultParams> = contextObserverCallback({
471
- observer: _pluginObserverBase,
472
- subject: chartSubject
473
- })
474
-
475
- // -- createPlugin(plugin) --
476
- const pluginSelection = d3.select(n[i])
477
- const pluginContext: PluginContext<T, typeof plugin.name, typeof plugin.defaultParams> = {
478
- selection: pluginSelection,
479
- rootSelection: svgSelection,
480
- name: plugin.name,
481
- chartType,
482
- subject: chartSubject,
483
- observer: pluginObserver
484
- }
485
-
486
- plugin.setPresetParams(mergedPresetWithDefault.pluginParams[plugin.name] ?? {})
487
- // 傳入context
488
- plugin.setContext(pluginContext)
489
-
490
- // 紀錄起來
491
- pluginEntityMap[pluginContext.name as string] = plugin
492
-
493
- // init plugin
494
- plugin.init()
495
-
496
- })
497
- },
498
- update => update,
499
- exit => {
500
- return exit
501
- .each((plugin: PluginEntity<T, unknown, unknown>, i, n) => {
502
- if (pluginEntityMap[plugin.name as string]) {
503
- pluginEntityMap[plugin.name as string].destroy()
504
- pluginEntityMap[plugin.name as string] = undefined
505
- }
506
- })
507
- .remove()
508
- }
509
- )
510
- .sort((a, b) => a.layerIndex - b.layerIndex)
511
-
512
- })
513
-
514
- return {
515
- ...chartSubject,
516
- selection: svgSelection,
517
- destroy () {
518
- d3.select(element).selectAll('svg').remove()
519
- destroy$.next(undefined)
520
- rootSizeSubscription.unsubscribe()
521
- }
522
- }
523
- }
524
- }
@@ -1,154 +0,0 @@
1
- import {
2
- catchError,
3
- of,
4
- takeUntil,
5
- map,
6
- switchMap,
7
- shareReplay,
8
- startWith,
9
- EMPTY,
10
- Subject,
11
- BehaviorSubject,
12
- Observable
13
- } from 'rxjs'
14
- import type {
15
- ChartType,
16
- CreateBasePlugin,
17
- DeepPartial,
18
- DefinePluginConfig,
19
- PluginInitFn,
20
- PluginContext,
21
- PluginEntity } from '../../lib/core-types'
22
- import { mergeOptionsWithDefault } from '../utils'
23
- import { createValidatorErrorMessage, createValidatorWarningMessage, createOrbChartsErrorMessage } from '../utils/errorMessage'
24
- import { validateColumns } from '../utils/validator'
25
-
26
- // 建立plugin實例
27
- function createPluginEntity <T extends ChartType, PluginName, PluginParams>({ chartType, config, initFn }: {
28
- chartType: T
29
- config: DefinePluginConfig<PluginName, PluginParams>
30
- initFn: PluginInitFn<T, PluginName, PluginParams>
31
- }): PluginEntity<T, PluginName, PluginParams> {
32
-
33
- const destroy$ = new Subject()
34
- const EntityWeakMap = new WeakMap() // <selection, pluginEntity> 避免只移除selection而沒回收pluginEntity的memory leak
35
- let pluginDestroyFn = () => {}
36
- let pluginContext: PluginContext<T, PluginName, PluginParams> | undefined
37
- const mergedDefaultParams$ = new BehaviorSubject(config.defaultParams)
38
- const params$: Subject<DeepPartial<typeof config.defaultParams>> = new BehaviorSubject({})
39
- const fullParams$ = mergedDefaultParams$.pipe(
40
- switchMap(mergedDefaultParams => {
41
- return params$
42
- .pipe(
43
- takeUntil(destroy$),
44
- map(d => {
45
- try {
46
- // 檢查 data$ 資料格式是否正確
47
- const { status, columnName, expectToBe } = config.validator(d, {
48
- validateColumns
49
- })
50
- if (status === 'error') {
51
- throw new Error(createValidatorErrorMessage({
52
- columnName,
53
- expectToBe,
54
- from: `${config.name}.params$`
55
- }))
56
- } else if (status === 'warning') {
57
- console.warn(createValidatorWarningMessage({
58
- columnName,
59
- expectToBe,
60
- from: `${config.name}.params$`
61
- }))
62
- }
63
- } catch (e) {
64
- // throw new Error(e.message)
65
- // 驗證失敗仍繼續執行,才不會把 Observable 資料流給中斷掉
66
- console.error(createOrbChartsErrorMessage(e))
67
- }
68
- return mergeOptionsWithDefault(d, mergedDefaultParams)
69
- }),
70
- // catchError((e) => {
71
- // console.error(createOrbChartsErrorMessage(e))
72
- // return EMPTY
73
- // })
74
- )
75
- }),
76
- shareReplay(1)
77
- )
78
-
79
- // 建立plugin實例
80
- return {
81
- params$,
82
- name: config.name,
83
- chartType,
84
- defaultParams: config.defaultParams,
85
- layerIndex: config.layerIndex,
86
- init () {
87
- if (!pluginContext) {
88
- return
89
- }
90
- // 執行
91
- pluginDestroyFn = (initFn(pluginContext) ?? (() => {})) // plugin執行會回傳destroy函式
92
- EntityWeakMap.set(pluginContext.selection, pluginContext)
93
- },
94
- destroy () {
95
- pluginDestroyFn()
96
- if (pluginContext) {
97
- pluginContext.selection.remove()
98
- pluginContext = undefined
99
- }
100
- destroy$.next(undefined)
101
- },
102
- setPresetParams: (presetParams: DeepPartial<PluginParams>) => {
103
- mergedDefaultParams$.next(mergeOptionsWithDefault(presetParams, config.defaultParams))
104
-
105
- },
106
- setContext: (_pluginContext: PluginContext<T, PluginName, PluginParams>) => {
107
- pluginContext = _pluginContext
108
- pluginContext.observer.fullParams$ = fullParams$
109
- }
110
- }
111
- }
112
-
113
- // 建立plugin類別
114
- export const createBasePlugin: CreateBasePlugin = <T extends ChartType>(chartType: T) => {
115
-
116
- // 定義plugin
117
- return function definePlugin<PluginName, PluginParams>(config: DefinePluginConfig<PluginName, PluginParams>) {
118
-
119
- // 定義plugin的初始化function
120
- return function definePluginInitFn (initFn: PluginInitFn<T, PluginName, PluginParams>) {
121
-
122
- return class Plugin {
123
- params$: Subject<DeepPartial<PluginParams>>
124
- name: PluginName
125
- chartType: T
126
- defaultParams: PluginParams
127
- layerIndex: number
128
- // presetParams: Partial<PluginParams>
129
- init: () => void
130
- destroy: () => void
131
- setPresetParams: (presetParams: DeepPartial<PluginParams>) => void
132
- setContext: (pluginContext: PluginContext<T, PluginName, PluginParams>) => void
133
- constructor () {
134
- const pluginEntity = createPluginEntity<T, PluginName, PluginParams>({
135
- chartType,
136
- config,
137
- initFn
138
- })
139
-
140
- this.params$ = pluginEntity.params$
141
- this.name = pluginEntity.name
142
- this.chartType = pluginEntity.chartType
143
- this.defaultParams = pluginEntity.defaultParams
144
- this.layerIndex = pluginEntity.layerIndex
145
- // this.presetParams = pluginEntity.presetParams
146
- this.init = pluginEntity.init
147
- this.destroy = pluginEntity.destroy
148
- this.setPresetParams = pluginEntity.setPresetParams
149
- this.setContext = pluginEntity.setContext
150
- }
151
- }
152
- }
153
- }
154
- }
@@ -1,24 +0,0 @@
1
- import type { ChartOptionsPartial, ChartType, ValidatorResult } from '../../../lib/core-types'
2
- import { validateColumns } from '../../utils/validator'
3
-
4
- export function chartOptionsValidator<T extends ChartType> (chartOptionsPartial: ChartOptionsPartial<T>): ValidatorResult {
5
- if (!chartOptionsPartial) {
6
- // chartOptions 可為空值
7
- return { status: 'success', columnName: '', expectToBe: '' }
8
- }
9
- const result = validateColumns(chartOptionsPartial, {
10
- width: {
11
- toBe: '"auto" | number',
12
- test: (value: any) => value === 'auto' || typeof value === 'number'
13
- },
14
- height: {
15
- toBe: '"auto" | number',
16
- test: (value: any) => value === 'auto' || typeof value === 'number'
17
- },
18
- preset: {
19
- toBeTypes: ['object']
20
- }
21
- })
22
-
23
- return result
24
- }