@orbcharts/core 3.0.0-alpha.48 → 3.0.0-alpha.49

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 (83) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-core.es.js +3 -2
  3. package/dist/orbcharts-core.umd.js +1 -1
  4. package/dist/src/types/Chart.d.ts +10 -12
  5. package/package.json +41 -41
  6. package/src/AbstractChart.ts +48 -48
  7. package/src/GridChart.ts +20 -20
  8. package/src/MultiGridChart.ts +20 -20
  9. package/src/MultiValueChart.ts +20 -20
  10. package/src/RelationshipChart.ts +20 -20
  11. package/src/SeriesChart.ts +20 -20
  12. package/src/TreeChart.ts +20 -20
  13. package/src/base/createBaseChart.ts +369 -368
  14. package/src/base/createBasePlugin.ts +95 -95
  15. package/src/defaults.ts +220 -220
  16. package/src/defineGridPlugin.ts +3 -3
  17. package/src/defineMultiGridPlugin.ts +3 -3
  18. package/src/defineMultiValuePlugin.ts +3 -3
  19. package/src/defineNoneDataPlugin.ts +4 -4
  20. package/src/defineRelationshipPlugin.ts +3 -3
  21. package/src/defineSeriesPlugin.ts +3 -3
  22. package/src/defineTreePlugin.ts +3 -3
  23. package/src/grid/computeGridData.ts +134 -134
  24. package/src/grid/createGridContextObserver.ts +155 -155
  25. package/src/grid/gridObservables.ts +600 -600
  26. package/src/index.ts +21 -21
  27. package/src/multiGrid/computeMultiGridData.ts +130 -130
  28. package/src/multiGrid/createMultiGridContextObserver.ts +40 -40
  29. package/src/multiGrid/multiGridObservables.ts +364 -364
  30. package/src/multiValue/computeMultiValueData.ts +143 -143
  31. package/src/multiValue/createMultiValueContextObserver.ts +12 -12
  32. package/src/relationship/computeRelationshipData.ts +118 -118
  33. package/src/relationship/createRelationshipContextObserver.ts +12 -12
  34. package/src/series/computeSeriesData.ts +90 -90
  35. package/src/series/createSeriesContextObserver.ts +93 -93
  36. package/src/series/seriesObservables.ts +175 -175
  37. package/src/tree/computeTreeData.ts +131 -131
  38. package/src/tree/createTreeContextObserver.ts +61 -61
  39. package/src/tree/treeObservables.ts +94 -94
  40. package/src/types/Chart.ts +50 -48
  41. package/src/types/ChartParams.ts +51 -51
  42. package/src/types/ComputedData.ts +83 -83
  43. package/src/types/ComputedDataGrid.ts +13 -13
  44. package/src/types/ComputedDataMultiGrid.ts +2 -2
  45. package/src/types/ComputedDataMultiValue.ts +9 -9
  46. package/src/types/ComputedDataRelationship.ts +19 -19
  47. package/src/types/ComputedDataSeries.ts +7 -7
  48. package/src/types/ComputedDataTree.ts +19 -19
  49. package/src/types/ContextObserver.ts +38 -38
  50. package/src/types/ContextObserverGrid.ts +42 -42
  51. package/src/types/ContextObserverMultiGrid.ts +15 -15
  52. package/src/types/ContextObserverMultiValue.ts +4 -4
  53. package/src/types/ContextObserverRelationship.ts +4 -4
  54. package/src/types/ContextObserverSeries.ts +29 -29
  55. package/src/types/ContextObserverTree.ts +11 -11
  56. package/src/types/ContextSubject.ts +18 -18
  57. package/src/types/Data.ts +45 -45
  58. package/src/types/DataFormatter.ts +74 -74
  59. package/src/types/DataFormatterGrid.ts +67 -67
  60. package/src/types/DataFormatterMultiGrid.ts +44 -44
  61. package/src/types/DataFormatterMultiValue.ts +23 -23
  62. package/src/types/DataFormatterRelationship.ts +25 -25
  63. package/src/types/DataFormatterSeries.ts +20 -20
  64. package/src/types/DataFormatterTree.ts +12 -12
  65. package/src/types/DataGrid.ts +11 -11
  66. package/src/types/DataMultiGrid.ts +6 -6
  67. package/src/types/DataMultiValue.ts +12 -12
  68. package/src/types/DataRelationship.ts +27 -27
  69. package/src/types/DataSeries.ts +11 -11
  70. package/src/types/DataTree.ts +20 -20
  71. package/src/types/Event.ts +153 -153
  72. package/src/types/Layout.ts +11 -11
  73. package/src/types/Padding.ts +5 -5
  74. package/src/types/Plugin.ts +60 -60
  75. package/src/types/TransformData.ts +7 -7
  76. package/src/types/index.ts +37 -37
  77. package/src/utils/commonUtils.ts +50 -50
  78. package/src/utils/d3Utils.ts +89 -89
  79. package/src/utils/index.ts +4 -4
  80. package/src/utils/observables.ts +201 -201
  81. package/src/utils/orbchartsUtils.ts +349 -349
  82. package/tsconfig.json +13 -13
  83. package/vite.config.js +44 -44
