@orbcharts/core 3.0.6 → 3.0.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 (68) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +779 -779
  3. package/dist/orbcharts-core.umd.js +1 -1
  4. package/lib/core-types.ts +7 -7
  5. package/package.json +46 -46
  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 +524 -524
  14. package/src/base/createBasePlugin.ts +154 -154
  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 +284 -284
  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 +209 -209
  29. package/src/grid/dataFormatterValidator.ts +125 -125
  30. package/src/grid/dataValidator.ts +12 -12
  31. package/src/grid/gridObservables.ts +698 -698
  32. package/src/index.ts +20 -20
  33. package/src/multiGrid/computedDataFn.ts +123 -123
  34. package/src/multiGrid/contextObserverCallback.ts +109 -109
  35. package/src/multiGrid/dataFormatterValidator.ts +120 -120
  36. package/src/multiGrid/dataValidator.ts +12 -12
  37. package/src/multiGrid/multiGridObservables.ts +366 -366
  38. package/src/multiValue/computedDataFn.ts +113 -113
  39. package/src/multiValue/contextObserverCallback.ts +328 -328
  40. package/src/multiValue/dataFormatterValidator.ts +94 -94
  41. package/src/multiValue/dataValidator.ts +12 -12
  42. package/src/multiValue/multiValueObservables.ts +865 -865
  43. package/src/relationship/computedDataFn.ts +159 -159
  44. package/src/relationship/contextObserverCallback.ts +80 -80
  45. package/src/relationship/dataFormatterValidator.ts +13 -13
  46. package/src/relationship/dataValidator.ts +13 -13
  47. package/src/relationship/relationshipObservables.ts +84 -84
  48. package/src/series/computedDataFn.ts +88 -88
  49. package/src/series/contextObserverCallback.ts +132 -132
  50. package/src/series/dataFormatterValidator.ts +46 -46
  51. package/src/series/dataValidator.ts +12 -12
  52. package/src/series/seriesObservables.ts +209 -209
  53. package/src/tree/computedDataFn.ts +129 -129
  54. package/src/tree/contextObserverCallback.ts +58 -58
  55. package/src/tree/dataFormatterValidator.ts +13 -13
  56. package/src/tree/dataValidator.ts +13 -13
  57. package/src/tree/treeObservables.ts +105 -105
  58. package/src/utils/commonUtils.ts +55 -55
  59. package/src/utils/d3Scale.ts +198 -198
  60. package/src/utils/errorMessage.ts +40 -40
  61. package/src/utils/index.ts +3 -3
  62. package/src/utils/observables.ts +307 -308
  63. package/src/utils/orbchartsUtils.ts +396 -396
  64. package/src/utils/validator.ts +126 -126
  65. package/tsconfig.base.json +13 -13
  66. package/tsconfig.json +2 -2
  67. package/vite-env.d.ts +6 -6
  68. package/vite.config.js +22 -22
