@orbcharts/core 3.0.0-beta.1 → 3.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +4 -3
  3. package/dist/orbcharts-core.umd.js +1 -1
  4. package/lib/core-types.ts +7 -7
  5. package/package.json +42 -42
  6. package/src/AbstractChart.ts +57 -57
  7. package/src/GridChart.ts +24 -24
  8. package/src/MultiGridChart.ts +24 -24
  9. package/src/MultiValueChart.ts +24 -24
  10. package/src/RelationshipChart.ts +24 -24
  11. package/src/SeriesChart.ts +24 -24
  12. package/src/TreeChart.ts +24 -24
  13. package/src/base/createBaseChart.ts +505 -505
  14. package/src/base/createBasePlugin.ts +153 -153
  15. package/src/base/validators/chartOptionsValidator.ts +23 -23
  16. package/src/base/validators/chartParamsValidator.ts +133 -133
  17. package/src/base/validators/elementValidator.ts +13 -13
  18. package/src/base/validators/pluginsValidator.ts +14 -14
  19. package/src/defaults.ts +232 -232
  20. package/src/defineGridPlugin.ts +3 -3
  21. package/src/defineMultiGridPlugin.ts +3 -3
  22. package/src/defineMultiValuePlugin.ts +3 -3
  23. package/src/defineNoneDataPlugin.ts +4 -4
  24. package/src/defineRelationshipPlugin.ts +3 -3
  25. package/src/defineSeriesPlugin.ts +3 -3
  26. package/src/defineTreePlugin.ts +3 -3
  27. package/src/grid/computedDataFn.ts +129 -129
  28. package/src/grid/contextObserverCallback.ts +155 -155
  29. package/src/grid/dataFormatterValidator.ts +101 -101
  30. package/src/grid/dataValidator.ts +12 -12
  31. package/src/index.ts +20 -20
  32. package/src/multiGrid/computedDataFn.ts +123 -123
  33. package/src/multiGrid/contextObserverCallback.ts +41 -41
  34. package/src/multiGrid/dataFormatterValidator.ts +115 -115
  35. package/src/multiGrid/dataValidator.ts +12 -12
  36. package/src/multiValue/computedDataFn.ts +176 -176
  37. package/src/multiValue/contextObserverCallback.ts +12 -12
  38. package/src/multiValue/dataFormatterValidator.ts +9 -9
  39. package/src/multiValue/dataValidator.ts +9 -9
  40. package/src/relationship/computedDataFn.ts +125 -125
  41. package/src/relationship/contextObserverCallback.ts +12 -12
  42. package/src/relationship/dataFormatterValidator.ts +9 -9
  43. package/src/relationship/dataValidator.ts +9 -9
  44. package/src/series/computedDataFn.ts +88 -88
  45. package/src/series/contextObserverCallback.ts +100 -100
  46. package/src/series/dataFormatterValidator.ts +41 -41
  47. package/src/series/dataValidator.ts +12 -12
  48. package/src/tree/computedDataFn.ts +130 -130
  49. package/src/tree/contextObserverCallback.ts +61 -61
  50. package/src/tree/dataFormatterValidator.ts +13 -13
  51. package/src/tree/dataValidator.ts +13 -13
  52. package/src/utils/commonUtils.ts +55 -55
  53. package/src/utils/d3Utils.ts +108 -108
  54. package/src/utils/errorMessage.ts +42 -42
  55. package/src/utils/gridObservables.ts +614 -611
  56. package/src/utils/index.ts +9 -9
  57. package/src/utils/multiGridObservables.ts +366 -366
  58. package/src/utils/observables.ts +219 -219
  59. package/src/utils/orbchartsUtils.ts +352 -352
  60. package/src/utils/seriesObservables.ts +175 -175
  61. package/src/utils/treeObservables.ts +94 -94
  62. package/src/utils/validator.ts +125 -125
  63. package/tsconfig.base.json +13 -13
  64. package/tsconfig.json +2 -2
  65. package/vite-env.d.ts +6 -6
  66. package/vite.config.js +22 -22