@@ -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
- SeriesContainerPosition,
18
- Layout } from '../types'
19
- import { calcSeriesContainerLayout } from '../utils/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 visibleComputedDataObservable = ({ 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 computedLayoutDataObservable = ({ 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<SeriesContainerPosition[]> => {
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 } = calcSeriesContainerPosition(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 } = calcSeriesContainerPosition(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<SeriesContainerPosition[]>
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, SeriesContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
169
- return [seriesLabel, data.seriesContainerPosition[seriesIndex] ?? data.seriesContainerPosition[0]]
170
- }))
171
- : new Map<string, SeriesContainerPosition>(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
+ SeriesContainerPosition,
18
+ Layout } from '../types'
19
+ import { calcSeriesContainerLayout } from '../utils/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 visibleComputedDataObservable = ({ 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 computedLayoutDataObservable = ({ 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<SeriesContainerPosition[]> => {
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 } = calcSeriesContainerPosition(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 } = calcSeriesContainerPosition(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<SeriesContainerPosition[]>
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, SeriesContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
169
+ return [seriesLabel, data.seriesContainerPosition[seriesIndex] ?? data.seriesContainerPosition[0]]
170
+ }))
171
+ : new Map<string, SeriesContainerPosition>(data.seriesLabels.map((seriesLabel, seriesIndex) => {
172
+ return [seriesLabel, data.seriesContainerPosition[0]]
173
+ }))
174
+ })
175
+ )
176
176
  }
