@orbcharts/plugins-basic 3.0.0-beta.16 → 3.0.0-beta.18

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 (112) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +11037 -10875
  3. package/dist/orbcharts-plugins-basic.umd.js +208 -148
  4. package/dist/src/utils/commonUtils.d.ts +1 -0
  5. package/dist/src/utils/d3Graphics.d.ts +2 -1
  6. package/lib/core-types.ts +7 -7
  7. package/lib/core.ts +6 -6
  8. package/lib/plugins-basic-types.ts +6 -6
  9. package/package.json +44 -44
  10. package/src/base/BaseBars.ts +765 -765
  11. package/src/base/BaseBarsTriangle.ts +676 -676
  12. package/src/base/BaseDots.ts +464 -464
  13. package/src/base/BaseGroupAxis.ts +691 -681
  14. package/src/base/BaseLegend.ts +684 -684
  15. package/src/base/BaseLineAreas.ts +629 -629
  16. package/src/base/BaseLines.ts +706 -706
  17. package/src/base/BaseRacingBars.ts +551 -551
  18. package/src/base/BaseRacingLabels.ts +396 -392
  19. package/src/base/BaseRacingValueLabels.ts +403 -403
  20. package/src/base/BaseStackedBars.ts +782 -782
  21. package/src/base/BaseTooltip.ts +386 -385
  22. package/src/base/BaseValueAxis.ts +600 -600
  23. package/src/base/BaseXAxis.ts +427 -427
  24. package/src/base/BaseYAxis.ts +389 -389
  25. package/src/base/types.ts +2 -2
  26. package/src/const.ts +30 -30
  27. package/src/grid/defaults.ts +213 -198
  28. package/src/grid/gridObservables.ts +612 -612
  29. package/src/grid/index.ts +16 -16
  30. package/src/grid/plugins/Bars.ts +69 -69
  31. package/src/grid/plugins/BarsPN.ts +66 -66
  32. package/src/grid/plugins/BarsTriangle.ts +73 -73
  33. package/src/grid/plugins/Dots.ts +68 -68
  34. package/src/grid/plugins/GridLegend.ts +107 -107
  35. package/src/grid/plugins/GridTooltip.ts +66 -66
  36. package/src/grid/plugins/GroupAux.ts +1120 -1103
  37. package/src/grid/plugins/GroupAxis.ts +73 -73
  38. package/src/grid/plugins/GroupZoom.ts +218 -218
  39. package/src/grid/plugins/LineAreas.ts +65 -65
  40. package/src/grid/plugins/Lines.ts +59 -59
  41. package/src/grid/plugins/StackedBars.ts +64 -64
  42. package/src/grid/plugins/StackedValueAxis.ts +96 -96
  43. package/src/grid/plugins/ValueAxis.ts +94 -94
  44. package/src/index.ts +6 -6
  45. package/src/multiGrid/defaults.ts +244 -228
  46. package/src/multiGrid/index.ts +14 -14
  47. package/src/multiGrid/multiGridObservables.ts +50 -50
  48. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  49. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  50. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  51. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  52. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  53. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  54. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  55. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  56. package/src/multiGrid/plugins/MultiStackedBars.ts +106 -106
  57. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +134 -134
  58. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  59. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  60. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  61. package/src/multiValue/defaults.ts +388 -299
  62. package/src/multiValue/index.ts +12 -12
  63. package/src/multiValue/multiValueObservables.ts +666 -666
  64. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  65. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  66. package/src/multiValue/plugins/RacingBars.ts +373 -362
  67. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  68. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  69. package/src/multiValue/plugins/RankingAxis_legacy.ts +109 -109
  70. package/src/multiValue/plugins/Scatter.ts +426 -426
  71. package/src/multiValue/plugins/ScatterBubbles.ts +554 -554
  72. package/src/multiValue/plugins/XAxis.ts +107 -107
  73. package/src/multiValue/plugins/XYAux.ts +682 -681
  74. package/src/multiValue/plugins/XYAxes.ts +194 -194
  75. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  76. package/src/multiValue/plugins/XZoom.ts +299 -299
  77. package/src/noneData/defaults.ts +102 -102
  78. package/src/noneData/index.ts +3 -3
  79. package/src/noneData/plugins/Container.ts +27 -27
  80. package/src/noneData/plugins/Tooltip.ts +373 -373
  81. package/src/relationship/defaults.ts +221 -218
  82. package/src/relationship/index.ts +5 -5
  83. package/src/relationship/plugins/ForceDirected.ts +1173 -1168
  84. package/src/relationship/plugins/ForceDirectedBubbles.ts +1411 -1403
  85. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  86. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  87. package/src/relationship/relationshipObservables.ts +49 -49
  88. package/src/series/defaults.ts +221 -219
  89. package/src/series/index.ts +9 -9
  90. package/src/series/plugins/Bubbles.ts +636 -636
  91. package/src/series/plugins/Pie.ts +623 -623
  92. package/src/series/plugins/PieEventTexts.ts +284 -284
  93. package/src/series/plugins/PieLabels.ts +640 -640
  94. package/src/series/plugins/Rose.ts +516 -516
  95. package/src/series/plugins/RoseLabels.ts +600 -600
  96. package/src/series/plugins/SeriesLegend.ts +107 -107
  97. package/src/series/plugins/SeriesTooltip.ts +66 -66
  98. package/src/series/seriesObservables.ts +145 -145
  99. package/src/series/seriesUtils.ts +51 -51
  100. package/src/tree/defaults.ts +102 -100
  101. package/src/tree/index.ts +4 -4
  102. package/src/tree/plugins/TreeLegend.ts +100 -100
  103. package/src/tree/plugins/TreeMap.ts +341 -341
  104. package/src/tree/plugins/TreeTooltip.ts +66 -66
  105. package/src/utils/commonUtils.ts +31 -22
  106. package/src/utils/d3Graphics.ts +176 -174
  107. package/src/utils/d3Utils.ts +92 -92
  108. package/src/utils/observables.ts +14 -14
  109. package/src/utils/orbchartsUtils.ts +129 -129
  110. package/tsconfig.base.json +13 -13
  111. package/tsconfig.json +2 -2
  112. package/vite.config.js +22 -22
@@ -1,341 +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, 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
- })
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
+ })