@@ -1,129 +1,129 @@
1
- import type { DataTree, DataTreeObj, DataTreeDatum, ComputedDataFn, ComputedDataTree } from '../../lib/core-types'
2
- import { isPlainObject } from '../utils/commonUtils'
3
- import { seriesColorPredicate, createDefaultCategoryLabel } from '../utils/orbchartsUtils'
4
-
5
- export const computedDataFn: ComputedDataFn<'tree'> = (context) => {
6
- const { data = [], dataFormatter, chartParams } = context
7
-
8
- const defaultCategoryLabel = createDefaultCategoryLabel()
9
-
10
- // <categoryLabel, categoryIndex>
11
- const CategoryIndexMap = new Map<string, number>(
12
- dataFormatter.categoryLabels.map((label, index) => [label, index])
13
- )
14
-
15
- let computedBranchData: ComputedDataTree = {
16
- id: '',
17
- index: 0,
18
- label: '',
19
- description: '',
20
- categoryIndex: -1,
21
- categoryLabel: '',
22
- color: '',
23
- visible: true,
24
- // tooltipContent: '',
25
- data: {},
26
- value: 0,
27
- level: 0,
28
- seq: 0,
29
- children: []
30
- }
31
-
32
- try {
33
- // 建立樹狀結構資料
34
- const dataTreeObj: DataTreeObj = (function () {
35
- if (isPlainObject(data) === true) {
36
- // 原本就是樹狀結構則直接複製
37
- // return structuredClone(data) as DataTreeObj
38
- return JSON.parse(JSON.stringify(data)) as DataTreeObj
39
- } else if (Array.isArray(data) === false) {
40
- return {
41
- id: ''
42
- }
43
- }
44
- // -- 陣列格式轉物件 --
45
- // let rootId = ''
46
- let root: DataTreeDatum | undefined = undefined
47
- // const DataMap: Map<string, DataTreeDatum> = new Map()
48
- const ChildrenMap: Map<string, DataTreeDatum[]> = new Map()
49
- ;(data as DataTreeDatum[]).forEach(d => {
50
- // DataMap.set(d.id, d)
51
-
52
- if (!d.parent) {
53
- // rootId = d.id
54
- root = d
55
- } else {
56
- const children: DataTreeDatum[] = ChildrenMap.get(d.parent) ?? []
57
- children.push(d)
58
- ChildrenMap.set(d.parent!, children)
59
- }
60
- })
61
-
62
- const createBranchData = (root: DataTreeDatum): DataTreeObj => {
63
- return {
64
- id: root.id,
65
- label: root.label,
66
- data: root.data,
67
- // tooltipContent: root.tooltipContent,
68
- value: root.value,
69
- categoryLabel: root.categoryLabel ?? defaultCategoryLabel,
70
- children: (ChildrenMap.get(root.id) ?? []).map(d => {
71
- // 遞迴
72
- return createBranchData(d)
73
- })
74
- }
75
- }
76
- if (root) {
77
- return createBranchData(root)
78
- } else {
79
- return {
80
- id: ''
81
- }
82
- }
83
- })()
84
-
85
- let index = 0
86
-
87
- const formatBranchData = (branch: DataTreeObj, level: number, seq: number): ComputedDataTree => {
88
- const childLayer = level + 1
89
- const categoryLabel: string = branch.categoryLabel ?? defaultCategoryLabel
90
- if (!CategoryIndexMap.has(categoryLabel)) {
91
- CategoryIndexMap.set(categoryLabel, CategoryIndexMap.size)
92
- }
93
- const categoryIndex = CategoryIndexMap.get(categoryLabel) ?? 0
94
-
95
- const currentIndex = index
96
- index++
97
- const formattedBranchData: ComputedDataTree = {
98
- id: branch.id,
99
- index: currentIndex,
100
- level,
101
- seq,
102
- label: branch.label ?? '',
103
- description: branch.description ?? '',
104
- categoryIndex,
105
- categoryLabel,
106
- color: seriesColorPredicate(categoryIndex, chartParams),
107
- data: branch.data ?? {},
108
- // tooltipContent: branch.tooltipContent ? branch.tooltipContent : dataFormatter.tooltipContentFormat(branch, level, seq, context),
109
- value: branch.value,
110
- visible: true, // 先給預設值
111
- children: (branch.children ?? []).map((d, i) => {
112
- // 遞迴
113
- return formatBranchData(d, childLayer, i)
114
- })
115
- }
116
-
117
- formattedBranchData.visible = dataFormatter.visibleFilter(formattedBranchData, context)
118
-
119
- return formattedBranchData
120
- }
121
- computedBranchData = formatBranchData(dataTreeObj, 0, 0)
122
- } catch (e) {
123
- // console.error(e)
124
- throw Error(e)
125
- }
126
-
127
- return computedBranchData
128
-
129
- }
1
+ import type { DataTree, DataTreeObj, DataTreeDatum, ComputedDataFn, ComputedDataTree } from '../../lib/core-types'
2
+ import { isPlainObject } from '../utils/commonUtils'
3
+ import { seriesColorPredicate, createDefaultCategoryLabel } from '../utils/orbchartsUtils'
4
+
5
+ export const computedDataFn: ComputedDataFn<'tree'> = (context) => {
6
+ const { data = [], dataFormatter, chartParams } = context
7
+
8
+ const defaultCategoryLabel = createDefaultCategoryLabel()
9
+
10
+ // <categoryLabel, categoryIndex>
11
+ const CategoryIndexMap = new Map<string, number>(
12
+ dataFormatter.categoryLabels.map((label, index) => [label, index])
13
+ )
14
+
15
+ let computedBranchData: ComputedDataTree = {
16
+ id: '',
17
+ index: 0,
18
+ label: '',
19
+ description: '',
20
+ categoryIndex: -1,
21
+ categoryLabel: '',
22
+ color: '',
23
+ visible: true,
24
+ // tooltipContent: '',
25
+ data: {},
26
+ value: 0,
27
+ level: 0,
28
+ seq: 0,
29
+ children: []
30
+ }
31
+
32
+ try {
33
+ // 建立樹狀結構資料
34
+ const dataTreeObj: DataTreeObj = (function () {
35
+ if (isPlainObject(data) === true) {
36
+ // 原本就是樹狀結構則直接複製
37
+ // return structuredClone(data) as DataTreeObj
38
+ return JSON.parse(JSON.stringify(data)) as DataTreeObj
39
+ } else if (Array.isArray(data) === false) {
40
+ return {
41
+ id: ''
42
+ }
43
+ }
44
+ // -- 陣列格式轉物件 --
45
+ // let rootId = ''
46
+ let root: DataTreeDatum | undefined = undefined
47
+ // const DataMap: Map<string, DataTreeDatum> = new Map()
48
+ const ChildrenMap: Map<string, DataTreeDatum[]> = new Map()
49
+ ;(data as DataTreeDatum[]).forEach(d => {
50
+ // DataMap.set(d.id, d)
51
+
52
+ if (!d.parent) {
53
+ // rootId = d.id
54
+ root = d
55
+ } else {
56
+ const children: DataTreeDatum[] = ChildrenMap.get(d.parent) ?? []
57
+ children.push(d)
58
+ ChildrenMap.set(d.parent!, children)
59
+ }
60
+ })
61
+
62
+ const createBranchData = (root: DataTreeDatum): DataTreeObj => {
63
+ return {
64
+ id: root.id,
65
+ label: root.label,
66
+ data: root.data,
67
+ // tooltipContent: root.tooltipContent,
68
+ value: root.value,
69
+ categoryLabel: root.categoryLabel ?? defaultCategoryLabel,
70
+ children: (ChildrenMap.get(root.id) ?? []).map(d => {
71
+ // 遞迴
72
+ return createBranchData(d)
73
+ })
74
+ }
75
+ }
76
+ if (root) {
77
+ return createBranchData(root)
78
+ } else {
79
+ return {
80
+ id: ''
81
+ }
82
+ }
83
+ })()
84
+
85
+ let index = 0
86
+
87
+ const formatBranchData = (branch: DataTreeObj, level: number, seq: number): ComputedDataTree => {
88
+ const childLayer = level + 1
89
+ const categoryLabel: string = branch.categoryLabel ?? defaultCategoryLabel
90
+ if (!CategoryIndexMap.has(categoryLabel)) {
91
+ CategoryIndexMap.set(categoryLabel, CategoryIndexMap.size)
92
+ }
93
+ const categoryIndex = CategoryIndexMap.get(categoryLabel) ?? 0
94
+
95
+ const currentIndex = index
96
+ index++
97
+ const formattedBranchData: ComputedDataTree = {
98
+ id: branch.id,
99
+ index: currentIndex,
100
+ level,
101
+ seq,
102
+ label: branch.label ?? '',
103
+ description: branch.description ?? '',
104
+ categoryIndex,
105
+ categoryLabel,
106
+ color: seriesColorPredicate(categoryIndex, chartParams),
107
+ data: branch.data ?? {},
108
+ // tooltipContent: branch.tooltipContent ? branch.tooltipContent : dataFormatter.tooltipContentFormat(branch, level, seq, context),
109
+ value: branch.value,
110
+ visible: true, // 先給預設值
111
+ children: (branch.children ?? []).map((d, i) => {
112
+ // 遞迴
113
+ return formatBranchData(d, childLayer, i)
114
+ })
115
+ }
116
+
117
+ formattedBranchData.visible = dataFormatter.visibleFilter(formattedBranchData, context)
118
+
119
+ return formattedBranchData
120
+ }
121
+ computedBranchData = formatBranchData(dataTreeObj, 0, 0)
122
+ } catch (e) {
123
+ // console.error(e)
124
+ throw Error(e)
125
+ }
126
+
127
+ return computedBranchData
128
+
129
+ }
@@ -1,58 +1,58 @@
1
- import { map, shareReplay } from 'rxjs'
2
- import type { ContextObserverCallback } from '../../lib/core-types'
3
- import { highlightObservable, categoryDataMapObservable, textSizePxObservable } from '../utils/observables'
4
- import {
5
- nodeListObservable,
6
- categoryLabelsObservable,
7
- treeVisibleComputedDataObservable
8
- } from './treeObservables'
9
-
10
- export const contextObserverCallback: ContextObserverCallback<'tree'> = ({ subject, observer }) => {
11
-
12
- const textSizePx$ = textSizePxObservable(observer.fullChartParams$).pipe(
13
- shareReplay(1)
14
- )
15
-
16
- const nodeList$ = nodeListObservable({
17
- computedData$: observer.computedData$
18
- }).pipe(
19
- shareReplay(1)
20
- )
21
-
22
- const treeHighlight$ = highlightObservable({
23
- datumList$: nodeList$,
24
- fullChartParams$: observer.fullChartParams$,
25
- event$: subject.event$
26
- }).pipe(
27
- shareReplay(1)
28
- )
29
-
30
- const CategoryDataMap$ = categoryDataMapObservable({
31
- datumList$: nodeList$
32
- }).pipe(
33
- shareReplay(1)
34
- )
35
-
36
- const categoryLabels$ = categoryLabelsObservable(CategoryDataMap$).pipe(
37
- shareReplay(1)
38
- )
39
-
40
- const visibleComputedData$ = treeVisibleComputedDataObservable({
41
- computedData$: observer.computedData$
42
- }).pipe(
43
- shareReplay(1)
44
- )
45
-
46
- return {
47
- fullParams$: observer.fullParams$,
48
- fullChartParams$: observer.fullChartParams$,
49
- fullDataFormatter$: observer.fullDataFormatter$,
50
- computedData$: observer.computedData$,
51
- layout$: observer.layout$,
52
- textSizePx$,
53
- treeHighlight$,
54
- categoryLabels$,
55
- CategoryDataMap$,
56
- visibleComputedData$
57
- }
58
- }
1
+ import { map, shareReplay } from 'rxjs'
2
+ import type { ContextObserverCallback } from '../../lib/core-types'
3
+ import { highlightObservable, categoryDataMapObservable, textSizePxObservable } from '../utils/observables'
4
+ import {
5
+ nodeListObservable,
6
+ categoryLabelsObservable,
7
+ treeVisibleComputedDataObservable
8
+ } from './treeObservables'
9
+
10
+ export const contextObserverCallback: ContextObserverCallback<'tree'> = ({ subject, observer }) => {
11
+
12
+ const textSizePx$ = textSizePxObservable(observer.fullChartParams$).pipe(
13
+ shareReplay(1)
14
+ )
15
+
16
+ const nodeList$ = nodeListObservable({
17
+ computedData$: observer.computedData$
18
+ }).pipe(
19
+ shareReplay(1)
20
+ )
21
+
22
+ const treeHighlight$ = highlightObservable({
23
+ datumList$: nodeList$,
24
+ fullChartParams$: observer.fullChartParams$,
25
+ event$: subject.event$
26
+ }).pipe(
27
+ shareReplay(1)
28
+ )
29
+
30
+ const CategoryDataMap$ = categoryDataMapObservable({
31
+ datumList$: nodeList$
32
+ }).pipe(
33
+ shareReplay(1)
34
+ )
35
+
36
+ const categoryLabels$ = categoryLabelsObservable(CategoryDataMap$).pipe(
37
+ shareReplay(1)
38
+ )
39
+
40
+ const visibleComputedData$ = treeVisibleComputedDataObservable({
41
+ computedData$: observer.computedData$
42
+ }).pipe(
43
+ shareReplay(1)
44
+ )
45
+
46
+ return {
47
+ fullParams$: observer.fullParams$,
48
+ fullChartParams$: observer.fullChartParams$,
49
+ fullDataFormatter$: observer.fullDataFormatter$,
50
+ computedData$: observer.computedData$,
51
+ layout$: observer.layout$,
52
+ textSizePx$,
53
+ treeHighlight$,
54
+ categoryLabels$,
55
+ CategoryDataMap$,
56
+ visibleComputedData$
57
+ }
58
+ }
@@ -1,14 +1,14 @@
1
- import type { DataFormatterValidator, DataFormatterTypeMap } from '../../lib/core-types'
2
- import { validateColumns } from '../utils/validator'
3
-
4
- export const dataFormatterValidator: DataFormatterValidator<'tree'> = (dataFormatter: DataFormatterTypeMap<'tree'>) => {
5
- const result = validateColumns(dataFormatter, {
6
- visibleFilter: {
7
- toBeTypes: ['Function']
8
- },
9
- categoryLabels: {
10
- toBeTypes: ['string[]']
11
- }
12
- })
13
- return result
1
+ import type { DataFormatterValidator, DataFormatterTypeMap } from '../../lib/core-types'
2
+ import { validateColumns } from '../utils/validator'
3
+
4
+ export const dataFormatterValidator: DataFormatterValidator<'tree'> = (dataFormatter: DataFormatterTypeMap<'tree'>) => {
5
+ const result = validateColumns(dataFormatter, {
6
+ visibleFilter: {
7
+ toBeTypes: ['Function']
8
+ },
9
+ categoryLabels: {
10
+ toBeTypes: ['string[]']
11
+ }
12
+ })
13
+ return result
14
14
  }
@@ -1,14 +1,14 @@
1
- import type { DataValidator, DataTypeMap } from '../../lib/core-types'
2
- import { validateColumns } from '../utils/validator'
3
- import { isPlainObject } from '../utils'
4
-
5
- export const dataValidator: DataValidator<'tree'> = (data: DataTypeMap<'tree'>) => {
6
- const result = validateColumns({ data }, {
7
- data: {
8
- toBe: 'DataTreeObj | DataTreeDatum[]',
9
- // 畢免資料量過大檢查不完,不深度檢查
10
- test: (value) => (isPlainObject(value) && value.id !== undefined) || Array.isArray(value)
11
- }
12
- })
13
- return result
1
+ import type { DataValidator, DataTypeMap } from '../../lib/core-types'
2
+ import { validateColumns } from '../utils/validator'
3
+ import { isPlainObject } from '../utils'
4
+
5
+ export const dataValidator: DataValidator<'tree'> = (data: DataTypeMap<'tree'>) => {
6
+ const result = validateColumns({ data }, {
7
+ data: {
8
+ toBe: 'DataTreeObj | DataTreeDatum[]',
9
+ // 畢免資料量過大檢查不完,不深度檢查
10
+ test: (value) => (isPlainObject(value) && value.id !== undefined) || Array.isArray(value)
11
+ }
12
+ })
13
+ return result
14
14
  }
@@ -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) === JSON.stringify(b)
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) === JSON.stringify(b)
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) === JSON.stringify(b)
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) === JSON.stringify(b)
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) === JSON.stringify(b)
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) === JSON.stringify(b)
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
  }