@orbcharts/plugins-basic 3.0.0-beta.2 → 3.0.0-beta.20

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 (182) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +15169 -10684
  3. package/dist/orbcharts-plugins-basic.umd.js +296 -83
  4. package/dist/src/base/BaseBars.d.ts +4 -4
  5. package/dist/src/base/BaseBarsTriangle.d.ts +4 -4
  6. package/dist/src/base/BaseDots.d.ts +4 -4
  7. package/dist/src/base/BaseGroupAxis.d.ts +2 -2
  8. package/dist/src/base/BaseLegend.d.ts +1 -1
  9. package/dist/src/base/BaseLineAreas.d.ts +5 -5
  10. package/dist/src/base/BaseLines.d.ts +5 -5
  11. package/dist/src/base/BaseRacingBars.d.ts +23 -0
  12. package/dist/src/base/BaseRacingLabels.d.ts +20 -0
  13. package/dist/src/base/BaseRacingValueLabels.d.ts +20 -0
  14. package/dist/src/base/{BaseBarStack.d.ts → BaseStackedBars.d.ts} +8 -8
  15. package/dist/src/base/BaseValueAxis.d.ts +6 -8
  16. package/dist/src/base/BaseXAxis.d.ts +20 -0
  17. package/dist/src/base/BaseYAxis.d.ts +18 -0
  18. package/dist/src/grid/defaults.d.ts +5 -5
  19. package/dist/src/grid/gridObservables.d.ts +12 -5
  20. package/dist/src/grid/index.d.ts +3 -3
  21. package/dist/src/grid/plugins/GroupZoom.d.ts +1 -0
  22. package/dist/src/grid/plugins/StackedBars.d.ts +1 -0
  23. package/dist/src/grid/plugins/StackedValueAxis.d.ts +1 -0
  24. package/dist/src/index.d.ts +2 -0
  25. package/dist/src/multiGrid/defaults.d.ts +4 -4
  26. package/dist/src/multiGrid/index.d.ts +3 -3
  27. package/dist/src/multiGrid/plugins/MultiStackedBars.d.ts +1 -0
  28. package/dist/src/multiGrid/plugins/MultiStackedValueAxis.d.ts +1 -0
  29. package/dist/src/multiGrid/plugins/OverlappingStackedValueAxes.d.ts +1 -0
  30. package/dist/src/multiValue/defaults.d.ts +13 -0
  31. package/dist/src/multiValue/index.d.ts +12 -0
  32. package/dist/src/multiValue/multiValueObservables.d.ts +56 -0
  33. package/dist/src/multiValue/plugins/MultiValueLegend.d.ts +1 -0
  34. package/dist/src/multiValue/plugins/MultiValueTooltip.d.ts +1 -0
  35. package/dist/src/multiValue/plugins/RacingBars.d.ts +1 -0
  36. package/dist/src/multiValue/plugins/RacingCounterTexts.d.ts +3 -0
  37. package/dist/src/multiValue/plugins/RacingValueAxis.d.ts +1 -0
  38. package/dist/src/multiValue/plugins/Scatter.d.ts +3 -0
  39. package/dist/src/multiValue/plugins/ScatterBubbles.d.ts +3 -0
  40. package/dist/src/multiValue/plugins/XAxis.d.ts +1 -0
  41. package/dist/src/multiValue/plugins/XYAux.d.ts +3 -0
  42. package/dist/src/multiValue/plugins/XYAxes.d.ts +1 -0
  43. package/dist/src/multiValue/plugins/XYAxes_legacy.d.ts +3 -0
  44. package/dist/src/multiValue/plugins/XZoom.d.ts +1 -0
  45. package/dist/src/noneData/plugins/Container.d.ts +0 -1
  46. package/dist/src/noneData/plugins/Tooltip.d.ts +0 -3
  47. package/dist/src/relationship/defaults.d.ts +6 -0
  48. package/dist/src/relationship/index.d.ts +5 -0
  49. package/dist/src/relationship/plugins/ForceDirected.d.ts +3 -0
  50. package/dist/src/relationship/plugins/ForceDirectedBubbles.d.ts +3 -0
  51. package/dist/src/relationship/plugins/RelationshipLegend.d.ts +1 -0
  52. package/dist/src/relationship/plugins/RelationshipTooltip.d.ts +1 -0
  53. package/dist/src/relationship/relationshipObservables.d.ts +1 -0
  54. package/dist/src/series/seriesObservables.d.ts +3 -3
  55. package/dist/src/utils/commonUtils.d.ts +2 -1
  56. package/dist/src/utils/d3Graphics.d.ts +2 -1
  57. package/dist/src/utils/d3Utils.d.ts +3 -2
  58. package/dist/src/utils/observables.d.ts +1 -1
  59. package/dist/src/utils/orbchartsUtils.d.ts +3 -3
  60. package/lib/core-types.ts +7 -7
  61. package/lib/core.ts +6 -6
  62. package/lib/plugins-basic-types.ts +6 -6
  63. package/package.json +44 -44
  64. package/src/base/BaseBars.ts +765 -765
  65. package/src/base/BaseBarsTriangle.ts +676 -674
  66. package/src/base/BaseDots.ts +464 -515
  67. package/src/base/BaseGroupAxis.ts +691 -679
  68. package/src/base/BaseLegend.ts +684 -680
  69. package/src/base/BaseLineAreas.ts +629 -629
  70. package/src/base/BaseLines.ts +706 -706
  71. package/src/base/BaseRacingBars.ts +552 -0
  72. package/src/base/BaseRacingLabels.ts +396 -0
  73. package/src/base/BaseRacingValueLabels.ts +403 -0
  74. package/src/base/{BaseBarStack.ts → BaseStackedBars.ts} +782 -780
  75. package/src/base/BaseTooltip.ts +386 -385
  76. package/src/base/BaseValueAxis.ts +600 -580
  77. package/src/base/BaseXAxis.ts +427 -0
  78. package/src/base/BaseYAxis.ts +389 -0
  79. package/src/base/types.ts +2 -2
  80. package/src/const.ts +30 -30
  81. package/src/grid/defaults.ts +213 -244
  82. package/src/grid/gridObservables.ts +612 -545
  83. package/src/grid/index.ts +16 -16
  84. package/src/grid/plugins/Bars.ts +69 -69
  85. package/src/grid/plugins/BarsPN.ts +66 -66
  86. package/src/grid/plugins/BarsTriangle.ts +73 -73
  87. package/src/grid/plugins/Dots.ts +68 -68
  88. package/src/grid/plugins/GridLegend.ts +107 -107
  89. package/src/grid/plugins/GridTooltip.ts +66 -66
  90. package/src/grid/plugins/GroupAux.ts +1120 -1098
  91. package/src/grid/plugins/GroupAxis.ts +73 -97
  92. package/src/grid/plugins/{GridZoom.ts → GroupZoom.ts} +218 -218
  93. package/src/grid/plugins/LineAreas.ts +65 -65
  94. package/src/grid/plugins/Lines.ts +59 -59
  95. package/src/grid/plugins/{BarStack.ts → StackedBars.ts} +64 -64
  96. package/src/grid/plugins/{ValueStackAxis.ts → StackedValueAxis.ts} +96 -95
  97. package/src/grid/plugins/ValueAxis.ts +94 -93
  98. package/src/index.ts +6 -9
  99. package/src/multiGrid/defaults.ts +244 -224
  100. package/src/multiGrid/index.ts +15 -15
  101. package/src/multiGrid/multiGridObservables.ts +50 -49
  102. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  103. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  104. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  105. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -159
  106. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  107. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  108. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  109. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  110. package/src/multiGrid/plugins/{MultiBarStack.ts → MultiStackedBars.ts} +106 -106
  111. package/src/multiGrid/plugins/{MultiValueStackAxis.ts → MultiStackedValueAxis.ts} +134 -133
  112. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -133
  113. package/src/multiGrid/plugins/{OverlappingValueStackAxes.ts → OverlappingStackedValueAxes.ts} +300 -298
  114. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -299
  115. package/src/multiValue/defaults.ts +432 -0
  116. package/src/multiValue/index.ts +13 -0
  117. package/src/multiValue/multiValueObservables.ts +667 -0
  118. package/src/multiValue/plugins/MultiValueLegend.ts +107 -0
  119. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -0
  120. package/src/multiValue/plugins/RacingBars.ts +373 -0
  121. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -0
  122. package/src/multiValue/plugins/RacingValueAxis.ts +115 -0
  123. package/src/multiValue/plugins/RankingAxis_legacy.ts +109 -0
  124. package/src/multiValue/plugins/Scatter.ts +426 -0
  125. package/src/multiValue/plugins/ScatterBubbles.ts +554 -0
  126. package/src/multiValue/plugins/XAxis.ts +108 -0
  127. package/src/multiValue/plugins/XYAux.ts +683 -0
  128. package/src/multiValue/plugins/XYAxes.ts +195 -0
  129. package/src/multiValue/plugins/XYAxes_legacy.ts +684 -0
  130. package/src/multiValue/plugins/XZoom.ts +300 -0
  131. package/src/noneData/defaults.ts +102 -102
  132. package/src/noneData/index.ts +3 -3
  133. package/src/noneData/plugins/Container.ts +28 -28
  134. package/src/noneData/plugins/Tooltip.ts +374 -374
  135. package/src/relationship/defaults.ts +222 -0
  136. package/src/relationship/index.ts +5 -0
  137. package/src/relationship/plugins/ForceDirected.ts +1174 -0
  138. package/src/relationship/plugins/ForceDirectedBubbles.ts +1412 -0
  139. package/src/relationship/plugins/RelationshipLegend.ts +100 -0
  140. package/src/relationship/plugins/RelationshipTooltip.ts +66 -0
  141. package/src/relationship/relationshipObservables.ts +50 -0
  142. package/src/series/defaults.ts +221 -206
  143. package/src/series/index.ts +9 -9
  144. package/src/series/plugins/Bubbles.ts +636 -603
  145. package/src/series/plugins/Pie.ts +623 -623
  146. package/src/series/plugins/PieEventTexts.ts +284 -283
  147. package/src/series/plugins/PieLabels.ts +640 -640
  148. package/src/series/plugins/Rose.ts +516 -516
  149. package/src/series/plugins/RoseLabels.ts +600 -600
  150. package/src/series/plugins/SeriesLegend.ts +107 -107
  151. package/src/series/plugins/SeriesTooltip.ts +66 -66
  152. package/src/series/seriesObservables.ts +145 -145
  153. package/src/series/seriesUtils.ts +51 -51
  154. package/src/tree/defaults.ts +102 -78
  155. package/src/tree/index.ts +4 -4
  156. package/src/tree/plugins/TreeLegend.ts +100 -107
  157. package/src/tree/plugins/TreeMap.ts +341 -333
  158. package/src/tree/plugins/TreeTooltip.ts +66 -66
  159. package/src/utils/commonUtils.ts +31 -22
  160. package/src/utils/d3Graphics.ts +176 -174
  161. package/src/utils/d3Utils.ts +92 -73
  162. package/src/utils/observables.ts +14 -14
  163. package/src/utils/orbchartsUtils.ts +130 -100
  164. package/tsconfig.base.json +13 -13
  165. package/tsconfig.json +2 -2
  166. package/vite.config.js +22 -22
  167. package/dist/src/grid/plugins/BarStack.d.ts +0 -1
  168. package/dist/src/grid/plugins/GridZoom.d.ts +0 -1
  169. package/dist/src/grid/plugins/ValueStackAxis.d.ts +0 -1
  170. package/dist/src/multiGrid/plugins/MultiBarStack.d.ts +0 -1
  171. package/dist/src/multiGrid/plugins/MultiValueStackAxis.d.ts +0 -1
  172. package/dist/src/multiGrid/plugins/OverlappingValueStackAxes.d.ts +0 -1
  173. package/src/grid/plugins/RankingAxis.ts +0 -0
  174. package/src/multiValue/plugins/ScatterAxes.ts +0 -0
  175. package/src/relationship/plugins/Relationship.ts +0 -0
  176. /package/dist/src/{base/BaseGroupArea.d.ts → multiValue/plugins/OrdinalBubbles.d.ts} +0 -0
  177. /package/dist/src/{grid/plugins/Ranking.d.ts → multiValue/plugins/OrdinalXAxis.d.ts} +0 -0
  178. /package/dist/src/{grid/plugins/RankingAxis.d.ts → multiValue/plugins/RacingCounterAxis.d.ts} +0 -0
  179. /package/dist/src/multiValue/plugins/{ScatterAxes.d.ts → RankingAxis_legacy.d.ts} +0 -0
  180. /package/{dist/src/relationship/plugins/Relationship.d.ts → src/multiValue/plugins/OrdinalBubbles.ts} +0 -0
  181. /package/src/{base/BaseGroupArea.ts → multiValue/plugins/OrdinalXAxis.ts} +0 -0
  182. /package/src/{grid/plugins/Ranking.ts → multiValue/plugins/RacingCounterAxis.ts} +0 -0
