@orbcharts/core 3.0.0-beta.5 → 3.0.0-beta.7

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 (71) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +1481 -1412
  3. package/dist/orbcharts-core.umd.js +4 -4
  4. package/dist/src/defaults.d.ts +22 -22
  5. package/dist/src/utils/orbchartsUtils.d.ts +7 -7
  6. package/dist/src/utils/relationshipObservables.d.ts +13 -0
  7. package/lib/core-types.ts +7 -7
  8. package/package.json +42 -42
  9. package/src/AbstractChart.ts +57 -57
  10. package/src/GridChart.ts +24 -24
  11. package/src/MultiGridChart.ts +24 -24
  12. package/src/MultiValueChart.ts +24 -24
  13. package/src/RelationshipChart.ts +24 -24
  14. package/src/SeriesChart.ts +24 -24
  15. package/src/TreeChart.ts +24 -24
  16. package/src/base/createBaseChart.ts +505 -505
  17. package/src/base/createBasePlugin.ts +153 -153
  18. package/src/base/validators/chartOptionsValidator.ts +23 -23
  19. package/src/base/validators/chartParamsValidator.ts +133 -133
  20. package/src/base/validators/elementValidator.ts +13 -13
  21. package/src/base/validators/pluginsValidator.ts +14 -14
  22. package/src/defaults.ts +235 -235
  23. package/src/defineGridPlugin.ts +3 -3
  24. package/src/defineMultiGridPlugin.ts +3 -3
  25. package/src/defineMultiValuePlugin.ts +3 -3
  26. package/src/defineNoneDataPlugin.ts +4 -4
  27. package/src/defineRelationshipPlugin.ts +3 -3
  28. package/src/defineSeriesPlugin.ts +3 -3
  29. package/src/defineTreePlugin.ts +3 -3
  30. package/src/grid/computedDataFn.ts +129 -129
  31. package/src/grid/contextObserverCallback.ts +176 -176
  32. package/src/grid/dataFormatterValidator.ts +101 -101
  33. package/src/grid/dataValidator.ts +12 -12
  34. package/src/index.ts +20 -20
  35. package/src/multiGrid/computedDataFn.ts +123 -123
  36. package/src/multiGrid/contextObserverCallback.ts +41 -41
  37. package/src/multiGrid/dataFormatterValidator.ts +115 -115
  38. package/src/multiGrid/dataValidator.ts +12 -12
  39. package/src/multiValue/computedDataFn.ts +110 -110
  40. package/src/multiValue/contextObserverCallback.ts +160 -160
  41. package/src/multiValue/dataFormatterValidator.ts +9 -9
  42. package/src/multiValue/dataValidator.ts +9 -9
  43. package/src/relationship/computedDataFn.ts +144 -125
  44. package/src/relationship/contextObserverCallback.ts +80 -12
  45. package/src/relationship/dataFormatterValidator.ts +9 -9
  46. package/src/relationship/dataValidator.ts +9 -9
  47. package/src/series/computedDataFn.ts +88 -88
  48. package/src/series/contextObserverCallback.ts +100 -100
  49. package/src/series/dataFormatterValidator.ts +41 -41
  50. package/src/series/dataValidator.ts +12 -12
  51. package/src/tree/computedDataFn.ts +129 -129
  52. package/src/tree/contextObserverCallback.ts +58 -58
  53. package/src/tree/dataFormatterValidator.ts +13 -13
  54. package/src/tree/dataValidator.ts +13 -13
  55. package/src/utils/commonUtils.ts +55 -55
  56. package/src/utils/d3Scale.ts +198 -198
  57. package/src/utils/errorMessage.ts +42 -42
  58. package/src/utils/gridObservables.ts +683 -683
  59. package/src/utils/index.ts +9 -9
  60. package/src/utils/multiGridObservables.ts +392 -392
  61. package/src/utils/multiValueObservables.ts +661 -661
  62. package/src/utils/observables.ts +219 -219
  63. package/src/utils/orbchartsUtils.ts +377 -377
  64. package/src/utils/relationshipObservables.ts +85 -0
  65. package/src/utils/seriesObservables.ts +175 -175
  66. package/src/utils/treeObservables.ts +105 -105
  67. package/src/utils/validator.ts +126 -126
  68. package/tsconfig.base.json +13 -13
  69. package/tsconfig.json +2 -2
  70. package/vite-env.d.ts +6 -6
  71. package/vite.config.js +22 -22
