@orbcharts/plugins-basic 3.0.0-alpha.44 → 3.0.0-alpha.45

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. package/LICENSE +200 -200
  2. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBarStack.d.ts +6 -4
  3. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBars.d.ts +6 -4
  4. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseBarsTriangle.d.ts +7 -4
  5. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseDots.d.ts +5 -3
  6. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseGroupAxis.d.ts +3 -3
  7. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLineAreas.d.ts +6 -3
  8. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLines.d.ts +6 -3
  9. package/dist/{src → orbcharts-plugins-basic/src}/base/BaseValueAxis.d.ts +3 -3
  10. package/dist/{src → orbcharts-plugins-basic/src}/grid/gridObservables.d.ts +4 -4
  11. package/dist/orbcharts-plugins-basic/src/index.d.ts +5 -0
  12. package/dist/orbcharts-plugins-basic/src/multiGrid/multiGridObservables.d.ts +8 -0
  13. package/dist/orbcharts-plugins-basic/src/series/plugins/PieEventTexts.d.ts +3 -0
  14. package/dist/orbcharts-plugins-basic/src/series/seriesObservables.d.ts +21 -0
  15. package/dist/{src → orbcharts-plugins-basic/src}/series/seriesUtils.d.ts +3 -3
  16. package/dist/orbcharts-plugins-basic.es.js +7696 -7529
  17. package/dist/orbcharts-plugins-basic.umd.js +8 -8
  18. package/dist/src/index.d.ts +1 -5
  19. package/package.json +42 -42
  20. package/src/base/BaseBarStack.ts +778 -881
  21. package/src/base/BaseBars.ts +764 -750
  22. package/src/base/BaseBarsTriangle.ts +672 -659
  23. package/src/base/BaseDots.ts +502 -639
  24. package/src/base/BaseGroupAxis.ts +496 -496
  25. package/src/base/BaseLegend.ts +641 -641
  26. package/src/base/BaseLineAreas.ts +625 -621
  27. package/src/base/BaseLines.ts +699 -692
  28. package/src/base/BaseValueAxis.ts +478 -479
  29. package/src/base/types.ts +2 -2
  30. package/src/grid/defaults.ts +121 -121
  31. package/src/grid/gridObservables.ts +247 -263
  32. package/src/grid/index.ts +15 -15
  33. package/src/grid/plugins/BarStack.ts +50 -37
  34. package/src/grid/plugins/Bars.ts +51 -37
  35. package/src/grid/plugins/BarsDiverging.ts +41 -39
  36. package/src/grid/plugins/BarsTriangle.ts +50 -34
  37. package/src/grid/plugins/Dots.ts +37 -35
  38. package/src/grid/plugins/GridLegend.ts +59 -59
  39. package/src/grid/plugins/GroupAux.ts +645 -646
  40. package/src/grid/plugins/GroupAxis.ts +42 -30
  41. package/src/grid/plugins/LineAreas.ts +39 -36
  42. package/src/grid/plugins/Lines.ts +38 -35
  43. package/src/grid/plugins/ScalingArea.ts +173 -174
  44. package/src/grid/plugins/ValueAxis.ts +43 -31
  45. package/src/grid/plugins/ValueStackAxis.ts +79 -70
  46. package/src/grid/types.ts +120 -120
  47. package/src/index.ts +9 -9
  48. package/src/multiGrid/defaults.ts +147 -147
  49. package/src/multiGrid/index.ts +11 -11
  50. package/src/multiGrid/multiGridObservables.ts +42 -289
  51. package/src/multiGrid/plugins/MultiBarStack.ts +74 -60
  52. package/src/multiGrid/plugins/MultiBars.ts +73 -59
  53. package/src/multiGrid/plugins/MultiBarsTriangle.ts +73 -58
  54. package/src/multiGrid/plugins/MultiDots.ts +60 -58
  55. package/src/multiGrid/plugins/MultiGridLegend.ts +89 -89
  56. package/src/multiGrid/plugins/MultiGroupAxis.ts +65 -53
  57. package/src/multiGrid/plugins/MultiLineAreas.ts +62 -59
  58. package/src/multiGrid/plugins/MultiLines.ts +61 -58
  59. package/src/multiGrid/plugins/MultiValueAxis.ts +65 -53
  60. package/src/multiGrid/plugins/OverlappingValueAxes.ts +169 -164
  61. package/src/multiGrid/types.ts +67 -67
  62. package/src/noneData/defaults.ts +64 -61
  63. package/src/noneData/index.ts +3 -3
  64. package/src/noneData/plugins/Container.ts +10 -10
  65. package/src/noneData/plugins/Tooltip.ts +310 -310
  66. package/src/noneData/types.ts +26 -26
  67. package/src/series/defaults.ts +109 -99
  68. package/src/series/index.ts +6 -6
  69. package/src/series/plugins/Bubbles.ts +571 -551
  70. package/src/series/plugins/Pie.ts +548 -600
  71. package/src/series/plugins/PieEventTexts.ts +258 -194
  72. package/src/series/plugins/PieLabels.ts +335 -288
  73. package/src/series/plugins/SeriesLegend.ts +59 -59
  74. package/src/series/seriesObservables.ts +145 -0
  75. package/src/series/seriesUtils.ts +50 -50
  76. package/src/series/types.ts +67 -67
  77. package/src/tree/defaults.ts +22 -22
  78. package/src/tree/index.ts +3 -3
  79. package/src/tree/plugins/TreeLegend.ts +59 -59
  80. package/src/tree/plugins/TreeMap.ts +305 -305
  81. package/src/tree/types.ts +23 -23
  82. package/src/utils/commonUtils.ts +21 -21
  83. package/src/utils/d3Graphics.ts +124 -124
  84. package/src/utils/d3Utils.ts +73 -73
  85. package/src/utils/observables.ts +14 -14
  86. package/src/utils/orbchartsUtils.ts +100 -100
  87. package/tsconfig.dev.json +16 -16
  88. package/tsconfig.json +16 -13
  89. package/tsconfig.prod.json +13 -13
  90. package/vite.config.js +49 -49
  91. package/dist/src/multiGrid/multiGridObservables.d.ts +0 -12
  92. package/dist/src/series/plugins/PieEventTexts.d.ts +0 -1
  93. /package/dist/{src → orbcharts-plugins-basic/src}/base/BaseGroupArea.d.ts +0 -0
  94. /package/dist/{src → orbcharts-plugins-basic/src}/base/BaseLegend.d.ts +0 -0
  95. /package/dist/{src → orbcharts-plugins-basic/src}/base/types.d.ts +0 -0
  96. /package/dist/{src → orbcharts-plugins-basic/src}/grid/defaults.d.ts +0 -0
  97. /package/dist/{src → orbcharts-plugins-basic/src}/grid/index.d.ts +0 -0
  98. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarStack.d.ts +0 -0
  99. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Bars.d.ts +0 -0
  100. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarsDiverging.d.ts +0 -0
  101. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/BarsTriangle.d.ts +0 -0
  102. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Dots.d.ts +0 -0
  103. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GridLegend.d.ts +0 -0
  104. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GroupAux.d.ts +0 -0
  105. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/GroupAxis.d.ts +0 -0
  106. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/LineAreas.d.ts +0 -0
  107. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Lines.d.ts +0 -0
  108. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/Ranking.d.ts +0 -0
  109. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/RankingAxis.d.ts +0 -0
  110. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ScalingArea.d.ts +0 -0
  111. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ValueAxis.d.ts +0 -0
  112. /package/dist/{src → orbcharts-plugins-basic/src}/grid/plugins/ValueStackAxis.d.ts +0 -0
  113. /package/dist/{src → orbcharts-plugins-basic/src}/grid/types.d.ts +0 -0
  114. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/defaults.d.ts +0 -0
  115. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/index.d.ts +0 -0
  116. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBarStack.d.ts +0 -0
  117. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBars.d.ts +0 -0
  118. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiBarsTriangle.d.ts +0 -0
  119. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiDots.d.ts +0 -0
  120. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGridLegend.d.ts +0 -0
  121. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiGroupAxis.d.ts +0 -0
  122. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiLineAreas.d.ts +0 -0
  123. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiLines.d.ts +0 -0
  124. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/MultiValueAxis.d.ts +0 -0
  125. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/plugins/OverlappingValueAxes.d.ts +0 -0
  126. /package/dist/{src → orbcharts-plugins-basic/src}/multiGrid/types.d.ts +0 -0
  127. /package/dist/{src → orbcharts-plugins-basic/src}/multiValue/index.d.ts +0 -0
  128. /package/dist/{src → orbcharts-plugins-basic/src}/multiValue/plugins/Scatter.d.ts +0 -0
  129. /package/dist/{src → orbcharts-plugins-basic/src}/multiValue/plugins/ScatterAxes.d.ts +0 -0
  130. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/defaults.d.ts +0 -0
  131. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/index.d.ts +0 -0
  132. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/plugins/Container.d.ts +0 -0
  133. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/plugins/Tooltip.d.ts +0 -0
  134. /package/dist/{src → orbcharts-plugins-basic/src}/noneData/types.d.ts +0 -0
  135. /package/dist/{src → orbcharts-plugins-basic/src}/relationship/index.d.ts +0 -0
  136. /package/dist/{src → orbcharts-plugins-basic/src}/relationship/plugins/Relationship.d.ts +0 -0
  137. /package/dist/{src → orbcharts-plugins-basic/src}/series/defaults.d.ts +0 -0
  138. /package/dist/{src → orbcharts-plugins-basic/src}/series/index.d.ts +0 -0
  139. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Bubbles.d.ts +0 -0
  140. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Pie.d.ts +0 -0
  141. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/PieLabels.d.ts +0 -0
  142. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/SeriesLegend.d.ts +0 -0
  143. /package/dist/{src → orbcharts-plugins-basic/src}/series/plugins/Waffle.d.ts +0 -0
  144. /package/dist/{src → orbcharts-plugins-basic/src}/series/types.d.ts +0 -0
  145. /package/dist/{src → orbcharts-plugins-basic/src}/tree/defaults.d.ts +0 -0
  146. /package/dist/{src → orbcharts-plugins-basic/src}/tree/index.d.ts +0 -0
  147. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeLegend.d.ts +0 -0
  148. /package/dist/{src → orbcharts-plugins-basic/src}/tree/plugins/TreeMap.d.ts +0 -0
  149. /package/dist/{src → orbcharts-plugins-basic/src}/tree/types.d.ts +0 -0
  150. /package/dist/{src → orbcharts-plugins-basic/src}/utils/commonUtils.d.ts +0 -0
  151. /package/dist/{src → orbcharts-plugins-basic/src}/utils/d3Graphics.d.ts +0 -0
  152. /package/dist/{src → orbcharts-plugins-basic/src}/utils/d3Utils.d.ts +0 -0
  153. /package/dist/{src → orbcharts-plugins-basic/src}/utils/observables.d.ts +0 -0
  154. /package/dist/{src → orbcharts-plugins-basic/src}/utils/orbchartsUtils.d.ts +0 -0
  155. /package/dist/{vite.config.d.ts → orbcharts-plugins-basic/vite.config.d.ts} +0 -0