@@ -1,131 +1,131 @@
1
- import type { DataTree, DataTreeObj, DataTreeDatum } from '../types/DataTree'
2
- import type { ComputedDataFn } from '../types/ComputedData'
3
- import type { ComputedDataTree } from '../types/ComputedDataTree'
4
- import { isPlainObject } from '../utils/commonUtils'
5
- import { seriesColorPredicate } from '../utils/orbchartsUtils'
6
-
7
- export const computeTreeData: ComputedDataFn<'tree'> = (context) => {
8
- const { data = [], dataFormatter, chartParams } = context
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: 0,
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
- } else if (Array.isArray(data) === false) {
39
- return {
40
- id: ''
41
- }
42
- }
43
- // -- 陣列格式轉物件 --
44
- // let rootId = ''
45
- let root: DataTreeDatum | undefined = undefined
46
- // const DataMap: Map<string, DataTreeDatum> = new Map()
47
- const ChildrenMap: Map<string, DataTreeDatum[]> = new Map()
48
- ;(data as DataTreeDatum[]).forEach(d => {
49
- // DataMap.set(d.id, d)
50
-
51
- if (!d.parent) {
52
- // rootId = d.id
53
- root = d
54
- } else {
55
- const children: DataTreeDatum[] = ChildrenMap.get(d.parent) ?? []
56
- children.push(d)
57
- ChildrenMap.set(d.parent!, children)
58
- }
59
- })
60
-
61
- const createBranchData = (root: DataTreeDatum): DataTreeObj => {
62
- return {
63
- id: root.id,
64
- label: root.label,
65
- data: root.data,
66
- // tooltipContent: root.tooltipContent,
67
- value: root.value,
68
- categoryLabel: root.categoryLabel,
69
- children: (ChildrenMap.get(root.id) ?? []).map(d => {
70
- // 遞迴
71
- return createBranchData(d)
72
- })
73
- }
74
- }
75
- if (root) {
76
- return createBranchData(root)
77
- } else {
78
- return {
79
- id: ''
80
- }
81
- }
82
- })()
83
-
84
- let index = 0
85
-
86
- const formatBranchData = (branch: DataTreeObj, level: number, seq: number): ComputedDataTree => {
87
- const childLayer = level + 1
88
- const categoryLabel: string | null = branch.categoryLabel ?? null
89
- let categoryIndex = 0
90
- if (categoryLabel != null) {
91
- if (!CategoryIndexMap.has(categoryLabel)) {
92
- CategoryIndexMap.set(categoryLabel, CategoryIndexMap.size)
93
- }
94
- categoryIndex = CategoryIndexMap.get(categoryLabel) ?? 0
95
- }
96
-
97
- const currentIndex = index
98
- index++
99
- const formattedBranchData: ComputedDataTree = {
100
- id: branch.id,
101
- index: currentIndex,
102
- level,
103
- seq,
104
- label: branch.label ?? '',
105
- description: branch.description ?? '',
106
- categoryIndex,
107
- categoryLabel,
108
- color: seriesColorPredicate(categoryIndex, chartParams),
109
- data: branch.data ?? {},
110
- // tooltipContent: branch.tooltipContent ? branch.tooltipContent : dataFormatter.tooltipContentFormat(branch, level, seq, context),
111
- value: branch.value,
112
- visible: true, // 先給預設值
113
- children: (branch.children ?? []).map((d, i) => {
114
- // 遞迴
115
- return formatBranchData(d, childLayer, i)
116
- })
117
- }
118
-
119
- formattedBranchData.visible = dataFormatter.visibleFilter(formattedBranchData, context)
120
-
121
- return formattedBranchData
122
- }
123
- computedBranchData = formatBranchData(dataTreeObj, 0, 0)
124
- } catch (e) {
125
- // console.error(e)
126
- throw Error(e)
127
- }
128
-
129
- return computedBranchData
130
-
131
- }
1
+ import type { DataTree, DataTreeObj, DataTreeDatum } from '../types/DataTree'
2
+ import type { ComputedDataFn } from '../types/ComputedData'
3
+ import type { ComputedDataTree } from '../types/ComputedDataTree'
4
+ import { isPlainObject } from '../utils/commonUtils'
5
+ import { seriesColorPredicate } from '../utils/orbchartsUtils'
6
+
7
+ export const computeTreeData: ComputedDataFn<'tree'> = (context) => {
8
+ const { data = [], dataFormatter, chartParams } = context
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: 0,
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
+ } else if (Array.isArray(data) === false) {
39
+ return {
40
+ id: ''
41
+ }
42
+ }
43
+ // -- 陣列格式轉物件 --
44
+ // let rootId = ''
45
+ let root: DataTreeDatum | undefined = undefined
46
+ // const DataMap: Map<string, DataTreeDatum> = new Map()
47
+ const ChildrenMap: Map<string, DataTreeDatum[]> = new Map()
48
+ ;(data as DataTreeDatum[]).forEach(d => {
49
+ // DataMap.set(d.id, d)
50
+
51
+ if (!d.parent) {
52
+ // rootId = d.id
53
+ root = d
54
+ } else {
55
+ const children: DataTreeDatum[] = ChildrenMap.get(d.parent) ?? []
56
+ children.push(d)
57
+ ChildrenMap.set(d.parent!, children)
58
+ }
59
+ })
60
+
61
+ const createBranchData = (root: DataTreeDatum): DataTreeObj => {
62
+ return {
63
+ id: root.id,
64
+ label: root.label,
65
+ data: root.data,
66
+ // tooltipContent: root.tooltipContent,
67
+ value: root.value,
68
+ categoryLabel: root.categoryLabel,
69
+ children: (ChildrenMap.get(root.id) ?? []).map(d => {
70
+ // 遞迴
71
+ return createBranchData(d)
72
+ })
73
+ }
74
+ }
75
+ if (root) {
76
+ return createBranchData(root)
77
+ } else {
78
+ return {
79
+ id: ''
80
+ }
81
+ }
82
+ })()
83
+
84
+ let index = 0
85
+
86
+ const formatBranchData = (branch: DataTreeObj, level: number, seq: number): ComputedDataTree => {
87
+ const childLayer = level + 1
88
+ const categoryLabel: string | null = branch.categoryLabel ?? null
89
+ let categoryIndex = 0
90
+ if (categoryLabel != null) {
91
+ if (!CategoryIndexMap.has(categoryLabel)) {
92
+ CategoryIndexMap.set(categoryLabel, CategoryIndexMap.size)
93
+ }
94
+ categoryIndex = CategoryIndexMap.get(categoryLabel) ?? 0
95
+ }
96
+
97
+ const currentIndex = index
98
+ index++
99
+ const formattedBranchData: ComputedDataTree = {
100
+ id: branch.id,
101
+ index: currentIndex,
102
+ level,
103
+ seq,
104
+ label: branch.label ?? '',
105
+ description: branch.description ?? '',
106
+ categoryIndex,
107
+ categoryLabel,
108
+ color: seriesColorPredicate(categoryIndex, chartParams),
109
+ data: branch.data ?? {},
110
+ // tooltipContent: branch.tooltipContent ? branch.tooltipContent : dataFormatter.tooltipContentFormat(branch, level, seq, context),
111
+ value: branch.value,
112
+ visible: true, // 先給預設值
113
+ children: (branch.children ?? []).map((d, i) => {
114
+ // 遞迴
115
+ return formatBranchData(d, childLayer, i)
116
+ })
117
+ }
118
+
119
+ formattedBranchData.visible = dataFormatter.visibleFilter(formattedBranchData, context)
120
+
121
+ return formattedBranchData
122
+ }
123
+ computedBranchData = formatBranchData(dataTreeObj, 0, 0)
124
+ } catch (e) {
125
+ // console.error(e)
126
+ throw Error(e)
127
+ }
128
+
129
+ return computedBranchData
130
+
131
+ }