@orbcharts/plugins-basic 3.0.0-alpha.50 → 3.0.0-alpha.52
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 +7234 -7303
- package/dist/orbcharts-plugins-basic.umd.js +9 -9
- package/dist/src/base/BaseGroupAxis.d.ts +1 -0
- package/dist/src/grid/index.d.ts +1 -1
- package/dist/src/grid/plugins/BarsPN.d.ts +1 -0
- package/dist/src/grid/types.d.ts +3 -2
- package/dist/src/series/types.d.ts +2 -3
- 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 +513 -502
- package/src/base/BaseGroupAxis.ts +558 -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 +125 -123
- package/src/grid/gridObservables.ts +248 -248
- package/src/grid/index.ts +15 -15
- package/src/grid/plugins/BarStack.ts +43 -43
- package/src/grid/plugins/Bars.ts +44 -44
- package/src/grid/plugins/{BarsDiverging.ts → BarsPN.ts} +41 -41
- package/src/grid/plugins/BarsTriangle.ts +42 -42
- package/src/grid/plugins/Dots.ts +37 -37
- package/src/grid/plugins/GridLegend.ts +59 -59
- package/src/grid/plugins/GroupAux.ts +922 -645
- package/src/grid/plugins/GroupAxis.ts +35 -35
- 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 +36 -36
- package/src/grid/plugins/ValueStackAxis.ts +38 -38
- package/src/grid/types.ts +122 -120
- package/src/index.ts +9 -9
- package/src/multiGrid/defaults.ts +153 -152
- package/src/multiGrid/index.ts +13 -13
- package/src/multiGrid/multiGridObservables.ts +44 -44
- package/src/multiGrid/plugins/MultiBarStack.ts +78 -78
- package/src/multiGrid/plugins/MultiBars.ts +77 -77
- package/src/multiGrid/plugins/MultiBarsTriangle.ts +77 -77
- package/src/multiGrid/plugins/MultiDots.ts +65 -65
- package/src/multiGrid/plugins/MultiGridLegend.ts +89 -89
- package/src/multiGrid/plugins/MultiGroupAxis.ts +69 -69
- package/src/multiGrid/plugins/MultiLineAreas.ts +67 -67
- package/src/multiGrid/plugins/MultiLines.ts +66 -66
- package/src/multiGrid/plugins/MultiValueAxis.ts +69 -69
- package/src/multiGrid/plugins/MultiValueStackAxis.ts +69 -69
- package/src/multiGrid/plugins/OverlappingValueAxes.ts +166 -166
- package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +167 -167
- package/src/multiGrid/types.ts +71 -71
- 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 +126 -126
- package/src/series/index.ts +9 -9
- package/src/series/plugins/Bubbles.ts +545 -602
- package/src/series/plugins/Pie.ts +576 -576
- package/src/series/plugins/PieEventTexts.ts +262 -262
- package/src/series/plugins/PieLabels.ts +304 -304
- package/src/series/plugins/Rose.ts +472 -472
- package/src/series/plugins/RoseLabels.ts +362 -362
- package/src/series/plugins/SeriesLegend.ts +59 -59
- package/src/series/seriesObservables.ts +145 -145
- package/src/series/seriesUtils.ts +51 -51
- package/src/series/types.ts +83 -83
- 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 -13
- package/tsconfig.prod.json +13 -13
- package/vite.config.js +49 -49
- package/dist/src/grid/plugins/BarsDiverging.d.ts +0 -1
@@ -1,304 +1,304 @@
|
|
1
|
-
import * as d3 from 'd3'
|
2
|
-
import {
|
3
|
-
combineLatest,
|
4
|
-
switchMap,
|
5
|
-
first,
|
6
|
-
map,
|
7
|
-
takeUntil,
|
8
|
-
Observable,
|
9
|
-
distinctUntilChanged,
|
10
|
-
Subject,
|
11
|
-
BehaviorSubject } from 'rxjs'
|
12
|
-
import {
|
13
|
-
defineSeriesPlugin } from '@orbcharts/core'
|
14
|
-
import type {
|
15
|
-
ComputedDatumSeries,
|
16
|
-
SeriesContainerPosition,
|
17
|
-
EventSeries,
|
18
|
-
ChartParams } from '@orbcharts/core'
|
19
|
-
import type { PieLabelsParams } from '../types'
|
20
|
-
import type { PieDatum } from '../seriesUtils'
|
21
|
-
import { DEFAULT_PIE_LABELS_PARAMS } from '../defaults'
|
22
|
-
import { makePieData } from '../seriesUtils'
|
23
|
-
import { makeD3Arc } from '../../utils/d3Utils'
|
24
|
-
import { getDatumColor, getClassName } from '../../utils/orbchartsUtils'
|
25
|
-
import { seriesCenterSelectionObservable } from '../seriesObservables'
|
26
|
-
|
27
|
-
interface RenderDatum {
|
28
|
-
pieDatum: PieDatum
|
29
|
-
arcIndex: number
|
30
|
-
arcLabel: string
|
31
|
-
x: number
|
32
|
-
y: number
|
33
|
-
mouseoverX: number
|
34
|
-
mouseoverY: number
|
35
|
-
}
|
36
|
-
|
37
|
-
const pluginName = 'PieLabels'
|
38
|
-
const textClassName = getClassName(pluginName, 'text')
|
39
|
-
|
40
|
-
function makeRenderData (pieData: PieDatum[], arc: d3.Arc<any, d3.DefaultArcObject>, mouseoverArc: d3.Arc<any, d3.DefaultArcObject>, centroid: number): RenderDatum[] {
|
41
|
-
return pieData
|
42
|
-
.map((d, i) => {
|
43
|
-
const [_x, _y] = arc!.centroid(d as any)
|
44
|
-
const [_mouseoverX, _mouseoverY] = mouseoverArc!.centroid(d as any)
|
45
|
-
return {
|
46
|
-
pieDatum: d,
|
47
|
-
arcIndex: i,
|
48
|
-
arcLabel: d.data.label,
|
49
|
-
x: _x * centroid!,
|
50
|
-
y: _y * centroid!,
|
51
|
-
mouseoverX: _mouseoverX * centroid!,
|
52
|
-
mouseoverY: _mouseoverY * centroid!
|
53
|
-
}
|
54
|
-
})
|
55
|
-
.filter(d => d.pieDatum.data.visible)
|
56
|
-
}
|
57
|
-
|
58
|
-
// 繪製圓餅圖
|
59
|
-
function renderLabel (selection: d3.Selection<SVGGElement, undefined, any, any>, data: RenderDatum[], pluginParams: PieLabelsParams, fullChartParams: ChartParams) {
|
60
|
-
// console.log(data)
|
61
|
-
// let update = this.gSelection.selectAll('g').data(pieData)
|
62
|
-
let update: d3.Selection<SVGPathElement, RenderDatum, any, any> = selection
|
63
|
-
.selectAll<SVGPathElement, RenderDatum>('text')
|
64
|
-
.data(data, d => d.pieDatum.id)
|
65
|
-
let enter = update.enter()
|
66
|
-
.append<SVGPathElement>('text')
|
67
|
-
.classed(textClassName, true)
|
68
|
-
let exit = update.exit()
|
69
|
-
|
70
|
-
enter
|
71
|
-
.append('text')
|
72
|
-
|
73
|
-
const labelSelection = update.merge(enter)
|
74
|
-
labelSelection
|
75
|
-
.attr('font-weight', 'bold')
|
76
|
-
.attr('text-anchor', 'middle')
|
77
|
-
.style('dominant-baseline', 'middle')
|
78
|
-
// .style('pointer-events', 'none')
|
79
|
-
.style('cursor', d => fullChartParams.highlightTarget && fullChartParams.highlightTarget != 'none'
|
80
|
-
? 'pointer'
|
81
|
-
: 'none')
|
82
|
-
// .text((d, i) => d.arcLabel)
|
83
|
-
.text(d => pluginParams.labelFn(d.pieDatum.data))
|
84
|
-
.attr('font-size', fullChartParams.styles.textSize)
|
85
|
-
.attr('fill', (d, i) => getDatumColor({ datum: d.pieDatum.data, colorType: pluginParams.labelColorType, fullChartParams }))
|
86
|
-
.transition()
|
87
|
-
.attr('transform', (d) => {
|
88
|
-
// console.log('transform', d)
|
89
|
-
return 'translate(' + d.x + ',' + d.y + ')'
|
90
|
-
})
|
91
|
-
// .on('end', () => initHighlight({ labelSelection, data, fullChartParams }))
|
92
|
-
exit.remove()
|
93
|
-
|
94
|
-
// 如無新增資料則不用等動畫
|
95
|
-
// if (enter.size() == 0) {
|
96
|
-
// this.initHighlight()
|
97
|
-
// }
|
98
|
-
|
99
|
-
return labelSelection
|
100
|
-
}
|
101
|
-
|
102
|
-
// function initHighlight ({ labelSelection, data, fullChartParams }: {
|
103
|
-
// labelSelection: (d3.Selection<SVGPathElement, RenderDatum, any, any>)
|
104
|
-
// data: RenderDatum[]
|
105
|
-
// fullChartParams: ChartParams
|
106
|
-
// }) {
|
107
|
-
// removeHighlight({ labelSelection })
|
108
|
-
// // if (fullParams.highlightSeriesId || fullParams.highlightDatumId) {
|
109
|
-
// highlight({
|
110
|
-
// labelSelection,
|
111
|
-
// data,
|
112
|
-
// id: fullChartParams.highlightDefault,
|
113
|
-
// label: fullChartParams.highlightDefault,
|
114
|
-
// fullChartParams
|
115
|
-
// })
|
116
|
-
// // }
|
117
|
-
// }
|
118
|
-
|
119
|
-
function highlight ({ labelSelection, ids, fullChartParams }: {
|
120
|
-
labelSelection: (d3.Selection<SVGPathElement, RenderDatum, any, any>)
|
121
|
-
ids: string[]
|
122
|
-
fullChartParams: ChartParams
|
123
|
-
}) {
|
124
|
-
labelSelection.interrupt('highlight')
|
125
|
-
|
126
|
-
if (!ids.length) {
|
127
|
-
labelSelection
|
128
|
-
.transition()
|
129
|
-
.duration(200)
|
130
|
-
.attr('transform', (d) => {
|
131
|
-
return 'translate(' + d.x + ',' + d.y + ')'
|
132
|
-
})
|
133
|
-
.style('opacity', 1)
|
134
|
-
return
|
135
|
-
}
|
136
|
-
|
137
|
-
labelSelection.each((d, i, n) => {
|
138
|
-
const segment = d3.select<SVGPathElement, RenderDatum>(n[i])
|
139
|
-
|
140
|
-
if (ids.includes(d.pieDatum.id)) {
|
141
|
-
segment
|
142
|
-
.style('opacity', 1)
|
143
|
-
.transition()
|
144
|
-
.duration(200)
|
145
|
-
.attr('transform', (d) => {
|
146
|
-
return 'translate(' + d.mouseoverX + ',' + d.mouseoverY + ')'
|
147
|
-
})
|
148
|
-
} else {
|
149
|
-
segment
|
150
|
-
.style('opacity', fullChartParams.styles.unhighlightedOpacity)
|
151
|
-
.transition()
|
152
|
-
.duration(200)
|
153
|
-
.attr('transform', (d) => {
|
154
|
-
return 'translate(' + d.x + ',' + d.y + ')'
|
155
|
-
})
|
156
|
-
}
|
157
|
-
})
|
158
|
-
}
|
159
|
-
|
160
|
-
|
161
|
-
function createEachPieLabel (pluginName: string, context: {
|
162
|
-
containerSelection: d3.Selection<SVGGElement, any, any, unknown>
|
163
|
-
// computedData$: Observable<ComputedDatumSeries[][]>
|
164
|
-
containerVisibleComputedLayoutData$: Observable<ComputedDatumSeries[]>
|
165
|
-
// SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
|
166
|
-
fullParams$: Observable<PieLabelsParams>
|
167
|
-
fullChartParams$: Observable<ChartParams>
|
168
|
-
seriesHighlight$: Observable<ComputedDatumSeries[]>
|
169
|
-
seriesContainerPosition$: Observable<SeriesContainerPosition>
|
170
|
-
event$: Subject<EventSeries>
|
171
|
-
}) {
|
172
|
-
const destroy$ = new Subject()
|
173
|
-
|
174
|
-
// const graphicSelection: d3.Selection<SVGGElement, any, any, any> = selection.append('g')
|
175
|
-
let labelSelection$: Subject<d3.Selection<SVGPathElement, RenderDatum, any, any>> = new Subject()
|
176
|
-
let renderData: RenderDatum[] = []
|
177
|
-
|
178
|
-
const shorterSideWith$ = context.seriesContainerPosition$.pipe(
|
179
|
-
takeUntil(destroy$),
|
180
|
-
map(d => d.width < d.height ? d.width : d.height),
|
181
|
-
distinctUntilChanged()
|
182
|
-
)
|
183
|
-
|
184
|
-
combineLatest({
|
185
|
-
shorterSideWith: shorterSideWith$,
|
186
|
-
containerVisibleComputedLayoutData: context.containerVisibleComputedLayoutData$,
|
187
|
-
fullParams: context.fullParams$,
|
188
|
-
fullChartParams: context.fullChartParams$
|
189
|
-
}).pipe(
|
190
|
-
takeUntil(destroy$),
|
191
|
-
switchMap(async (d) => d),
|
192
|
-
).subscribe(data => {
|
193
|
-
|
194
|
-
// const shorterSideWith = data.layout.width < data.layout.height ? data.layout.width : data.layout.height
|
195
|
-
|
196
|
-
// 弧產生器 (d3.arc())
|
197
|
-
const arc = makeD3Arc({
|
198
|
-
axisWidth: data.shorterSideWith,
|
199
|
-
innerRadius: 0,
|
200
|
-
outerRadius: data.fullParams.outerRadius,
|
201
|
-
padAngle: 0,
|
202
|
-
cornerRadius: 0
|
203
|
-
})
|
204
|
-
|
205
|
-
const arcMouseover = makeD3Arc({
|
206
|
-
axisWidth: data.shorterSideWith,
|
207
|
-
innerRadius: 0,
|
208
|
-
outerRadius: data.fullParams.
|
209
|
-
padAngle: 0,
|
210
|
-
cornerRadius: 0
|
211
|
-
})
|
212
|
-
|
213
|
-
const pieData = makePieData({
|
214
|
-
data: data.containerVisibleComputedLayoutData,
|
215
|
-
startAngle: data.fullParams.startAngle,
|
216
|
-
endAngle: data.fullParams.endAngle
|
217
|
-
})
|
218
|
-
|
219
|
-
renderData = makeRenderData(pieData, arc, arcMouseover, data.fullParams.labelCentroid)
|
220
|
-
|
221
|
-
const labelSelection = renderLabel(context.containerSelection, renderData, data.fullParams, data.fullChartParams)
|
222
|
-
|
223
|
-
labelSelection$.next(labelSelection)
|
224
|
-
|
225
|
-
})
|
226
|
-
|
227
|
-
combineLatest({
|
228
|
-
labelSelection: labelSelection$,
|
229
|
-
highlight: context.seriesHighlight$.pipe(
|
230
|
-
map(data => data.map(d => d.id))
|
231
|
-
),
|
232
|
-
fullChartParams: context.fullChartParams$,
|
233
|
-
}).pipe(
|
234
|
-
takeUntil(destroy$),
|
235
|
-
switchMap(async d => d)
|
236
|
-
).subscribe(data => {
|
237
|
-
highlight({
|
238
|
-
labelSelection: data.labelSelection,
|
239
|
-
ids: data.highlight,
|
240
|
-
fullChartParams: data.fullChartParams,
|
241
|
-
})
|
242
|
-
})
|
243
|
-
|
244
|
-
return () => {
|
245
|
-
destroy$.next(undefined)
|
246
|
-
}
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
export const PieLabels = defineSeriesPlugin(pluginName, DEFAULT_PIE_LABELS_PARAMS)(({ selection, observer, subject }) => {
|
251
|
-
|
252
|
-
const destroy$ = new Subject()
|
253
|
-
|
254
|
-
const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
|
255
|
-
selection: selection,
|
256
|
-
pluginName,
|
257
|
-
separateSeries$: observer.separateSeries$,
|
258
|
-
seriesLabels$: observer.seriesLabels$,
|
259
|
-
seriesContainerPosition$: observer.seriesContainerPosition$
|
260
|
-
})
|
261
|
-
|
262
|
-
const unsubscribeFnArr: (() => void)[] = []
|
263
|
-
|
264
|
-
seriesCenterSelection$
|
265
|
-
.pipe(
|
266
|
-
takeUntil(destroy$)
|
267
|
-
)
|
268
|
-
.subscribe(seriesCenterSelection => {
|
269
|
-
// 每次重新計算時,清除之前的訂閱
|
270
|
-
unsubscribeFnArr.forEach(fn => fn())
|
271
|
-
|
272
|
-
seriesCenterSelection.each((d, containerIndex, g) => {
|
273
|
-
|
274
|
-
const containerSelection = d3.select(g[containerIndex])
|
275
|
-
|
276
|
-
const containerVisibleComputedLayoutData$ = observer.visibleComputedLayoutData$.pipe(
|
277
|
-
takeUntil(destroy$),
|
278
|
-
map(data => data[containerIndex] ?? data[0])
|
279
|
-
)
|
280
|
-
|
281
|
-
const containerPosition$ = observer.seriesContainerPosition$.pipe(
|
282
|
-
takeUntil(destroy$),
|
283
|
-
map(data => data[containerIndex] ?? data[0])
|
284
|
-
)
|
285
|
-
|
286
|
-
unsubscribeFnArr[containerIndex] = createEachPieLabel(pluginName, {
|
287
|
-
containerSelection: containerSelection,
|
288
|
-
// computedData$: observer.computedData$,
|
289
|
-
containerVisibleComputedLayoutData$: containerVisibleComputedLayoutData$,
|
290
|
-
// SeriesDataMap$: observer.SeriesDataMap$,
|
291
|
-
fullParams$: observer.fullParams$,
|
292
|
-
fullChartParams$: observer.fullChartParams$,
|
293
|
-
seriesHighlight$: observer.seriesHighlight$,
|
294
|
-
seriesContainerPosition$: containerPosition$,
|
295
|
-
event$: subject.event$,
|
296
|
-
})
|
297
|
-
|
298
|
-
})
|
299
|
-
})
|
300
|
-
|
301
|
-
return () => {
|
302
|
-
destroy$.next(undefined)
|
303
|
-
}
|
304
|
-
})
|
1
|
+
import * as d3 from 'd3'
|
2
|
+
import {
|
3
|
+
combineLatest,
|
4
|
+
switchMap,
|
5
|
+
first,
|
6
|
+
map,
|
7
|
+
takeUntil,
|
8
|
+
Observable,
|
9
|
+
distinctUntilChanged,
|
10
|
+
Subject,
|
11
|
+
BehaviorSubject } from 'rxjs'
|
12
|
+
import {
|
13
|
+
defineSeriesPlugin } from '@orbcharts/core'
|
14
|
+
import type {
|
15
|
+
ComputedDatumSeries,
|
16
|
+
SeriesContainerPosition,
|
17
|
+
EventSeries,
|
18
|
+
ChartParams } from '@orbcharts/core'
|
19
|
+
import type { PieLabelsParams } from '../types'
|
20
|
+
import type { PieDatum } from '../seriesUtils'
|
21
|
+
import { DEFAULT_PIE_LABELS_PARAMS } from '../defaults'
|
22
|
+
import { makePieData } from '../seriesUtils'
|
23
|
+
import { makeD3Arc } from '../../utils/d3Utils'
|
24
|
+
import { getDatumColor, getClassName } from '../../utils/orbchartsUtils'
|
25
|
+
import { seriesCenterSelectionObservable } from '../seriesObservables'
|
26
|
+
|
27
|
+
interface RenderDatum {
|
28
|
+
pieDatum: PieDatum
|
29
|
+
arcIndex: number
|
30
|
+
arcLabel: string
|
31
|
+
x: number
|
32
|
+
y: number
|
33
|
+
mouseoverX: number
|
34
|
+
mouseoverY: number
|
35
|
+
}
|
36
|
+
|
37
|
+
const pluginName = 'PieLabels'
|
38
|
+
const textClassName = getClassName(pluginName, 'text')
|
39
|
+
|
40
|
+
function makeRenderData (pieData: PieDatum[], arc: d3.Arc<any, d3.DefaultArcObject>, mouseoverArc: d3.Arc<any, d3.DefaultArcObject>, centroid: number): RenderDatum[] {
|
41
|
+
return pieData
|
42
|
+
.map((d, i) => {
|
43
|
+
const [_x, _y] = arc!.centroid(d as any)
|
44
|
+
const [_mouseoverX, _mouseoverY] = mouseoverArc!.centroid(d as any)
|
45
|
+
return {
|
46
|
+
pieDatum: d,
|
47
|
+
arcIndex: i,
|
48
|
+
arcLabel: d.data.label,
|
49
|
+
x: _x * centroid!,
|
50
|
+
y: _y * centroid!,
|
51
|
+
mouseoverX: _mouseoverX * centroid!,
|
52
|
+
mouseoverY: _mouseoverY * centroid!
|
53
|
+
}
|
54
|
+
})
|
55
|
+
.filter(d => d.pieDatum.data.visible)
|
56
|
+
}
|
57
|
+
|
58
|
+
// 繪製圓餅圖
|
59
|
+
function renderLabel (selection: d3.Selection<SVGGElement, undefined, any, any>, data: RenderDatum[], pluginParams: PieLabelsParams, fullChartParams: ChartParams) {
|
60
|
+
// console.log(data)
|
61
|
+
// let update = this.gSelection.selectAll('g').data(pieData)
|
62
|
+
let update: d3.Selection<SVGPathElement, RenderDatum, any, any> = selection
|
63
|
+
.selectAll<SVGPathElement, RenderDatum>('text')
|
64
|
+
.data(data, d => d.pieDatum.id)
|
65
|
+
let enter = update.enter()
|
66
|
+
.append<SVGPathElement>('text')
|
67
|
+
.classed(textClassName, true)
|
68
|
+
let exit = update.exit()
|
69
|
+
|
70
|
+
enter
|
71
|
+
.append('text')
|
72
|
+
|
73
|
+
const labelSelection = update.merge(enter)
|
74
|
+
labelSelection
|
75
|
+
.attr('font-weight', 'bold')
|
76
|
+
.attr('text-anchor', 'middle')
|
77
|
+
.style('dominant-baseline', 'middle')
|
78
|
+
// .style('pointer-events', 'none')
|
79
|
+
.style('cursor', d => fullChartParams.highlightTarget && fullChartParams.highlightTarget != 'none'
|
80
|
+
? 'pointer'
|
81
|
+
: 'none')
|
82
|
+
// .text((d, i) => d.arcLabel)
|
83
|
+
.text(d => pluginParams.labelFn(d.pieDatum.data))
|
84
|
+
.attr('font-size', fullChartParams.styles.textSize)
|
85
|
+
.attr('fill', (d, i) => getDatumColor({ datum: d.pieDatum.data, colorType: pluginParams.labelColorType, fullChartParams }))
|
86
|
+
.transition()
|
87
|
+
.attr('transform', (d) => {
|
88
|
+
// console.log('transform', d)
|
89
|
+
return 'translate(' + d.x + ',' + d.y + ')'
|
90
|
+
})
|
91
|
+
// .on('end', () => initHighlight({ labelSelection, data, fullChartParams }))
|
92
|
+
exit.remove()
|
93
|
+
|
94
|
+
// 如無新增資料則不用等動畫
|
95
|
+
// if (enter.size() == 0) {
|
96
|
+
// this.initHighlight()
|
97
|
+
// }
|
98
|
+
|
99
|
+
return labelSelection
|
100
|
+
}
|
101
|
+
|
102
|
+
// function initHighlight ({ labelSelection, data, fullChartParams }: {
|
103
|
+
// labelSelection: (d3.Selection<SVGPathElement, RenderDatum, any, any>)
|
104
|
+
// data: RenderDatum[]
|
105
|
+
// fullChartParams: ChartParams
|
106
|
+
// }) {
|
107
|
+
// removeHighlight({ labelSelection })
|
108
|
+
// // if (fullParams.highlightSeriesId || fullParams.highlightDatumId) {
|
109
|
+
// highlight({
|
110
|
+
// labelSelection,
|
111
|
+
// data,
|
112
|
+
// id: fullChartParams.highlightDefault,
|
113
|
+
// label: fullChartParams.highlightDefault,
|
114
|
+
// fullChartParams
|
115
|
+
// })
|
116
|
+
// // }
|
117
|
+
// }
|
118
|
+
|
119
|
+
function highlight ({ labelSelection, ids, fullChartParams }: {
|
120
|
+
labelSelection: (d3.Selection<SVGPathElement, RenderDatum, any, any>)
|
121
|
+
ids: string[]
|
122
|
+
fullChartParams: ChartParams
|
123
|
+
}) {
|
124
|
+
labelSelection.interrupt('highlight')
|
125
|
+
|
126
|
+
if (!ids.length) {
|
127
|
+
labelSelection
|
128
|
+
.transition()
|
129
|
+
.duration(200)
|
130
|
+
.attr('transform', (d) => {
|
131
|
+
return 'translate(' + d.x + ',' + d.y + ')'
|
132
|
+
})
|
133
|
+
.style('opacity', 1)
|
134
|
+
return
|
135
|
+
}
|
136
|
+
|
137
|
+
labelSelection.each((d, i, n) => {
|
138
|
+
const segment = d3.select<SVGPathElement, RenderDatum>(n[i])
|
139
|
+
|
140
|
+
if (ids.includes(d.pieDatum.id)) {
|
141
|
+
segment
|
142
|
+
.style('opacity', 1)
|
143
|
+
.transition()
|
144
|
+
.duration(200)
|
145
|
+
.attr('transform', (d) => {
|
146
|
+
return 'translate(' + d.mouseoverX + ',' + d.mouseoverY + ')'
|
147
|
+
})
|
148
|
+
} else {
|
149
|
+
segment
|
150
|
+
.style('opacity', fullChartParams.styles.unhighlightedOpacity)
|
151
|
+
.transition()
|
152
|
+
.duration(200)
|
153
|
+
.attr('transform', (d) => {
|
154
|
+
return 'translate(' + d.x + ',' + d.y + ')'
|
155
|
+
})
|
156
|
+
}
|
157
|
+
})
|
158
|
+
}
|
159
|
+
|
160
|
+
|
161
|
+
function createEachPieLabel (pluginName: string, context: {
|
162
|
+
containerSelection: d3.Selection<SVGGElement, any, any, unknown>
|
163
|
+
// computedData$: Observable<ComputedDatumSeries[][]>
|
164
|
+
containerVisibleComputedLayoutData$: Observable<ComputedDatumSeries[]>
|
165
|
+
// SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
|
166
|
+
fullParams$: Observable<PieLabelsParams>
|
167
|
+
fullChartParams$: Observable<ChartParams>
|
168
|
+
seriesHighlight$: Observable<ComputedDatumSeries[]>
|
169
|
+
seriesContainerPosition$: Observable<SeriesContainerPosition>
|
170
|
+
event$: Subject<EventSeries>
|
171
|
+
}) {
|
172
|
+
const destroy$ = new Subject()
|
173
|
+
|
174
|
+
// const graphicSelection: d3.Selection<SVGGElement, any, any, any> = selection.append('g')
|
175
|
+
let labelSelection$: Subject<d3.Selection<SVGPathElement, RenderDatum, any, any>> = new Subject()
|
176
|
+
let renderData: RenderDatum[] = []
|
177
|
+
|
178
|
+
const shorterSideWith$ = context.seriesContainerPosition$.pipe(
|
179
|
+
takeUntil(destroy$),
|
180
|
+
map(d => d.width < d.height ? d.width : d.height),
|
181
|
+
distinctUntilChanged()
|
182
|
+
)
|
183
|
+
|
184
|
+
combineLatest({
|
185
|
+
shorterSideWith: shorterSideWith$,
|
186
|
+
containerVisibleComputedLayoutData: context.containerVisibleComputedLayoutData$,
|
187
|
+
fullParams: context.fullParams$,
|
188
|
+
fullChartParams: context.fullChartParams$
|
189
|
+
}).pipe(
|
190
|
+
takeUntil(destroy$),
|
191
|
+
switchMap(async (d) => d),
|
192
|
+
).subscribe(data => {
|
193
|
+
|
194
|
+
// const shorterSideWith = data.layout.width < data.layout.height ? data.layout.width : data.layout.height
|
195
|
+
|
196
|
+
// 弧產生器 (d3.arc())
|
197
|
+
const arc = makeD3Arc({
|
198
|
+
axisWidth: data.shorterSideWith,
|
199
|
+
innerRadius: 0,
|
200
|
+
outerRadius: data.fullParams.outerRadius,
|
201
|
+
padAngle: 0,
|
202
|
+
cornerRadius: 0
|
203
|
+
})
|
204
|
+
|
205
|
+
const arcMouseover = makeD3Arc({
|
206
|
+
axisWidth: data.shorterSideWith,
|
207
|
+
innerRadius: 0,
|
208
|
+
outerRadius: data.fullParams.outerRadiusWhileHighlight, // 外半徑變化
|
209
|
+
padAngle: 0,
|
210
|
+
cornerRadius: 0
|
211
|
+
})
|
212
|
+
|
213
|
+
const pieData = makePieData({
|
214
|
+
data: data.containerVisibleComputedLayoutData,
|
215
|
+
startAngle: data.fullParams.startAngle,
|
216
|
+
endAngle: data.fullParams.endAngle
|
217
|
+
})
|
218
|
+
|
219
|
+
renderData = makeRenderData(pieData, arc, arcMouseover, data.fullParams.labelCentroid)
|
220
|
+
|
221
|
+
const labelSelection = renderLabel(context.containerSelection, renderData, data.fullParams, data.fullChartParams)
|
222
|
+
|
223
|
+
labelSelection$.next(labelSelection)
|
224
|
+
|
225
|
+
})
|
226
|
+
|
227
|
+
combineLatest({
|
228
|
+
labelSelection: labelSelection$,
|
229
|
+
highlight: context.seriesHighlight$.pipe(
|
230
|
+
map(data => data.map(d => d.id))
|
231
|
+
),
|
232
|
+
fullChartParams: context.fullChartParams$,
|
233
|
+
}).pipe(
|
234
|
+
takeUntil(destroy$),
|
235
|
+
switchMap(async d => d)
|
236
|
+
).subscribe(data => {
|
237
|
+
highlight({
|
238
|
+
labelSelection: data.labelSelection,
|
239
|
+
ids: data.highlight,
|
240
|
+
fullChartParams: data.fullChartParams,
|
241
|
+
})
|
242
|
+
})
|
243
|
+
|
244
|
+
return () => {
|
245
|
+
destroy$.next(undefined)
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
export const PieLabels = defineSeriesPlugin(pluginName, DEFAULT_PIE_LABELS_PARAMS)(({ selection, observer, subject }) => {
|
251
|
+
|
252
|
+
const destroy$ = new Subject()
|
253
|
+
|
254
|
+
const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
|
255
|
+
selection: selection,
|
256
|
+
pluginName,
|
257
|
+
separateSeries$: observer.separateSeries$,
|
258
|
+
seriesLabels$: observer.seriesLabels$,
|
259
|
+
seriesContainerPosition$: observer.seriesContainerPosition$
|
260
|
+
})
|
261
|
+
|
262
|
+
const unsubscribeFnArr: (() => void)[] = []
|
263
|
+
|
264
|
+
seriesCenterSelection$
|
265
|
+
.pipe(
|
266
|
+
takeUntil(destroy$)
|
267
|
+
)
|
268
|
+
.subscribe(seriesCenterSelection => {
|
269
|
+
// 每次重新計算時,清除之前的訂閱
|
270
|
+
unsubscribeFnArr.forEach(fn => fn())
|
271
|
+
|
272
|
+
seriesCenterSelection.each((d, containerIndex, g) => {
|
273
|
+
|
274
|
+
const containerSelection = d3.select(g[containerIndex])
|
275
|
+
|
276
|
+
const containerVisibleComputedLayoutData$ = observer.visibleComputedLayoutData$.pipe(
|
277
|
+
takeUntil(destroy$),
|
278
|
+
map(data => data[containerIndex] ?? data[0])
|
279
|
+
)
|
280
|
+
|
281
|
+
const containerPosition$ = observer.seriesContainerPosition$.pipe(
|
282
|
+
takeUntil(destroy$),
|
283
|
+
map(data => data[containerIndex] ?? data[0])
|
284
|
+
)
|
285
|
+
|
286
|
+
unsubscribeFnArr[containerIndex] = createEachPieLabel(pluginName, {
|
287
|
+
containerSelection: containerSelection,
|
288
|
+
// computedData$: observer.computedData$,
|
289
|
+
containerVisibleComputedLayoutData$: containerVisibleComputedLayoutData$,
|
290
|
+
// SeriesDataMap$: observer.SeriesDataMap$,
|
291
|
+
fullParams$: observer.fullParams$,
|
292
|
+
fullChartParams$: observer.fullChartParams$,
|
293
|
+
seriesHighlight$: observer.seriesHighlight$,
|
294
|
+
seriesContainerPosition$: containerPosition$,
|
295
|
+
event$: subject.event$,
|
296
|
+
})
|
297
|
+
|
298
|
+
})
|
299
|
+
})
|
300
|
+
|
301
|
+
return () => {
|
302
|
+
destroy$.next(undefined)
|
303
|
+
}
|
304
|
+
})
|