@orbcharts/plugins-basic 3.0.0-alpha.68 → 3.0.0-alpha.69
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 +3436 -3358
- package/dist/orbcharts-plugins-basic.umd.js +14 -13
- package/dist/src/utils/d3Graphics.d.ts +10 -0
- package/package.json +42 -42
- package/src/base/BaseBarStack.ts +779 -779
- package/src/base/BaseBars.ts +764 -764
- package/src/base/BaseBarsTriangle.ts +672 -672
- package/src/base/BaseDots.ts +513 -513
- package/src/base/BaseGroupAxis.ts +675 -652
- package/src/base/BaseLegend.ts +642 -642
- package/src/base/BaseLineAreas.ts +628 -628
- package/src/base/BaseLines.ts +704 -704
- package/src/base/BaseValueAxis.ts +578 -578
- package/src/base/types.ts +2 -2
- package/src/grid/defaults.ts +128 -128
- package/src/grid/gridObservables.ts +543 -543
- 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/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 +1014 -991
- package/src/grid/plugins/GroupAxis.ts +36 -36
- package/src/grid/plugins/LineAreas.ts +40 -40
- package/src/grid/plugins/Lines.ts +40 -40
- package/src/grid/plugins/ScalingArea.ts +174 -174
- package/src/grid/plugins/ValueAxis.ts +36 -36
- package/src/grid/plugins/ValueStackAxis.ts +38 -38
- package/src/grid/types.ts +123 -123
- package/src/index.ts +9 -9
- package/src/multiGrid/defaults.ts +158 -158
- package/src/multiGrid/index.ts +13 -13
- package/src/multiGrid/multiGridObservables.ts +49 -49
- 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 +70 -70
- package/src/multiGrid/plugins/MultiLineAreas.ts +77 -77
- package/src/multiGrid/plugins/MultiLines.ts +77 -77
- package/src/multiGrid/plugins/MultiValueAxis.ts +69 -69
- package/src/multiGrid/plugins/MultiValueStackAxis.ts +69 -69
- package/src/multiGrid/plugins/OverlappingValueAxes.ts +170 -170
- package/src/multiGrid/plugins/OverlappingValueStackAxes.ts +169 -169
- package/src/multiGrid/types.ts +72 -72
- package/src/noneData/defaults.ts +102 -102
- package/src/noneData/index.ts +3 -3
- package/src/noneData/plugins/Container.ts +10 -10
- package/src/noneData/plugins/Tooltip.ts +327 -327
- package/src/noneData/types.ts +26 -26
- package/src/series/defaults.ts +149 -149
- package/src/series/index.ts +9 -9
- package/src/series/plugins/Bubbles.ts +545 -545
- package/src/series/plugins/Pie.ts +584 -584
- package/src/series/plugins/PieEventTexts.ts +262 -262
- package/src/series/plugins/PieLabels.ts +604 -598
- package/src/series/plugins/Rose.ts +481 -481
- package/src/series/plugins/RoseLabels.ts +571 -565
- 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 +87 -87
- package/src/tree/defaults.ts +23 -23
- 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 +174 -124
- package/src/utils/d3Utils.ts +73 -73
- package/src/utils/observables.ts +14 -14
- package/src/utils/orbchartsUtils.ts +100 -100
- package/tsconfig.base.json +13 -13
- package/tsconfig.json +2 -2
- package/vite.config.js +22 -22
@@ -1,565 +1,571 @@
|
|
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 { RoseLabelsParams } from '../types'
|
20
|
-
import type { PieDatum } from '../seriesUtils'
|
21
|
-
import { DEFAULT_ROSE_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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
const
|
47
|
-
const
|
48
|
-
const
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
.
|
73
|
-
.
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
.
|
82
|
-
.
|
83
|
-
.
|
84
|
-
.
|
85
|
-
|
86
|
-
|
87
|
-
const [
|
88
|
-
const
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
//
|
126
|
-
|
127
|
-
|
128
|
-
.
|
129
|
-
.
|
130
|
-
.
|
131
|
-
.
|
132
|
-
.attr('
|
133
|
-
.
|
134
|
-
|
135
|
-
.style('
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
// .text(d =>
|
140
|
-
.
|
141
|
-
.attr('
|
142
|
-
.
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
//
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
const
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
const
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
.
|
290
|
-
.attr("
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
//
|
298
|
-
//
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
}
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
.
|
327
|
-
.
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
.
|
346
|
-
.
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
.
|
365
|
-
.
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
const
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
const
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
const
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
map(d =>
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
}
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
}
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
.
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
})
|
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 { RoseLabelsParams } from '../types'
|
20
|
+
import type { PieDatum } from '../seriesUtils'
|
21
|
+
import { DEFAULT_ROSE_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
|
+
import { renderTspansOnQuadrant } from '../../utils/d3Graphics'
|
27
|
+
|
28
|
+
interface RenderDatum {
|
29
|
+
pieDatum: PieDatum
|
30
|
+
arcIndex: number
|
31
|
+
arcLabels: string[]
|
32
|
+
lineStartX: number
|
33
|
+
lineStartY: number
|
34
|
+
lineStartMouseoverX: number
|
35
|
+
lineStartMouseoverY: number
|
36
|
+
x: number
|
37
|
+
y: number
|
38
|
+
mouseoverX: number
|
39
|
+
mouseoverY: number
|
40
|
+
textWidth: number, // 文字寬度
|
41
|
+
collisionShiftX: number // 避免碰撞的位移
|
42
|
+
collisionShiftY: number
|
43
|
+
quadrant: number // 第幾象限
|
44
|
+
}
|
45
|
+
|
46
|
+
const pluginName = 'RoseLabels'
|
47
|
+
const labelGClassName = getClassName(pluginName, 'label-g')
|
48
|
+
const lineGClassName = getClassName(pluginName, 'line-g')
|
49
|
+
const textClassName = getClassName(pluginName, 'text')
|
50
|
+
|
51
|
+
const pieOuterCentroid = 2
|
52
|
+
|
53
|
+
function makeRenderData ({ pieData, labelCentroid, arcScaleType, maxValue, axisWidth, outerRadius, lineStartCentroid, fullParams }: {
|
54
|
+
pieData: PieDatum[]
|
55
|
+
// arc: d3.Arc<any, d3.DefaultArcObject>
|
56
|
+
labelCentroid: number
|
57
|
+
arcScaleType: 'area' | 'radius'
|
58
|
+
maxValue: number
|
59
|
+
axisWidth: number
|
60
|
+
outerRadius: number
|
61
|
+
lineStartCentroid: number
|
62
|
+
fullParams: RoseLabelsParams
|
63
|
+
}): RenderDatum[] {
|
64
|
+
|
65
|
+
const outerRadiusWidth = (axisWidth / 2) * outerRadius
|
66
|
+
|
67
|
+
const exponent = arcScaleType === 'area'
|
68
|
+
? 0.5 // 比例映射面積(0.5為取平方根)
|
69
|
+
: 1 // 比例映射半徑
|
70
|
+
|
71
|
+
const arcScale = d3.scalePow()
|
72
|
+
.domain([0, maxValue])
|
73
|
+
.range([0, outerRadiusWidth])
|
74
|
+
.exponent(exponent)
|
75
|
+
|
76
|
+
return pieData
|
77
|
+
.map((d, i) => {
|
78
|
+
const eachOuterRadius = arcScale(d.value)
|
79
|
+
|
80
|
+
const arc = d3.arc()
|
81
|
+
.innerRadius(0)
|
82
|
+
.outerRadius(eachOuterRadius)
|
83
|
+
.padAngle(0)
|
84
|
+
.padRadius(eachOuterRadius)
|
85
|
+
.cornerRadius(0)
|
86
|
+
|
87
|
+
const [_x, _y] = arc!.centroid(d as any)
|
88
|
+
const [_mouseoverX, _mouseoverY] = [_x, _y]
|
89
|
+
const arcLabel = fullParams.labelFn(d.data)
|
90
|
+
return {
|
91
|
+
pieDatum: d,
|
92
|
+
arcIndex: i,
|
93
|
+
arcLabels: arcLabel.split('\n'),
|
94
|
+
lineStartX: _x * lineStartCentroid,
|
95
|
+
lineStartY: _y * lineStartCentroid,
|
96
|
+
lineStartMouseoverX: _mouseoverX * lineStartCentroid,
|
97
|
+
lineStartMouseoverY: _mouseoverY * lineStartCentroid,
|
98
|
+
x: _x * labelCentroid!,
|
99
|
+
y: _y * labelCentroid!,
|
100
|
+
mouseoverX: _mouseoverX * labelCentroid!,
|
101
|
+
mouseoverY: _mouseoverY * labelCentroid!,
|
102
|
+
textWidth: 0, // 後面再做計算
|
103
|
+
collisionShiftX: 0, // 後面再做計算
|
104
|
+
collisionShiftY: 0, // 後面再做計算
|
105
|
+
quadrant: _x >= 0 && _y <= 0
|
106
|
+
? 1
|
107
|
+
: _x < 0 && _y <= 0
|
108
|
+
? 2
|
109
|
+
: _x < 0 && _y > 0
|
110
|
+
? 3
|
111
|
+
: 4
|
112
|
+
}
|
113
|
+
})
|
114
|
+
.filter(d => d.pieDatum.data.visible)
|
115
|
+
}
|
116
|
+
|
117
|
+
// 繪製圓餅圖
|
118
|
+
function renderLabel ({ labelGSelection, data, fullParams, fullChartParams, textSizePx }: {
|
119
|
+
labelGSelection: d3.Selection<SVGGElement, undefined, any, any>
|
120
|
+
data: RenderDatum[]
|
121
|
+
fullParams: RoseLabelsParams
|
122
|
+
fullChartParams: ChartParams
|
123
|
+
textSizePx: number
|
124
|
+
}) {
|
125
|
+
// console.log(data)
|
126
|
+
// let update = this.gSelection.selectAll('g').data(pieData)
|
127
|
+
const textSelection = labelGSelection
|
128
|
+
.selectAll<SVGTextElement, RenderDatum>('text')
|
129
|
+
.data(data, d => d.pieDatum.id)
|
130
|
+
.join('text')
|
131
|
+
.classed(textClassName, true)
|
132
|
+
.attr('font-weight', 'bold')
|
133
|
+
.attr('text-anchor', d => d.quadrant == 1 || d.quadrant == 4 ? 'start' : 'end')
|
134
|
+
.style('dominant-baseline', d => d.quadrant == 1 || d.quadrant == 2 ? 'auto' : 'hanging')
|
135
|
+
// .style('pointer-events', 'none')
|
136
|
+
.style('cursor', d => fullChartParams.highlightTarget && fullChartParams.highlightTarget != 'none'
|
137
|
+
? 'pointer'
|
138
|
+
: 'none')
|
139
|
+
// .text((d, i) => d.arcLabel)
|
140
|
+
// .text(d => fullParams.labelFn(d.pieDatum.data))
|
141
|
+
.attr('font-size', fullChartParams.styles.textSize)
|
142
|
+
.attr('fill', (d, i) => getDatumColor({ datum: d.pieDatum.data, colorType: fullParams.labelColorType, fullChartParams }))
|
143
|
+
.each((d, i, n) => {
|
144
|
+
// const textNode = d3.select<SVGTextElement, RenderDatum>(n[i])
|
145
|
+
// .selectAll('tspan')
|
146
|
+
// .data(d.arcLabels)
|
147
|
+
// .join('tspan')
|
148
|
+
// .attr('x', 0)
|
149
|
+
// .attr('y', (_d, _i) => d.quadrant == 1 || d.quadrant == 2
|
150
|
+
// ? - (d.arcLabels.length - 1 - _i) * textSizePx
|
151
|
+
// : _i * textSizePx)
|
152
|
+
// .text(d => d)
|
153
|
+
renderTspansOnQuadrant(d3.select<SVGTextElement, RenderDatum>(n[i]), {
|
154
|
+
textArr: d.arcLabels,
|
155
|
+
textSizePx,
|
156
|
+
quadrant: d.quadrant
|
157
|
+
})
|
158
|
+
})
|
159
|
+
textSelection
|
160
|
+
.transition()
|
161
|
+
.attr('transform', (d) => {
|
162
|
+
return 'translate(' + d.x + ',' + d.y + ')'
|
163
|
+
})
|
164
|
+
// .on('end', () => initHighlight({ labelSelection, data, fullChartParams }))
|
165
|
+
|
166
|
+
// 如無新增資料則不用等動畫
|
167
|
+
// if (enter.size() == 0) {
|
168
|
+
// this.initHighlight()
|
169
|
+
// }
|
170
|
+
|
171
|
+
return textSelection
|
172
|
+
}
|
173
|
+
|
174
|
+
// 獲取每個文字元素的邊界框並檢查是否重疊
|
175
|
+
function resolveCollisions(textSelection: d3.Selection<SVGTextElement, RenderDatum, any, any>, data: RenderDatum[], textSizePx: number) {
|
176
|
+
const textArray = textSelection.nodes();
|
177
|
+
const padding = textSizePx // 調整文字間的間距
|
178
|
+
|
179
|
+
// 存儲每個標籤的當前位置
|
180
|
+
const positions = textArray.map((textNode, i) => {
|
181
|
+
const bbox = textNode.getBBox();
|
182
|
+
// const arcCentroid = arc.centroid(data[i]);
|
183
|
+
const arcCentroid = [data[i].x, data[i].y];
|
184
|
+
return {
|
185
|
+
node: textNode,
|
186
|
+
x: arcCentroid[0],
|
187
|
+
y: arcCentroid[1],
|
188
|
+
width: bbox.width,
|
189
|
+
height: bbox.height
|
190
|
+
}
|
191
|
+
})
|
192
|
+
|
193
|
+
// 順時針碰撞檢測(只處理 2、4 象限,將較後面的文字碰撞時往外偏移)
|
194
|
+
for (let i = 0; i < positions.length; i++) {
|
195
|
+
const a = positions[i]
|
196
|
+
|
197
|
+
for (let j = i + 1; j < positions.length; j++) {
|
198
|
+
const b = positions[j]
|
199
|
+
|
200
|
+
// 記錄文字寬度
|
201
|
+
data[i].textWidth = a.width
|
202
|
+
|
203
|
+
const ax = a.x + data[i].collisionShiftX
|
204
|
+
const ay = a.y + data[i].collisionShiftY
|
205
|
+
const bx = b.x + data[j].collisionShiftX
|
206
|
+
const by = b.y + data[j].collisionShiftY
|
207
|
+
|
208
|
+
// 檢查是否重疊
|
209
|
+
if (!(ax + a.width / 2 < bx - b.width / 2 ||
|
210
|
+
ax - a.width / 2 > bx + b.width / 2 ||
|
211
|
+
ay + a.height / 2 < by - b.height / 2 ||
|
212
|
+
ay - a.height / 2 > by + b.height / 2)) {
|
213
|
+
|
214
|
+
if (data[j].quadrant == 2) {
|
215
|
+
const moveDown = (by > ay)
|
216
|
+
? -padding * 2
|
217
|
+
: -padding
|
218
|
+
data[j].collisionShiftY += moveDown // 由後一個累加高度
|
219
|
+
} else if (data[j].quadrant == 4) {
|
220
|
+
const moveDown = (by > ay)
|
221
|
+
? padding
|
222
|
+
: padding * 2
|
223
|
+
data[j].collisionShiftY += moveDown // 由後一個累加高度
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
// 逆時針碰撞檢測(只處理 1、3 象限,將較前面的文字碰撞時往外偏移)
|
230
|
+
for (let i = positions.length - 1; i >= 0; i--) {
|
231
|
+
const a = positions[i]
|
232
|
+
|
233
|
+
for (let j = i - 1; j >= 0; j--) {
|
234
|
+
const b = positions[j]
|
235
|
+
|
236
|
+
// 記錄文字寬度
|
237
|
+
data[i].textWidth = a.width
|
238
|
+
|
239
|
+
const ax = a.x + data[i].collisionShiftX
|
240
|
+
const ay = a.y + data[i].collisionShiftY
|
241
|
+
const bx = b.x + data[j].collisionShiftX
|
242
|
+
const by = b.y + data[j].collisionShiftY
|
243
|
+
|
244
|
+
// 檢查是否重疊
|
245
|
+
if (!(ax + a.width / 2 < bx - b.width / 2 ||
|
246
|
+
ax - a.width / 2 > bx + b.width / 2 ||
|
247
|
+
ay + a.height / 2 < by - b.height / 2 ||
|
248
|
+
ay - a.height / 2 > by + b.height / 2)) {
|
249
|
+
|
250
|
+
if (data[j].quadrant == 1) {
|
251
|
+
const moveDown = (by > ay)
|
252
|
+
? -padding * 2
|
253
|
+
: -padding
|
254
|
+
data[j].collisionShiftY += moveDown // 由前一個累加高度
|
255
|
+
} else if (data[j].quadrant == 3) {
|
256
|
+
const moveDown = (by > ay)
|
257
|
+
? padding
|
258
|
+
: padding * 2
|
259
|
+
data[j].collisionShiftY += moveDown // 由前一個累加高度
|
260
|
+
}
|
261
|
+
}
|
262
|
+
}
|
263
|
+
}
|
264
|
+
|
265
|
+
// 全部算完再來 render
|
266
|
+
textSelection
|
267
|
+
.data(data)
|
268
|
+
.transition()
|
269
|
+
.attr('transform', (d) => {
|
270
|
+
return `translate(${d.x + d.collisionShiftX},${d.y + d.collisionShiftY})`
|
271
|
+
})
|
272
|
+
}
|
273
|
+
|
274
|
+
function renderLine ({ lineGSelection, data, fullParams, fullChartParams }: {
|
275
|
+
lineGSelection: d3.Selection<SVGGElement, undefined, any, any>
|
276
|
+
data: RenderDatum[]
|
277
|
+
fullParams: RoseLabelsParams
|
278
|
+
fullChartParams: ChartParams
|
279
|
+
}) {
|
280
|
+
|
281
|
+
// 只顯示在有偏移的標籤
|
282
|
+
const filteredData = data.filter(d => d.collisionShiftX || d.collisionShiftY)
|
283
|
+
|
284
|
+
// 添加標籤的連接線
|
285
|
+
const lines = lineGSelection.selectAll<SVGPolylineElement, RenderDatum>("polyline")
|
286
|
+
.data(filteredData, d => d.pieDatum.id)
|
287
|
+
.join("polyline")
|
288
|
+
.attr("stroke", d => getDatumColor({ datum: d.pieDatum.data, colorType: fullParams.labelColorType, fullChartParams }))
|
289
|
+
.attr("stroke-width", 1)
|
290
|
+
.attr("fill", "none")
|
291
|
+
.attr("points", (d) => {
|
292
|
+
return [[d.lineStartX, d.lineStartY], [d.lineStartX, d.lineStartY]] as any // 畫出從弧線中心到延伸點的線
|
293
|
+
})
|
294
|
+
lines
|
295
|
+
.transition()
|
296
|
+
.attr("points", (d) => {
|
297
|
+
// const pos = arc.centroid(d) // 起點:弧線的中心點
|
298
|
+
// const outerPos = [pos[0] * 2.5, pos[1] * 2.5] // 外部延伸的點(乘以倍率來延長線段)
|
299
|
+
|
300
|
+
let lineEndX = d.x + d.collisionShiftX
|
301
|
+
let lineEndY = d.y + d.collisionShiftY
|
302
|
+
// if (d.lineStartX >= Math.abs(d.lineStartY)) {
|
303
|
+
// lineEndX -= d.textWidth / 2
|
304
|
+
// } else if (d.lineStartX <= - Math.abs(d.lineStartY)) {
|
305
|
+
// lineEndX += d.textWidth / 2
|
306
|
+
// }
|
307
|
+
|
308
|
+
return [[lineEndX, lineEndY], [d.lineStartX, d.lineStartY]] as any // 畫出從弧線中心到延伸點的線
|
309
|
+
})
|
310
|
+
|
311
|
+
return lines
|
312
|
+
}
|
313
|
+
|
314
|
+
function highlight ({ textSelection, lineSelection, ids, fullChartParams }: {
|
315
|
+
textSelection: d3.Selection<SVGTextElement, RenderDatum, any, any>
|
316
|
+
lineSelection: d3.Selection<SVGPolylineElement, RenderDatum, any, any>
|
317
|
+
ids: string[]
|
318
|
+
fullChartParams: ChartParams
|
319
|
+
}) {
|
320
|
+
textSelection.interrupt('highlight')
|
321
|
+
lineSelection.interrupt('highlight')
|
322
|
+
|
323
|
+
if (!ids.length) {
|
324
|
+
textSelection
|
325
|
+
.transition('highlight')
|
326
|
+
.duration(200)
|
327
|
+
.attr('transform', (d) => {
|
328
|
+
return `translate(${d.x + d.collisionShiftX},${d.y + d.collisionShiftY})`
|
329
|
+
})
|
330
|
+
.style('opacity', 1)
|
331
|
+
lineSelection
|
332
|
+
.transition('highlight')
|
333
|
+
.duration(200)
|
334
|
+
.style('opacity', 1)
|
335
|
+
return
|
336
|
+
}
|
337
|
+
|
338
|
+
textSelection.each((d, i, n) => {
|
339
|
+
const segment = d3.select<SVGTextElement, RenderDatum>(n[i])
|
340
|
+
|
341
|
+
if (ids.includes(d.pieDatum.data.id)) {
|
342
|
+
segment
|
343
|
+
.style('opacity', 1)
|
344
|
+
.transition('highlight')
|
345
|
+
.duration(200)
|
346
|
+
.attr('transform', (d) => {
|
347
|
+
return `translate(${d.mouseoverX + d.collisionShiftX},${d.mouseoverY + d.collisionShiftY})`
|
348
|
+
})
|
349
|
+
} else {
|
350
|
+
segment
|
351
|
+
.style('opacity', fullChartParams.styles.unhighlightedOpacity)
|
352
|
+
.transition('highlight')
|
353
|
+
.duration(200)
|
354
|
+
.attr('transform', (d) => {
|
355
|
+
return `translate(${d.x + d.collisionShiftX},${d.y + d.collisionShiftY})`
|
356
|
+
})
|
357
|
+
}
|
358
|
+
})
|
359
|
+
lineSelection.each((d, i, n) => {
|
360
|
+
const segment = d3.select<SVGPolylineElement, RenderDatum>(n[i])
|
361
|
+
|
362
|
+
if (ids.includes(d.pieDatum.data.id)) {
|
363
|
+
segment
|
364
|
+
.style('opacity', 1)
|
365
|
+
.transition('highlight')
|
366
|
+
.duration(200)
|
367
|
+
} else {
|
368
|
+
segment
|
369
|
+
.style('opacity', fullChartParams.styles.unhighlightedOpacity)
|
370
|
+
.transition('highlight')
|
371
|
+
.duration(200)
|
372
|
+
}
|
373
|
+
})
|
374
|
+
}
|
375
|
+
|
376
|
+
|
377
|
+
function createEachPieLabel (pluginName: string, context: {
|
378
|
+
containerSelection: d3.Selection<SVGGElement, any, any, unknown>
|
379
|
+
// computedData$: Observable<ComputedDatumSeries[][]>
|
380
|
+
visibleComputedLayoutData$: Observable<ComputedDatumSeries[][]>
|
381
|
+
containerVisibleComputedLayoutData$: Observable<ComputedDatumSeries[]>
|
382
|
+
// SeriesDataMap$: Observable<Map<string, ComputedDatumSeries[]>>
|
383
|
+
fullParams$: Observable<RoseLabelsParams>
|
384
|
+
fullChartParams$: Observable<ChartParams>
|
385
|
+
textSizePx$: Observable<number>
|
386
|
+
seriesHighlight$: Observable<ComputedDatumSeries[]>
|
387
|
+
seriesContainerPosition$: Observable<SeriesContainerPosition>
|
388
|
+
event$: Subject<EventSeries>
|
389
|
+
}) {
|
390
|
+
const destroy$ = new Subject()
|
391
|
+
|
392
|
+
context.containerSelection.selectAll('g').remove()
|
393
|
+
|
394
|
+
const lineGSelection: d3.Selection<SVGGElement, any, any, unknown> = context.containerSelection.append('g')
|
395
|
+
lineGSelection.classed(lineGClassName, true)
|
396
|
+
const labelGSelection: d3.Selection<SVGGElement, any, any, unknown> = context.containerSelection.append('g')
|
397
|
+
labelGSelection.classed(labelGClassName, true)
|
398
|
+
|
399
|
+
const textSelection$: Subject<d3.Selection<SVGTextElement, RenderDatum, any, any>> = new Subject()
|
400
|
+
const lineSelection$: Subject<d3.Selection<SVGPolylineElement, RenderDatum, any, any>> = new Subject()
|
401
|
+
let renderData: RenderDatum[] = []
|
402
|
+
|
403
|
+
const shorterSideWith$ = context.seriesContainerPosition$.pipe(
|
404
|
+
takeUntil(destroy$),
|
405
|
+
map(d => d.width < d.height ? d.width : d.height),
|
406
|
+
distinctUntilChanged()
|
407
|
+
)
|
408
|
+
|
409
|
+
const maxValue$ = context.visibleComputedLayoutData$.pipe(
|
410
|
+
map(data => Math.max(...data.flat().map(d => d.value))),
|
411
|
+
distinctUntilChanged()
|
412
|
+
)
|
413
|
+
|
414
|
+
const lineStartCentroid$ = context.fullParams$.pipe(
|
415
|
+
takeUntil(destroy$),
|
416
|
+
map(d => {
|
417
|
+
return d.labelCentroid >= pieOuterCentroid
|
418
|
+
? pieOuterCentroid // 當 label在 pie的外側時,線條從 pie的邊緣開始
|
419
|
+
: d.labelCentroid // 當 label在 pie的內側時,線條從 label未偏移前的位置開始
|
420
|
+
|
421
|
+
})
|
422
|
+
)
|
423
|
+
|
424
|
+
combineLatest({
|
425
|
+
// layout: context.seriesContainerPosition$,
|
426
|
+
shorterSideWith: shorterSideWith$,
|
427
|
+
containerVisibleComputedLayoutData: context.containerVisibleComputedLayoutData$,
|
428
|
+
maxValue: maxValue$,
|
429
|
+
fullParams: context.fullParams$,
|
430
|
+
fullChartParams: context.fullChartParams$,
|
431
|
+
textSizePx: context.textSizePx$,
|
432
|
+
lineStartCentroid: lineStartCentroid$
|
433
|
+
}).pipe(
|
434
|
+
takeUntil(destroy$),
|
435
|
+
switchMap(async (d) => d),
|
436
|
+
).subscribe(data => {
|
437
|
+
|
438
|
+
const eachAngle = Math.PI * 2 / data.containerVisibleComputedLayoutData.length
|
439
|
+
|
440
|
+
const pieData = data.containerVisibleComputedLayoutData.map((d, i) => {
|
441
|
+
return {
|
442
|
+
id: d.id,
|
443
|
+
data: d,
|
444
|
+
index: i,
|
445
|
+
value: d.value,
|
446
|
+
startAngle: eachAngle * i,
|
447
|
+
endAngle: eachAngle * (i + 1),
|
448
|
+
padAngle: 0,
|
449
|
+
// prevValue: lastPieData[i] ? lastPieData[i].value : 0
|
450
|
+
}
|
451
|
+
})
|
452
|
+
|
453
|
+
renderData = makeRenderData({
|
454
|
+
pieData,
|
455
|
+
labelCentroid: data.fullParams.labelCentroid,
|
456
|
+
arcScaleType: data.fullParams.arcScaleType,
|
457
|
+
maxValue: data.maxValue,
|
458
|
+
axisWidth: data.shorterSideWith,
|
459
|
+
outerRadius: data.fullParams.outerRadius,
|
460
|
+
lineStartCentroid: data.lineStartCentroid,
|
461
|
+
fullParams: data.fullParams
|
462
|
+
})
|
463
|
+
|
464
|
+
// 先移除線條,等偏移後再重新繪製
|
465
|
+
lineGSelection.selectAll('polyline').remove()
|
466
|
+
|
467
|
+
const textSelection = renderLabel({
|
468
|
+
labelGSelection,
|
469
|
+
data: renderData,
|
470
|
+
fullParams: data.fullParams,
|
471
|
+
fullChartParams: data.fullChartParams,
|
472
|
+
textSizePx: data.textSizePx
|
473
|
+
})
|
474
|
+
|
475
|
+
// 等 label 本身的 transition 結束後再進行碰撞檢測
|
476
|
+
setTimeout(() => {
|
477
|
+
// 偏移 label
|
478
|
+
resolveCollisions(textSelection, renderData, data.textSizePx)
|
479
|
+
|
480
|
+
const lineSelection = renderLine({ lineGSelection, data: renderData, fullParams: data.fullParams, fullChartParams: data.fullChartParams })
|
481
|
+
|
482
|
+
lineSelection$.next(lineSelection)
|
483
|
+
|
484
|
+
}, 1000)
|
485
|
+
|
486
|
+
textSelection$.next(textSelection)
|
487
|
+
|
488
|
+
})
|
489
|
+
|
490
|
+
combineLatest({
|
491
|
+
textSelection: textSelection$,
|
492
|
+
lineSelection: lineSelection$,
|
493
|
+
highlight: context.seriesHighlight$.pipe(
|
494
|
+
map(data => data.map(d => d.id))
|
495
|
+
),
|
496
|
+
fullChartParams: context.fullChartParams$,
|
497
|
+
}).pipe(
|
498
|
+
takeUntil(destroy$),
|
499
|
+
switchMap(async d => d)
|
500
|
+
).subscribe(data => {
|
501
|
+
highlight({
|
502
|
+
textSelection: data.textSelection,
|
503
|
+
lineSelection: data.lineSelection,
|
504
|
+
ids: data.highlight,
|
505
|
+
fullChartParams: data.fullChartParams,
|
506
|
+
})
|
507
|
+
})
|
508
|
+
|
509
|
+
return () => {
|
510
|
+
destroy$.next(undefined)
|
511
|
+
}
|
512
|
+
}
|
513
|
+
|
514
|
+
|
515
|
+
export const RoseLabels = defineSeriesPlugin(pluginName, DEFAULT_ROSE_LABELS_PARAMS)(({ selection, observer, subject }) => {
|
516
|
+
|
517
|
+
const destroy$ = new Subject()
|
518
|
+
|
519
|
+
const { seriesCenterSelection$ } = seriesCenterSelectionObservable({
|
520
|
+
selection: selection,
|
521
|
+
pluginName,
|
522
|
+
separateSeries$: observer.separateSeries$,
|
523
|
+
seriesLabels$: observer.seriesLabels$,
|
524
|
+
seriesContainerPosition$: observer.seriesContainerPosition$
|
525
|
+
})
|
526
|
+
|
527
|
+
const unsubscribeFnArr: (() => void)[] = []
|
528
|
+
|
529
|
+
seriesCenterSelection$
|
530
|
+
.pipe(
|
531
|
+
takeUntil(destroy$)
|
532
|
+
)
|
533
|
+
.subscribe(seriesCenterSelection => {
|
534
|
+
// 每次重新計算時,清除之前的訂閱
|
535
|
+
unsubscribeFnArr.forEach(fn => fn())
|
536
|
+
|
537
|
+
seriesCenterSelection.each((d, containerIndex, g) => {
|
538
|
+
|
539
|
+
const containerSelection = d3.select(g[containerIndex])
|
540
|
+
|
541
|
+
const containerVisibleComputedLayoutData$ = observer.visibleComputedLayoutData$.pipe(
|
542
|
+
takeUntil(destroy$),
|
543
|
+
map(data => JSON.parse(JSON.stringify(data[containerIndex] ?? data[0])))
|
544
|
+
)
|
545
|
+
|
546
|
+
const containerPosition$ = observer.seriesContainerPosition$.pipe(
|
547
|
+
takeUntil(destroy$),
|
548
|
+
map(data => JSON.parse(JSON.stringify(data[containerIndex] ?? data[0])))
|
549
|
+
)
|
550
|
+
|
551
|
+
unsubscribeFnArr[containerIndex] = createEachPieLabel(pluginName, {
|
552
|
+
containerSelection: containerSelection,
|
553
|
+
// computedData$: observer.computedData$,
|
554
|
+
visibleComputedLayoutData$: observer.visibleComputedLayoutData$,
|
555
|
+
containerVisibleComputedLayoutData$: containerVisibleComputedLayoutData$,
|
556
|
+
// SeriesDataMap$: observer.SeriesDataMap$,
|
557
|
+
fullParams$: observer.fullParams$,
|
558
|
+
fullChartParams$: observer.fullChartParams$,
|
559
|
+
textSizePx$: observer.textSizePx$,
|
560
|
+
seriesHighlight$: observer.seriesHighlight$,
|
561
|
+
seriesContainerPosition$: containerPosition$,
|
562
|
+
event$: subject.event$,
|
563
|
+
})
|
564
|
+
|
565
|
+
})
|
566
|
+
})
|
567
|
+
|
568
|
+
return () => {
|
569
|
+
destroy$.next(undefined)
|
570
|
+
}
|
571
|
+
})
|