@@ -1,333 +1,341 @@
1
- import * as d3 from 'd3'
2
- import {
3
- Subject,
4
- Observable,
5
- of,
6
- takeUntil,
7
- map,
8
- switchMap,
9
- combineLatest,
10
- debounceTime,
11
- distinctUntilChanged } from 'rxjs'
12
- import type { DefinePluginConfig } from '../../../lib/core-types'
13
- import {
14
- defineTreePlugin } from '../../../lib/core'
15
- import type { Layout, ComputedDataTree, DataFormatterTree, ChartParams } from '../../../lib/core-types'
16
- import type { TreeMapParams } from '../../../lib/plugins-basic-types'
17
- import { DEFAULT_TREE_MAP_PARAMS } from '../defaults'
18
- import { getClassName, getColor } from '../../utils/orbchartsUtils'
19
- import { LAYER_INDEX_OF_GRAPHIC } from '../../const'
20
-
21
- const pluginName = 'TreeMap'
22
- const treeClassName = getClassName(pluginName, 'tree')
23
- const tileClassName = getClassName(pluginName, 'tile')
24
-
25
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_TREE_MAP_PARAMS> = {
26
- name: pluginName,
27
- defaultParams: DEFAULT_TREE_MAP_PARAMS,
28
- layerIndex: LAYER_INDEX_OF_GRAPHIC,
29
- validator: (params, { validateColumns }) => {
30
- const result = validateColumns(params, {
31
- paddingInner: {
32
- toBeTypes: ['number']
33
- },
34
- paddingOuter: {
35
- toBeTypes: ['number']
36
- },
37
- labelColorType: {
38
- toBeOption: 'ColorType'
39
- },
40
- squarifyRatio: {
41
- toBeTypes: ['number']
42
- },
43
- sort: {
44
- toBeTypes: ['Function']
45
- }
46
- })
47
- return result
48
- }
49
- }
50
-
51
- function renderTree ({ selection, treeData, fullParams, fullChartParams, textSizePx }: {
52
- selection: d3.Selection<any, any, any, any>
53
- treeData: d3.HierarchyRectangularNode<ComputedDataTree>[]
54
- fullParams: TreeMapParams
55
- fullChartParams: ChartParams
56
- textSizePx: number
57
- }) {
58
- const padding = textSizePx / 2
59
- const lineHeight = textSizePx // 行高
60
-
61
- const cell = selection.selectAll<SVGGElement, d3.HierarchyRectangularNode<ComputedDataTree>>(`g.${treeClassName}`)
62
- .data(treeData, d => d.data.id)
63
- .join('g')
64
- .attr('class', treeClassName)
65
-
66
- cell
67
- // .transition()
68
- // .duration(fullChartParams.transitionDuration)
69
- .attr('transform', (d) => !d.x0 || !d.y0 ? null : `translate(${d.x0},${d.y0})`)
70
- .each((d, i, nodes) => {
71
- const eachCell = d3.select(nodes[i])
72
-
73
- const tile = eachCell
74
- .selectAll<SVGRectElement, d3.HierarchyRectangularNode<ComputedDataTree>>(`rect.${tileClassName}`)
75
- .data([d], d => d.data.id)
76
- .join('rect')
77
- .attr("id", d => d.data.id)
78
- .attr("class", tileClassName)
79
- .attr('cursor', 'pointer')
80
- .attr("width", (d) => d.x1 - d.x0)
81
- .attr("height", (d) => d.y1 - d.y0)
82
- .attr('fill', d => d.data.color)
83
- .attr('data-name', d => d.data.label)
84
- .attr('data-category', d => d.data.categoryLabel)
85
- .attr('data-value', d => d.data.value)
86
-
87
- const label = eachCell
88
- .selectAll('g')
89
- .data([d])
90
- .join('g')
91
- .each((d, i, nodes) => {
92
- const eachLabel = d3.select(nodes[i])
93
- const text = eachLabel
94
- .selectAll('text')
95
- .data([d])
96
- .join('text')
97
- .text(d => d.data.label)
98
- .attr('dominant-baseline', 'hanging')
99
- .attr("x", padding)
100
- .attr("y", padding)
101
- .attr('font-size', fullChartParams.styles.textSize)
102
- .each(function(d) {
103
- // -- tspan(自動斷行) --
104
- const textElement = d3.select(this);
105
- const words = d.data.label.split(/\s+/).reverse() // 以空隔分割字串
106
- let word;
107
- let line: string[] = []
108
- const x = textElement.attr("x")
109
- let y = textElement.attr("y")
110
- let dy = 0
111
- let tspan = textElement
112
- .text(null)
113
- .append("tspan")
114
- .attr('cursor', 'pointer')
115
- .attr('fill', getColor(fullParams.labelColorType, fullChartParams))
116
- .attr('font-size', fullChartParams.styles.textSize)
117
- .attr("x", x)
118
- .attr("y", y)
119
-
120
- while (word = words.pop()) {
121
- line.push(word)
122
- tspan.text(line.join(" "))
123
- if (tspan.node().getComputedTextLength() > (d.x1 - d.x0 - padding)) {
124
- line.pop()
125
- tspan.text(line.join(" "))
126
- line = [word]
127
- dy += lineHeight
128
- tspan = textElement
129
- .append("tspan")
130
- .attr('cursor', 'pointer')
131
- .attr('fill', getColor(fullParams.labelColorType, fullChartParams))
132
- .attr('font-size', fullChartParams.styles.textSize)
133
- .attr("x", x)
134
- .attr("y", y)
135
- .attr("dy", dy + "px")
136
- .text(word)
137
- }
138
- }
139
- })
140
- })
141
-
142
- })
143
-
144
- return cell
145
- }
146
-
147
- function highlight ({ selection, ids, fullChartParams }: {
148
- selection: d3.Selection<any, d3.HierarchyRectangularNode<ComputedDataTree>, any, any>
149
- ids: string[]
150
- fullChartParams: ChartParams
151
- }) {
152
- selection.interrupt('highlight')
153
-
154
- if (!ids.length) {
155
- // remove highlight
156
- selection
157
- .transition('highlight')
158
- .duration(200)
159
- .style('opacity', 1)
160
- return
161
- }
162
-
163
- selection
164
- .each((d, i, n) => {
165
- if (ids.includes(d.data.id)) {
166
- d3.select(n[i])
167
- .style('opacity', 1)
168
- } else {
169
- d3.select(n[i])
170
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
171
- }
172
- })
173
- }
174
-
175
- export const TreeMap = defineTreePlugin(pluginConfig)(({ selection, name, subject, observer }) => {
176
- const destroy$ = new Subject()
177
-
178
- const treeData$ = combineLatest({
179
- layout: observer.layout$,
180
- visibleComputedData: observer.visibleComputedData$,
181
- fullParams: observer.fullParams$,
182
- fullDataFormatter: observer.fullDataFormatter$,
183
- fullChartParams: observer.fullChartParams$
184
- }).pipe(
185
- takeUntil(destroy$),
186
- switchMap(async d => d),
187
- map(data => {
188
- const treemap = d3.treemap()
189
- .size([data.layout.width, data.layout.height])
190
- .paddingInner(data.fullParams.paddingInner)
191
- .paddingOuter(data.fullParams.paddingOuter)
192
- .round(true)
193
- .tile(d3.treemapSquarify.ratio(data.fullParams.squarifyRatio))
194
-
195
- const root = d3.hierarchy(data.visibleComputedData)
196
- .sum(d => d.value)
197
- .sort(data.fullParams.sort as (a: any, b: any) => number)
198
-
199
- //call treemap
200
- treemap(root)
201
-
202
- const treeData: d3.HierarchyRectangularNode<ComputedDataTree>[] = root.leaves() as any
203
-
204
- return treeData
205
- })
206
- )
207
-
208
- const cellSelection$ = combineLatest({
209
- selection: of(selection),
210
- treeData: treeData$,
211
- fullParams: observer.fullParams$,
212
- fullChartParams: observer.fullChartParams$,
213
- textSizePx: observer.textSizePx$
214
- }).pipe(
215
- takeUntil(destroy$),
216
- switchMap(async d => d),
217
- map(data => {
218
- return renderTree({
219
- selection,
220
- treeData: data.treeData,
221
- fullParams: data.fullParams,
222
- fullChartParams: data.fullChartParams,
223
- textSizePx: data.textSizePx
224
- })
225
- })
226
- )
227
-
228
- const highlightTarget$ = observer.fullChartParams$.pipe(
229
- takeUntil(destroy$),
230
- map(d => d.highlightTarget),
231
- distinctUntilChanged()
232
- )
233
-
234
- combineLatest({
235
- cellSelection: cellSelection$,
236
- computedData: observer.computedData$,
237
- treeData: treeData$,
238
- fullParams: observer.fullParams$,
239
- fullChartParams: observer.fullChartParams$,
240
- highlightTarget: highlightTarget$,
241
- CategoryDataMap: observer.CategoryDataMap$,
242
- }).pipe(
243
- takeUntil(destroy$),
244
- switchMap(async d => d)
245
- ).subscribe(data => {
246
- data.cellSelection
247
- .on('mouseover', (event, datum) => {
248
- event.stopPropagation()
249
-
250
- subject.event$.next({
251
- type: 'tree',
252
- eventName: 'mouseover',
253
- pluginName,
254
- highlightTarget: data.highlightTarget,
255
- datum: datum.data,
256
- category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
257
- categoryIndex: datum.data.categoryIndex,
258
- categoryLabel: datum.data.categoryLabel,
259
- event,
260
- data: data.computedData
261
- })
262
- })
263
- .on('mousemove', (event, datum) => {
264
- event.stopPropagation()
265
-
266
- subject.event$.next({
267
- type: 'tree',
268
- eventName: 'mousemove',
269
- pluginName,
270
- highlightTarget: data.highlightTarget,
271
- datum: datum.data,
272
- category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
273
- categoryIndex: datum.data.categoryIndex,
274
- categoryLabel: datum.data.categoryLabel,
275
- event,
276
- data: data.computedData
277
- })
278
- })
279
- .on('mouseout', (event, datum) => {
280
- event.stopPropagation()
281
-
282
- subject.event$.next({
283
- type: 'tree',
284
- eventName: 'mouseout',
285
- pluginName,
286
- highlightTarget: data.highlightTarget,
287
- datum: datum.data,
288
- category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
289
- categoryIndex: datum.data.categoryIndex,
290
- categoryLabel: datum.data.categoryLabel,
291
- event,
292
- data: data.computedData
293
- })
294
- })
295
- .on('click', (event, datum) => {
296
- event.stopPropagation()
297
-
298
- subject.event$.next({
299
- type: 'tree',
300
- eventName: 'click',
301
- pluginName,
302
- highlightTarget: data.highlightTarget,
303
- datum: datum.data,
304
- category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
305
- categoryIndex: datum.data.categoryIndex,
306
- categoryLabel: datum.data.categoryLabel,
307
- event,
308
- data: data.computedData
309
- })
310
- })
311
- })
312
-
313
- combineLatest({
314
- cellSelection: cellSelection$,
315
- highlight: observer.treeHighlight$.pipe(
316
- map(data => data.map(d => d.id))
317
- ),
318
- fullChartParams: observer.fullChartParams$
319
- }).pipe(
320
- takeUntil(destroy$),
321
- switchMap(async d => d)
322
- ).subscribe(data => {
323
- highlight({
324
- selection: data.cellSelection,
325
- ids: data.highlight,
326
- fullChartParams: data.fullChartParams
327
- })
328
- })
329
-
330
- return () => {
331
- destroy$.next(undefined)
332
- }
333
- })
1
+ import * as d3 from 'd3'
2
+ import {
3
+ Subject,
4
+ Observable,
5
+ of,
6
+ takeUntil,
7
+ map,
8
+ switchMap,
9
+ combineLatest,
10
+ debounceTime,
11
+ distinctUntilChanged } from 'rxjs'
12
+ import type { DefinePluginConfig } from '../../../lib/core-types'
13
+ import {
14
+ defineTreePlugin } from '../../../lib/core'
15
+ import type { Layout, ComputedDataTree, DataFormatterTree, ChartParams } from '../../../lib/core-types'
16
+ import type { TreeMapParams } from '../../../lib/plugins-basic-types'
17
+ import { DEFAULT_TREE_MAP_PARAMS } from '../defaults'
18
+ import { getClassName, getDatumColor } from '../../utils/orbchartsUtils'
19
+ import { LAYER_INDEX_OF_GRAPHIC } from '../../const'
20
+
21
+ const pluginName = 'TreeMap'
22
+ const treeClassName = getClassName(pluginName, 'tree')
23
+ const tileClassName = getClassName(pluginName, 'tile')
24
+
25
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_TREE_MAP_PARAMS> = {
26
+ name: pluginName,
27
+ defaultParams: DEFAULT_TREE_MAP_PARAMS,
28
+ layerIndex: LAYER_INDEX_OF_GRAPHIC,
29
+ validator: (params, { validateColumns }) => {
30
+ const result = validateColumns(params, {
31
+ paddingInner: {
32
+ toBeTypes: ['number']
33
+ },
34
+ paddingOuter: {
35
+ toBeTypes: ['number']
36
+ },
37
+ labelColorType: {
38
+ toBeOption: 'ColorType'
39
+ },
40
+ squarifyRatio: {
41
+ toBeTypes: ['number']
42
+ },
43
+ sort: {
44
+ toBeTypes: ['Function']
45
+ }
46
+ })
47
+ return result
48
+ }
49
+ }
50
+
51
+ function renderTree ({ selection, treeData, fullParams, fullChartParams, textSizePx }: {
52
+ selection: d3.Selection<any, any, any, any>
53
+ treeData: d3.HierarchyRectangularNode<ComputedDataTree>[]
54
+ fullParams: TreeMapParams
55
+ fullChartParams: ChartParams
56
+ textSizePx: number
57
+ }) {
58
+ const padding = textSizePx / 2
59
+ const lineHeight = textSizePx // 行高
60
+
61
+ const cell = selection.selectAll<SVGGElement, d3.HierarchyRectangularNode<ComputedDataTree>>(`g.${treeClassName}`)
62
+ .data(treeData, d => d.data.id)
63
+ .join('g')
64
+ .attr('class', treeClassName)
65
+
66
+ cell
67
+ // .transition()
68
+ // .duration(fullChartParams.transitionDuration)
69
+ .attr('transform', (d) => !d.x0 || !d.y0 ? null : `translate(${d.x0},${d.y0})`)
70
+ .each((d, i, nodes) => {
71
+ const eachCell = d3.select(nodes[i])
72
+
73
+ const tile = eachCell
74
+ .selectAll<SVGRectElement, d3.HierarchyRectangularNode<ComputedDataTree>>(`rect.${tileClassName}`)
75
+ .data([d], d => d.data.id)
76
+ .join('rect')
77
+ .attr("id", d => d.data.id)
78
+ .attr("class", tileClassName)
79
+ .attr('cursor', 'pointer')
80
+ .attr("width", (d) => d.x1 - d.x0)
81
+ .attr("height", (d) => d.y1 - d.y0)
82
+ .attr('fill', d => d.data.color)
83
+ .attr('data-name', d => d.data.label)
84
+ .attr('data-category', d => d.data.categoryLabel)
85
+ .attr('data-value', d => d.data.value)
86
+
87
+ const label = eachCell
88
+ .selectAll('g')
89
+ .data([d])
90
+ .join('g')
91
+ .each((d, i, nodes) => {
92
+ const eachLabel = d3.select(nodes[i])
93
+ const text = eachLabel
94
+ .selectAll('text')
95
+ .data([d])
96
+ .join('text')
97
+ .text(d => d.data.label)
98
+ .attr('dominant-baseline', 'hanging')
99
+ .attr("x", padding)
100
+ .attr("y", padding)
101
+ .attr('font-size', fullChartParams.styles.textSize)
102
+ .each(function(d) {
103
+ // -- tspan(自動斷行) --
104
+ const textElement = d3.select(this);
105
+ const words = d.data.label.split(/\s+/).reverse() // 以空隔分割字串
106
+ let word;
107
+ let line: string[] = []
108
+ const x = textElement.attr("x")
109
+ let y = textElement.attr("y")
110
+ let dy = 0
111
+ let tspan = textElement
112
+ .text(null)
113
+ .append("tspan")
114
+ .attr('cursor', 'pointer')
115
+ .attr('fill', getDatumColor({
116
+ colorType: fullParams.labelColorType,
117
+ datum: d.data,
118
+ fullChartParams
119
+ }))
120
+ .attr('font-size', fullChartParams.styles.textSize)
121
+ .attr("x", x)
122
+ .attr("y", y)
123
+
124
+ while (word = words.pop()) {
125
+ line.push(word)
126
+ tspan.text(line.join(" "))
127
+ if (tspan.node().getComputedTextLength() > (d.x1 - d.x0 - padding)) {
128
+ line.pop()
129
+ tspan.text(line.join(" "))
130
+ line = [word]
131
+ dy += lineHeight
132
+ tspan = textElement
133
+ .append("tspan")
134
+ .attr('cursor', 'pointer')
135
+ .attr('fill', getDatumColor({
136
+ colorType: fullParams.labelColorType,
137
+ datum: d.data,
138
+ fullChartParams
139
+ }))
140
+ .attr('font-size', fullChartParams.styles.textSize)
141
+ .attr("x", x)
142
+ .attr("y", y)
143
+ .attr("dy", dy + "px")
144
+ .text(word)
145
+ }
146
+ }
147
+ })
148
+ })
149
+
150
+ })
151
+
152
+ return cell
153
+ }
154
+
155
+ function highlight ({ selection, ids, fullChartParams }: {
156
+ selection: d3.Selection<any, d3.HierarchyRectangularNode<ComputedDataTree>, any, any>
157
+ ids: string[]
158
+ fullChartParams: ChartParams
159
+ }) {
160
+ selection.interrupt('highlight')
161
+
162
+ if (!ids.length) {
163
+ // remove highlight
164
+ selection
165
+ .transition('highlight')
166
+ .duration(200)
167
+ .style('opacity', 1)
168
+ return
169
+ }
170
+
171
+ selection
172
+ .each((d, i, n) => {
173
+ if (ids.includes(d.data.id)) {
174
+ d3.select(n[i])
175
+ .style('opacity', 1)
176
+ } else {
177
+ d3.select(n[i])
178
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
179
+ }
180
+ })
181
+ }
182
+
183
+ export const TreeMap = defineTreePlugin(pluginConfig)(({ selection, name, subject, observer }) => {
184
+ const destroy$ = new Subject()
185
+
186
+ const treeData$ = combineLatest({
187
+ layout: observer.layout$,
188
+ visibleComputedData: observer.visibleComputedData$,
189
+ fullParams: observer.fullParams$,
190
+ fullDataFormatter: observer.fullDataFormatter$,
191
+ fullChartParams: observer.fullChartParams$
192
+ }).pipe(
193
+ takeUntil(destroy$),
194
+ switchMap(async d => d),
195
+ map(data => {
196
+ const treemap = d3.treemap()
197
+ .size([data.layout.width, data.layout.height])
198
+ .paddingInner(data.fullParams.paddingInner)
199
+ .paddingOuter(data.fullParams.paddingOuter)
200
+ .round(true)
201
+ .tile(d3.treemapSquarify.ratio(data.fullParams.squarifyRatio))
202
+
203
+ const root = d3.hierarchy(data.visibleComputedData)
204
+ .sum(d => d.value)
205
+ .sort(data.fullParams.sort as (a: any, b: any) => number)
206
+
207
+ //call treemap
208
+ treemap(root)
209
+
210
+ const treeData: d3.HierarchyRectangularNode<ComputedDataTree>[] = root.leaves() as any
211
+
212
+ return treeData
213
+ })
214
+ )
215
+
216
+ const cellSelection$ = combineLatest({
217
+ selection: of(selection),
218
+ treeData: treeData$,
219
+ fullParams: observer.fullParams$,
220
+ fullChartParams: observer.fullChartParams$,
221
+ textSizePx: observer.textSizePx$
222
+ }).pipe(
223
+ takeUntil(destroy$),
224
+ switchMap(async d => d),
225
+ map(data => {
226
+ return renderTree({
227
+ selection,
228
+ treeData: data.treeData,
229
+ fullParams: data.fullParams,
230
+ fullChartParams: data.fullChartParams,
231
+ textSizePx: data.textSizePx
232
+ })
233
+ })
234
+ )
235
+
236
+ const highlightTarget$ = observer.fullChartParams$.pipe(
237
+ takeUntil(destroy$),
238
+ map(d => d.highlightTarget),
239
+ distinctUntilChanged()
240
+ )
241
+
242
+ combineLatest({
243
+ cellSelection: cellSelection$,
244
+ computedData: observer.computedData$,
245
+ treeData: treeData$,
246
+ fullParams: observer.fullParams$,
247
+ fullChartParams: observer.fullChartParams$,
248
+ highlightTarget: highlightTarget$,
249
+ CategoryDataMap: observer.CategoryDataMap$,
250
+ }).pipe(
251
+ takeUntil(destroy$),
252
+ switchMap(async d => d)
253
+ ).subscribe(data => {
254
+ data.cellSelection
255
+ .on('mouseover', (event, datum) => {
256
+ event.stopPropagation()
257
+
258
+ subject.event$.next({
259
+ type: 'tree',
260
+ eventName: 'mouseover',
261
+ pluginName,
262
+ highlightTarget: data.highlightTarget,
263
+ datum: datum.data,
264
+ category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
265
+ categoryIndex: datum.data.categoryIndex,
266
+ categoryLabel: datum.data.categoryLabel,
267
+ event,
268
+ data: data.computedData
269
+ })
270
+ })
271
+ .on('mousemove', (event, datum) => {
272
+ event.stopPropagation()
273
+
274
+ subject.event$.next({
275
+ type: 'tree',
276
+ eventName: 'mousemove',
277
+ pluginName,
278
+ highlightTarget: data.highlightTarget,
279
+ datum: datum.data,
280
+ category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
281
+ categoryIndex: datum.data.categoryIndex,
282
+ categoryLabel: datum.data.categoryLabel,
283
+ event,
284
+ data: data.computedData
285
+ })
286
+ })
287
+ .on('mouseout', (event, datum) => {
288
+ event.stopPropagation()
289
+
290
+ subject.event$.next({
291
+ type: 'tree',
292
+ eventName: 'mouseout',
293
+ pluginName,
294
+ highlightTarget: data.highlightTarget,
295
+ datum: datum.data,
296
+ category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
297
+ categoryIndex: datum.data.categoryIndex,
298
+ categoryLabel: datum.data.categoryLabel,
299
+ event,
300
+ data: data.computedData
301
+ })
302
+ })
303
+ .on('click', (event, datum) => {
304
+ event.stopPropagation()
305
+
306
+ subject.event$.next({
307
+ type: 'tree',
308
+ eventName: 'click',
309
+ pluginName,
310
+ highlightTarget: data.highlightTarget,
311
+ datum: datum.data,
312
+ category: data.CategoryDataMap.get(datum.data.categoryLabel)!,
313
+ categoryIndex: datum.data.categoryIndex,
314
+ categoryLabel: datum.data.categoryLabel,
315
+ event,
316
+ data: data.computedData
317
+ })
318
+ })
319
+ })
320
+
321
+ combineLatest({
322
+ cellSelection: cellSelection$,
323
+ highlight: observer.treeHighlight$.pipe(
324
+ map(data => data.map(d => d.id))
325
+ ),
326
+ fullChartParams: observer.fullChartParams$
327
+ }).pipe(
328
+ takeUntil(destroy$),
329
+ switchMap(async d => d)
330
+ ).subscribe(data => {
331
+ highlight({
332
+ selection: data.cellSelection,
333
+ ids: data.highlight,
334
+ fullChartParams: data.fullChartParams
335
+ })
336
+ })
337
+
338
+ return () => {
339
+ destroy$.next(undefined)
340
+ }
341
+ })