@orbcharts/plugins-basic 3.0.8 → 3.0.10

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 (120) hide show
  1. package/LICENSE +200 -200
  2. package/dist/orbcharts-plugins-basic.es.js +6182 -6004
  3. package/dist/orbcharts-plugins-basic.umd.js +44 -44
  4. package/dist/src/series/defaults.d.ts +2 -1
  5. package/dist/src/series/index.d.ts +1 -0
  6. package/dist/src/series/plugins/Indicator.d.ts +3 -0
  7. package/lib/core-types.ts +7 -7
  8. package/lib/core.ts +6 -6
  9. package/lib/gridObservables.ts +6 -6
  10. package/lib/plugins-basic-types.ts +6 -6
  11. package/package.json +48 -48
  12. package/src/base/BaseBars.ts +765 -765
  13. package/src/base/BaseBarsTriangle.ts +676 -676
  14. package/src/base/BaseDots.ts +464 -464
  15. package/src/base/BaseGroupAxis.ts +691 -691
  16. package/src/base/BaseLegend.ts +684 -684
  17. package/src/base/BaseLineAreas.ts +629 -629
  18. package/src/base/BaseLines.ts +706 -706
  19. package/src/base/BaseOrdinalBubbles.ts +729 -729
  20. package/src/base/BaseRacingBars.ts +582 -582
  21. package/src/base/BaseRacingLabels.ts +404 -404
  22. package/src/base/BaseRacingValueLabels.ts +403 -403
  23. package/src/base/BaseStackedBars.ts +793 -793
  24. package/src/base/BaseTooltip.ts +408 -408
  25. package/src/base/BaseValueAxis.ts +600 -600
  26. package/src/base/BaseXAxis.ts +427 -427
  27. package/src/base/BaseXZoom.ts +241 -241
  28. package/src/base/BaseYAxis.ts +389 -389
  29. package/src/base/types.ts +2 -2
  30. package/src/const.ts +30 -30
  31. package/src/grid/defaults.ts +213 -213
  32. package/src/grid/gridObservables.ts +635 -635
  33. package/src/grid/index.ts +16 -16
  34. package/src/grid/plugins/Bars.ts +69 -69
  35. package/src/grid/plugins/BarsPN.ts +66 -66
  36. package/src/grid/plugins/BarsTriangle.ts +73 -73
  37. package/src/grid/plugins/Dots.ts +68 -68
  38. package/src/grid/plugins/GridLegend.ts +107 -107
  39. package/src/grid/plugins/GridTooltip.ts +66 -66
  40. package/src/grid/plugins/GroupAux.ts +1095 -1095
  41. package/src/grid/plugins/GroupAxis.ts +73 -73
  42. package/src/grid/plugins/GroupZoom.ts +218 -218
  43. package/src/grid/plugins/LineAreas.ts +65 -65
  44. package/src/grid/plugins/Lines.ts +59 -59
  45. package/src/grid/plugins/StackedBars.ts +66 -66
  46. package/src/grid/plugins/StackedValueAxis.ts +97 -97
  47. package/src/grid/plugins/ValueAxis.ts +94 -94
  48. package/src/index.ts +6 -6
  49. package/src/multiGrid/defaults.ts +244 -244
  50. package/src/multiGrid/index.ts +14 -14
  51. package/src/multiGrid/multiGridObservables.ts +50 -50
  52. package/src/multiGrid/plugins/MultiBars.ts +108 -108
  53. package/src/multiGrid/plugins/MultiBarsTriangle.ts +114 -114
  54. package/src/multiGrid/plugins/MultiDots.ts +102 -102
  55. package/src/multiGrid/plugins/MultiGridLegend.ts +169 -169
  56. package/src/multiGrid/plugins/MultiGridTooltip.ts +66 -66
  57. package/src/multiGrid/plugins/MultiGroupAxis.ts +137 -137
  58. package/src/multiGrid/plugins/MultiLineAreas.ts +107 -107
  59. package/src/multiGrid/plugins/MultiLines.ts +101 -101
  60. package/src/multiGrid/plugins/MultiStackedBars.ts +109 -109
  61. package/src/multiGrid/plugins/MultiStackedValueAxis.ts +135 -135
  62. package/src/multiGrid/plugins/MultiValueAxis.ts +134 -134
  63. package/src/multiGrid/plugins/OverlappingStackedValueAxes.ts +300 -300
  64. package/src/multiGrid/plugins/OverlappingValueAxes.ts +300 -300
  65. package/src/multiValue/defaults.ts +523 -523
  66. package/src/multiValue/index.ts +16 -16
  67. package/src/multiValue/multiValueObservables.ts +781 -781
  68. package/src/multiValue/plugins/MultiValueLegend.ts +107 -107
  69. package/src/multiValue/plugins/MultiValueTooltip.ts +66 -66
  70. package/src/multiValue/plugins/OrdinalAux.ts +660 -660
  71. package/src/multiValue/plugins/OrdinalAxis.ts +524 -524
  72. package/src/multiValue/plugins/OrdinalBubbles.ts +226 -226
  73. package/src/multiValue/plugins/OrdinalZoom.ts +57 -57
  74. package/src/multiValue/plugins/RacingBars.ts +375 -375
  75. package/src/multiValue/plugins/RacingCounterTexts.ts +300 -300
  76. package/src/multiValue/plugins/RacingValueAxis.ts +114 -114
  77. package/src/multiValue/plugins/Scatter.ts +486 -486
  78. package/src/multiValue/plugins/ScatterBubbles.ts +635 -635
  79. package/src/multiValue/plugins/XAxis.ts +107 -107
  80. package/src/multiValue/plugins/XYAux.ts +683 -683
  81. package/src/multiValue/plugins/XYAxes.ts +194 -194
  82. package/src/multiValue/plugins/XYAxes_legacy.ts +683 -683
  83. package/src/multiValue/plugins/XZoom.ts +40 -40
  84. package/src/noneData/defaults.ts +102 -102
  85. package/src/noneData/index.ts +3 -3
  86. package/src/noneData/plugins/Container.ts +27 -27
  87. package/src/noneData/plugins/Tooltip.ts +373 -373
  88. package/src/relationship/defaults.ts +221 -221
  89. package/src/relationship/index.ts +5 -5
  90. package/src/relationship/plugins/ForceDirected.ts +1056 -1056
  91. package/src/relationship/plugins/ForceDirectedBubbles.ts +1294 -1294
  92. package/src/relationship/plugins/RelationshipLegend.ts +100 -100
  93. package/src/relationship/plugins/RelationshipTooltip.ts +66 -66
  94. package/src/relationship/relationshipObservables.ts +49 -49
  95. package/src/series/defaults.ts +236 -224
  96. package/src/series/index.ts +10 -9
  97. package/src/series/plugins/Bubbles.ts +784 -784
  98. package/src/series/plugins/Indicator.ts +298 -0
  99. package/src/series/plugins/Pie.ts +622 -622
  100. package/src/series/plugins/PieEventTexts.ts +283 -283
  101. package/src/series/plugins/PieLabels.ts +639 -639
  102. package/src/series/plugins/Rose.ts +515 -515
  103. package/src/series/plugins/RoseLabels.ts +599 -599
  104. package/src/series/plugins/SeriesLegend.ts +107 -107
  105. package/src/series/plugins/SeriesTooltip.ts +66 -66
  106. package/src/series/seriesObservables.ts +168 -168
  107. package/src/series/seriesUtils.ts +51 -51
  108. package/src/tree/defaults.ts +102 -102
  109. package/src/tree/index.ts +4 -4
  110. package/src/tree/plugins/TreeLegend.ts +100 -100
  111. package/src/tree/plugins/TreeMap.ts +341 -341
  112. package/src/tree/plugins/TreeTooltip.ts +66 -66
  113. package/src/utils/commonUtils.ts +31 -31
  114. package/src/utils/d3Graphics.ts +176 -176
  115. package/src/utils/d3Utils.ts +92 -92
  116. package/src/utils/observables.ts +14 -14
  117. package/src/utils/orbchartsUtils.ts +129 -129
  118. package/tsconfig.base.json +13 -13
  119. package/tsconfig.json +2 -2
  120. package/vite.config.js +22 -22