@@ -1,220 +1,220 @@
1
- import {
2
- combineLatest,
3
- distinctUntilChanged,
4
- filter,
5
- map,
6
- merge,
7
- takeUntil,
8
- shareReplay,
9
- switchMap,
10
- Subject,
11
- Observable } from 'rxjs'
12
- import type {
13
- ChartType,
14
- ChartParams,
15
- ComputedDatumBase,
16
- ComputedDataTypeMap,
17
- ComputedDatumTypeMap,
18
- DataFormatterTypeMap,
19
- EventTypeMap,
20
- HighlightTarget,
21
- Layout,
22
- TransformData } from '../../lib/core-types'
23
-
24
- // interface DatumUnknown {
25
- // value: number | null
26
- // id: string
27
- // // label: string
28
- // seriesLabel?: string // 要符合每一種computedData所以不一定會有seriesLabel
29
- // groupLabel?: string // 要符合每一種computedData所以不一定會有groupLabel
30
- // }
31
-
32
- export function resizeObservable(elem: HTMLElement | Element): Observable<DOMRectReadOnly> {
33
- return new Observable(subscriber => {
34
- const ro = new ResizeObserver(entries => {
35
- const entry = entries[0]
36
- if (entry && entry.contentRect) {
37
- subscriber.next(entry.contentRect)
38
- }
39
- })
40
-
41
- ro.observe(elem)
42
- return function unsubscribe() {
43
- ro.unobserve(elem)
44
- }
45
- })
46
- }
47
-
48
- // 通用 highlight Observable
49
- export const highlightObservable = <T extends ChartType, D>({ datumList$, fullChartParams$, event$ }: {
50
- datumList$: Observable<D[]>
51
- fullChartParams$: Observable<ChartParams>
52
- event$: Subject<EventTypeMap<T>>
53
- }): Observable<D[]> => {
54
- const destroy$ = new Subject()
55
-
56
- // 預設的highlight
57
- const highlightDefault$ = fullChartParams$.pipe(
58
- takeUntil(destroy$),
59
- map(d => {
60
- return {
61
- id: null,
62
- seriesLabel: null,
63
- groupLabel: null,
64
- categoryLabel: null,
65
- highlightDefault: d.highlightDefault
66
- }
67
- }),
68
- distinctUntilChanged()
69
- )
70
-
71
- // 事件觸發的highlight
72
- const highlightMouseover$ = event$.pipe(
73
- takeUntil(destroy$),
74
- // filter(d => d.eventName === 'mouseover' || d.eventName === 'mousemove'),
75
- filter(d => d.eventName === 'mouseover'),
76
- // distinctUntilChanged((prev, current) => prev.eventName === current.eventName)
77
- map(d => {
78
- return (d as any).datum
79
- ? {
80
- id: ((d as any).datum as any).id,
81
- seriesLabel: ((d as any).datum as any).seriesLabel,
82
- groupLabel: ((d as any).datum as any).groupLabel,
83
- categoryLabel: ((d as any).datum as any).categoryLabel,
84
- highlightDefault: null
85
- }
86
- : {
87
- id: null,
88
- seriesLabel: null,
89
- groupLabel: null,
90
- categoryLabel: null,
91
- highlightDefault: null
92
- }
93
- })
94
- )
95
- const highlightMouseout$ = event$.pipe(
96
- takeUntil(destroy$),
97
- filter(d => d.eventName === 'mouseout'),
98
- // distinctUntilChanged((prev, current) => prev.eventName === current.eventName)
99
- // map(d => {
100
- // return { id: '', label: '' }
101
- // })
102
- switchMap(d => highlightDefault$)
103
- )
104
-
105
- function getDatumIds (datumList: ComputedDatumTypeMap<T>[], id: string | null) {
106
- const datum = datumList.find(d => (d as ComputedDatumBase).id === id)
107
- return datum ? [datum] : []
108
- }
109
-
110
- function getSeriesIds (datumList: ComputedDatumTypeMap<T>[], seriesLabel: string | null) {
111
- return seriesLabel == null
112
- ? []
113
- : datumList.filter(d => (d as ComputedDatumTypeMap<"series">).seriesLabel === seriesLabel)
114
- }
115
-
116
- function getGroupIds (datumList: ComputedDatumTypeMap<T>[], groupLabel: string | null) {
117
- return groupLabel == null
118
- ? []
119
- : datumList.filter(d => (d as ComputedDatumTypeMap<"grid">).groupLabel === groupLabel)
120
- }
121
-
122
- function getCategoryIds (datumList: ComputedDatumTypeMap<T>[], categoryLabel: string | null) {
123
- return categoryLabel == null
124
- ? []
125
- : datumList.filter(d => (d as ComputedDatumTypeMap<"multiValue" | "relationship" | "tree">).categoryLabel === categoryLabel)
126
- }
127
-
128
- return new Observable<D[]>(subscriber => {
129
- combineLatest({
130
- target: merge(highlightMouseover$, highlightMouseout$, highlightDefault$),
131
- datumList: datumList$,
132
- fullChartParams: fullChartParams$,
133
- }).pipe(
134
- takeUntil(destroy$),
135
- switchMap(async d => d)
136
- ).subscribe(data => {
137
- let datumList: ComputedDatumTypeMap<T>[] = []
138
- if (data.fullChartParams.highlightTarget === 'datum') {
139
- datumList = getDatumIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.id)
140
- } else if (data.fullChartParams.highlightTarget === 'series') {
141
- datumList = getSeriesIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.seriesLabel)
142
- } else if (data.fullChartParams.highlightTarget === 'group') {
143
- datumList = getGroupIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.groupLabel)
144
- } else if (data.fullChartParams.highlightTarget === 'category') {
145
- datumList = getCategoryIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.categoryLabel)
146
- }
147
- subscriber.next(datumList as D[])
148
- })
149
-
150
- return function unsubscribe () {
151
- destroy$.next(undefined)
152
- }
153
- })
154
- }
155
-
156
- export const seriesDataMapObservable = <DatumType extends ComputedDatumTypeMap<'series' | 'grid'>>({ datumList$ }: { datumList$: Observable<DatumType[]> }) => {
157
- return datumList$.pipe(
158
- map(data => {
159
- const SeriesDataMap: Map<string, DatumType[]> = new Map()
160
- data.forEach(d => {
161
- const seriesData = SeriesDataMap.get(d.seriesLabel) ?? []
162
- seriesData.push(d)
163
- SeriesDataMap.set(d.seriesLabel, seriesData)
164
- })
165
- return SeriesDataMap
166
- })
167
- )
168
- }
169
-
170
- export const groupDataMapObservable = <DatumType extends ComputedDatumTypeMap<'grid'>> ({ datumList$ }: { datumList$: Observable<DatumType[]> }) => {
171
- return datumList$.pipe(
172
- map(data => {
173
- const GroupDataMap: Map<string, DatumType[]> = new Map()
174
- data.forEach(d => {
175
- const groupData = GroupDataMap.get(d.groupLabel) ?? []
176
- groupData.push(d)
177
- GroupDataMap.set(d.groupLabel, groupData)
178
- })
179
- return GroupDataMap
180
- })
181
- )
182
- }
183
-
184
- export const categoryDataMapObservable = <DatumType extends ComputedDatumTypeMap<'multiValue' | 'relationship' | 'tree'>> ({ datumList$ }: { datumList$: Observable<DatumType[]> }) => {
185
- return datumList$.pipe(
186
- map(data => {
187
- const GroupDataMap: Map<string, DatumType[]> = new Map()
188
- data
189
- .filter(d => d.categoryLabel != null)
190
- .forEach(d => {
191
- const groupData = GroupDataMap.get(d.categoryLabel) ?? []
192
- groupData.push(d)
193
- GroupDataMap.set(d.categoryLabel, groupData)
194
- })
195
- return GroupDataMap
196
- })
197
- )
198
- }
199
-
200
- export const textSizePxObservable = (chartParams$: Observable<ChartParams>) => {
201
- return chartParams$.pipe(
202
- map(d => d.styles.textSize),
203
- distinctUntilChanged(),
204
- map(data => {
205
- let value = NaN
206
- if (typeof data === 'string') {
207
- if (data.includes('rem')) {
208
- const rootFontSizePx = parseFloat(getComputedStyle(document.documentElement).fontSize)
209
- const num = parseFloat(data)
210
- value = num * rootFontSizePx
211
- } else if (data.includes('px')) {
212
- value = parseFloat(data)
213
- }
214
- } else if (typeof data === 'number') {
215
- return data
216
- }
217
- return value ? value : 14 // default
218
- })
219
- )
1
+ import {
2
+ combineLatest,
3
+ distinctUntilChanged,
4
+ filter,
5
+ map,
6
+ merge,
7
+ takeUntil,
8
+ shareReplay,
9
+ switchMap,
10
+ Subject,
11
+ Observable } from 'rxjs'
12
+ import type {
13
+ ChartType,
14
+ ChartParams,
15
+ ComputedDatumBase,
16
+ ComputedDataTypeMap,
17
+ ComputedDatumTypeMap,
18
+ DataFormatterTypeMap,
19
+ EventTypeMap,
20
+ HighlightTarget,
21
+ Layout,
22
+ TransformData } from '../../lib/core-types'
23
+
24
+ // interface DatumUnknown {
25
+ // value: number | null
26
+ // id: string
27
+ // // label: string
28
+ // seriesLabel?: string // 要符合每一種computedData所以不一定會有seriesLabel
29
+ // groupLabel?: string // 要符合每一種computedData所以不一定會有groupLabel
30
+ // }
31
+
32
+ export function resizeObservable(elem: HTMLElement | Element): Observable<DOMRectReadOnly> {
33
+ return new Observable(subscriber => {
34
+ const ro = new ResizeObserver(entries => {
35
+ const entry = entries[0]
36
+ if (entry && entry.contentRect) {
37
+ subscriber.next(entry.contentRect)
38
+ }
39
+ })
40
+
41
+ ro.observe(elem)
42
+ return function unsubscribe() {
43
+ ro.unobserve(elem)
44
+ }
45
+ })
46
+ }
47
+
48
+ // 通用 highlight Observable
49
+ export const highlightObservable = <T extends ChartType, D>({ datumList$, fullChartParams$, event$ }: {
50
+ datumList$: Observable<D[]>
51
+ fullChartParams$: Observable<ChartParams>
52
+ event$: Subject<EventTypeMap<T>>
53
+ }): Observable<D[]> => {
54
+ const destroy$ = new Subject()
55
+
56
+ // 預設的highlight
57
+ const highlightDefault$ = fullChartParams$.pipe(
58
+ takeUntil(destroy$),
59
+ map(d => {
60
+ return {
61
+ id: null,
62
+ seriesLabel: null,
63
+ groupLabel: null,
64
+ categoryLabel: null,
65
+ highlightDefault: d.highlightDefault
66
+ }
67
+ }),
68
+ distinctUntilChanged()
69
+ )
70
+
71
+ // 事件觸發的highlight
72
+ const highlightMouseover$ = event$.pipe(
73
+ takeUntil(destroy$),
74
+ // filter(d => d.eventName === 'mouseover' || d.eventName === 'mousemove'),
75
+ filter(d => d.eventName === 'mouseover'),
76
+ // distinctUntilChanged((prev, current) => prev.eventName === current.eventName)
77
+ map(d => {
78
+ return (d as any).datum
79
+ ? {
80
+ id: ((d as any).datum as any).id,
81
+ seriesLabel: ((d as any).datum as any).seriesLabel,
82
+ groupLabel: ((d as any).datum as any).groupLabel,
83
+ categoryLabel: ((d as any).datum as any).categoryLabel,
84
+ highlightDefault: null
85
+ }
86
+ : {
87
+ id: null,
88
+ seriesLabel: null,
89
+ groupLabel: null,
90
+ categoryLabel: null,
91
+ highlightDefault: null
92
+ }
93
+ })
94
+ )
95
+ const highlightMouseout$ = event$.pipe(
96
+ takeUntil(destroy$),
97
+ filter(d => d.eventName === 'mouseout'),
98
+ // distinctUntilChanged((prev, current) => prev.eventName === current.eventName)
99
+ // map(d => {
100
+ // return { id: '', label: '' }
101
+ // })
102
+ switchMap(d => highlightDefault$)
103
+ )
104
+
105
+ function getDatumIds (datumList: ComputedDatumTypeMap<T>[], id: string | null) {
106
+ const datum = datumList.find(d => (d as ComputedDatumBase).id === id)
107
+ return datum ? [datum] : []
108
+ }
109
+
110
+ function getSeriesIds (datumList: ComputedDatumTypeMap<T>[], seriesLabel: string | null) {
111
+ return seriesLabel == null
112
+ ? []
113
+ : datumList.filter(d => (d as ComputedDatumTypeMap<"series">).seriesLabel === seriesLabel)
114
+ }
115
+
116
+ function getGroupIds (datumList: ComputedDatumTypeMap<T>[], groupLabel: string | null) {
117
+ return groupLabel == null
118
+ ? []
119
+ : datumList.filter(d => (d as ComputedDatumTypeMap<"grid">).groupLabel === groupLabel)
120
+ }
121
+
122
+ function getCategoryIds (datumList: ComputedDatumTypeMap<T>[], categoryLabel: string | null) {
123
+ return categoryLabel == null
124
+ ? []
125
+ : datumList.filter(d => (d as ComputedDatumTypeMap<"multiValue" | "relationship" | "tree">).categoryLabel === categoryLabel)
126
+ }
127
+
128
+ return new Observable<D[]>(subscriber => {
129
+ combineLatest({
130
+ target: merge(highlightMouseover$, highlightMouseout$, highlightDefault$),
131
+ datumList: datumList$,
132
+ fullChartParams: fullChartParams$,
133
+ }).pipe(
134
+ takeUntil(destroy$),
135
+ switchMap(async d => d)
136
+ ).subscribe(data => {
137
+ let datumList: ComputedDatumTypeMap<T>[] = []
138
+ if (data.fullChartParams.highlightTarget === 'datum') {
139
+ datumList = getDatumIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.id)
140
+ } else if (data.fullChartParams.highlightTarget === 'series') {
141
+ datumList = getSeriesIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.seriesLabel)
142
+ } else if (data.fullChartParams.highlightTarget === 'group') {
143
+ datumList = getGroupIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.groupLabel)
144
+ } else if (data.fullChartParams.highlightTarget === 'category') {
145
+ datumList = getCategoryIds(data.datumList as ComputedDatumTypeMap<T>[], data.target.categoryLabel)
146
+ }
147
+ subscriber.next(datumList as D[])
148
+ })
149
+
150
+ return function unsubscribe () {
151
+ destroy$.next(undefined)
152
+ }
153
+ })
154
+ }
155
+
156
+ export const seriesDataMapObservable = <DatumType extends ComputedDatumTypeMap<'series' | 'grid'>>({ datumList$ }: { datumList$: Observable<DatumType[]> }) => {
157
+ return datumList$.pipe(
158
+ map(data => {
159
+ const SeriesDataMap: Map<string, DatumType[]> = new Map()
160
+ data.forEach(d => {
161
+ const seriesData = SeriesDataMap.get(d.seriesLabel) ?? []
162
+ seriesData.push(d)
163
+ SeriesDataMap.set(d.seriesLabel, seriesData)
164
+ })
165
+ return SeriesDataMap
166
+ })
167
+ )
168
+ }
169
+
170
+ export const groupDataMapObservable = <DatumType extends ComputedDatumTypeMap<'grid'>> ({ datumList$ }: { datumList$: Observable<DatumType[]> }) => {
171
+ return datumList$.pipe(
172
+ map(data => {
173
+ const GroupDataMap: Map<string, DatumType[]> = new Map()
174
+ data.forEach(d => {
175
+ const groupData = GroupDataMap.get(d.groupLabel) ?? []
176
+ groupData.push(d)
177
+ GroupDataMap.set(d.groupLabel, groupData)
178
+ })
179
+ return GroupDataMap
180
+ })
181
+ )
182
+ }
183
+
184
+ export const categoryDataMapObservable = <DatumType extends ComputedDatumTypeMap<'multiValue' | 'relationship' | 'tree'>> ({ datumList$ }: { datumList$: Observable<DatumType[]> }) => {
185
+ return datumList$.pipe(
186
+ map(data => {
187
+ const GroupDataMap: Map<string, DatumType[]> = new Map()
188
+ data
189
+ .filter(d => d.categoryLabel != null)
190
+ .forEach(d => {
191
+ const groupData = GroupDataMap.get(d.categoryLabel) ?? []
192
+ groupData.push(d)
193
+ GroupDataMap.set(d.categoryLabel, groupData)
194
+ })
195
+ return GroupDataMap
196
+ })
197
+ )
198
+ }
199
+
200
+ export const textSizePxObservable = (chartParams$: Observable<ChartParams>) => {
201
+ return chartParams$.pipe(
202
+ map(d => d.styles.textSize),
203
+ distinctUntilChanged(),
204
+ map(data => {
205
+ let value = NaN
206
+ if (typeof data === 'string') {
207
+ if (data.includes('rem')) {
208
+ const rootFontSizePx = parseFloat(getComputedStyle(document.documentElement).fontSize)
209
+ const num = parseFloat(data)
210
+ value = num * rootFontSizePx
211
+ } else if (data.includes('px')) {
212
+ value = parseFloat(data)
213
+ }
214
+ } else if (typeof data === 'number') {
215
+ return data
216
+ }
217
+ return value ? value : 14 // default
218
+ })
219
+ )
220
220
  }