@@ -0,0 +1,85 @@
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
+ ChartParams,
14
+ ComputedDataRelationship,
15
+ ComputedDataTypeMap,
16
+ ComputedNode,
17
+ ComputedEdge,
18
+ DataFormatterTree } from '../../lib/core-types'
19
+
20
+
21
+ export const categoryLabelsObservable = (
22
+ CategoryNodeMap$: Observable<Map<string, ComputedNode[]>>,
23
+ CategoryEdgeMap$: Observable<Map<string, ComputedEdge[]>>
24
+ ): Observable<string[]> => {
25
+ return combineLatest({
26
+ CategoryNodeMap: CategoryNodeMap$,
27
+ CategoryEdgeMap: CategoryEdgeMap$
28
+ }).pipe(
29
+ switchMap(async d => d),
30
+ map(data => {
31
+ return [...Array.from(data.CategoryNodeMap.keys()), ...Array.from(data.CategoryEdgeMap.keys())]
32
+ }),
33
+ distinctUntilChanged((a, b) => {
34
+ return JSON.stringify(a).length === JSON.stringify(b).length
35
+ }),
36
+ )
37
+ }
38
+
39
+ export const NodeMapObservable = (computedData$: Observable<ComputedDataRelationship>) => {
40
+ return computedData$.pipe(
41
+ map(data => {
42
+ const nodeMap = new Map<string, ComputedNode>()
43
+ data.nodes.forEach(node => {
44
+ nodeMap.set(node.id, node)
45
+ })
46
+ return nodeMap
47
+ }),
48
+ )
49
+ }
50
+
51
+ export const EdgeMapObservable = (computedData$: Observable<ComputedDataRelationship>) => {
52
+ return computedData$.pipe(
53
+ map(data => {
54
+ const edgeMap = new Map<string, ComputedEdge>()
55
+ data.edges.forEach(edge => {
56
+ edgeMap.set(edge.id, edge)
57
+ })
58
+ return edgeMap
59
+ }),
60
+ )
61
+ }
62
+
63
+ // 所有可見的節點
64
+ export const relationshipVisibleComputedDataObservable = ({ computedData$, NodeMap$ }: {
65
+ computedData$: Observable<ComputedDataRelationship>
66
+ NodeMap$: Observable<Map<string, ComputedNode>>
67
+ }) => {
68
+ return combineLatest({
69
+ computedData: computedData$,
70
+ NodeMap: NodeMap$
71
+ }).pipe(
72
+ switchMap(async d => d),
73
+ map(data => {
74
+ return {
75
+ nodes: data.computedData.nodes.filter(node => node.visible),
76
+ edges: data.computedData.edges
77
+ .filter(edge => edge.visible)
78
+ // 依照節點是否存在篩選
79
+ .filter(edge => {
80
+ return data.NodeMap.has(edge.startNode.id) && data.NodeMap.has(edge.endNode.id)
81
+ })
82
+ }
83
+ })
84
+ )
85
+ }
@@ -1,176 +1,176 @@
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
- ChartParams,
14
- ComputedDatumSeries,
15
- ComputedDataTypeMap,
16
- DataFormatterTypeMap,
17
- ContainerPosition,
18
- Layout } from '../../lib/core-types'
19
- import { calcSeriesContainerLayout } from './orbchartsUtils'
20
-
21
- export const separateSeriesObservable = ({ fullDataFormatter$ }: { fullDataFormatter$: Observable<DataFormatterTypeMap<'series'>> }) => {
22
- return fullDataFormatter$.pipe(
23
- map(data => data.separateSeries),
24
- distinctUntilChanged(),
25
- )
26
- }
27
-
28
- export const seriesLabelsObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'series'>> }) => {
29
- return computedData$.pipe(
30
- map(data => {
31
- return data
32
- .filter(series => series.length)
33
- .map(series => {
34
- return series[0].seriesLabel
35
- })
36
- }),
37
- distinctUntilChanged((a, b) => {
38
- return JSON.stringify(a).length === JSON.stringify(b).length
39
- }),
40
- )
41
- }
42
-
43
- export const seriesVisibleComputedDataObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'series'>> }) => {
44
- return computedData$.pipe(
45
- map(data => {
46
- return data.map(series => {
47
- return series.filter(datum => datum.visible != false)
48
- })
49
- })
50
- )
51
- }
52
-
53
- export const seriesComputedLayoutDataObservable = ({ computedData$, fullDataFormatter$ }: {
54
- computedData$: Observable<ComputedDataTypeMap<'series'>>,
55
- fullDataFormatter$: Observable<DataFormatterTypeMap<'series'>>
56
- }) => {
57
- return combineLatest({
58
- computedData: computedData$,
59
- fullDataFormatter: fullDataFormatter$
60
- }).pipe(
61
- switchMap(async (d) => d),
62
- map(data => {
63
- const sumData: ComputedDatumSeries[][] = data.fullDataFormatter.sumSeries == true
64
- ? data.computedData.map(d => {
65
- return [
66
- // 加總為一筆資料
67
- d.reduce((acc, current) => {
68
- if (acc == null) {
69
- return current // 取得第一筆資料
70
- }
71
- acc.value = acc.value + current.value
72
- return acc
73
- }, null)
74
- ]
75
- })
76
- : data.computedData
77
-
78
- return data.fullDataFormatter.separateSeries == true
79
- // 有拆分的話每個series為一組
80
- ? sumData
81
- .map(series => {
82
- return series.sort((a, b) => a.seq - b.seq)
83
- })
84
- // 無拆分的話所有資料為一組
85
- : [
86
- sumData
87
- .flat()
88
- .sort((a, b) => a.seq - b.seq)
89
- ]
90
- })
91
- )
92
- }
93
-
94
-
95
- // 所有container位置(對應series)
96
- export const seriesContainerPositionObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
97
- computedData$: Observable<ComputedDataTypeMap<'series'>>
98
- fullDataFormatter$: Observable<DataFormatterTypeMap<'series'>>
99
- layout$: Observable<Layout>
100
- }): Observable<ContainerPosition[]> => {
101
-
102
- const gridContainerPosition$ = combineLatest({
103
- computedData: computedData$,
104
- fullDataFormatter: fullDataFormatter$,
105
- layout: layout$,
106
- }).pipe(
107
- switchMap(async (d) => d),
108
- map(data => {
109
-
110
- if (data.fullDataFormatter.separateSeries) {
111
- // -- 依slotIndexes計算 --
112
- return calcSeriesContainerLayout(data.layout, data.fullDataFormatter.container, data.computedData.length)
113
- // return data.computedData.map((seriesData, seriesIndex) => {
114
- // const columnIndex = seriesIndex % data.fullDataFormatter.container.columnAmount
115
- // const rowIndex = Math.floor(seriesIndex / data.fullDataFormatter.container.columnAmount)
116
- // const { startX, startY, centerX, centerY, width, height } = calcContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
117
- // return {
118
- // slotIndex: seriesIndex,
119
- // rowIndex,
120
- // columnIndex,
121
- // startX,
122
- // startY,
123
- // centerX,
124
- // centerY,
125
- // width,
126
- // height,
127
- // }
128
- // })
129
- } else {
130
- // -- 無拆分 --
131
- return calcSeriesContainerLayout(data.layout, data.fullDataFormatter.container, 1)
132
- // const columnIndex = 0
133
- // const rowIndex = 0
134
- // return data.computedData.map((seriesData, seriesIndex) => {
135
- // const { startX, startY, centerX, centerY, width, height } = calcContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
136
- // return {
137
- // slotIndex: 0,
138
- // rowIndex,
139
- // columnIndex,
140
- // startX,
141
- // startY,
142
- // centerX,
143
- // centerY,
144
- // width,
145
- // height,
146
- // }
147
- // })
148
- }
149
- })
150
- )
151
-
152
- return gridContainerPosition$
153
- }
154
-
155
- export const seriesContainerPositionMapObservable = ({ seriesContainerPosition$, seriesLabels$, separateSeries$ }: {
156
- seriesContainerPosition$: Observable<ContainerPosition[]>
157
- seriesLabels$: Observable<string[]>
158
- separateSeries$: Observable<boolean>
159
- }) => {
160
- return combineLatest({
161
- seriesContainerPosition: seriesContainerPosition$,
162
- seriesLabels: seriesLabels$,
163
- separateSeries: separateSeries$,
164
- }).pipe(
165
- switchMap(async (d) => d),
166
- map(data => {
167
- return data.separateSeries
168
- ? new Map<string, ContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
169
- return [seriesLabel, data.seriesContainerPosition[seriesIndex] ?? data.seriesContainerPosition[0]]
170
- }))
171
- : new Map<string, ContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
172
- return [seriesLabel, data.seriesContainerPosition[0]]
173
- }))
174
- })
175
- )
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
+ ChartParams,
14
+ ComputedDatumSeries,
15
+ ComputedDataTypeMap,
16
+ DataFormatterTypeMap,
17
+ ContainerPosition,
18
+ Layout } from '../../lib/core-types'
19
+ import { calcSeriesContainerLayout } from './orbchartsUtils'
20
+
21
+ export const separateSeriesObservable = ({ fullDataFormatter$ }: { fullDataFormatter$: Observable<DataFormatterTypeMap<'series'>> }) => {
22
+ return fullDataFormatter$.pipe(
23
+ map(data => data.separateSeries),
24
+ distinctUntilChanged(),
25
+ )
26
+ }
27
+
28
+ export const seriesLabelsObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'series'>> }) => {
29
+ return computedData$.pipe(
30
+ map(data => {
31
+ return data
32
+ .filter(series => series.length)
33
+ .map(series => {
34
+ return series[0].seriesLabel
35
+ })
36
+ }),
37
+ distinctUntilChanged((a, b) => {
38
+ return JSON.stringify(a).length === JSON.stringify(b).length
39
+ }),
40
+ )
41
+ }
42
+
43
+ export const seriesVisibleComputedDataObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTypeMap<'series'>> }) => {
44
+ return computedData$.pipe(
45
+ map(data => {
46
+ return data.map(series => {
47
+ return series.filter(datum => datum.visible != false)
48
+ })
49
+ })
50
+ )
51
+ }
52
+
53
+ export const seriesComputedLayoutDataObservable = ({ computedData$, fullDataFormatter$ }: {
54
+ computedData$: Observable<ComputedDataTypeMap<'series'>>,
55
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'series'>>
56
+ }) => {
57
+ return combineLatest({
58
+ computedData: computedData$,
59
+ fullDataFormatter: fullDataFormatter$
60
+ }).pipe(
61
+ switchMap(async (d) => d),
62
+ map(data => {
63
+ const sumData: ComputedDatumSeries[][] = data.fullDataFormatter.sumSeries == true
64
+ ? data.computedData.map(d => {
65
+ return [
66
+ // 加總為一筆資料
67
+ d.reduce((acc, current) => {
68
+ if (acc == null) {
69
+ return current // 取得第一筆資料
70
+ }
71
+ acc.value = acc.value + current.value
72
+ return acc
73
+ }, null)
74
+ ]
75
+ })
76
+ : data.computedData
77
+
78
+ return data.fullDataFormatter.separateSeries == true
79
+ // 有拆分的話每個series為一組
80
+ ? sumData
81
+ .map(series => {
82
+ return series.sort((a, b) => a.seq - b.seq)
83
+ })
84
+ // 無拆分的話所有資料為一組
85
+ : [
86
+ sumData
87
+ .flat()
88
+ .sort((a, b) => a.seq - b.seq)
89
+ ]
90
+ })
91
+ )
92
+ }
93
+
94
+
95
+ // 所有container位置(對應series)
96
+ export const seriesContainerPositionObservable = ({ computedData$, fullDataFormatter$, layout$ }: {
97
+ computedData$: Observable<ComputedDataTypeMap<'series'>>
98
+ fullDataFormatter$: Observable<DataFormatterTypeMap<'series'>>
99
+ layout$: Observable<Layout>
100
+ }): Observable<ContainerPosition[]> => {
101
+
102
+ const gridContainerPosition$ = combineLatest({
103
+ computedData: computedData$,
104
+ fullDataFormatter: fullDataFormatter$,
105
+ layout: layout$,
106
+ }).pipe(
107
+ switchMap(async (d) => d),
108
+ map(data => {
109
+
110
+ if (data.fullDataFormatter.separateSeries) {
111
+ // -- 依slotIndexes計算 --
112
+ return calcSeriesContainerLayout(data.layout, data.fullDataFormatter.container, data.computedData.length)
113
+ // return data.computedData.map((seriesData, seriesIndex) => {
114
+ // const columnIndex = seriesIndex % data.fullDataFormatter.container.columnAmount
115
+ // const rowIndex = Math.floor(seriesIndex / data.fullDataFormatter.container.columnAmount)
116
+ // const { startX, startY, centerX, centerY, width, height } = calcContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
117
+ // return {
118
+ // slotIndex: seriesIndex,
119
+ // rowIndex,
120
+ // columnIndex,
121
+ // startX,
122
+ // startY,
123
+ // centerX,
124
+ // centerY,
125
+ // width,
126
+ // height,
127
+ // }
128
+ // })
129
+ } else {
130
+ // -- 無拆分 --
131
+ return calcSeriesContainerLayout(data.layout, data.fullDataFormatter.container, 1)
132
+ // const columnIndex = 0
133
+ // const rowIndex = 0
134
+ // return data.computedData.map((seriesData, seriesIndex) => {
135
+ // const { startX, startY, centerX, centerY, width, height } = calcContainerPosition(data.layout, data.fullDataFormatter.container, rowIndex, columnIndex)
136
+ // return {
137
+ // slotIndex: 0,
138
+ // rowIndex,
139
+ // columnIndex,
140
+ // startX,
141
+ // startY,
142
+ // centerX,
143
+ // centerY,
144
+ // width,
145
+ // height,
146
+ // }
147
+ // })
148
+ }
149
+ })
150
+ )
151
+
152
+ return gridContainerPosition$
153
+ }
154
+
155
+ export const seriesContainerPositionMapObservable = ({ seriesContainerPosition$, seriesLabels$, separateSeries$ }: {
156
+ seriesContainerPosition$: Observable<ContainerPosition[]>
157
+ seriesLabels$: Observable<string[]>
158
+ separateSeries$: Observable<boolean>
159
+ }) => {
160
+ return combineLatest({
161
+ seriesContainerPosition: seriesContainerPosition$,
162
+ seriesLabels: seriesLabels$,
163
+ separateSeries: separateSeries$,
164
+ }).pipe(
165
+ switchMap(async (d) => d),
166
+ map(data => {
167
+ return data.separateSeries
168
+ ? new Map<string, ContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
169
+ return [seriesLabel, data.seriesContainerPosition[seriesIndex] ?? data.seriesContainerPosition[0]]
170
+ }))
171
+ : new Map<string, ContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
172
+ return [seriesLabel, data.seriesContainerPosition[0]]
173
+ }))
174
+ })
175
+ )
176
176
  }