@@ -1,300 +1,300 @@
1
- import * as d3 from 'd3'
2
- import {
3
- combineLatest,
4
- switchMap,
5
- first,
6
- takeUntil,
7
- map,
8
- distinctUntilChanged,
9
- shareReplay,
10
- Observable,
11
- Subject } from 'rxjs'
12
- import type { DefinePluginConfig } from '../../../lib/core-types'
13
- import type { Subscription } from 'rxjs'
14
- import {
15
- defineMultiValuePlugin} from '../../../lib/core'
16
- import type {
17
- ComputedDatumMultiValue,
18
- ComputedDataMultiValue,
19
- ChartParams,
20
- ContainerPositionScaled,
21
- ContainerSize
22
- } from '../../../lib/core-types'
23
- import type { RacingCounterTextsParams } from '../../../lib/plugins-basic-types'
24
- import { DEFAULT_RACING_COUNTER_TEXTS_PARAMS } from '../defaults'
25
- // import { getD3TransitionEase } from '../../utils/d3Utils'
26
- import { getClassName } from '../../utils/orbchartsUtils'
27
- import { LAYER_INDEX_OF_LABEL } from '../../const'
28
-
29
- type TextDatum = {
30
- text: string
31
- attr: { [key:string]: any }
32
- style: { [key:string]: any }
33
- }
34
-
35
- const pluginName = 'RacingCounterTexts'
36
- const containerClassName = getClassName(pluginName, 'container')
37
- const boxClassName = getClassName(pluginName, 'box')
38
- const textClassName = getClassName(pluginName, 'text')
39
-
40
- const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_RACING_COUNTER_TEXTS_PARAMS> = {
41
- name: pluginName,
42
- defaultParams: DEFAULT_RACING_COUNTER_TEXTS_PARAMS,
43
- layerIndex: LAYER_INDEX_OF_LABEL,
44
- validator: (params, { validateColumns }) => {
45
- const result = validateColumns(params, {
46
- renderFn: {
47
- toBeTypes: ['Function'],
48
- },
49
- textAttrs: {
50
- toBeTypes: ['object[]'],
51
- },
52
- textStyles: {
53
- toBeTypes: ['object[]'],
54
- },
55
- paddingRight: {
56
- toBeTypes: ['number']
57
- },
58
- paddingBottom: {
59
- toBeTypes: ['number']
60
- },
61
- })
62
- return result
63
- }
64
- }
65
-
66
- function renderText ({ selection, data, fullParams, containerSize }: {
67
- selection: d3.Selection<SVGGElement, unknown, any, any>,
68
- data: Array<TextDatum>
69
- fullParams: RacingCounterTextsParams,
70
- containerSize: ContainerSize
71
- }): d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> {
72
-
73
- const x = containerSize.width - fullParams.paddingRight
74
- const y = containerSize.height - fullParams.paddingBottom
75
-
76
- const gSelection = selection
77
- .selectAll<SVGGElement, unknown>(`g.${boxClassName}`)
78
- .data([boxClassName])
79
- .join('g')
80
- .classed(boxClassName, true)
81
- .attr('transform', `translate(${x}, ${y})`)
82
- .each((d, i, g) => {
83
- const _g = d3.select(g[i])
84
- const textSelection = _g.selectAll<SVGTextElement, TextDatum>(`text.${textClassName}`)
85
- .data(data)
86
- .join('text')
87
- .classed(textClassName, true)
88
- .each((d, i, g) => {
89
- const t = d3.select(g[i])
90
- .text(d.text)
91
- Object.keys(d.attr)
92
- .forEach(key => {
93
- t.attr(key, d.attr[key])
94
- })
95
- Object.keys(d.style)
96
- .forEach(key => {
97
- t.style(key, d.style[key])
98
- })
99
- })
100
- })
101
-
102
- return gSelection.selectAll<SVGTextElement, TextDatum>(`text.${textClassName}`)
103
- }
104
-
105
- function createTextData ({ computedData, valueLabel, valueIndex, renderFn, textAttrs, textStyles }: {
106
- computedData: ComputedDataMultiValue
107
- valueLabel: string
108
- valueIndex: number
109
- // eventData: EventMultiValue,
110
- // t: number,
111
- renderFn: (valueLabel: string, valueIndex: number, data: ComputedDataMultiValue) => string[] | string
112
- textAttrs: Array<{ [key:string]: string | number }>
113
- textStyles: Array<{ [key:string]: string | number }>
114
- }): TextDatum[] {
115
- const text = valueLabel || String(valueIndex) // 如果沒有文字就跑數字
116
- const callbackText = renderFn(text, valueIndex, computedData)
117
- const textArr = Array.isArray(callbackText) ? callbackText : [callbackText]
118
- return textArr.map((d, i) => {
119
- return {
120
- text: d,
121
- attr: textAttrs[i],
122
- style: textStyles[i]
123
- }
124
- })
125
- }
126
-
127
- function createEachPieEventTexts (pluginName: string, context: {
128
- containerSelection: d3.Selection<SVGGElement, any, any, unknown>
129
- textData$: Observable<TextDatum[]>
130
- // computedData$: Observable<ComputedDatumMultiValue[][]>
131
- // containerComputedSortedData$: Observable<ComputedDatumMultiValue[]>
132
- // CategoryDataMap$: Observable<Map<string, ComputedDatumMultiValue[]>>
133
- fullParams$: Observable<RacingCounterTextsParams>
134
- // fullChartParams$: Observable<ChartParams>
135
- xyValueIndex$: Observable<[number, number]>
136
- valueLabel$: Observable<string>
137
- // multiValueHighlight$: Observable<ComputedDatumMultiValue[]>
138
- // multiValueContainerPosition$: Observable<ContainerPositionScaled>
139
- // event$: Subject<EventMultiValue>
140
- containerSize$: Observable<ContainerSize>
141
- }) {
142
- const destroy$ = new Subject()
143
-
144
- let textSelection: d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> | undefined
145
-
146
- combineLatest({
147
- textData: context.textData$,
148
- fullParams: context.fullParams$,
149
- containerSize: context.containerSize$,
150
- }).pipe(
151
- takeUntil(destroy$),
152
- ).subscribe(data => {
153
- textSelection = renderText({
154
- selection: context.containerSelection,
155
- data: data.textData,
156
- fullParams: data.fullParams,
157
- containerSize: data.containerSize
158
- })
159
- })
160
-
161
- return () => {
162
- destroy$.next(undefined)
163
- }
164
- }
165
-
166
- export const RacingCounterTexts = defineMultiValuePlugin(pluginConfig)(({ selection, name, observer, subject }) => {
167
- const destroy$ = new Subject()
168
-
169
- const containerSelection$ = combineLatest({
170
- computedData: observer.computedData$.pipe(
171
- distinctUntilChanged((a, b) => {
172
- // 只有當series的數量改變時,才重新計算
173
- return a.length === b.length
174
- }),
175
- ),
176
- isCategorySeprate: observer.isCategorySeprate$
177
- }).pipe(
178
- takeUntil(destroy$),
179
- switchMap(async (d) => d),
180
- map(data => {
181
- return data.isCategorySeprate
182
- // category分開的時候顯示各別axis
183
- ? data.computedData
184
- // category合併的時候只顯示第一個axis
185
- : [data.computedData[0]]
186
- }),
187
- map((computedData, i) => {
188
- return selection
189
- .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${containerClassName}`)
190
- .data(computedData, d => (d && d[0]) ? d[0].categoryIndex : i)
191
- .join('g')
192
- .classed(containerClassName, true)
193
- })
194
- )
195
-
196
- const axisSelection$ = containerSelection$.pipe(
197
- takeUntil(destroy$),
198
- map((containerSelection, i) => {
199
- return containerSelection
200
- .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${boxClassName}`)
201
- .data([boxClassName])
202
- .join('g')
203
- .classed(boxClassName, true)
204
- })
205
- )
206
-
207
- combineLatest({
208
- containerSelection: containerSelection$,
209
- gridContainerPosition: observer.containerPosition$
210
- }).pipe(
211
- takeUntil(destroy$),
212
- switchMap(async d => d)
213
- ).subscribe(data => {
214
- data.containerSelection
215
- .attr('transform', (d, i) => {
216
- const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
217
- const translate = gridContainerPosition.translate
218
- const scale = gridContainerPosition.scale
219
- // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
220
- return `translate(${translate[0]}, ${translate[1]})`
221
- })
222
- // .attr('opacity', 0)
223
- // .transition()
224
- // .attr('opacity', 1)
225
- })
226
-
227
-
228
- const valueLabel$ = combineLatest({
229
- xyValueIndex: observer.xyValueIndex$,
230
- fullDataFormatter: observer.fullDataFormatter$,
231
- }).pipe(
232
- takeUntil(destroy$),
233
- switchMap(async d => d),
234
- map(d => d.fullDataFormatter.valueLabels[d.xyValueIndex[0]] ?? ''),
235
- distinctUntilChanged(),
236
- shareReplay(1)
237
- )
238
-
239
- const textData$ = combineLatest({
240
- xyValueIndex: observer.xyValueIndex$,
241
- valueLabel: valueLabel$,
242
- computedData: observer.computedData$,
243
- fullParams: observer.fullParams$,
244
- }).pipe(
245
- takeUntil(destroy$),
246
- switchMap(async d => d),
247
- map(data => {
248
- return createTextData({
249
- valueIndex: data.xyValueIndex[0],
250
- valueLabel: data.valueLabel,
251
- computedData: data.computedData,
252
- renderFn: data.fullParams.renderFn!,
253
- textAttrs: data.fullParams.textAttrs!,
254
- textStyles: data.fullParams.textStyles!,
255
- })
256
- }),
257
- shareReplay(1)
258
- )
259
-
260
- const unsubscribeFnArr: (() => void)[] = []
261
-
262
- containerSelection$
263
- .pipe(
264
- takeUntil(destroy$)
265
- )
266
- .subscribe(seriesCenterSelection => {
267
- // 每次重新計算時,清除之前的訂閱
268
- unsubscribeFnArr.forEach(fn => fn())
269
-
270
- seriesCenterSelection.each((d, containerIndex, g) => {
271
-
272
- const containerSelection = d3.select(g[containerIndex])
273
-
274
- const containerComputedData$ = observer.visibleComputedRankingByIndexData$.pipe(
275
- takeUntil(destroy$),
276
- map(data => data[containerIndex] ?? data[0])
277
- )
278
-
279
- const containerPosition$ = observer.containerPosition$.pipe(
280
- takeUntil(destroy$),
281
- map(data => data[containerIndex] ?? data[0])
282
- )
283
-
284
- unsubscribeFnArr[containerIndex] = createEachPieEventTexts(pluginName, {
285
- containerSelection: containerSelection,
286
- textData$: textData$,
287
- fullParams$: observer.fullParams$,
288
- valueLabel$: valueLabel$,
289
- xyValueIndex$: observer.xyValueIndex$,
290
- containerSize$: observer.containerSize$
291
- })
292
-
293
- })
294
- })
295
-
296
- return () => {
297
- destroy$.next(undefined)
298
- unsubscribeFnArr.forEach(fn => fn())
299
- }
300
- })
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ switchMap,
5
+ first,
6
+ takeUntil,
7
+ map,
8
+ distinctUntilChanged,
9
+ shareReplay,
10
+ Observable,
11
+ Subject } from 'rxjs'
12
+ import type { DefinePluginConfig } from '../../../lib/core-types'
13
+ import type { Subscription } from 'rxjs'
14
+ import {
15
+ defineMultiValuePlugin} from '../../../lib/core'
16
+ import type {
17
+ ComputedDatumMultiValue,
18
+ ComputedDataMultiValue,
19
+ ChartParams,
20
+ ContainerPositionScaled,
21
+ ContainerSize
22
+ } from '../../../lib/core-types'
23
+ import type { RacingCounterTextsParams } from '../../../lib/plugins-basic-types'
24
+ import { DEFAULT_RACING_COUNTER_TEXTS_PARAMS } from '../defaults'
25
+ // import { getD3TransitionEase } from '../../utils/d3Utils'
26
+ import { getClassName } from '../../utils/orbchartsUtils'
27
+ import { LAYER_INDEX_OF_LABEL } from '../../const'
28
+
29
+ type TextDatum = {
30
+ text: string
31
+ attr: { [key:string]: any }
32
+ style: { [key:string]: any }
33
+ }
34
+
35
+ const pluginName = 'RacingCounterTexts'
36
+ const containerClassName = getClassName(pluginName, 'container')
37
+ const boxClassName = getClassName(pluginName, 'box')
38
+ const textClassName = getClassName(pluginName, 'text')
39
+
40
+ const pluginConfig: DefinePluginConfig<typeof pluginName, typeof DEFAULT_RACING_COUNTER_TEXTS_PARAMS> = {
41
+ name: pluginName,
42
+ defaultParams: DEFAULT_RACING_COUNTER_TEXTS_PARAMS,
43
+ layerIndex: LAYER_INDEX_OF_LABEL,
44
+ validator: (params, { validateColumns }) => {
45
+ const result = validateColumns(params, {
46
+ renderFn: {
47
+ toBeTypes: ['Function'],
48
+ },
49
+ textAttrs: {
50
+ toBeTypes: ['object[]'],
51
+ },
52
+ textStyles: {
53
+ toBeTypes: ['object[]'],
54
+ },
55
+ paddingRight: {
56
+ toBeTypes: ['number']
57
+ },
58
+ paddingBottom: {
59
+ toBeTypes: ['number']
60
+ },
61
+ })
62
+ return result
63
+ }
64
+ }
65
+
66
+ function renderText ({ selection, data, fullParams, containerSize }: {
67
+ selection: d3.Selection<SVGGElement, unknown, any, any>,
68
+ data: Array<TextDatum>
69
+ fullParams: RacingCounterTextsParams,
70
+ containerSize: ContainerSize
71
+ }): d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> {
72
+
73
+ const x = containerSize.width - fullParams.paddingRight
74
+ const y = containerSize.height - fullParams.paddingBottom
75
+
76
+ const gSelection = selection
77
+ .selectAll<SVGGElement, unknown>(`g.${boxClassName}`)
78
+ .data([boxClassName])
79
+ .join('g')
80
+ .classed(boxClassName, true)
81
+ .attr('transform', `translate(${x}, ${y})`)
82
+ .each((d, i, g) => {
83
+ const _g = d3.select(g[i])
84
+ const textSelection = _g.selectAll<SVGTextElement, TextDatum>(`text.${textClassName}`)
85
+ .data(data)
86
+ .join('text')
87
+ .classed(textClassName, true)
88
+ .each((d, i, g) => {
89
+ const t = d3.select(g[i])
90
+ .text(d.text)
91
+ Object.keys(d.attr)
92
+ .forEach(key => {
93
+ t.attr(key, d.attr[key])
94
+ })
95
+ Object.keys(d.style)
96
+ .forEach(key => {
97
+ t.style(key, d.style[key])
98
+ })
99
+ })
100
+ })
101
+
102
+ return gSelection.selectAll<SVGTextElement, TextDatum>(`text.${textClassName}`)
103
+ }
104
+
105
+ function createTextData ({ computedData, valueLabel, valueIndex, renderFn, textAttrs, textStyles }: {
106
+ computedData: ComputedDataMultiValue
107
+ valueLabel: string
108
+ valueIndex: number
109
+ // eventData: EventMultiValue,
110
+ // t: number,
111
+ renderFn: (valueLabel: string, valueIndex: number, data: ComputedDataMultiValue) => string[] | string
112
+ textAttrs: Array<{ [key:string]: string | number }>
113
+ textStyles: Array<{ [key:string]: string | number }>
114
+ }): TextDatum[] {
115
+ const text = valueLabel || String(valueIndex) // 如果沒有文字就跑數字
116
+ const callbackText = renderFn(text, valueIndex, computedData)
117
+ const textArr = Array.isArray(callbackText) ? callbackText : [callbackText]
118
+ return textArr.map((d, i) => {
119
+ return {
120
+ text: d,
121
+ attr: textAttrs[i],
122
+ style: textStyles[i]
123
+ }
124
+ })
125
+ }
126
+
127
+ function createEachPieEventTexts (pluginName: string, context: {
128
+ containerSelection: d3.Selection<SVGGElement, any, any, unknown>
129
+ textData$: Observable<TextDatum[]>
130
+ // computedData$: Observable<ComputedDatumMultiValue[][]>
131
+ // containerComputedSortedData$: Observable<ComputedDatumMultiValue[]>
132
+ // CategoryDataMap$: Observable<Map<string, ComputedDatumMultiValue[]>>
133
+ fullParams$: Observable<RacingCounterTextsParams>
134
+ // fullChartParams$: Observable<ChartParams>
135
+ xyValueIndex$: Observable<[number, number]>
136
+ valueLabel$: Observable<string>
137
+ // multiValueHighlight$: Observable<ComputedDatumMultiValue[]>
138
+ // multiValueContainerPosition$: Observable<ContainerPositionScaled>
139
+ // event$: Subject<EventMultiValue>
140
+ containerSize$: Observable<ContainerSize>
141
+ }) {
142
+ const destroy$ = new Subject()
143
+
144
+ let textSelection: d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> | undefined
145
+
146
+ combineLatest({
147
+ textData: context.textData$,
148
+ fullParams: context.fullParams$,
149
+ containerSize: context.containerSize$,
150
+ }).pipe(
151
+ takeUntil(destroy$),
152
+ ).subscribe(data => {
153
+ textSelection = renderText({
154
+ selection: context.containerSelection,
155
+ data: data.textData,
156
+ fullParams: data.fullParams,
157
+ containerSize: data.containerSize
158
+ })
159
+ })
160
+
161
+ return () => {
162
+ destroy$.next(undefined)
163
+ }
164
+ }
165
+
166
+ export const RacingCounterTexts = defineMultiValuePlugin(pluginConfig)(({ selection, name, observer, subject }) => {
167
+ const destroy$ = new Subject()
168
+
169
+ const containerSelection$ = combineLatest({
170
+ computedData: observer.computedData$.pipe(
171
+ distinctUntilChanged((a, b) => {
172
+ // 只有當series的數量改變時,才重新計算
173
+ return a.length === b.length
174
+ }),
175
+ ),
176
+ isCategorySeprate: observer.isCategorySeprate$
177
+ }).pipe(
178
+ takeUntil(destroy$),
179
+ switchMap(async (d) => d),
180
+ map(data => {
181
+ return data.isCategorySeprate
182
+ // category分開的時候顯示各別axis
183
+ ? data.computedData
184
+ // category合併的時候只顯示第一個axis
185
+ : [data.computedData[0]]
186
+ }),
187
+ map((computedData, i) => {
188
+ return selection
189
+ .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${containerClassName}`)
190
+ .data(computedData, d => (d && d[0]) ? d[0].categoryIndex : i)
191
+ .join('g')
192
+ .classed(containerClassName, true)
193
+ })
194
+ )
195
+
196
+ const axisSelection$ = containerSelection$.pipe(
197
+ takeUntil(destroy$),
198
+ map((containerSelection, i) => {
199
+ return containerSelection
200
+ .selectAll<SVGGElement, ComputedDatumMultiValue[]>(`g.${boxClassName}`)
201
+ .data([boxClassName])
202
+ .join('g')
203
+ .classed(boxClassName, true)
204
+ })
205
+ )
206
+
207
+ combineLatest({
208
+ containerSelection: containerSelection$,
209
+ gridContainerPosition: observer.containerPosition$
210
+ }).pipe(
211
+ takeUntil(destroy$),
212
+ switchMap(async d => d)
213
+ ).subscribe(data => {
214
+ data.containerSelection
215
+ .attr('transform', (d, i) => {
216
+ const gridContainerPosition = data.gridContainerPosition[i] ?? data.gridContainerPosition[0]
217
+ const translate = gridContainerPosition.translate
218
+ const scale = gridContainerPosition.scale
219
+ // return `translate(${translate[0]}, ${translate[1]}) scale(${scale[0]}, ${scale[1]})`
220
+ return `translate(${translate[0]}, ${translate[1]})`
221
+ })
222
+ // .attr('opacity', 0)
223
+ // .transition()
224
+ // .attr('opacity', 1)
225
+ })
226
+
227
+
228
+ const valueLabel$ = combineLatest({
229
+ xyValueIndex: observer.xyValueIndex$,
230
+ fullDataFormatter: observer.fullDataFormatter$,
231
+ }).pipe(
232
+ takeUntil(destroy$),
233
+ switchMap(async d => d),
234
+ map(d => d.fullDataFormatter.valueLabels[d.xyValueIndex[0]] ?? ''),
235
+ distinctUntilChanged(),
236
+ shareReplay(1)
237
+ )
238
+
239
+ const textData$ = combineLatest({
240
+ xyValueIndex: observer.xyValueIndex$,
241
+ valueLabel: valueLabel$,
242
+ computedData: observer.computedData$,
243
+ fullParams: observer.fullParams$,
244
+ }).pipe(
245
+ takeUntil(destroy$),
246
+ switchMap(async d => d),
247
+ map(data => {
248
+ return createTextData({
249
+ valueIndex: data.xyValueIndex[0],
250
+ valueLabel: data.valueLabel,
251
+ computedData: data.computedData,
252
+ renderFn: data.fullParams.renderFn!,
253
+ textAttrs: data.fullParams.textAttrs!,
254
+ textStyles: data.fullParams.textStyles!,
255
+ })
256
+ }),
257
+ shareReplay(1)
258
+ )
259
+
260
+ const unsubscribeFnArr: (() => void)[] = []
261
+
262
+ containerSelection$
263
+ .pipe(
264
+ takeUntil(destroy$)
265
+ )
266
+ .subscribe(seriesCenterSelection => {
267
+ // 每次重新計算時,清除之前的訂閱
268
+ unsubscribeFnArr.forEach(fn => fn())
269
+
270
+ seriesCenterSelection.each((d, containerIndex, g) => {
271
+
272
+ const containerSelection = d3.select(g[containerIndex])
273
+
274
+ const containerComputedData$ = observer.visibleComputedRankingByIndexData$.pipe(
275
+ takeUntil(destroy$),
276
+ map(data => data[containerIndex] ?? data[0])
277
+ )
278
+
279
+ const containerPosition$ = observer.containerPosition$.pipe(
280
+ takeUntil(destroy$),
281
+ map(data => data[containerIndex] ?? data[0])
282
+ )
283
+
284
+ unsubscribeFnArr[containerIndex] = createEachPieEventTexts(pluginName, {
285
+ containerSelection: containerSelection,
286
+ textData$: textData$,
287
+ fullParams$: observer.fullParams$,
288
+ valueLabel$: valueLabel$,
289
+ xyValueIndex$: observer.xyValueIndex$,
290
+ containerSize$: observer.containerSize$
291
+ })
292
+
293
+ })
294
+ })
295
+
296
+ return () => {
297
+ destroy$.next(undefined)
298
+ unsubscribeFnArr.forEach(fn => fn())
299
+ }
300
+ })