@@ -1,601 +1,549 @@
1
- import * as d3 from 'd3'
2
- import {
3
- of,
4
- combineLatest,
5
- map,
6
- merge,
7
- take,
8
- filter,
9
- switchMap,
10
- first,
11
- takeUntil,
12
- distinctUntilChanged,
13
- BehaviorSubject,
14
- Subject,
15
- Observable } from 'rxjs'
16
- import {
17
- defineSeriesPlugin } from '@orbcharts/core'
18
- import type {
19
- ChartParams } from '@orbcharts/core'
20
- import type { PieParams } from '../types'
21
- import type { PieDatum } from '../seriesUtils'
22
- import { DEFAULT_PIE_PARAMS } from '../defaults'
23
- import { makePieData } from '../seriesUtils'
24
- import { getD3TransitionEase, makeD3Arc } from '../../utils/d3Utils'
25
- import { getClassName } from '../../utils/orbchartsUtils'
26
-
27
- const pluginName = 'Pie'
28
- const pathClassName = getClassName(pluginName, 'path')
29
-
30
- function makeTweenPieRenderDataFn ({ enter, exit, data, lastData, fullParams }: {
31
- enter: d3.Selection<d3.EnterElement, PieDatum, any, any>
32
- exit: d3.Selection<SVGPathElement, unknown, any, any>
33
- data: PieDatum[]
34
- lastData: PieDatum[]
35
- fullParams: PieParams
36
- }): (t: number) => PieDatum[] {
37
- // 無更新資料項目則只計算資料變化 (新資料 * t + 舊資料 * (1 - t))
38
- if (!enter.size() && !exit.size()) {
39
- return (t: number) => {
40
- const tweenData: PieDatum[] = data.map((_d, _i) => {
41
- const lastDatum = lastData[_i] ?? {
42
- startAngle: 0,
43
- endAngle: 0,
44
- value: 0
45
- }
46
- return {
47
- ..._d,
48
- startAngle: (_d.startAngle * t) + (lastDatum.startAngle * (1 - t)),
49
- endAngle: (_d.endAngle * t) + (lastDatum.endAngle * (1 - t)),
50
- value: (_d.value * t) + (lastDatum.value * (1 - t))
51
- }
52
- })
53
-
54
- return makePieRenderData(
55
- tweenData,
56
- fullParams.startAngle!,
57
- fullParams.endAngle!,
58
- 1
59
- )
60
- }
61
- // 有更新資料則重新繪圖
62
- } else {
63
- return (t: number) => {
64
- return makePieRenderData(
65
- data,
66
- fullParams.startAngle!,
67
- fullParams.endAngle!,
68
- t
69
- )
70
- }
71
- }
72
- }
73
-
74
- function makePieRenderData (data: PieDatum[], startAngle: number, endAngle: number, t: number): PieDatum[] {
75
- return data.map((d, i) => {
76
- const _startAngle = startAngle + (d.startAngle - startAngle) * t
77
- const _endAngle = _startAngle + (d.endAngle - d.startAngle) * t
78
- return {
79
- ...d,
80
- startAngle: _startAngle,
81
- endAngle: _endAngle
82
- }
83
- })
84
- }
85
-
86
- function renderPie ({ selection, renderData, arc }: {
87
- selection: d3.Selection<SVGGElement, unknown, any, unknown>
88
- renderData: PieDatum[]
89
- arc: d3.Arc<any, d3.DefaultArcObject>
90
- }): d3.Selection<SVGPathElement, PieDatum, any, any> {
91
- let update: d3.Selection<SVGPathElement, PieDatum, any, any> = selection
92
- .selectAll<SVGPathElement, PieDatum>('path')
93
- .data(renderData, d => d.id)
94
- let enter = update.enter()
95
- .append<SVGPathElement>('path')
96
- .classed(pathClassName, true)
97
- let exit = update.exit()
98
-
99
- enter
100
- .append('path')
101
-
102
- const pathSelection = update.merge(enter)
103
- pathSelection
104
- .style('cursor', 'pointer')
105
- .attr('fill', (d, i) => d.data.color)
106
- // .transition()
107
- .attr('d', (d, i) => {
108
- return arc!(d as any)
109
- })
110
- exit.remove()
111
-
112
- return pathSelection
113
- }
114
-
115
- function highlight ({ pathSelection, ids, fullChartParams, arc, arcMouseover }: {
116
- pathSelection: d3.Selection<SVGPathElement, PieDatum, any, any>
117
- ids: string[]
118
- fullChartParams: ChartParams
119
- arc: d3.Arc<any, d3.DefaultArcObject>
120
- arcMouseover: d3.Arc<any, d3.DefaultArcObject>
121
- }) {
122
- pathSelection.interrupt('highlight')
123
-
124
- if (!ids.length) {
125
- // 取消放大
126
- pathSelection
127
- .transition('highlight')
128
- .style('opacity', 1)
129
- .attr('d', (d) => {
130
- return arc!(d as any)
131
- })
132
- return
133
- }
134
-
135
- pathSelection.each((d, i, n) => {
136
- const segment = d3.select(n[i])
137
-
138
- if (ids.includes(d.data.id)) {
139
- segment
140
- .style('opacity', 1)
141
- .transition('highlight')
142
- .ease(d3.easeElastic)
143
- .duration(500)
144
- .attr('d', (d: any) => {
145
- return arcMouseover!(d)
146
- })
147
- // .on('interrupt', () => {
148
- // // this.pathSelection!.select('path').attr('d', (d) => {
149
- // // return this.arc!(d as any)
150
- // // })
151
- // this.initHighlight()
152
- // })
153
- } else {
154
- // 取消放大
155
- segment
156
- .style('opacity', fullChartParams.styles.unhighlightedOpacity)
157
- .transition('highlight')
158
- .attr('d', (d) => {
159
- return arc!(d as any)
160
- })
161
- }
162
- })
163
- }
164
-
165
-
166
- export const Pie = defineSeriesPlugin(pluginName, DEFAULT_PIE_PARAMS)(({ selection, name, observer, subject }) => {
167
-
168
- const destroy$ = new Subject()
169
-
170
- const graphicGSelection: d3.Selection<SVGGElement, any, any, any> = selection.append('g')
171
- // let pathSelection: d3.Selection<SVGPathElement, PieDatum, any, any> | undefined
172
- const pathSelection$: Subject<d3.Selection<SVGPathElement, PieDatum, any, any>> = new Subject()
173
- let lastData: PieDatum[] = []
174
- let renderData: PieDatum[] = []
175
- // let originHighlight: Highlight | null = null
176
-
177
- observer.layout$
178
- .pipe(
179
- first()
180
- )
181
- .subscribe(size => {
182
- selection
183
- .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
184
- observer.layout$
185
- .pipe(
186
- takeUntil(destroy$)
187
- )
188
- .subscribe(size => {
189
- selection
190
- .transition()
191
- .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
192
- })
193
- })
194
-
195
- const shorterSideWith$ = observer.layout$.pipe(
196
- takeUntil(destroy$),
197
- map(d => d.width < d.height ? d.width : d.height)
198
- )
199
-
200
- const pieData$: Observable<PieDatum[]> = new Observable(subscriber => {
201
- combineLatest({
202
- computedData: observer.computedData$,
203
- fullParams: observer.fullParams$,
204
- }).pipe(
205
- takeUntil(destroy$),
206
- // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
207
- switchMap(async (d) => d),
208
- ).subscribe(data => {
209
- const pieData: PieDatum[] = makePieData({
210
- computedDataSeries: data.computedData,
211
- startAngle: data.fullParams.startAngle,
212
- endAngle: data.fullParams.endAngle
213
- })
214
- subscriber.next(pieData)
215
- })
216
- })
217
-
218
- // const SeriesDataMap$ = observer.computedData$.pipe(
219
- // takeUntil(destroy$),
220
- // map(d => makeSeriesDataMap(d))
221
- // )
222
-
223
- const arc$: Observable<d3.Arc<any, d3.DefaultArcObject>> = new Observable(subscriber => {
224
- combineLatest({
225
- shorterSideWith: shorterSideWith$,
226
- fullParams: observer.fullParams$,
227
- }).pipe(
228
- takeUntil(destroy$),
229
- // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
230
- switchMap(async (d) => d),
231
- ).subscribe(data => {
232
- const arc = makeD3Arc({
233
- axisWidth: data.shorterSideWith,
234
- innerRadius: data.fullParams.innerRadius,
235
- outerRadius: data.fullParams.outerRadius,
236
- padAngle: data.fullParams.padAngle,
237
- cornerRadius: data.fullParams.cornerRadius
238
- })
239
- subscriber.next(arc)
240
- })
241
- })
242
-
243
- const arcMouseover$: Observable<d3.Arc<any, d3.DefaultArcObject>> = new Observable(subscriber => {
244
- combineLatest({
245
- shorterSideWith: shorterSideWith$,
246
- fullParams: observer.fullParams$,
247
- }).pipe(
248
- takeUntil(destroy$),
249
- // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
250
- switchMap(async (d) => d),
251
- ).subscribe(data => {
252
- const arcMouseover = makeD3Arc({
253
- axisWidth: data.shorterSideWith,
254
- innerRadius: data.fullParams.innerRadius,
255
- outerRadius: data.fullParams.outerMouseoverRadius, // 外半徑變化
256
- padAngle: data.fullParams.padAngle,
257
- cornerRadius: data.fullParams.cornerRadius
258
- })
259
- subscriber.next(arcMouseover)
260
- })
261
- })
262
-
263
- // combineLatest({
264
- // pieData: pieData$,
265
- // SeriesDataMap: SeriesDataMap$,
266
- // arc: arc$,
267
- // arcMouseover: arcMouseover$,
268
- // computedData: computedData$,
269
- // fullParams: fullParams$,
270
- // // fullChartParams: fullChartParams$
271
- // }).pipe(
272
- // takeUntil(destroy$),
273
- // // 轉換後會退訂前一個未完成的訂閱事件,因此可以取到「同時間」最後一次的訂閱事件
274
- // switchMap(d => combineLatest({
275
- // pieData: pieData$,
276
- // SeriesDataMap: SeriesDataMap$,
277
- // arc: arc$,
278
- // arcMouseover: arcMouseover$,
279
- // computedData: computedData$,
280
- // fullParams: fullParams$,
281
- // fullChartParams: fullChartParams$
282
- // })),
283
- // take(1)
284
- const highlightTarget$ = observer.fullChartParams$.pipe(
285
- takeUntil(destroy$),
286
- map(d => d.highlightTarget),
287
- distinctUntilChanged()
288
- )
289
-
290
- combineLatest({
291
- pieData: pieData$,
292
- SeriesDataMap: observer.SeriesDataMap$,
293
- arc: arc$,
294
- arcMouseover: arcMouseover$,
295
- computedData: observer.computedData$,
296
- fullParams: observer.fullParams$,
297
- fullChartParams: observer.fullChartParams$,
298
- highlightTarget: highlightTarget$
299
- }).pipe(
300
- takeUntil(destroy$),
301
- switchMap(async d => d)
302
- ).subscribe(data => {
303
- graphicGSelection.interrupt('graphicMove')
304
- // console.log('graphic', data)
305
- let update: d3.Selection<SVGPathElement, PieDatum, any, any> = selection
306
- .selectAll<SVGPathElement, PieDatum>('path')
307
- .data(data.pieData, d => d.data.id)
308
- let enter = update.enter()
309
- let exit = update.exit()
310
-
311
- const makeTweenPieRenderData = makeTweenPieRenderDataFn({
312
- enter,
313
- exit,
314
- data: data.pieData,
315
- lastData,
316
- fullParams: data.fullParams
317
- })
318
-
319
- graphicGSelection
320
- .transition('graphicMove')
321
- .duration(data.fullChartParams.transitionDuration)
322
- .ease(getD3TransitionEase(data.fullChartParams.transitionEase))
323
- .tween('move', (self, t) => {
324
- return (t) => {
325
- renderData = makeTweenPieRenderData(t)
326
-
327
- const pathSelection = renderPie({ selection: graphicGSelection, renderData, arc: data.arc })
328
-
329
- subject.event$.next({
330
- type: 'series',
331
- pluginName: name,
332
- eventName: 'transitionMove',
333
- event: undefined,
334
- highlightTarget: data.highlightTarget,
335
- datum: null,
336
- series: [],
337
- seriesIndex: -1,
338
- seriesLabel: '',
339
- data: data.computedData
340
- })
341
- // const callbackData = makeEnterDurationCallbackData(data.computedData, )
342
- // enterDurationCallback(callbackData, t)
343
- }
344
- })
345
- .on('end', (self, t) => {
346
- renderData = makePieRenderData(
347
- data.pieData,
348
- data.fullParams.startAngle,
349
- data.fullParams.endAngle,
350
- 1
351
- )
352
- // console.log('renderData', renderData)
353
- const pathSelection = renderPie({ selection: graphicGSelection, renderData, arc: data.arc })
354
-
355
- // if (data.fullParams.highlightTarget && data.fullParams.highlightTarget != 'none') {
356
- // if (data.fullChartParams.highlightTarget && data.fullChartParams.highlightTarget != 'none') {
357
- // pathSelection!.style('cursor', 'pointer')
358
- // }
359
-
360
- pathSelection$.next(pathSelection)
361
-
362
- // pathSelection && setPathEvent({
363
- // pathSelection,
364
- // pluginName: name,
365
- // data: data.computedData,
366
- // fullChartParams: data.fullChartParams,
367
- // arc: data.arc,
368
- // arcMouseover: data.arcMouseover,
369
- // SeriesDataMap: data.SeriesDataMap,
370
- // event$: store.event$
371
- // })
372
-
373
- // 渲染完後紀錄為前次的資料
374
- lastData = Object.assign([], data.pieData)
375
-
376
- subject.event$.next({
377
- type: 'series',
378
- pluginName: name,
379
- eventName: 'transitionEnd',
380
- event: undefined,
381
- highlightTarget: data.highlightTarget,
382
- datum: null,
383
- series: [],
384
- seriesIndex: -1,
385
- seriesLabel: '',
386
- data: data.computedData
387
- })
388
-
389
- pathSelection!
390
- .on('mouseover', (event, pieDatum) => {
391
- event.stopPropagation()
392
-
393
- subject.event$.next({
394
- type: 'series',
395
- eventName: 'mouseover',
396
- pluginName: name,
397
- highlightTarget: data.highlightTarget,
398
- datum: pieDatum.data,
399
- series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
400
- seriesIndex: pieDatum.data.seriesIndex,
401
- seriesLabel: pieDatum.data.seriesLabel,
402
- event,
403
- data: data.computedData
404
- })
405
- })
406
- .on('mousemove', (event, pieDatum) => {
407
- event.stopPropagation()
408
-
409
- subject.event$.next({
410
- type: 'series',
411
- eventName: 'mousemove',
412
- pluginName: name,
413
- highlightTarget: data.highlightTarget,
414
- datum: pieDatum.data,
415
- series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
416
- seriesIndex: pieDatum.data.seriesIndex,
417
- seriesLabel: pieDatum.data.seriesLabel,
418
- event,
419
- data: data.computedData,
420
- })
421
- })
422
- .on('mouseout', (event, pieDatum) => {
423
- event.stopPropagation()
424
-
425
- subject.event$.next({
426
- type: 'series',
427
- eventName: 'mouseout',
428
- pluginName: name,
429
- highlightTarget: data.highlightTarget,
430
- datum: pieDatum.data,
431
- series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
432
- seriesIndex: pieDatum.data.seriesIndex,
433
- seriesLabel: pieDatum.data.seriesLabel,
434
- event,
435
- data: data.computedData,
436
- })
437
- })
438
- .on('click', (event, pieDatum) => {
439
- event.stopPropagation()
440
-
441
- subject.event$.next({
442
- type: 'series',
443
- eventName: 'click',
444
- pluginName: name,
445
- highlightTarget: data.highlightTarget,
446
- datum: pieDatum.data,
447
- series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
448
- seriesIndex: pieDatum.data.seriesIndex,
449
- seriesLabel: pieDatum.data.seriesLabel,
450
- event,
451
- data: data.computedData,
452
- })
453
- })
454
- })
455
- })
456
-
457
- // 事件觸發的highlight
458
- // const highlightMouseover$ = store.event$.pipe(
459
- // takeUntil(destroy$),
460
- // filter(d => d.eventName === 'mouseover'),
461
- // // distinctUntilChanged((prev, current) => prev.eventName === current.eventName)
462
- // map(d => {
463
- // return d.datum
464
- // ? { id: d.datum?.id, label: d.datum.label }
465
- // : { id: '', label: '' }
466
- // })
467
- // )
468
- // const highlightMouseout$ = store.event$.pipe(
469
- // takeUntil(destroy$),
470
- // filter(d => d.eventName === 'mouseout'),
471
- // // distinctUntilChanged((prev, current) => prev.eventName === current.eventName)
472
- // map(d => {
473
- // return { id: '', label: '' }
474
- // })
475
- // )
476
-
477
- // // 預設的highlight
478
- // const highlightDefault$ = fullChartParams$.pipe(
479
- // takeUntil(destroy$),
480
- // map(d => {
481
- // return { id: d.highlightDefault, label: d.highlightDefault }
482
- // })
483
- // )
484
-
485
- // combineLatest({
486
- // target: merge(highlightMouseover$, highlightMouseout$, highlightDefault$),
487
- // pathSelection: pathSelection$,
488
- // computedData: computedData$,
489
- // fullChartParams: fullChartParams$,
490
- // arc: arc$,
491
- // arcMouseover: arcMouseover$
492
- // }).pipe(
493
- // takeUntil(destroy$)
494
- // ).subscribe(data => {
495
- // // console.log('target', data.target)
496
- // const ids = getSeriesHighlightIds({
497
- // id: data.target.id,
498
- // label: data.target.label,
499
- // trigger: data.fullChartParams.highlightDefault.trigger,
500
- // data: data.computedData
501
- // })
502
- // // console.log('ids', ids)
503
- // highlight({
504
- // pathSelection: data.pathSelection,
505
- // ids: ids,
506
- // fullChartParams: data.fullChartParams,
507
- // arc: data.arc,
508
- // arcMouseover: data.arcMouseover
509
- // })
510
- // })
511
-
512
- combineLatest({
513
- pathSelection: pathSelection$,
514
- highlight: observer.seriesHighlight$.pipe(
515
- map(data => data.map(d => d.id))
516
- ),
517
- fullChartParams: observer.fullChartParams$,
518
- arc: arc$,
519
- arcMouseover: arcMouseover$
520
- }).pipe(
521
- takeUntil(destroy$),
522
- switchMap(async d => d)
523
- ).subscribe(data => {
524
- highlight({
525
- pathSelection: data.pathSelection,
526
- ids: data.highlight,
527
- fullChartParams: data.fullChartParams,
528
- arc: data.arc,
529
- arcMouseover: data.arcMouseover
530
- })
531
- })
532
-
533
- // d.fullParams
534
-
535
- // console.log('selection', selection)
536
-
537
- // fullChartParams$
538
- // .subscribe(d => {
539
- // console.log('fullChartParams', d)
540
- // })
541
-
542
- // computedData$
543
- // .subscribe(d => {
544
- // console.log('computedData', d)
545
- // })
546
- // console.log('-- defineSeriesPlugin --')
547
- // console.log('selector', selector)
548
- // // data$.subscribe(d => {
549
- // // console.log('data$', d)
550
- // // })
551
-
552
- // store.dataFormatter$.subscribe(d => {
553
- // console.log('store.dataFormatter$', d)
554
- // })
555
-
556
- // computedData$.subscribe(d => {
557
- // console.log('computedData$', d)
558
- // })
559
-
560
- // event$.subscribe(d => {
561
- // console.log('event$', d)
562
- // })
563
-
564
- // fullParams$.subscribe(d => {
565
- // console.log('fullParams$', d)
566
- // })
567
-
568
- // store.data$.subscribe(d => {
569
- // console.log('store.data$', d)
570
- // })
571
-
572
- // store.dataFormatter$.subscribe(d => {
573
- // console.log('store.dataFormatter$', d)
574
- // })
575
-
576
- // store.event$.subscribe(d => {
577
- // console.log('store.event$', d)
578
- // })
579
-
580
- // store.fullParams$.subscribe(d => {
581
- // console.log('store.fullParams$', d)
582
- // })
583
-
584
- // layout$.subscribe(d => {
585
- // console.log('layout$', d)
586
- // })
587
-
588
- // console.log('-- end --')
589
- // // const newData = data.map(d => d.map(_d => {
590
- // // return {
591
- // // ..._d as any,
592
- // // value: (_d as any).value + 10
593
- // // }
594
- // // }))
595
-
596
- // // data$.next(newData)
597
-
598
- return () => {
599
- destroy$.next(undefined)
600
- }
1
+ import * as d3 from 'd3'
2
+ import {
3
+ combineLatest,
4
+ map,
5
+ switchMap,
6
+ takeUntil,
7
+ distinctUntilChanged,
8
+ shareReplay,
9
+ Observable,
10
+ Subject } from 'rxjs'
11
+ import type {
12
+ ComputedDataSeries,
13
+ ComputedDatumSeries,
14
+ SeriesContainerPosition,
15
+ ChartParams,
16
+ EventSeries,
17
+ Layout } from '@orbcharts/core'
18
+ import type { PieDatum } from '../seriesUtils'
19
+ import type { PieParams } from '../types'
20
+ import {
21
+ defineSeriesPlugin } from '@orbcharts/core'
22
+ import { DEFAULT_PIE_PARAMS } from '../defaults'
23
+ import { makePieData } from '../seriesUtils'
24
+ import { getD3TransitionEase, makeD3Arc } from '../../utils/d3Utils'
25
+ import { getClassName } from '../../utils/orbchartsUtils'
26
+ import { seriesCenterSelectionObservable } from '../seriesObservables'
27
+
28
+
29
+ const pluginName = 'Pie'
30
+
31
+
32
+ function makeTweenPieRenderDataFn ({ enter, exit, data, lastTweenData, fullParams }: {
33
+ enter: d3.Selection<d3.EnterElement, PieDatum, any, any>
34
+ exit: d3.Selection<SVGPathElement, unknown, any, any>
35
+ data: PieDatum[]
36
+ lastTweenData: PieDatum[]
37
+ fullParams: PieParams
38
+ }): (t: number) => PieDatum[] {
39
+ // 無更新資料項目則只計算資料變化 (新資料 * t + 舊資料 * (1 - t))
40
+ if (!enter.size() && !exit.size()) {
41
+ return (t: number) => {
42
+ const tweenData: PieDatum[] = data.map((_d, _i) => {
43
+ const lastDatum = lastTweenData[_i] ?? {
44
+ startAngle: 0,
45
+ endAngle: 0,
46
+ value: 0
47
+ }
48
+ return {
49
+ ..._d,
50
+ startAngle: (_d.startAngle * t) + (lastDatum.startAngle * (1 - t)),
51
+ endAngle: (_d.endAngle * t) + (lastDatum.endAngle * (1 - t)),
52
+ value: (_d.value * t) + (lastDatum.value * (1 - t))
53
+ }
54
+ })
55
+
56
+ return makePieRenderData(
57
+ tweenData,
58
+ fullParams.startAngle!,
59
+ fullParams.endAngle!,
60
+ 1
61
+ )
62
+ }
63
+ // 有更新資料則重新繪圖
64
+ } else {
65
+ return (t: number) => {
66
+ return makePieRenderData(
67
+ data,
68
+ fullParams.startAngle!,
69
+ fullParams.endAngle!,
70
+ t
71
+ )
72
+ }
73
+ }
74
+ }
75
+
76
+ function makePieRenderData (data: PieDatum[], startAngle: number, endAngle: number, t: number): PieDatum[] {
77
+ return data.map((d, i) => {
78
+ const _startAngle = startAngle + (d.startAngle - startAngle) * t
79
+ const _endAngle = _startAngle + (d.endAngle - d.startAngle) * t
80
+ return {
81
+ ...d,
82
+ startAngle: _startAngle,
83
+ endAngle: _endAngle
84
+ }
85
+ })
86
+ }
87
+
88
+ function renderPie ({ selection, data, arc, pathClassName }: {
89
+ selection: d3.Selection<SVGGElement, unknown, any, unknown>
90
+ data: PieDatum[]
91
+ arc: d3.Arc<any, d3.DefaultArcObject>
92
+ pathClassName: string
93
+ }): d3.Selection<SVGPathElement, PieDatum, any, any> {
94
+ // console.log('data', data)
95
+ const pathSelection: d3.Selection<SVGPathElement, PieDatum, any, any> = selection
96
+ .selectAll<SVGPathElement, PieDatum>('path')
97
+ .data(data, d => d.id)
98
+ .join('path')
99
+ .classed(pathClassName, true)
100
+ .style('cursor', 'pointer')
101
+ .attr('fill', (d, i) => d.data.color)
102
+ .attr('d', (d, i) => {
103
+ return arc!(d as any)
104
+ })
105
+
106
+ return pathSelection
107
+ }
108
+
109
+ function highlight ({ pathSelection, ids, fullChartParams, arc, arcMouseover }: {
110
+ pathSelection: d3.Selection<SVGPathElement, PieDatum, any, any>
111
+ ids: string[]
112
+ fullChartParams: ChartParams
113
+ arc: d3.Arc<any, d3.DefaultArcObject>
114
+ arcMouseover: d3.Arc<any, d3.DefaultArcObject>
115
+ }) {
116
+ pathSelection.interrupt('highlight')
117
+
118
+ if (!ids.length) {
119
+ // 取消放大
120
+ pathSelection
121
+ .transition('highlight')
122
+ .style('opacity', 1)
123
+ .attr('d', (d) => {
124
+ return arc!(d as any)
125
+ })
126
+ return
127
+ }
128
+
129
+ pathSelection.each((d, i, n) => {
130
+ const segment = d3.select(n[i])
131
+
132
+ if (ids.includes(d.data.id)) {
133
+ segment
134
+ .style('opacity', 1)
135
+ .transition('highlight')
136
+ .ease(d3.easeElastic)
137
+ .duration(500)
138
+ .attr('d', (d: any) => {
139
+ return arcMouseover!(d)
140
+ })
141
+ // .on('interrupt', () => {
142
+ // // this.pathSelection!.select('path').attr('d', (d) => {
143
+ // // return this.arc!(d as any)
144
+ // // })
145
+ // this.initHighlight()
146
+ // })
147
+ } else {
148
+ // 取消放大
149
+ segment
150
+ .style('opacity', fullChartParams.styles.unhighlightedOpacity)
151
+ .transition('highlight')
152
+ .attr('d', (d) => {
153
+ return arc!(d as any)
154
+ })
155
+ }
156
+ })
157
+ }
158
+
159
+ // 各別的pie
160
+ function createEachPie (pluginName: string, context: {
161
+ containerSelection: d3.Selection<SVGGElement, any, any, unknown>
162
+ computedData$: Observable<ComputedDatumSeries[][]>
163
+ containerComputedLayoutData$: Observable<ComputedDatumSeries[]>
164
+ SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
165
+ fullParams$: Observable<PieParams>
166
+ fullChartParams$: Observable<ChartParams>
167
+ seriesHighlight$: Observable<ComputedDatumSeries[]>
168
+ seriesContainerPosition$: Observable<SeriesContainerPosition>
169
+ event$: Subject<EventSeries>
170
+ }) {
171
+ const destroy$ = new Subject()
172
+
173
+ const pathClassName = getClassName(pluginName, 'path')
174
+
175
+ let lastTweenData: PieDatum[] = [] // 紀錄補間動畫前次的資料
176
+ let tweenData: PieDatum[] = [] // 紀錄補間動畫用的資料
177
+ // let originHighlight: Highlight | null = null
178
+
179
+ // context.layout$
180
+ // .pipe(
181
+ // first()
182
+ // )
183
+ // .subscribe(size => {
184
+ // selection
185
+ // .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
186
+ // context.layout$
187
+ // .pipe(
188
+ // takeUntil(destroy$)
189
+ // )
190
+ // .subscribe(size => {
191
+ // selection
192
+ // .transition()
193
+ // .attr('transform', `translate(${size.width / 2}, ${size.height / 2})`)
194
+ // })
195
+ // })
196
+
197
+
198
+ const shorterSideWith$ = context.seriesContainerPosition$.pipe(
199
+ takeUntil(destroy$),
200
+ map(d => d.width < d.height ? d.width : d.height)
201
+ )
202
+
203
+ const pieData$: Observable<PieDatum[]> = new Observable(subscriber => {
204
+ combineLatest({
205
+ containerComputedLayoutData: context.containerComputedLayoutData$,
206
+ fullParams: context.fullParams$,
207
+ }).pipe(
208
+ takeUntil(destroy$),
209
+ switchMap(async (d) => d),
210
+ ).subscribe(data => {
211
+ // console.log('pieData', data)
212
+ const pieData: PieDatum[] = makePieData({
213
+ data: data.containerComputedLayoutData,
214
+ startAngle: data.fullParams.startAngle,
215
+ endAngle: data.fullParams.endAngle
216
+ })
217
+ // console.log('pieData', pieData)
218
+ subscriber.next(pieData)
219
+ })
220
+ })
221
+
222
+ // const SeriesDataMap$ = context.computedData$.pipe(
223
+ // takeUntil(destroy$),
224
+ // map(d => makeSeriesDataMap(d))
225
+ // )
226
+
227
+ const arc$: Observable<d3.Arc<any, d3.DefaultArcObject>> = new Observable(subscriber => {
228
+ combineLatest({
229
+ shorterSideWith: shorterSideWith$,
230
+ fullParams: context.fullParams$,
231
+ }).pipe(
232
+ takeUntil(destroy$),
233
+ switchMap(async (d) => d),
234
+ ).subscribe(data => {
235
+ const arc = makeD3Arc({
236
+ axisWidth: data.shorterSideWith,
237
+ innerRadius: data.fullParams.innerRadius,
238
+ outerRadius: data.fullParams.outerRadius,
239
+ padAngle: data.fullParams.padAngle,
240
+ cornerRadius: data.fullParams.cornerRadius
241
+ })
242
+ subscriber.next(arc)
243
+ })
244
+ })
245
+
246
+ const arcMouseover$: Observable<d3.Arc<any, d3.DefaultArcObject>> = new Observable(subscriber => {
247
+ combineLatest({
248
+ shorterSideWith: shorterSideWith$,
249
+ fullParams: context.fullParams$,
250
+ }).pipe(
251
+ takeUntil(destroy$),
252
+ switchMap(async (d) => d),
253
+ ).subscribe(data => {
254
+ const arcMouseover = makeD3Arc({
255
+ axisWidth: data.shorterSideWith,
256
+ innerRadius: data.fullParams.innerRadius,
257
+ outerRadius: data.fullParams.outerMouseoverRadius, // 外半徑變化
258
+ padAngle: data.fullParams.padAngle,
259
+ cornerRadius: data.fullParams.cornerRadius
260
+ })
261
+ subscriber.next(arcMouseover)
262
+ })
263
+ })
264
+
265
+ const highlightTarget$ = context.fullChartParams$.pipe(
266
+ takeUntil(destroy$),
267
+ map(d => d.highlightTarget),
268
+ distinctUntilChanged()
269
+ )
270
+
271
+ const pathSelection$ = new Observable<d3.Selection<SVGPathElement, PieDatum, any, any>>(subscriber => {
272
+ combineLatest({
273
+ pieData: pieData$,
274
+ arc: arc$,
275
+ computedData: context.computedData$,
276
+ fullParams: context.fullParams$,
277
+ fullChartParams: context.fullChartParams$,
278
+ highlightTarget: highlightTarget$
279
+ }).pipe(
280
+ takeUntil(destroy$),
281
+ switchMap(async d => d)
282
+ ).subscribe(data => {
283
+ context.containerSelection.interrupt('graphicMove')
284
+ // console.log('graphic', data)
285
+ let update: d3.Selection<SVGPathElement, PieDatum, any, any> = context.containerSelection
286
+ .selectAll<SVGPathElement, PieDatum>('path')
287
+ .data(data.pieData, d => d.id)
288
+ let enter = update.enter()
289
+ let exit = update.exit()
290
+
291
+ const makeTweenPieRenderData = makeTweenPieRenderDataFn({
292
+ enter,
293
+ exit,
294
+ data: data.pieData,
295
+ lastTweenData,
296
+ fullParams: data.fullParams
297
+ })
298
+
299
+ // -- enter資料使用補間動畫 --
300
+ enter
301
+ .transition('graphicMove')
302
+ .duration(data.fullChartParams.transitionDuration)
303
+ .ease(getD3TransitionEase(data.fullChartParams.transitionEase))
304
+ .tween('move', (self, t) => {
305
+ return (t) => {
306
+ tweenData = makeTweenPieRenderData(t)
307
+
308
+ const pathSelection = renderPie({
309
+ selection: context.containerSelection,
310
+ data: tweenData,
311
+ arc: data.arc,
312
+ pathClassName
313
+ })
314
+
315
+ context.event$.next({
316
+ type: 'series',
317
+ pluginName,
318
+ eventName: 'transitionMove',
319
+ event: undefined,
320
+ highlightTarget: data.highlightTarget,
321
+ datum: null,
322
+ series: [],
323
+ seriesIndex: -1,
324
+ seriesLabel: '',
325
+ data: data.computedData
326
+ })
327
+ // const callbackData = makeEnterDurationCallbackData(data.computedData, )
328
+ // enterDurationCallback(callbackData, t)
329
+ }
330
+ })
331
+ .on('end', (self, t) => {
332
+ tweenData = makePieRenderData(
333
+ data.pieData,
334
+ data.fullParams.startAngle,
335
+ data.fullParams.endAngle,
336
+ 1
337
+ )
338
+ // console.log('tweenData', tweenData)
339
+ const pathSelection = renderPie({
340
+ selection: context.containerSelection,
341
+ data: tweenData,
342
+ arc: data.arc,
343
+ pathClassName
344
+ })
345
+
346
+ // if (data.fullParams.highlightTarget && data.fullParams.highlightTarget != 'none') {
347
+ // if (data.fullChartParams.highlightTarget && data.fullChartParams.highlightTarget != 'none') {
348
+ // pathSelection!.style('cursor', 'pointer')
349
+ // }
350
+
351
+ subscriber.next(pathSelection)
352
+
353
+ // pathSelection && setPathEvent({
354
+ // pathSelection,
355
+ // pluginName: name,
356
+ // data: data.computedData,
357
+ // fullChartParams: data.fullChartParams,
358
+ // arc: data.arc,
359
+ // arcMouseover: data.arcMouseover,
360
+ // SeriesDataMap: data.SeriesDataMap,
361
+ // event$: store.event$
362
+ // })
363
+
364
+ // 渲染完後紀錄為前次的資料
365
+ lastTweenData = Object.assign([], data.pieData)
366
+
367
+ context.event$.next({
368
+ type: 'series',
369
+ pluginName,
370
+ eventName: 'transitionEnd',
371
+ event: undefined,
372
+ highlightTarget: data.highlightTarget,
373
+ datum: null,
374
+ series: [],
375
+ seriesIndex: -1,
376
+ seriesLabel: '',
377
+ data: data.computedData
378
+ })
379
+
380
+
381
+ })
382
+
383
+ // -- 更新資料 --
384
+ if (!enter.size() && update.size() > 0) {
385
+ // console.log('test')
386
+ const pathSelection = renderPie({
387
+ selection: context.containerSelection,
388
+ data: data.pieData,
389
+ arc: data.arc,
390
+ pathClassName
391
+ })
392
+ subscriber.next(pathSelection)
393
+ }
394
+ })
395
+ })
396
+
397
+ combineLatest({
398
+ pathSelection: pathSelection$,
399
+ SeriesDataMap: context.SeriesDataMap$,
400
+ computedData: context.computedData$,
401
+ highlightTarget: highlightTarget$
402
+ }).pipe(
403
+ takeUntil(destroy$),
404
+ switchMap(async d => d)
405
+ ).subscribe(data => {
406
+ data.pathSelection
407
+ .on('mouseover', (event, pieDatum) => {
408
+ event.stopPropagation()
409
+
410
+ context.event$.next({
411
+ type: 'series',
412
+ eventName: 'mouseover',
413
+ pluginName,
414
+ highlightTarget: data.highlightTarget,
415
+ datum: pieDatum.data,
416
+ series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
417
+ seriesIndex: pieDatum.data.seriesIndex,
418
+ seriesLabel: pieDatum.data.seriesLabel,
419
+ event,
420
+ data: data.computedData
421
+ })
422
+ })
423
+ .on('mousemove', (event, pieDatum) => {
424
+ event.stopPropagation()
425
+
426
+ context.event$.next({
427
+ type: 'series',
428
+ eventName: 'mousemove',
429
+ pluginName,
430
+ highlightTarget: data.highlightTarget,
431
+ datum: pieDatum.data,
432
+ series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
433
+ seriesIndex: pieDatum.data.seriesIndex,
434
+ seriesLabel: pieDatum.data.seriesLabel,
435
+ event,
436
+ data: data.computedData,
437
+ })
438
+ })
439
+ .on('mouseout', (event, pieDatum) => {
440
+ event.stopPropagation()
441
+
442
+ context.event$.next({
443
+ type: 'series',
444
+ eventName: 'mouseout',
445
+ pluginName,
446
+ highlightTarget: data.highlightTarget,
447
+ datum: pieDatum.data,
448
+ series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
449
+ seriesIndex: pieDatum.data.seriesIndex,
450
+ seriesLabel: pieDatum.data.seriesLabel,
451
+ event,
452
+ data: data.computedData,
453
+ })
454
+ })
455
+ .on('click', (event, pieDatum) => {
456
+ event.stopPropagation()
457
+
458
+ context.event$.next({
459
+ type: 'series',
460
+ eventName: 'click',
461
+ pluginName,
462
+ highlightTarget: data.highlightTarget,
463
+ datum: pieDatum.data,
464
+ series: data.SeriesDataMap.get(pieDatum.data.seriesLabel)!,
465
+ seriesIndex: pieDatum.data.seriesIndex,
466
+ seriesLabel: pieDatum.data.seriesLabel,
467
+ event,
468
+ data: data.computedData,
469
+ })
470
+ })
471
+ })
472
+
473
+ combineLatest({
474
+ pathSelection: pathSelection$,
475
+ highlight: context.seriesHighlight$.pipe(
476
+ map(data => data.map(d => d.id))
477
+ ),
478
+ fullChartParams: context.fullChartParams$,
479
+ arc: arc$,
480
+ arcMouseover: arcMouseover$
481
+ }).pipe(
482
+ takeUntil(destroy$),
483
+ switchMap(async d => d)
484
+ ).subscribe(data => {
485
+ highlight({
486
+ pathSelection: data.pathSelection,
487
+ ids: data.highlight,
488
+ fullChartParams: data.fullChartParams,
489
+ arc: data.arc,
490
+ arcMouseover: data.arcMouseover
491
+ })
492
+ })
493
+
494
+ return () => {
495
+ destroy$.next(undefined)
496
+ }
497
+ }
498
+
499
+ export const Pie = defineSeriesPlugin(pluginName, DEFAULT_PIE_PARAMS)(({ selection, name, subject, observer }) => {
500
+ const destroy$ = new Subject()
501
+
502
+ const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
503
+ selection: selection,
504
+ pluginName,
505
+ seriesSeparate$: observer.seriesSeparate$,
506
+ seriesLabels$: observer.seriesLabels$,
507
+ seriesContainerPosition$: observer.seriesContainerPosition$
508
+ })
509
+
510
+ const unsubscribeFnArr: (() => void)[] = []
511
+
512
+ seriesCenterSelection$.subscribe(seriesCenterSelection => {
513
+ // 每次重新計算時,清除之前的訂閱
514
+ unsubscribeFnArr.forEach(fn => fn())
515
+
516
+ seriesCenterSelection.each((d, containerIndex, g) => {
517
+
518
+ const containerSelection = d3.select(g[containerIndex])
519
+
520
+ const containerComputedLayoutData$ = observer.computedLayoutData$.pipe(
521
+ takeUntil(destroy$),
522
+ map(data => data[containerIndex] ?? data[0])
523
+ )
524
+
525
+ const containerPosition$ = observer.seriesContainerPosition$.pipe(
526
+ takeUntil(destroy$),
527
+ map(data => data[containerIndex] ?? data[0])
528
+ )
529
+
530
+ unsubscribeFnArr[containerIndex] = createEachPie(pluginName, {
531
+ containerSelection: containerSelection,
532
+ computedData$: observer.computedData$,
533
+ containerComputedLayoutData$: containerComputedLayoutData$,
534
+ SeriesDataMap$: observer.SeriesDataMap$,
535
+ fullParams$: observer.fullParams$,
536
+ fullChartParams$: observer.fullChartParams$,
537
+ seriesHighlight$: observer.seriesHighlight$,
538
+ seriesContainerPosition$: containerPosition$,
539
+ event$: subject.event$,
540
+ })
541
+
542
+ })
543
+ })
544
+
545
+ return () => {
546
+ destroy$.next(undefined)
547
+ unsubscribeFnArr.forEach(fn => fn())
548
+ }
601
549
  })