@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,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
+ })