@orbcharts/plugins-basic 3.0.0-alpha.45 → 3.0.0-alpha.46
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +200 -200
- package/dist/orbcharts-plugins-basic.es.js +182 -159
- package/dist/orbcharts-plugins-basic.umd.js +7 -7
- package/dist/src/index.d.ts +5 -1
- package/dist/{orbcharts-plugins-basic/src → src}/series/seriesObservables.d.ts +4 -4
- package/package.json +42 -42
- package/src/base/BaseBarStack.ts +778 -778
- package/src/base/BaseBars.ts +764 -764
- package/src/base/BaseBarsTriangle.ts +672 -672
- package/src/base/BaseDots.ts +502 -502
- package/src/base/BaseGroupAxis.ts +496 -496
- package/src/base/BaseLegend.ts +641 -641
- package/src/base/BaseLineAreas.ts +625 -625
- package/src/base/BaseLines.ts +699 -699
- package/src/base/BaseValueAxis.ts +478 -478
- package/src/base/types.ts +2 -2
- package/src/grid/defaults.ts +121 -121
- package/src/grid/gridObservables.ts +247 -247
- package/src/grid/index.ts +15 -15
- package/src/grid/plugins/BarStack.ts +50 -50
- package/src/grid/plugins/Bars.ts +51 -51
- package/src/grid/plugins/BarsDiverging.ts +41 -41
- package/src/grid/plugins/BarsTriangle.ts +50 -50
- package/src/grid/plugins/Dots.ts +37 -37
- package/src/grid/plugins/GridLegend.ts +59 -59
- package/src/grid/plugins/GroupAux.ts +645 -645
- package/src/grid/plugins/GroupAxis.ts +42 -42
- package/src/grid/plugins/LineAreas.ts +39 -39
- package/src/grid/plugins/Lines.ts +38 -38
- package/src/grid/plugins/ScalingArea.ts +173 -173
- package/src/grid/plugins/ValueAxis.ts +43 -43
- package/src/grid/plugins/ValueStackAxis.ts +79 -79
- package/src/grid/types.ts +120 -120
- package/src/index.ts +9 -9
- package/src/multiGrid/defaults.ts +147 -147
- package/src/multiGrid/index.ts +11 -11
- package/src/multiGrid/multiGridObservables.ts +42 -42
- package/src/multiGrid/plugins/MultiBarStack.ts +74 -74
- package/src/multiGrid/plugins/MultiBars.ts +73 -73
- package/src/multiGrid/plugins/MultiBarsTriangle.ts +73 -73
- package/src/multiGrid/plugins/MultiDots.ts +60 -60
- package/src/multiGrid/plugins/MultiGridLegend.ts +89 -89
- package/src/multiGrid/plugins/MultiGroupAxis.ts +65 -65
- package/src/multiGrid/plugins/MultiLineAreas.ts +62 -62
- package/src/multiGrid/plugins/MultiLines.ts +61 -61
- package/src/multiGrid/plugins/MultiValueAxis.ts +65 -65
- package/src/multiGrid/plugins/OverlappingValueAxes.ts +169 -169
- package/src/multiGrid/types.ts +67 -67
- package/src/noneData/defaults.ts +64 -64
- package/src/noneData/index.ts +3 -3
- package/src/noneData/plugins/Container.ts +10 -10
- package/src/noneData/plugins/Tooltip.ts +310 -310
- package/src/noneData/types.ts +26 -26
- package/src/series/defaults.ts +109 -109
- package/src/series/index.ts +6 -6
- package/src/series/plugins/Bubbles.ts +602 -571
- package/src/series/plugins/Pie.ts +555 -548
- package/src/series/plugins/PieEventTexts.ts +258 -258
- package/src/series/plugins/PieLabels.ts +335 -335
- package/src/series/plugins/SeriesLegend.ts +59 -59
- package/src/series/seriesObservables.ts +145 -145
- package/src/series/seriesUtils.ts +50 -50
- package/src/series/types.ts +67 -67
- package/src/tree/defaults.ts +22 -22
- package/src/tree/index.ts +3 -3
- package/src/tree/plugins/TreeLegend.ts +59 -59
- package/src/tree/plugins/TreeMap.ts +305 -305
- package/src/tree/types.ts +23 -23
- package/src/utils/commonUtils.ts +21 -21
- package/src/utils/d3Graphics.ts +124 -124
- package/src/utils/d3Utils.ts +73 -73
- package/src/utils/observables.ts +14 -14
- package/src/utils/orbchartsUtils.ts +100 -100
- package/tsconfig.dev.json +16 -16
- package/tsconfig.json +13 -16
- package/tsconfig.prod.json +13 -13
- package/vite.config.js +49 -49
- package/dist/orbcharts-plugins-basic/src/index.d.ts +0 -5
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseBarStack.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseBars.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseBarsTriangle.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseDots.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseGroupArea.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseGroupAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseLegend.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseLineAreas.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseLines.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/BaseValueAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/base/types.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/defaults.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/gridObservables.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/BarStack.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Bars.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/BarsDiverging.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/BarsTriangle.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Dots.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/GridLegend.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/GroupAux.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/GroupAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/LineAreas.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Lines.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/Ranking.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/RankingAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/ScalingArea.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/ValueAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/plugins/ValueStackAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/grid/types.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/defaults.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/multiGridObservables.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiBarStack.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiBars.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiBarsTriangle.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiDots.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiGridLegend.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiGroupAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiLineAreas.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiLines.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/MultiValueAxis.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/plugins/OverlappingValueAxes.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiGrid/types.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiValue/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiValue/plugins/Scatter.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/multiValue/plugins/ScatterAxes.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/noneData/defaults.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/noneData/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/noneData/plugins/Container.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/noneData/plugins/Tooltip.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/noneData/types.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/relationship/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/relationship/plugins/Relationship.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/defaults.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/Bubbles.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/Pie.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/PieEventTexts.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/PieLabels.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/SeriesLegend.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/plugins/Waffle.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/seriesUtils.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/series/types.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/tree/defaults.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/tree/index.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/tree/plugins/TreeLegend.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/tree/plugins/TreeMap.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/tree/types.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/utils/commonUtils.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/utils/d3Graphics.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/utils/d3Utils.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/utils/observables.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/src → src}/utils/orbchartsUtils.d.ts +0 -0
- /package/dist/{orbcharts-plugins-basic/vite.config.d.ts → vite.config.d.ts} +0 -0
@@ -1,258 +1,258 @@
|
|
1
|
-
import * as d3 from 'd3'
|
2
|
-
import {
|
3
|
-
combineLatest,
|
4
|
-
switchMap,
|
5
|
-
first,
|
6
|
-
takeUntil,
|
7
|
-
map,
|
8
|
-
distinctUntilChanged,
|
9
|
-
Observable,
|
10
|
-
Subject } from 'rxjs'
|
11
|
-
import type { Subscription } from 'rxjs'
|
12
|
-
import {
|
13
|
-
defineSeriesPlugin} from '@orbcharts/core'
|
14
|
-
import type {
|
15
|
-
ComputedDatumSeries,
|
16
|
-
ChartParams,
|
17
|
-
SeriesContainerPosition,
|
18
|
-
EventName,
|
19
|
-
EventSeries } from '@orbcharts/core'
|
20
|
-
import type { PieEventTextsParams } from '../types'
|
21
|
-
import { DEFAULT_PIE_EVENT_TEXTS_PARAMS } from '../defaults'
|
22
|
-
import { getD3TransitionEase } from '../../utils/d3Utils'
|
23
|
-
import { getClassName } from '../../utils/orbchartsUtils'
|
24
|
-
import { seriesCenterSelectionObservable } from '../seriesObservables'
|
25
|
-
|
26
|
-
|
27
|
-
type TextDatum = {
|
28
|
-
text: string
|
29
|
-
attr: { [key:string]: any }
|
30
|
-
style: { [key:string]: any }
|
31
|
-
}
|
32
|
-
|
33
|
-
const pluginName = 'PieEventTexts'
|
34
|
-
const textClassName = getClassName(pluginName, 'text')
|
35
|
-
|
36
|
-
function renderText (
|
37
|
-
selection: d3.Selection<SVGGElement, unknown, any, any>,
|
38
|
-
data: Array<TextDatum>
|
39
|
-
): d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> {
|
40
|
-
const textUpdate = selection
|
41
|
-
.selectAll<SVGTextElement, TextDatum>(`text.${textClassName}`)
|
42
|
-
.data(data)
|
43
|
-
const textEnter = textUpdate.enter()
|
44
|
-
.append('text')
|
45
|
-
.classed(textClassName, true)
|
46
|
-
const text = textUpdate.merge(textEnter)
|
47
|
-
text
|
48
|
-
.each((d, i, g) => {
|
49
|
-
const t = d3.select(g[i])
|
50
|
-
.text(d.text)
|
51
|
-
Object.keys(d.attr)
|
52
|
-
.forEach(key => {
|
53
|
-
t.attr(key, d.attr[key])
|
54
|
-
})
|
55
|
-
Object.keys(d.style)
|
56
|
-
.forEach(key => {
|
57
|
-
t.style(key, d.style[key])
|
58
|
-
})
|
59
|
-
})
|
60
|
-
|
61
|
-
textUpdate.exit().remove()
|
62
|
-
|
63
|
-
return text
|
64
|
-
}
|
65
|
-
|
66
|
-
function makeTextData ({ eventData, eventName, t, eventFn, textAttrs, textStyles }: {
|
67
|
-
eventData: EventSeries,
|
68
|
-
eventName: EventName,
|
69
|
-
t: number,
|
70
|
-
eventFn: (d: EventSeries, eventName: EventName, t: number) => string[]
|
71
|
-
textAttrs: Array<{ [key:string]: string | number }>
|
72
|
-
textStyles: Array<{ [key:string]: string | number }>
|
73
|
-
}): TextDatum[] {
|
74
|
-
const callbackText = eventFn(eventData, eventName, t)
|
75
|
-
return callbackText.map((d, i) => {
|
76
|
-
return {
|
77
|
-
text: d,
|
78
|
-
attr: textAttrs[i],
|
79
|
-
style: textStyles[i]
|
80
|
-
}
|
81
|
-
})
|
82
|
-
}
|
83
|
-
|
84
|
-
function createEachPieEventTexts (pluginName: string, context: {
|
85
|
-
containerSelection: d3.Selection<SVGGElement, any, any, unknown>
|
86
|
-
computedData$: Observable<ComputedDatumSeries[][]>
|
87
|
-
containerComputedLayoutData$: Observable<ComputedDatumSeries[]>
|
88
|
-
SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
|
89
|
-
fullParams$: Observable<PieEventTextsParams>
|
90
|
-
fullChartParams$: Observable<ChartParams>
|
91
|
-
seriesHighlight$: Observable<ComputedDatumSeries[]>
|
92
|
-
seriesContainerPosition$: Observable<SeriesContainerPosition>
|
93
|
-
event$: Subject<EventSeries>
|
94
|
-
}) {
|
95
|
-
const destroy$ = new Subject()
|
96
|
-
|
97
|
-
// const graphicSelection: d3.Selection<SVGGElement, any, any, any> = selection.append('g')
|
98
|
-
let centerTextSelection: d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> | undefined
|
99
|
-
let storeEventSubscription: Subscription | undefined
|
100
|
-
|
101
|
-
// context.layout$
|
102
|
-
// .pipe(
|
103
|
-
// first()
|
104
|
-
// )
|
105
|
-
// .subscribe(size => {
|
106
|
-
// selection
|
107
|
-
// .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
|
108
|
-
// context.layout$
|
109
|
-
// .pipe(
|
110
|
-
// takeUntil(destroy$)
|
111
|
-
// )
|
112
|
-
// .subscribe(size => {
|
113
|
-
// selection
|
114
|
-
// .transition()
|
115
|
-
// .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
|
116
|
-
// })
|
117
|
-
// })
|
118
|
-
|
119
|
-
const highlightTarget$ = context.fullChartParams$.pipe(
|
120
|
-
takeUntil(destroy$),
|
121
|
-
map(d => d.highlightTarget),
|
122
|
-
distinctUntilChanged()
|
123
|
-
)
|
124
|
-
|
125
|
-
combineLatest({
|
126
|
-
computedData: context.computedData$,
|
127
|
-
fullParams: context.fullParams$,
|
128
|
-
fullChartParams: context.fullChartParams$,
|
129
|
-
highlightTarget: highlightTarget$,
|
130
|
-
}).pipe(
|
131
|
-
takeUntil(destroy$),
|
132
|
-
switchMap(async (d) => d),
|
133
|
-
).subscribe(data => {
|
134
|
-
|
135
|
-
context.containerSelection
|
136
|
-
.transition('move')
|
137
|
-
.duration(data.fullChartParams.transitionDuration!)
|
138
|
-
// .ease(getD3TransitionEase(data.fullChartParams.transitionEase!))
|
139
|
-
.tween('move', (event, datum) => {
|
140
|
-
return (t) => {
|
141
|
-
const renderData = makeTextData({
|
142
|
-
eventData: {
|
143
|
-
type: 'series',
|
144
|
-
pluginName,
|
145
|
-
eventName: 'transitionMove',
|
146
|
-
event,
|
147
|
-
highlightTarget: data.highlightTarget,
|
148
|
-
data: data.computedData,
|
149
|
-
series: [],
|
150
|
-
seriesIndex: -1,
|
151
|
-
seriesLabel: '',
|
152
|
-
datum: null
|
153
|
-
},
|
154
|
-
eventName: 'transitionMove',
|
155
|
-
t,
|
156
|
-
eventFn: data.fullParams.eventFn!,
|
157
|
-
textAttrs: data.fullParams.textAttrs!,
|
158
|
-
textStyles: data.fullParams.textStyles!
|
159
|
-
})
|
160
|
-
centerTextSelection = renderText(context.containerSelection, renderData)
|
161
|
-
}
|
162
|
-
})
|
163
|
-
.on('end', (event, datum) => {
|
164
|
-
const renderData = makeTextData({
|
165
|
-
eventData: {
|
166
|
-
type: 'series',
|
167
|
-
pluginName,
|
168
|
-
eventName: 'transitionEnd',
|
169
|
-
event,
|
170
|
-
highlightTarget: data.highlightTarget,
|
171
|
-
data: data.computedData,
|
172
|
-
series: [],
|
173
|
-
seriesIndex: -1,
|
174
|
-
seriesLabel: '',
|
175
|
-
datum: null
|
176
|
-
},
|
177
|
-
eventName: 'transitionMove',
|
178
|
-
t: 1,
|
179
|
-
eventFn: data.fullParams.eventFn!,
|
180
|
-
textAttrs: data.fullParams.textAttrs!,
|
181
|
-
textStyles: data.fullParams.textStyles!
|
182
|
-
})
|
183
|
-
centerTextSelection = renderText(context.containerSelection, renderData)
|
184
|
-
|
185
|
-
if (storeEventSubscription) {
|
186
|
-
storeEventSubscription.unsubscribe()
|
187
|
-
}
|
188
|
-
storeEventSubscription = context.event$
|
189
|
-
.subscribe(eventData => {
|
190
|
-
const renderData = makeTextData({
|
191
|
-
eventData,
|
192
|
-
eventName: eventData.eventName,
|
193
|
-
t: 1,
|
194
|
-
eventFn: data.fullParams.eventFn!,
|
195
|
-
textAttrs: data.fullParams.textAttrs!,
|
196
|
-
textStyles: data.fullParams.textStyles!
|
197
|
-
})
|
198
|
-
centerTextSelection = renderText(context.containerSelection, renderData)
|
199
|
-
})
|
200
|
-
})
|
201
|
-
})
|
202
|
-
|
203
|
-
return () => {
|
204
|
-
destroy$.next(undefined)
|
205
|
-
}
|
206
|
-
}
|
207
|
-
|
208
|
-
export const PieEventTexts = defineSeriesPlugin(pluginName, DEFAULT_PIE_EVENT_TEXTS_PARAMS)(({ selection, name, observer, subject }) => {
|
209
|
-
const destroy$ = new Subject()
|
210
|
-
|
211
|
-
const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
|
212
|
-
selection: selection,
|
213
|
-
pluginName,
|
214
|
-
|
215
|
-
seriesLabels$: observer.seriesLabels$,
|
216
|
-
seriesContainerPosition$: observer.seriesContainerPosition$
|
217
|
-
})
|
218
|
-
|
219
|
-
const unsubscribeFnArr: (() => void)[] = []
|
220
|
-
|
221
|
-
seriesCenterSelection$.subscribe(seriesCenterSelection => {
|
222
|
-
// 每次重新計算時,清除之前的訂閱
|
223
|
-
unsubscribeFnArr.forEach(fn => fn())
|
224
|
-
|
225
|
-
seriesCenterSelection.each((d, containerIndex, g) => {
|
226
|
-
|
227
|
-
const containerSelection = d3.select(g[containerIndex])
|
228
|
-
|
229
|
-
const containerComputedLayoutData$ = observer.computedLayoutData$.pipe(
|
230
|
-
takeUntil(destroy$),
|
231
|
-
map(data => data[containerIndex] ?? data[0])
|
232
|
-
)
|
233
|
-
|
234
|
-
const containerPosition$ = observer.seriesContainerPosition$.pipe(
|
235
|
-
takeUntil(destroy$),
|
236
|
-
map(data => data[containerIndex] ?? data[0])
|
237
|
-
)
|
238
|
-
|
239
|
-
unsubscribeFnArr[containerIndex] = createEachPieEventTexts(pluginName, {
|
240
|
-
containerSelection: containerSelection,
|
241
|
-
computedData$: observer.computedData$,
|
242
|
-
containerComputedLayoutData$: containerComputedLayoutData$,
|
243
|
-
SeriesDataMap$: observer.SeriesDataMap$,
|
244
|
-
fullParams$: observer.fullParams$,
|
245
|
-
fullChartParams$: observer.fullChartParams$,
|
246
|
-
seriesHighlight$: observer.seriesHighlight$,
|
247
|
-
seriesContainerPosition$: containerPosition$,
|
248
|
-
event$: subject.event$,
|
249
|
-
})
|
250
|
-
|
251
|
-
})
|
252
|
-
})
|
253
|
-
|
254
|
-
return () => {
|
255
|
-
destroy$.next(undefined)
|
256
|
-
unsubscribeFnArr.forEach(fn => fn())
|
257
|
-
}
|
258
|
-
})
|
1
|
+
import * as d3 from 'd3'
|
2
|
+
import {
|
3
|
+
combineLatest,
|
4
|
+
switchMap,
|
5
|
+
first,
|
6
|
+
takeUntil,
|
7
|
+
map,
|
8
|
+
distinctUntilChanged,
|
9
|
+
Observable,
|
10
|
+
Subject } from 'rxjs'
|
11
|
+
import type { Subscription } from 'rxjs'
|
12
|
+
import {
|
13
|
+
defineSeriesPlugin} from '@orbcharts/core'
|
14
|
+
import type {
|
15
|
+
ComputedDatumSeries,
|
16
|
+
ChartParams,
|
17
|
+
SeriesContainerPosition,
|
18
|
+
EventName,
|
19
|
+
EventSeries } from '@orbcharts/core'
|
20
|
+
import type { PieEventTextsParams } from '../types'
|
21
|
+
import { DEFAULT_PIE_EVENT_TEXTS_PARAMS } from '../defaults'
|
22
|
+
import { getD3TransitionEase } from '../../utils/d3Utils'
|
23
|
+
import { getClassName } from '../../utils/orbchartsUtils'
|
24
|
+
import { seriesCenterSelectionObservable } from '../seriesObservables'
|
25
|
+
|
26
|
+
|
27
|
+
type TextDatum = {
|
28
|
+
text: string
|
29
|
+
attr: { [key:string]: any }
|
30
|
+
style: { [key:string]: any }
|
31
|
+
}
|
32
|
+
|
33
|
+
const pluginName = 'PieEventTexts'
|
34
|
+
const textClassName = getClassName(pluginName, 'text')
|
35
|
+
|
36
|
+
function renderText (
|
37
|
+
selection: d3.Selection<SVGGElement, unknown, any, any>,
|
38
|
+
data: Array<TextDatum>
|
39
|
+
): d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> {
|
40
|
+
const textUpdate = selection
|
41
|
+
.selectAll<SVGTextElement, TextDatum>(`text.${textClassName}`)
|
42
|
+
.data(data)
|
43
|
+
const textEnter = textUpdate.enter()
|
44
|
+
.append('text')
|
45
|
+
.classed(textClassName, true)
|
46
|
+
const text = textUpdate.merge(textEnter)
|
47
|
+
text
|
48
|
+
.each((d, i, g) => {
|
49
|
+
const t = d3.select(g[i])
|
50
|
+
.text(d.text)
|
51
|
+
Object.keys(d.attr)
|
52
|
+
.forEach(key => {
|
53
|
+
t.attr(key, d.attr[key])
|
54
|
+
})
|
55
|
+
Object.keys(d.style)
|
56
|
+
.forEach(key => {
|
57
|
+
t.style(key, d.style[key])
|
58
|
+
})
|
59
|
+
})
|
60
|
+
|
61
|
+
textUpdate.exit().remove()
|
62
|
+
|
63
|
+
return text
|
64
|
+
}
|
65
|
+
|
66
|
+
function makeTextData ({ eventData, eventName, t, eventFn, textAttrs, textStyles }: {
|
67
|
+
eventData: EventSeries,
|
68
|
+
eventName: EventName,
|
69
|
+
t: number,
|
70
|
+
eventFn: (d: EventSeries, eventName: EventName, t: number) => string[]
|
71
|
+
textAttrs: Array<{ [key:string]: string | number }>
|
72
|
+
textStyles: Array<{ [key:string]: string | number }>
|
73
|
+
}): TextDatum[] {
|
74
|
+
const callbackText = eventFn(eventData, eventName, t)
|
75
|
+
return callbackText.map((d, i) => {
|
76
|
+
return {
|
77
|
+
text: d,
|
78
|
+
attr: textAttrs[i],
|
79
|
+
style: textStyles[i]
|
80
|
+
}
|
81
|
+
})
|
82
|
+
}
|
83
|
+
|
84
|
+
function createEachPieEventTexts (pluginName: string, context: {
|
85
|
+
containerSelection: d3.Selection<SVGGElement, any, any, unknown>
|
86
|
+
computedData$: Observable<ComputedDatumSeries[][]>
|
87
|
+
containerComputedLayoutData$: Observable<ComputedDatumSeries[]>
|
88
|
+
SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
|
89
|
+
fullParams$: Observable<PieEventTextsParams>
|
90
|
+
fullChartParams$: Observable<ChartParams>
|
91
|
+
seriesHighlight$: Observable<ComputedDatumSeries[]>
|
92
|
+
seriesContainerPosition$: Observable<SeriesContainerPosition>
|
93
|
+
event$: Subject<EventSeries>
|
94
|
+
}) {
|
95
|
+
const destroy$ = new Subject()
|
96
|
+
|
97
|
+
// const graphicSelection: d3.Selection<SVGGElement, any, any, any> = selection.append('g')
|
98
|
+
let centerTextSelection: d3.Selection<SVGTextElement, TextDatum, SVGGElement, unknown> | undefined
|
99
|
+
let storeEventSubscription: Subscription | undefined
|
100
|
+
|
101
|
+
// context.layout$
|
102
|
+
// .pipe(
|
103
|
+
// first()
|
104
|
+
// )
|
105
|
+
// .subscribe(size => {
|
106
|
+
// selection
|
107
|
+
// .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
|
108
|
+
// context.layout$
|
109
|
+
// .pipe(
|
110
|
+
// takeUntil(destroy$)
|
111
|
+
// )
|
112
|
+
// .subscribe(size => {
|
113
|
+
// selection
|
114
|
+
// .transition()
|
115
|
+
// .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
|
116
|
+
// })
|
117
|
+
// })
|
118
|
+
|
119
|
+
const highlightTarget$ = context.fullChartParams$.pipe(
|
120
|
+
takeUntil(destroy$),
|
121
|
+
map(d => d.highlightTarget),
|
122
|
+
distinctUntilChanged()
|
123
|
+
)
|
124
|
+
|
125
|
+
combineLatest({
|
126
|
+
computedData: context.computedData$,
|
127
|
+
fullParams: context.fullParams$,
|
128
|
+
fullChartParams: context.fullChartParams$,
|
129
|
+
highlightTarget: highlightTarget$,
|
130
|
+
}).pipe(
|
131
|
+
takeUntil(destroy$),
|
132
|
+
switchMap(async (d) => d),
|
133
|
+
).subscribe(data => {
|
134
|
+
|
135
|
+
context.containerSelection
|
136
|
+
.transition('move')
|
137
|
+
.duration(data.fullChartParams.transitionDuration!)
|
138
|
+
// .ease(getD3TransitionEase(data.fullChartParams.transitionEase!))
|
139
|
+
.tween('move', (event, datum) => {
|
140
|
+
return (t) => {
|
141
|
+
const renderData = makeTextData({
|
142
|
+
eventData: {
|
143
|
+
type: 'series',
|
144
|
+
pluginName,
|
145
|
+
eventName: 'transitionMove',
|
146
|
+
event,
|
147
|
+
highlightTarget: data.highlightTarget,
|
148
|
+
data: data.computedData,
|
149
|
+
series: [],
|
150
|
+
seriesIndex: -1,
|
151
|
+
seriesLabel: '',
|
152
|
+
datum: null
|
153
|
+
},
|
154
|
+
eventName: 'transitionMove',
|
155
|
+
t,
|
156
|
+
eventFn: data.fullParams.eventFn!,
|
157
|
+
textAttrs: data.fullParams.textAttrs!,
|
158
|
+
textStyles: data.fullParams.textStyles!
|
159
|
+
})
|
160
|
+
centerTextSelection = renderText(context.containerSelection, renderData)
|
161
|
+
}
|
162
|
+
})
|
163
|
+
.on('end', (event, datum) => {
|
164
|
+
const renderData = makeTextData({
|
165
|
+
eventData: {
|
166
|
+
type: 'series',
|
167
|
+
pluginName,
|
168
|
+
eventName: 'transitionEnd',
|
169
|
+
event,
|
170
|
+
highlightTarget: data.highlightTarget,
|
171
|
+
data: data.computedData,
|
172
|
+
series: [],
|
173
|
+
seriesIndex: -1,
|
174
|
+
seriesLabel: '',
|
175
|
+
datum: null
|
176
|
+
},
|
177
|
+
eventName: 'transitionMove',
|
178
|
+
t: 1,
|
179
|
+
eventFn: data.fullParams.eventFn!,
|
180
|
+
textAttrs: data.fullParams.textAttrs!,
|
181
|
+
textStyles: data.fullParams.textStyles!
|
182
|
+
})
|
183
|
+
centerTextSelection = renderText(context.containerSelection, renderData)
|
184
|
+
|
185
|
+
if (storeEventSubscription) {
|
186
|
+
storeEventSubscription.unsubscribe()
|
187
|
+
}
|
188
|
+
storeEventSubscription = context.event$
|
189
|
+
.subscribe(eventData => {
|
190
|
+
const renderData = makeTextData({
|
191
|
+
eventData,
|
192
|
+
eventName: eventData.eventName,
|
193
|
+
t: 1,
|
194
|
+
eventFn: data.fullParams.eventFn!,
|
195
|
+
textAttrs: data.fullParams.textAttrs!,
|
196
|
+
textStyles: data.fullParams.textStyles!
|
197
|
+
})
|
198
|
+
centerTextSelection = renderText(context.containerSelection, renderData)
|
199
|
+
})
|
200
|
+
})
|
201
|
+
})
|
202
|
+
|
203
|
+
return () => {
|
204
|
+
destroy$.next(undefined)
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
208
|
+
export const PieEventTexts = defineSeriesPlugin(pluginName, DEFAULT_PIE_EVENT_TEXTS_PARAMS)(({ selection, name, observer, subject }) => {
|
209
|
+
const destroy$ = new Subject()
|
210
|
+
|
211
|
+
const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
|
212
|
+
selection: selection,
|
213
|
+
pluginName,
|
214
|
+
separateSeries$: observer.separateSeries$,
|
215
|
+
seriesLabels$: observer.seriesLabels$,
|
216
|
+
seriesContainerPosition$: observer.seriesContainerPosition$
|
217
|
+
})
|
218
|
+
|
219
|
+
const unsubscribeFnArr: (() => void)[] = []
|
220
|
+
|
221
|
+
seriesCenterSelection$.subscribe(seriesCenterSelection => {
|
222
|
+
// 每次重新計算時,清除之前的訂閱
|
223
|
+
unsubscribeFnArr.forEach(fn => fn())
|
224
|
+
|
225
|
+
seriesCenterSelection.each((d, containerIndex, g) => {
|
226
|
+
|
227
|
+
const containerSelection = d3.select(g[containerIndex])
|
228
|
+
|
229
|
+
const containerComputedLayoutData$ = observer.computedLayoutData$.pipe(
|
230
|
+
takeUntil(destroy$),
|
231
|
+
map(data => data[containerIndex] ?? data[0])
|
232
|
+
)
|
233
|
+
|
234
|
+
const containerPosition$ = observer.seriesContainerPosition$.pipe(
|
235
|
+
takeUntil(destroy$),
|
236
|
+
map(data => data[containerIndex] ?? data[0])
|
237
|
+
)
|
238
|
+
|
239
|
+
unsubscribeFnArr[containerIndex] = createEachPieEventTexts(pluginName, {
|
240
|
+
containerSelection: containerSelection,
|
241
|
+
computedData$: observer.computedData$,
|
242
|
+
containerComputedLayoutData$: containerComputedLayoutData$,
|
243
|
+
SeriesDataMap$: observer.SeriesDataMap$,
|
244
|
+
fullParams$: observer.fullParams$,
|
245
|
+
fullChartParams$: observer.fullChartParams$,
|
246
|
+
seriesHighlight$: observer.seriesHighlight$,
|
247
|
+
seriesContainerPosition$: containerPosition$,
|
248
|
+
event$: subject.event$,
|
249
|
+
})
|
250
|
+
|
251
|
+
})
|
252
|
+
})
|
253
|
+
|
254
|
+
return () => {
|
255
|
+
destroy$.next(undefined)
|
256
|
+
unsubscribeFnArr.forEach(fn => fn())
|
257
|
+
}
|
258
|
+
})
|