@@ -1,106 +1,106 @@
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
- ChartParams,
14
- ComputedDataTree,
15
- ComputedDataTypeMap,
16
- DataFormatterTree } from '../../lib/core-types'
17
-
18
-
19
- // 所有節點list結構
20
- export const nodeListObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTree> }) => {
21
- return computedData$.pipe(
22
- map(data => {
23
- function setNodeList (accNodeList: ComputedDataTree[], branch: ComputedDataTree) {
24
- accNodeList.push(branch)
25
- if (branch.children) {
26
- branch.children.forEach(childBranch => {
27
- accNodeList = setNodeList(accNodeList, childBranch) // 遞迴子節點
28
- })
29
- }
30
- return accNodeList
31
- }
32
- return setNodeList([], data)
33
- })
34
- )
35
- }
36
-
37
- // export const categoryLabelsObservable = ({ nodeList$, fullDataFormatter$ }: {
38
- // nodeList$: Observable<ComputedDataTree[]>
39
- // fullDataFormatter$: Observable<DataFormatterTree>
40
- // }) => {
41
-
42
- // const categoryLabels$ = fullDataFormatter$.pipe(
43
- // map(d => d.categoryLabels),
44
- // distinctUntilChanged((a, b) => {
45
- // return JSON.stringify(a).length === JSON.stringify(b).length
46
- // }),
47
- // )
48
-
49
- // return combineLatest({
50
- // nodeList: nodeList$,
51
- // categoryLabels: categoryLabels$
52
- // }).pipe(
53
- // switchMap(async d => d),
54
- // map(data => {
55
- // const CurrentLabelSet = new Set(data.categoryLabels)
56
- // const ExistLabelSet = new Set(
57
- // data.nodeList.filter(node => node.visible).map(node => node.categoryLabel)
58
- // )
59
- // // 加入已存在的label(data.nodeList有,但是dataFormatter.categoryLabels沒有)
60
- // Array.from(ExistLabelSet).forEach(label => {
61
- // if (!CurrentLabelSet.has(label)) {
62
- // CurrentLabelSet.add(label)
63
- // }
64
- // })
65
- // // 移除不存在的label(dataFormatter.categoryLabels有,但是data.nodeList沒有)
66
- // Array.from(CurrentLabelSet).forEach(label => {
67
- // if (!ExistLabelSet.has(label)) {
68
- // ExistLabelSet.delete(label)
69
- // }
70
- // })
71
-
72
- // return Array.from(CurrentLabelSet)
73
- // }),
74
- // distinctUntilChanged((a, b) => {
75
- // return JSON.stringify(a).length === JSON.stringify(b).length
76
- // }),
77
- // )
78
- // }
79
-
80
- export const categoryLabelsObservable = (CategoryDataMap$: Observable<Map<string, ComputedDataTree[]>>) => {
81
- return CategoryDataMap$.pipe(
82
- map(data => {
83
- return Array.from(data.keys())
84
- }),
85
- distinctUntilChanged((a, b) => {
86
- return JSON.stringify(a).length === JSON.stringify(b).length
87
- }),
88
- )
89
- }
90
-
91
- // 所有可見的節點
92
- export const treeVisibleComputedDataObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTree> }) => {
93
- return computedData$.pipe(
94
- map(data => {
95
- function filterChildren (accTree: ComputedDataTree) {
96
- if (accTree.children) {
97
- accTree.children = accTree.children
98
- .filter(child => child.visible) // 篩選visible
99
- .map(child => filterChildren(child)) // 遞迴子節點
100
- }
101
- return accTree
102
- }
103
- return filterChildren(data)
104
- })
105
- )
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
+ ChartParams,
14
+ ComputedDataTree,
15
+ ComputedDataTypeMap,
16
+ DataFormatterTree } from '../../lib/core-types'
17
+
18
+
19
+ // 所有節點list結構
20
+ export const nodeListObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTree> }) => {
21
+ return computedData$.pipe(
22
+ map(data => {
23
+ function setNodeList (accNodeList: ComputedDataTree[], branch: ComputedDataTree) {
24
+ accNodeList.push(branch)
25
+ if (branch.children) {
26
+ branch.children.forEach(childBranch => {
27
+ accNodeList = setNodeList(accNodeList, childBranch) // 遞迴子節點
28
+ })
29
+ }
30
+ return accNodeList
31
+ }
32
+ return setNodeList([], data)
33
+ })
34
+ )
35
+ }
36
+
37
+ // export const categoryLabelsObservable = ({ nodeList$, fullDataFormatter$ }: {
38
+ // nodeList$: Observable<ComputedDataTree[]>
39
+ // fullDataFormatter$: Observable<DataFormatterTree>
40
+ // }) => {
41
+
42
+ // const categoryLabels$ = fullDataFormatter$.pipe(
43
+ // map(d => d.categoryLabels),
44
+ // distinctUntilChanged((a, b) => {
45
+ // return JSON.stringify(a).length === JSON.stringify(b).length
46
+ // }),
47
+ // )
48
+
49
+ // return combineLatest({
50
+ // nodeList: nodeList$,
51
+ // categoryLabels: categoryLabels$
52
+ // }).pipe(
53
+ // switchMap(async d => d),
54
+ // map(data => {
55
+ // const CurrentLabelSet = new Set(data.categoryLabels)
56
+ // const ExistLabelSet = new Set(
57
+ // data.nodeList.filter(node => node.visible).map(node => node.categoryLabel)
58
+ // )
59
+ // // 加入已存在的label(data.nodeList有,但是dataFormatter.categoryLabels沒有)
60
+ // Array.from(ExistLabelSet).forEach(label => {
61
+ // if (!CurrentLabelSet.has(label)) {
62
+ // CurrentLabelSet.add(label)
63
+ // }
64
+ // })
65
+ // // 移除不存在的label(dataFormatter.categoryLabels有,但是data.nodeList沒有)
66
+ // Array.from(CurrentLabelSet).forEach(label => {
67
+ // if (!ExistLabelSet.has(label)) {
68
+ // ExistLabelSet.delete(label)
69
+ // }
70
+ // })
71
+
72
+ // return Array.from(CurrentLabelSet)
73
+ // }),
74
+ // distinctUntilChanged((a, b) => {
75
+ // return JSON.stringify(a).length === JSON.stringify(b).length
76
+ // }),
77
+ // )
78
+ // }
79
+
80
+ export const categoryLabelsObservable = (CategoryDataMap$: Observable<Map<string, ComputedDataTree[]>>) => {
81
+ return CategoryDataMap$.pipe(
82
+ map(data => {
83
+ return Array.from(data.keys())
84
+ }),
85
+ distinctUntilChanged((a, b) => {
86
+ return JSON.stringify(a).length === JSON.stringify(b).length
87
+ }),
88
+ )
89
+ }
90
+
91
+ // 所有可見的節點
92
+ export const treeVisibleComputedDataObservable = ({ computedData$ }: { computedData$: Observable<ComputedDataTree> }) => {
93
+ return computedData$.pipe(
94
+ map(data => {
95
+ function filterChildren (accTree: ComputedDataTree) {
96
+ if (accTree.children) {
97
+ accTree.children = accTree.children
98
+ .filter(child => child.visible) // 篩選visible
99
+ .map(child => filterChildren(child)) // 遞迴子節點
100
+ }
101
+ return accTree
102
+ }
103
+ return filterChildren(data)
104
+ })
105
+ )
106
106
  }