@orbcharts/plugins-basic 3.0.7 → 3.0.9

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 (121) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +6837 -6656
  3. package/dist/orbcharts-plugins-basic.umd.js +45 -45
  4. package/dist/src/base/BaseStackedBars.d.ts +2 -0
  5. package/dist/src/series/defaults.d.ts +2 -1
  6. package/dist/src/series/index.d.ts +1 -0
  7. package/dist/src/series/plugins/Indicator.d.ts +3 -0
  8. package/lib/core-types.ts +7 -7
  9. package/lib/core.ts +6 -6
  10. package/lib/gridObservables.ts +6 -6
  11. package/lib/plugins-basic-types.ts +6 -6
  12. package/package.json +48 -48
  13. package/src/base/BaseBars.ts +765 -765
  14. package/src/base/BaseBarsTriangle.ts +676 -676
  15. package/src/base/BaseDots.ts +464 -464
  16. package/src/base/BaseGroupAxis.ts +691 -691
  17. package/src/base/BaseLegend.ts +684 -684
  18. package/src/base/BaseLineAreas.ts +629 -629
  19. package/src/base/BaseLines.ts +706 -706
  20. package/src/base/BaseOrdinalBubbles.ts +729 -729
  21. package/src/base/BaseRacingBars.ts +582 -582
  22. package/src/base/BaseRacingLabels.ts +404 -404
  23. package/src/base/BaseRacingValueLabels.ts +403 -403
  24. package/src/base/BaseStackedBars.ts +793 -782
  25. package/src/base/BaseTooltip.ts +408 -408
  26. package/src/base/BaseValueAxis.ts +600 -600
  27. package/src/base/BaseXAxis.ts +427 -427
  28. package/src/base/BaseXZoom.ts +241 -241
  29. package/src/base/BaseYAxis.ts +389 -389
  30. package/src/base/types.ts +2 -2
  31. package/src/const.ts +30 -30
  32. package/src/grid/defaults.ts +213 -213
  33. package/src/grid/gridObservables.ts +635 -635
  34. package/src/grid/index.ts +16 -16
  35. package/src/grid/plugins/Bars.ts +69 -69
  36. package/src/grid/plugins/BarsPN.ts +66 -66
  37. package/src/grid/plugins/BarsTriangle.ts +73 -73
  38. package/src/grid/plugins/Dots.ts +68 -68
  39. package/src/grid/plugins/GridLegend.ts +107 -107
  40. package/src/grid/plugins/GridTooltip.ts +66 -66
  41. package/src/grid/plugins/GroupAux.ts +1095 -1095
  42. package/src/grid/plugins/GroupAxis.ts +73 -73
  43. package/src/grid/plugins/GroupZoom.ts +218 -218
  44. package/src/grid/plugins/LineAreas.ts +65 -65
  45. package/src/grid/plugins/Lines.ts +59 -59
  46. package/src/grid/plugins/StackedBars.ts +66 -64
  47. package/src/grid/plugins/StackedValueAxis.ts +97 -96
  48. package/src/grid/plugins/ValueAxis.ts +94 -94
  49. package/src/index.ts +6 -6
  50. package/src/multiGrid/defaults.ts +244 -244
  51. package/src/multiGrid/index.ts +14 -14
  52. package/src/multiGrid/multiGridObservables.ts +50 -50
  53. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  54. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  55. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  56. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  57. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  58. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  59. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  60. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  61. package/src/multiGrid/plugins/MultiStackedBars.ts +109 -106
  62. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +135 -134
  63. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  64. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  65. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  66. package/src/multiValue/defaults.ts +523 -523
  67. package/src/multiValue/index.ts +16 -16
  68. package/src/multiValue/multiValueObservables.ts +781 -781
  69. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  70. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  71. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  72. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  73. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  74. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  75. package/src/multiValue/plugins/RacingBars.ts +375 -375
  76. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  77. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  78. package/src/multiValue/plugins/Scatter.ts +486 -486
  79. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  80. package/src/multiValue/plugins/XAxis.ts +107 -107
  81. package/src/multiValue/plugins/XYAux.ts +683 -683
  82. package/src/multiValue/plugins/XYAxes.ts +194 -194
  83. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  84. package/src/multiValue/plugins/XZoom.ts +40 -40
  85. package/src/noneData/defaults.ts +102 -102
  86. package/src/noneData/index.ts +3 -3
  87. package/src/noneData/plugins/Container.ts +27 -27
  88. package/src/noneData/plugins/Tooltip.ts +373 -373
  89. package/src/relationship/defaults.ts +221 -221
  90. package/src/relationship/index.ts +5 -5
  91. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  92. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  93. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  94. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  95. package/src/relationship/relationshipObservables.ts +49 -49
  96. package/src/series/defaults.ts +236 -224
  97. package/src/series/index.ts +10 -9
  98. package/src/series/plugins/Bubbles.ts +784 -784
  99. package/src/series/plugins/Indicator.ts +292 -0
  100. package/src/series/plugins/Pie.ts +622 -622
  101. package/src/series/plugins/PieEventTexts.ts +283 -283
  102. package/src/series/plugins/PieLabels.ts +639 -639
  103. package/src/series/plugins/Rose.ts +515 -515
  104. package/src/series/plugins/RoseLabels.ts +599 -599
  105. package/src/series/plugins/SeriesLegend.ts +107 -107
  106. package/src/series/plugins/SeriesTooltip.ts +66 -66
  107. package/src/series/seriesObservables.ts +168 -168
  108. package/src/series/seriesUtils.ts +51 -51
  109. package/src/tree/defaults.ts +102 -102
  110. package/src/tree/index.ts +4 -4
  111. package/src/tree/plugins/TreeLegend.ts +100 -100
  112. package/src/tree/plugins/TreeMap.ts +341 -341
  113. package/src/tree/plugins/TreeTooltip.ts +66 -66
  114. package/src/utils/commonUtils.ts +31 -31
  115. package/src/utils/d3Graphics.ts +176 -176
  116. package/src/utils/d3Utils.ts +92 -92
  117. package/src/utils/observables.ts +14 -14
  118. package/src/utils/orbchartsUtils.ts +129 -129
  119. package/tsconfig.base.json +13 -13
  120. package/tsconfig.json +2 -2
  121. 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
+ })