@easyv/charts 1.2.6 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/.babelrc +8 -8
  2. package/.husky/commit-msg +3 -3
  3. package/CHANGELOG.md +18 -18
  4. package/lib/components/AnimateData.js +7 -11
  5. package/lib/components/Axis.js +14 -18
  6. package/lib/components/Background.js +2 -2
  7. package/lib/components/Band.js +4 -8
  8. package/lib/components/BaseLine.js +7 -9
  9. package/lib/components/Brush.js +2 -2
  10. package/lib/components/Carousel.js +8 -15
  11. package/lib/components/CartesianChart.js +7 -11
  12. package/lib/components/Chart.js +6 -9
  13. package/lib/components/ChartContainer.js +4 -8
  14. package/lib/components/ConicalGradient.js +23 -27
  15. package/lib/components/ExtentData.js +4 -7
  16. package/lib/components/FilterData.js +4 -7
  17. package/lib/components/Indicator.js +2 -2
  18. package/lib/components/Label.js +2 -2
  19. package/lib/components/Legend.js +5 -11
  20. package/lib/components/Lighter.js +179 -179
  21. package/lib/components/Line.js +5 -11
  22. package/lib/components/LinearGradient.js +2 -2
  23. package/lib/components/Mapping.js +5 -8
  24. package/lib/components/Marquee.js +5 -5
  25. package/lib/components/PieChart.js +12 -22
  26. package/lib/components/StackData.js +4 -7
  27. package/lib/components/StereoBar.js +4 -8
  28. package/lib/components/TextOverflow.js +5 -5
  29. package/lib/components/Tooltip.js +2 -2
  30. package/lib/components/index.js +49 -49
  31. package/lib/css/index.module.css +41 -41
  32. package/lib/css/piechart.module.css +26 -26
  33. package/lib/element/ConicGradient.js +72 -72
  34. package/lib/formatter/legend.js +2 -2
  35. package/lib/hooks/index.js +14 -14
  36. package/lib/hooks/useAnimateData.js +7 -7
  37. package/lib/hooks/useAxes.js +7 -7
  38. package/lib/hooks/useCarouselAxisX.js +7 -7
  39. package/lib/hooks/useExtentData.js +8 -8
  40. package/lib/hooks/useFilterData.js +8 -8
  41. package/lib/hooks/useStackData.js +8 -10
  42. package/lib/hooks/useTooltip.js +10 -10
  43. package/lib/index.js +2 -6
  44. package/lib/utils/index.js +5 -8
  45. package/package.json +53 -53
  46. package/src/components/AnimateData.tsx +24 -24
  47. package/src/components/Axis.tsx +354 -354
  48. package/src/components/Background.tsx +45 -45
  49. package/src/components/Band.tsx +173 -173
  50. package/src/components/BaseLine.js +76 -76
  51. package/src/components/Brush.js +159 -159
  52. package/src/components/Carousel.tsx +144 -144
  53. package/src/components/Chart.js +99 -99
  54. package/src/components/ChartContainer.tsx +63 -63
  55. package/src/components/ConicalGradient.js +258 -258
  56. package/src/components/ExtentData.js +17 -17
  57. package/src/components/FilterData.js +23 -23
  58. package/src/components/Indicator.js +13 -13
  59. package/src/components/Label.js +206 -206
  60. package/src/components/Legend.js +158 -158
  61. package/src/components/Lighter.jsx +173 -173
  62. package/src/components/Line.js +144 -144
  63. package/src/components/LinearGradient.js +29 -29
  64. package/src/components/Mapping.js +71 -71
  65. package/src/components/Marquee.js +93 -93
  66. package/src/components/PieChart.js +1287 -1288
  67. package/src/components/StackData.js +20 -20
  68. package/src/components/StereoBar.tsx +310 -310
  69. package/src/components/TextOverflow.js +51 -51
  70. package/src/components/Tooltip.js +169 -169
  71. package/src/components/index.js +55 -55
  72. package/src/context/index.js +2 -2
  73. package/src/css/index.module.css +41 -41
  74. package/src/css/piechart.module.css +26 -26
  75. package/src/element/ConicGradient.jsx +55 -55
  76. package/src/element/Line.tsx +33 -33
  77. package/src/element/index.ts +3 -3
  78. package/src/formatter/index.js +1 -1
  79. package/src/formatter/legend.js +90 -90
  80. package/src/hooks/index.js +17 -17
  81. package/src/hooks/useAnimateData.ts +67 -67
  82. package/src/hooks/useAxes.js +144 -144
  83. package/src/hooks/useCarouselAxisX.js +163 -163
  84. package/src/hooks/useExtentData.js +88 -88
  85. package/src/hooks/useFilterData.js +72 -72
  86. package/src/hooks/useStackData.js +100 -100
  87. package/src/hooks/useTooltip.ts +96 -96
  88. package/src/index.js +6 -6
  89. package/src/types/index.d.ts +67 -67
  90. package/src/utils/index.js +714 -714
  91. package/tsconfig.json +22 -22
@@ -1,1288 +1,1287 @@
1
- /**
2
- * 饼环图
3
- */
4
- import React, {
5
- memo,
6
- useMemo,
7
- useCallback,
8
- useRef,
9
- useState,
10
- useContext,
11
- useLayoutEffect,
12
- Fragment,
13
- } from 'react';
14
- import { ChartContainer, Carousel, Legend, ConicalGradient, Mapping } from '.';
15
- import { chartContext } from '../context';
16
- import {
17
- getFontStyle,
18
- sortPie,
19
- getDataWithPercent,
20
- getColorList,
21
- } from '../utils';
22
- import { pie, arc, extent, scaleLinear } from 'd3v7';
23
- import { animate, linear } from 'popmotion';
24
- import LinearGradient from './LinearGradient';
25
- import { pieLegendFormatter as legendFormatter } from '../formatter';
26
- import ringCss from '../css/piechart.module.css';
27
-
28
- const PI = Math.PI;
29
-
30
- const defaultChart = {
31
- outerRadius: 1,
32
- innerRadius: 0,
33
- cornerRadius: 0,
34
- rose: false,
35
- roseType: 'radius',
36
- baseRadius: 0,
37
- padAngle: 0,
38
- };
39
- const defaultAngle = { startAngle: 0, endAngle: 360, antiClockwise: false };
40
-
41
- const nameDy = (showValue, showPercent, mode, dir) => {
42
- if (showValue || showPercent) {
43
- if (mode == 'vertical') {
44
- return dir == 1 ? '1.1em' : '-2.6em';
45
- } else {
46
- return 0;
47
- }
48
- } else {
49
- if (mode == 'vertical') {
50
- return dir * 1.1 + 'em';
51
- } else {
52
- return 0;
53
- }
54
- }
55
- };
56
- const valueDy = (value1, mode, dir) => {
57
- if (value1) {
58
- if (mode == 'vertical') {
59
- return '1.5em';
60
- } else {
61
- return 0;
62
- }
63
- } else {
64
- if (mode == 'vertical') {
65
- return dir == 1 ? '1.1em' : '-1.1em';
66
- } else {
67
- return 0;
68
- }
69
- }
70
- };
71
-
72
- const percentDy_ = (showName, showValue, mode, dir) => {
73
- if (showValue) {
74
- return 0;
75
- }
76
- if (showName) {
77
- if (mode == 'vertical') {
78
- return '1.5em';
79
- } else {
80
- return 0;
81
- }
82
- } else {
83
- if (mode == 'vertical') {
84
- return dir * 1.1 + 'em';
85
- } else {
86
- return 0;
87
- }
88
- }
89
- };
90
-
91
- const percentX = (showName, showValue, mode, x) => {
92
- if (showValue) {
93
- return '';
94
- }
95
- if (showName) {
96
- if (mode == 'vertical') {
97
- return x;
98
- } else {
99
- return '';
100
- }
101
- } else {
102
- return x;
103
- }
104
- };
105
-
106
- const percentDx = (showName, showValue, mode) => {
107
- if (showValue) {
108
- return '0.5em';
109
- }
110
- if (showName) {
111
- if (mode == 'vertical') {
112
- return 0;
113
- } else {
114
- return '0.5em';
115
- }
116
- } else {
117
- return 0;
118
- }
119
- };
120
-
121
- const percentDy = (showName, showValue, mode) => {
122
- if (showValue) {
123
- return 0;
124
- }
125
- if (showName) {
126
- if (mode == 'vertical') {
127
- return '1.5em';
128
- } else {
129
- return 0;
130
- }
131
- } else {
132
- return 0;
133
- }
134
- };
135
-
136
- const valueDx = (showName, mode) => {
137
- if (!showName) {
138
- return '';
139
- }
140
- if (mode == 'vertical') {
141
- return '';
142
- } else {
143
- return '0.5em';
144
- }
145
- };
146
-
147
- const getCoord = (deg, radius) => {
148
- var x = Math.cos(deg) * radius,
149
- y = Math.sin(deg) * radius;
150
- return [x, y];
151
- };
152
-
153
- const getRoseRadius = ({ innerRadius, baseRadius }) =>
154
- innerRadius + (1 - innerRadius) * baseRadius;
155
-
156
- const getAngle = ({ startAngle, endAngle, antiClockwise, ...rest }) => {
157
- if (antiClockwise)
158
- return {
159
- ...rest,
160
- startAngle: endAngle - 180,
161
- endAngle: startAngle - 180,
162
- };
163
- return { ...rest, startAngle, endAngle };
164
- };
165
-
166
- const getArc = (
167
- radius,
168
- {
169
- padAngle = 0,
170
- innerRadius = 0,
171
- outerRadius = 1,
172
- cornerRadius = 0,
173
- startAngle = 0,
174
- endAngle = 360,
175
- ...rest
176
- },
177
- series
178
- ) => ({
179
- ...rest,
180
- type: 'pie',
181
- fieldName: series.fieldName,
182
- displayName: series.displayName || rest.data.s,
183
- series: series,
184
- innerRadius: innerRadius * radius,
185
- outerRadius: outerRadius * radius,
186
- arc: arc()
187
- .innerRadius(innerRadius * radius)
188
- .outerRadius(outerRadius * radius)
189
- .cornerRadius(cornerRadius)
190
- .startAngle(startAngle)
191
- .endAngle(endAngle)
192
- .padAngle(padAngle),
193
- });
194
-
195
- const getCircleScale = ({ count, color, width, length } = tick, radius) => {
196
- let data = [],
197
- arcs = [],
198
- centroids = [];
199
- for (let i = 0; i < count; i++) {
200
- data.push(1);
201
- }
202
- let scaleData = pie()(data);
203
- scaleData.map((data) => {
204
- let _arc = arc()
205
- .innerRadius(radius + length / 2)
206
- .outerRadius(radius + length / 2)
207
- .startAngle(data.startAngle)
208
- .endAngle(data.endAngle);
209
- arcs.push(_arc());
210
- centroids.push(_arc.centroid());
211
- });
212
- return (
213
- <g>
214
- {centroids.map((center, index) => {
215
- let x = center[0],
216
- y = center[1];
217
- let rate = length / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
218
- return (
219
- <path
220
- key={index}
221
- d={`M${x},${y}l${x * rate},${y * rate}`}
222
- strokeWidth={width}
223
- stroke={color}
224
- />
225
- );
226
- })}
227
- </g>
228
- );
229
- };
230
-
231
- const Component = memo(
232
- ({
233
- config: {
234
- chart: {
235
- dimension: {
236
- chartDimension: { width, height },
237
- },
238
- label,
239
- legend: { formatter = legendFormatter, ...legend },
240
- margin: { marginLeft, marginTop },
241
- },
242
- fan: {
243
- chart = defaultChart,
244
- chart: { outerRadius = defaultChart.outerRadius, padAngle },
245
- angle = defaultAngle,
246
- stroke: { show, strokeWidth, color } = { show: false },
247
- decorate,
248
- decorate2,
249
- categoryText,
250
- outerDecorate,
251
- current,
252
- } = {},
253
- order,
254
- series,
255
- animation: {
256
- on,
257
- current: {
258
- heighten = 0,
259
- opacity = 0,
260
- width: radiusWidthAdd = 0,
261
- color: animateColor,
262
- textStyle: animateCTS,
263
- gap = 0,
264
- },
265
- rotate = 0,
266
- },
267
- },
268
- state: { currentIndex, trigger },
269
- onEvent,
270
- data = [],
271
- }) => {
272
- const prevIndex = useRef(null);
273
- const { precision: legendPrecision } = legend.config.percent;
274
- const {
275
- id,
276
- width: chartWidth,
277
- height: chartHeight,
278
- triggerOnRelative,
279
- onEmit,
280
- } = useContext(chartContext);
281
-
282
- const [y, setY] = useState(1);
283
- const radius = (Math.min(chartWidth, chartHeight) / 2) * outerRadius;
284
-
285
- const arcsFunc = useMemo(() => {
286
- const { startAngle = 0, endAngle = 360 } = getAngle(angle);
287
- const arcsFunc = pie()
288
- .startAngle((startAngle * PI) / 180)
289
- .endAngle((endAngle * PI) / 180)
290
- .value((d) => d.y);
291
- return arcsFunc;
292
- }, [angle]);
293
-
294
- const arcs = useMemo(() => {
295
- const _chart = Object.assign(defaultChart, chart);
296
- const {
297
- innerRadius,
298
- outerRadius,
299
- rose,
300
- cornerRadius,
301
- padAngle,
302
- roseType,
303
- } = _chart;
304
- const _padAngle = (padAngle * Math.PI) / 180;
305
-
306
- switch (order) {
307
- case '':
308
- arcsFunc.sort(null);
309
- break;
310
- case 'desc':
311
- arcsFunc.sort((a, b) => b.y - a.y);
312
- break;
313
- case 'asc':
314
- arcsFunc.sort((a, b) => a.y - b.y);
315
- break;
316
- }
317
-
318
- const arcs = arcsFunc(data);
319
- const legendDataWithPercent = getDataWithPercent(arcs, legendPrecision);
320
- const _legendDataWithPercent = sortPie(legendDataWithPercent, order);
321
-
322
- if (rose) {
323
- const domain = extent(_legendDataWithPercent, (d) => d.value);
324
- const roseRadius = getRoseRadius(_chart);
325
- const scaler = scaleLinear().domain(domain).range([roseRadius, 1]);
326
-
327
- const angle = (PI * 2) / _legendDataWithPercent.length;
328
- return _legendDataWithPercent.map(
329
- ({ startAngle, endAngle, ...arc }, index) => ({
330
- ...arc,
331
- id: id + '_linear_' + index,
332
- startAngle: roseType == 'area' ? angle * index : startAngle,
333
- endAngle: roseType == 'area' ? angle * (index + 1) : endAngle,
334
- cornerRadius,
335
- padAngle: _padAngle,
336
- innerRadius,
337
- outerRadius: scaler(arc.value),
338
- })
339
- );
340
- }
341
- return _legendDataWithPercent.map((arc, index) => ({
342
- ...arc,
343
- id: id + '_linear_' + index,
344
- cornerRadius,
345
- padAngle: _padAngle,
346
- innerRadius,
347
- outerRadius,
348
- }));
349
- }, [data, arcsFunc, chart, legendPrecision]);
350
-
351
- const _arcs = useMemo(() => {
352
- const seriesLength = series.size;
353
- if (!seriesLength) return [];
354
- const _series = [...series.values()];
355
- return arcs.map((arc, index) => getArc(radius, arc, _series[index]));
356
- }, [series, arcs, radius]);
357
- const onClick = useCallback(
358
- (e) =>
359
- onEvent({
360
- currentIndex: +e.currentTarget.dataset.index,
361
- type: 'onClick',
362
- }),
363
- [onEvent]
364
- );
365
-
366
- const onMouseEnter = useCallback(
367
- (e) =>
368
- onEvent({
369
- currentIndex: +e.currentTarget.dataset.index,
370
- type: 'onMouseEnter',
371
- }),
372
- [onEvent]
373
- );
374
-
375
- const onMouseLeave = useCallback(
376
- (e) =>
377
- onEvent({
378
- currentIndex: +e.currentTarget.dataset.index,
379
- type: 'onMouseLeave',
380
- }),
381
- [onEvent]
382
- );
383
-
384
- useLayoutEffect(() => {
385
- let animation;
386
- if (!!on) {
387
- animation = animate({
388
- from: 0,
389
- to: 1,
390
- duration: 500,
391
- ease: linear,
392
- onPlay: () => {},
393
- onUpdate: (v) => {
394
- setY(v);
395
- },
396
- onComplete: () => {
397
- const _data = arcs[+currentIndex].data;
398
- triggerOnRelative(_data);
399
- onEmit('carousel', _data);
400
- },
401
- });
402
- } else {
403
- if (currentIndex !== null && trigger === 'onClick') {
404
- const _data = arcs[+currentIndex].data;
405
- triggerOnRelative(_data);
406
- onEmit(trigger, _data);
407
- }
408
- }
409
- return () => {
410
- prevIndex.current = currentIndex;
411
- animation && animation.stop();
412
- animation = null;
413
- };
414
- }, [
415
- JSON.stringify(arcs),
416
- on,
417
- currentIndex,
418
- trigger,
419
- onEmit,
420
- triggerOnRelative,
421
- ]);
422
-
423
- const halfChartWidth = chartWidth / 2;
424
- const halfChartHeight = chartHeight / 2;
425
-
426
- const rotate_ = decorate2
427
- ? (-(arcs[+currentIndex].startAngle + arcs[+currentIndex].endAngle) *
428
- 90) /
429
- Math.PI +
430
- rotate
431
- : 0;
432
- let maxRadius = 0;
433
- _arcs.map((d) => {
434
- maxRadius = Math.max(maxRadius, d.outerRadius);
435
- });
436
- let centerRadius = 0.5 * maxRadius + 0.5 * _arcs[0].innerRadius;
437
- return outerDecorate ? (
438
- <>
439
- <ChartContainer //用于生成甜甜圈图,判断依据是外环装饰这个配置项(outerDecorate)
440
- width={width}
441
- height={height}
442
- marginLeft={marginLeft}
443
- marginTop={marginTop}
444
- >
445
- <g
446
- style={{
447
- transition: 'transform ease-in-out 0.3s',
448
- transform:
449
- 'translate(' +
450
- halfChartWidth +
451
- 'px, ' +
452
- halfChartHeight +
453
- 'px) rotate(' +
454
- rotate_ +
455
- 'deg)',
456
- }}
457
- >
458
- {
459
- //用于生成外环装饰的刻度
460
- outerDecorate.tick.show &&
461
- getCircleScale(outerDecorate.tick, maxRadius)
462
- }
463
- <circle //外环装饰
464
- cx='0'
465
- cy='0'
466
- r={maxRadius + 2}
467
- fill='none'
468
- stroke={outerDecorate.color}
469
- strokeWidth={outerDecorate.width}
470
- />
471
- {_arcs.map(
472
- (
473
- {
474
- id,
475
- value,
476
- series,
477
- arc,
478
- innerRadius,
479
- outerRadius,
480
- index: dataIndex,
481
- },
482
- index
483
- ) => {
484
- const arcWidth = outerRadius - innerRadius;
485
- const path = arc
486
- .innerRadius(centerRadius)
487
- .outerRadius(centerRadius)(value);
488
- const dashLength = Math.ceil(
489
- (Math.PI * centerRadius * 2) / _arcs.length
490
- );
491
- const pie = getColorList(series.color);
492
- return (
493
- <Fragment key={index}>
494
- <path
495
- className={ringCss['inner-arc']}
496
- style={{
497
- strokeDasharray: `${dashLength},${2 * dashLength}`,
498
- strokeDashoffset: dashLength,
499
- animationDelay: `${index * 2000}ms`,
500
- }}
501
- data-index={dataIndex}
502
- onClick={onClick}
503
- onMouseEnter={onMouseEnter}
504
- onMouseLeave={onMouseLeave}
505
- d={path.split('L')[0]}
506
- stroke={'url(#' + id + ')'}
507
- strokeWidth={arcWidth}
508
- fill='none'
509
- />
510
- <defs>
511
- <LinearGradient
512
- id={id}
513
- colors={pie}
514
- rotate={series.color.linear.angle + 180}
515
- // gradientUnits='objectBoundingBox'
516
- />
517
- </defs>
518
- </Fragment>
519
- );
520
- }
521
- )}
522
- {label && (
523
- <RingLabel
524
- config={{ ...label, maxRadius: maxRadius + 2 }}
525
- arcs={_arcs}
526
- />
527
- )}
528
- </g>
529
- </ChartContainer>
530
- <Legend {...legend} series={_arcs} formatter={formatter} />
531
- </>
532
- ) : (
533
- <>
534
- <ChartContainer
535
- width={width}
536
- height={height}
537
- marginLeft={marginLeft}
538
- marginTop={marginTop}
539
- >
540
- <g
541
- style={{
542
- transition: 'transform ease-in-out 0.3s',
543
- transform:
544
- 'translate(' +
545
- halfChartWidth +
546
- 'px, ' +
547
- halfChartHeight +
548
- 'px) rotate(' +
549
- rotate_ +
550
- 'deg)',
551
- }}
552
- >
553
- {_arcs.map(
554
- (
555
- {
556
- id,
557
- value,
558
- series,
559
- arc,
560
- innerRadius,
561
- outerRadius,
562
- index: dataIndex,
563
- },
564
- index
565
- ) => {
566
- const current = index == currentIndex;
567
- const prev = index == prevIndex.current;
568
- const offset = current ? y : prev ? 1 - y : 0;
569
-
570
- const fillOpacity = animateColor
571
- ? 1
572
- : current
573
- ? opacity / 100
574
- : 1;
575
- const deltaHeighten = offset * heighten;
576
- const path = arc
577
- .innerRadius(innerRadius + deltaHeighten)
578
- .outerRadius(outerRadius + deltaHeighten)(value);
579
- const pie = getColorList(series.color);
580
- const currentPie = animateColor
581
- ? getColorList(animateColor)
582
- : getColorList(series.color);
583
- let textPath = '',
584
- categoryTextStyle = {};
585
- if (categoryText && categoryText.show) {
586
- //如果有类目文本,则需要计算文字路径
587
- //let offsetWidth=decorate2.radiusWidth/2 + radiusWidthAdd/2; //当前文字需生成在装饰物内,故而半径需要减小
588
- let textArc = arc
589
- .innerRadius(
590
- outerRadius + (current ? gap : categoryText.gap)
591
- )
592
- .outerRadius(
593
- outerRadius + (current ? gap : categoryText.gap)
594
- )(value);
595
- let lastA = textArc.lastIndexOf('A');
596
- textPath = textArc.slice(
597
- 0,
598
- lastA > 0 ? lastA : textArc.length
599
- ); //文字路径
600
- categoryTextStyle = current
601
- ? animateCTS
602
- : categoryText.textStyle; //这里把textstyle拿出来
603
- }
604
-
605
- return (
606
- <Fragment key={index}>
607
- <path
608
- data-index={dataIndex}
609
- onClick={onClick}
610
- onMouseEnter={onMouseEnter}
611
- onMouseLeave={onMouseLeave}
612
- d={path}
613
- stroke={show ? color : 'none'}
614
- strokeWidth={show ? strokeWidth : '0'}
615
- fill={'url(#' + id + ')'}
616
- fillOpacity={fillOpacity}
617
- />
618
- {
619
- //装饰物2,产生于每个弧的外部
620
- decorate2 && decorate2.show && (
621
- <path
622
- data-index={dataIndex}
623
- onClick={onClick}
624
- onMouseEnter={onMouseEnter}
625
- onMouseLeave={onMouseLeave}
626
- d={arc
627
- .innerRadius(outerRadius)
628
- .outerRadius(
629
- outerRadius +
630
- decorate2.radiusWidth +
631
- (current ? radiusWidthAdd : 0)
632
- )(value)}
633
- stroke={show ? color : 'none'}
634
- strokeWidth={show ? strokeWidth : '0'}
635
- fill={'url(#' + id + ')'}
636
- fillOpacity={decorate2.opacity / 100}
637
- />
638
- )
639
- }
640
- {
641
- //类目文本
642
- categoryText && categoryText.show && (
643
- <g>
644
- <path
645
- onClick={onClick}
646
- onMouseEnter={onMouseEnter}
647
- onMouseLeave={onMouseLeave}
648
- id={id + '_text_' + index}
649
- d={textPath}
650
- fill='none'
651
- stroke='none'
652
- />
653
- <text
654
- textAnchor='middle'
655
- style={{
656
- ...categoryTextStyle,
657
- fontWeight: categoryTextStyle.bold
658
- ? 'bold'
659
- : 'normal',
660
- fontStyle: categoryTextStyle.italic
661
- ? 'italic'
662
- : 'normal',
663
- pointerEvents: 'none',
664
- }}
665
- fill={categoryTextStyle.color}
666
- >
667
- <textPath
668
- startOffset='50%'
669
- href={'#' + id + '_text_' + index}
670
- >
671
- {_arcs[index].displayName ||
672
- _arcs[index].fieldName}
673
- </textPath>
674
- </text>
675
- </g>
676
- )
677
- }
678
- <defs>
679
- <LinearGradient
680
- id={id}
681
- colors={current ? currentPie : pie}
682
- rotate={
683
- current
684
- ? animateColor
685
- ? animateColor.linear.angle + 180
686
- : series.color.linear.angle + 180
687
- : series.color.linear.angle + 180
688
- }
689
- // gradientUnits='objectBoundingBox'
690
- />
691
- </defs>
692
- </Fragment>
693
- );
694
- }
695
- )}
696
- {label && <Label config={label} arcs={_arcs} />}
697
- {current && (
698
- <g
699
- fillOpacity={y}
700
- style={{ transform: 'rotate(' + -rotate_ + 'deg)' }}
701
- >
702
- <Current
703
- config={current}
704
- width={width}
705
- height={height}
706
- data={_arcs}
707
- currentIndex={+currentIndex}
708
- />
709
- </g>
710
- )}
711
- </g>
712
- </ChartContainer>
713
- {decorate && (
714
- <ConicalGradient
715
- width={width}
716
- height={height}
717
- centerX={halfChartWidth + marginLeft}
718
- centerY={halfChartHeight + marginTop}
719
- config={decorate}
720
- arcs={_arcs}
721
- radius={radius}
722
- />
723
- )}
724
- <Legend {...legend} series={_arcs} formatter={formatter} />
725
- </>
726
- );
727
- }
728
- );
729
-
730
- const Current = ({
731
- config: {
732
- show,
733
- gap,
734
- name: { show: showName, sameColor: nameColor, font: nameFont, textBreak },
735
- percent: {
736
- show: showPercent,
737
- sameColor: percentColor,
738
- font: percentFont,
739
- precision,
740
- translate: { x: translatePercentX, y: translatePercentY },
741
- },
742
- value: {
743
- show: showValue,
744
- sameColor: valueColor,
745
- font: valueFont,
746
- translate: { x: translateValueX, y: translateValueY },
747
- suffix: {
748
- show: showSuffix,
749
- fontSize,
750
- text,
751
- translate: { x: translateSuffixX, y: translateSuffixY },
752
- },
753
- },
754
- },
755
- width,
756
- height,
757
- data,
758
- currentIndex,
759
- }) => {
760
- const _data = useMemo(() => {
761
- const legendDataWithPercent = getDataWithPercent(data, precision);
762
- return sortPie(legendDataWithPercent, '');
763
- }, [data, precision]);
764
- const currentData = _data[currentIndex];
765
-
766
- if (!currentData) return null;
767
-
768
- const { displayName, fieldName, value, percent } = currentData;
769
- let nameTemp = displayName ? displayName : fieldName; //类目名
770
- let nameList = [];
771
- if (textBreak) {
772
- //如果限制了首行字符,则切割组件
773
- while (nameTemp.length > textBreak) {
774
- nameList.push(nameTemp.slice(0, textBreak));
775
- nameTemp = nameTemp.slice(textBreak);
776
- }
777
- }
778
- nameList.push(nameTemp);
779
- let foreignStyle = {
780
- //foreignObject标签样式,
781
- width,
782
- height,
783
- transform: `translate(-${width / 2}px,-${height / 2}px)`,
784
- pointerEvents: 'none',
785
- },
786
- boxStyle = {
787
- //弹性盒子样式,用于当前值的上下居中对齐等
788
- width,
789
- height,
790
- display: 'flex',
791
- flexDirection: 'column',
792
- justifyContent: 'center',
793
- alignItems: 'center',
794
- };
795
- let seriesColor = currentData.series.color;
796
- seriesColor =
797
- seriesColor.type == 'pure'
798
- ? seriesColor.pure
799
- : seriesColor.linear.stops[0].color;
800
- return (
801
- show && (
802
- <foreignObject style={foreignStyle}>
803
- <div style={boxStyle}>
804
- {
805
- //类目名称
806
- showName && (
807
- <div
808
- style={{
809
- ...getFontStyle(nameFont),
810
- margin: gap / 2 + 'px 0',
811
- display: 'flex',
812
- flexDirection: 'column',
813
- alignItems: 'center',
814
- color: nameColor ? seriesColor : nameFont.color,
815
- }}
816
- >
817
- {nameList.map((d, i) => {
818
- return <span key={i}>{d}</span>;
819
- })}
820
- </div>
821
- )
822
- }
823
- {
824
- //真实值
825
- showValue && (
826
- <span
827
- style={{
828
- ...getFontStyle(valueFont),
829
- transform:
830
- 'translate(' +
831
- translateValueX +
832
- 'px,' +
833
- translateValueY +
834
- 'px)',
835
- margin: gap / 2 + 'px 0',
836
- color: valueColor ? seriesColor : valueFont.color,
837
- }}
838
- >
839
- {value}
840
- {showSuffix && text && (
841
- <span
842
- style={{
843
- transform:
844
- 'translate(' +
845
- translateSuffixX +
846
- 'px,' +
847
- translateSuffixY +
848
- 'px)',
849
- fontSize: fontSize,
850
- }}
851
- >
852
- {text}
853
- </span>
854
- )}
855
- </span>
856
- )
857
- }
858
- {
859
- //百分比值
860
- showPercent && (
861
- <span
862
- style={{
863
- transform:
864
- 'translate(' +
865
- translatePercentX +
866
- 'px,' +
867
- translatePercentY +
868
- 'px)',
869
- ...getFontStyle(percentFont),
870
- margin: gap / 2 + 'px 0',
871
- color: percentColor ? seriesColor : percentFont.color,
872
- }}
873
- >
874
- {percent + '%'}
875
- </span>
876
- )
877
- }
878
- </div>
879
- </foreignObject>
880
- )
881
- );
882
- };
883
-
884
- const Label = ({
885
- config: {
886
- maxRadius = 0,
887
- lineLength,
888
- lineColor,
889
- distance,
890
- mode,
891
- show,
892
- translate: { x: translateX, y: translateY },
893
- name: { show: showName, font: nameFont },
894
- value: {
895
- show: showValue,
896
- font: valueFont,
897
- suffix: {
898
- show: showSuffix,
899
- text,
900
- fontSize: suffixFontSize,
901
- translate: { x: suffixTranslateX, y: suffixTranslateY },
902
- },
903
- sameColor: valueSameColor = false,
904
- },
905
- percent: {
906
- show: showPercent,
907
- font: percentFont,
908
- precision,
909
- sameColor: percentSameColor = false,
910
- },
911
- },
912
- arcs,
913
- animation,
914
- }) => {
915
- // const [labels, setLabels] = useState(null);
916
- // const [opacity, setOpacity] = useState(0);
917
-
918
- const _arcs = useMemo(
919
- () => getDataWithPercent(arcs, precision),
920
- [arcs, precision]
921
- );
922
- // useEffect(() => {
923
- // if (labels) {
924
- // const children = [...labels.children];
925
- // const bbox = children.reduce(
926
- // (prev, current) => {
927
- // const { topRight, bottomRight, bottomLeft, topLeft } = prev;
928
- // const { x, y, height } = current.getBBox();
929
-
930
- // current._y1 = y;
931
- // current._y2 = y + height;
932
- // current._delta = 0;
933
-
934
- // if (x > 0) {
935
- // if (y > 0) {
936
- // bottomRight.push(current);
937
- // } else {
938
- // topRight.push(current);
939
- // }
940
- // } else {
941
- // if (y > 0) {
942
- // bottomLeft.push(current);
943
- // } else {
944
- // topLeft.push(current);
945
- // }
946
- // }
947
- // return prev;
948
- // },
949
- // {
950
- // topRight: [],
951
- // bottomRight: [],
952
- // bottomLeft: [],
953
- // topLeft: [],
954
- // }
955
- // );
956
- // console.log('bbox: ', bbox);
957
- // }
958
- // }, [labels]);
959
-
960
- return (
961
- <g
962
- // style={{ opacity }} ref={setLabels}
963
- >
964
- {_arcs.map(
965
- (
966
- {
967
- series: {
968
- color: {
969
- type,
970
- pure,
971
- linear: { stops },
972
- },
973
- },
974
- displayName,
975
- value,
976
- percent,
977
- arc,
978
- outerRadius,
979
- index: actualIndex,
980
- },
981
- index
982
- ) => {
983
- const [x, y] = arc.centroid();
984
- const midAngle = Math.atan2(y, x);
985
-
986
- const [x1, y1] = getCoord(
987
- midAngle,
988
- maxRadius ? maxRadius : outerRadius
989
- );
990
-
991
- const radius = (maxRadius ? maxRadius : outerRadius) + distance;
992
- const [x2, y2] = getCoord(midAngle, radius);
993
-
994
- const direction = x2 < 0 ? -1 : 1;
995
- const x3 = x2 + lineLength * direction;
996
-
997
- const _x = x3 + (translateX + 6) * direction;
998
-
999
- const _showName = showName && displayName;
1000
- const _showValue = showValue && (value || showSuffix);
1001
-
1002
- return (
1003
- show &&
1004
- (_showName || showPercent || showValue) && (
1005
- <g key={index}>
1006
- <path
1007
- className={animation ? ringCss['label-line'] : ''}
1008
- style={{
1009
- animationDelay: `${
1010
- animation ? (actualIndex + 1) * 2000 - 800 : 0
1011
- }ms`,
1012
- }}
1013
- d={
1014
- 'M' +
1015
- x1 +
1016
- ', ' +
1017
- y1 +
1018
- 'L' +
1019
- x2 +
1020
- ', ' +
1021
- y2 +
1022
- 'L' +
1023
- x3 +
1024
- ', ' +
1025
- y2
1026
- }
1027
- stroke={
1028
- lineColor
1029
- ? lineColor
1030
- : type == 'pure'
1031
- ? pure
1032
- : stops[0].color
1033
- }
1034
- fill='none'
1035
- />
1036
- <text
1037
- className={animation ? ringCss['label-text'] : ''}
1038
- style={{
1039
- animationDelay: `${
1040
- animation ? (actualIndex + 1) * 2000 - 800 : 0
1041
- }ms`,
1042
- }}
1043
- x={_x}
1044
- y={y2 + translateY}
1045
- dominantBaseline='middle'
1046
- textAnchor={x3 >= 0 ? 'start' : 'end'}
1047
- >
1048
- {_showName && (
1049
- <tspan style={getFontStyle(nameFont, 'svg')}>
1050
- {displayName + (showValue || showPercent ? ':' : '')}
1051
- </tspan>
1052
- )}
1053
- {showValue && (
1054
- <>
1055
- <tspan
1056
- x={!!(_showName && mode == 'vertical') ? _x : ''}
1057
- dx={valueDx(_showName, mode)}
1058
- dy={!!(_showName && mode == 'vertical') ? '1.5em' : ''}
1059
- style={getFontStyle(valueFont, 'svg')}
1060
- >
1061
- {value}
1062
- </tspan>
1063
- {showSuffix && (
1064
- <tspan
1065
- style={{
1066
- ...getFontStyle(valueFont, 'svg'),
1067
- fontSize: suffixFontSize,
1068
- }}
1069
- dx={suffixTranslateX}
1070
- dy={suffixTranslateY}
1071
- >
1072
- {text}
1073
- </tspan>
1074
- )}
1075
- </>
1076
- )}
1077
- {showPercent && (
1078
- <tspan
1079
- x={percentX(_showName, _showValue, mode, _x)}
1080
- dx={percentDx(_showName, _showValue, mode)}
1081
- dy={
1082
- percentDy(_showName, _showValue, mode) +
1083
- (_showValue && showSuffix ? suffixTranslateY * -1 : '')
1084
- }
1085
- style={getFontStyle(percentFont, 'svg')}
1086
- >
1087
- {(_showValue ? '(' : '') +
1088
- percent +
1089
- '%' +
1090
- (_showValue ? ')' : '')}
1091
- </tspan>
1092
- )}
1093
- </text>
1094
- </g>
1095
- )
1096
- );
1097
- }
1098
- )}
1099
- </g>
1100
- );
1101
- };
1102
-
1103
- const RingLabel = ({
1104
- config: {
1105
- maxRadius = 0,
1106
- lineLength,
1107
- lineColor,
1108
- distance,
1109
- mode,
1110
- show,
1111
- translate: { x: translateX, y: translateY },
1112
- name: { show: showName, font: nameFont },
1113
- value: {
1114
- show: showValue,
1115
- font: valueFont,
1116
- suffix: {
1117
- show: showSuffix,
1118
- text,
1119
- fontSize: suffixFontSize,
1120
- translate: { x: suffixTranslateX, y: suffixTranslateY },
1121
- },
1122
- sameColor: valueSameColor = false,
1123
- },
1124
- percent: {
1125
- show: showPercent,
1126
- font: percentFont,
1127
- precision,
1128
- sameColor: percentSameColor = false,
1129
- },
1130
- },
1131
- arcs,
1132
- }) => {
1133
- const _arcs = useMemo(
1134
- () => getDataWithPercent(arcs, precision),
1135
- [arcs, precision]
1136
- );
1137
-
1138
- return (
1139
- <g>
1140
- {_arcs.map(
1141
- (
1142
- {
1143
- series: {
1144
- color: {
1145
- type,
1146
- pure,
1147
- linear: { stops },
1148
- },
1149
- },
1150
- data: realData,
1151
- displayName,
1152
- value,
1153
- percent,
1154
- arc,
1155
- outerRadius,
1156
- index: actualIndex,
1157
- },
1158
- index
1159
- ) => {
1160
- const [x, y] = arc.centroid();
1161
-
1162
- const midAngle = Math.atan2(y, x);
1163
-
1164
- const [x1, y1] = getCoord(
1165
- midAngle,
1166
- maxRadius ? maxRadius : outerRadius
1167
- );
1168
-
1169
- const radius = (maxRadius ? maxRadius : outerRadius) + distance;
1170
- const [x2, y2] = getCoord(midAngle, radius);
1171
-
1172
- const directionX = x2 < 0 ? -1 : 1;
1173
- const directionY = y2 < 0 ? -1 : 1;
1174
- const x3 = x2 + lineLength * directionX;
1175
- const _x = x3 + (translateX + 6) * directionX;
1176
-
1177
- const _showName = showName && displayName;
1178
- const _showValue = showValue && (value || showSuffix);
1179
-
1180
- return (
1181
- show &&
1182
- (_showName || showPercent || _showValue) && (
1183
- <g key={index}>
1184
- <path
1185
- className={ringCss['label-line']}
1186
- style={{
1187
- animationDelay: `${(actualIndex + 1) * 2000 - 800}ms`,
1188
- }}
1189
- d={
1190
- 'M' +
1191
- x1 +
1192
- ', ' +
1193
- y1 +
1194
- 'L' +
1195
- x2 +
1196
- ', ' +
1197
- y2 +
1198
- 'L' +
1199
- x3 +
1200
- ', ' +
1201
- y2
1202
- }
1203
- stroke={
1204
- lineColor
1205
- ? lineColor
1206
- : type == 'pure'
1207
- ? pure
1208
- : stops[0].color
1209
- }
1210
- fill='none'
1211
- />
1212
- <text
1213
- className={ringCss['label-text']}
1214
- style={{
1215
- animationDelay: `${(actualIndex + 1) * 2000 - 800}ms`,
1216
- }}
1217
- x={mode == 'horizontal' ? _x : x2}
1218
- y={y2 + translateY}
1219
- dominantBaseline='middle'
1220
- textAnchor={x3 >= 0 ? 'start' : 'end'}
1221
- >
1222
- {_showName && (
1223
- <tspan
1224
- dy={nameDy(_showValue, showPercent, mode, directionY)}
1225
- style={getFontStyle(nameFont, 'svg')}
1226
- >
1227
- {displayName + (showValue || showPercent ? ':' : '')}
1228
- </tspan>
1229
- )}
1230
- {_showValue && (
1231
- <>
1232
- <tspan
1233
- x={_showName ? (mode == 'horizontal' ? '' : x2) : ''}
1234
- dx={valueDx(_showName, mode)}
1235
- dy={valueDy(_showName, mode, directionY)}
1236
- style={getFontStyle(valueFont, 'svg')}
1237
- >
1238
- {realData.y}
1239
- </tspan>
1240
- {showSuffix && (
1241
- <tspan
1242
- style={{
1243
- ...getFontStyle(valueFont, 'svg'),
1244
- fontSize: suffixFontSize,
1245
- }}
1246
- dx={suffixTranslateX}
1247
- dy={suffixTranslateY}
1248
- >
1249
- {text}
1250
- </tspan>
1251
- )}
1252
- </>
1253
- )}
1254
- {showPercent && (
1255
- <tspan
1256
- x={
1257
- _showName
1258
- ? _showValue
1259
- ? ''
1260
- : mode == 'vertical'
1261
- ? x2
1262
- : ''
1263
- : ''
1264
- }
1265
- dx={percentDx(_showName, _showValue, mode)}
1266
- dy={
1267
- percentDy_(_showName, _showValue, mode, directionY) +
1268
- (_showValue && showSuffix ? suffixTranslateY * -1 : '')
1269
- }
1270
- style={getFontStyle(percentFont, 'svg')}
1271
- >
1272
- {(_showValue ? '(' : '') +
1273
- percent +
1274
- '%' +
1275
- (_showValue ? ')' : '')}
1276
- </tspan>
1277
- )}
1278
- </text>
1279
- </g>
1280
- )
1281
- );
1282
- }
1283
- )}
1284
- </g>
1285
- );
1286
- };
1287
-
1288
- export default Mapping(Carousel(Component));
1
+ /**
2
+ * 饼环图
3
+ */
4
+ import React, {
5
+ memo,
6
+ useMemo,
7
+ useCallback,
8
+ useRef,
9
+ useState,
10
+ useContext,
11
+ useLayoutEffect,
12
+ Fragment,
13
+ } from 'react';
14
+ import { ChartContainer, Carousel, Legend, ConicalGradient, Mapping } from '.';
15
+ import { chartContext } from '../context';
16
+ import {
17
+ getFontStyle,
18
+ sortPie,
19
+ getDataWithPercent,
20
+ getColorList,
21
+ } from '../utils';
22
+ import { pie, arc, extent, scaleLinear } from 'd3v7';
23
+ import { animate, linear } from 'popmotion';
24
+ import LinearGradient from './LinearGradient';
25
+ import { pieLegendFormatter as legendFormatter } from '../formatter';
26
+ import ringCss from '../css/piechart.module.css';
27
+
28
+ const PI = Math.PI;
29
+
30
+ const defaultChart = {
31
+ outerRadius: 1,
32
+ innerRadius: 0,
33
+ cornerRadius: 0,
34
+ rose: false,
35
+ roseType: 'radius',
36
+ baseRadius: 0,
37
+ padAngle: 0,
38
+ };
39
+ const defaultAngle = { startAngle: 0, endAngle: 360, antiClockwise: false };
40
+
41
+ const nameDy = (showValue, showPercent, mode, dir) => {
42
+ if (showValue || showPercent) {
43
+ if (mode == 'vertical') {
44
+ return dir == 1 ? '1.1em' : '-2.6em';
45
+ } else {
46
+ return 0;
47
+ }
48
+ } else {
49
+ if (mode == 'vertical') {
50
+ return dir * 1.1 + 'em';
51
+ } else {
52
+ return 0;
53
+ }
54
+ }
55
+ };
56
+ const valueDy = (value1, mode, dir) => {
57
+ if (value1) {
58
+ if (mode == 'vertical') {
59
+ return '1.5em';
60
+ } else {
61
+ return 0;
62
+ }
63
+ } else {
64
+ if (mode == 'vertical') {
65
+ return dir == 1 ? '1.1em' : '-1.1em';
66
+ } else {
67
+ return 0;
68
+ }
69
+ }
70
+ };
71
+
72
+ const percentDy_ = (showName, showValue, mode, dir) => {
73
+ if (showValue) {
74
+ return 0;
75
+ }
76
+ if (showName) {
77
+ if (mode == 'vertical') {
78
+ return '1.5em';
79
+ } else {
80
+ return 0;
81
+ }
82
+ } else {
83
+ if (mode == 'vertical') {
84
+ return dir * 1.1 + 'em';
85
+ } else {
86
+ return 0;
87
+ }
88
+ }
89
+ };
90
+
91
+ const percentX = (showName, showValue, mode, x) => {
92
+ if (showValue) {
93
+ return '';
94
+ }
95
+ if (showName) {
96
+ if (mode == 'vertical') {
97
+ return x;
98
+ } else {
99
+ return '';
100
+ }
101
+ } else {
102
+ return x;
103
+ }
104
+ };
105
+
106
+ const percentDx = (showName, showValue, mode) => {
107
+ if (showValue) {
108
+ return '0.5em';
109
+ }
110
+ if (showName) {
111
+ if (mode == 'vertical') {
112
+ return 0;
113
+ } else {
114
+ return '0.5em';
115
+ }
116
+ } else {
117
+ return 0;
118
+ }
119
+ };
120
+
121
+ const percentDy = (showName, showValue, mode) => {
122
+ if (showValue) {
123
+ return 0;
124
+ }
125
+ if (showName) {
126
+ if (mode == 'vertical') {
127
+ return '1.5em';
128
+ } else {
129
+ return 0;
130
+ }
131
+ } else {
132
+ return 0;
133
+ }
134
+ };
135
+
136
+ const valueDx = (showName, mode) => {
137
+ if (!showName) {
138
+ return '';
139
+ }
140
+ if (mode == 'vertical') {
141
+ return '';
142
+ } else {
143
+ return '0.5em';
144
+ }
145
+ };
146
+
147
+ const getCoord = (deg, radius) => {
148
+ var x = Math.cos(deg) * radius,
149
+ y = Math.sin(deg) * radius;
150
+ return [x, y];
151
+ };
152
+
153
+ const getRoseRadius = ({ innerRadius, baseRadius }) =>
154
+ innerRadius + (1 - innerRadius) * baseRadius;
155
+
156
+ const getAngle = ({ startAngle, endAngle, antiClockwise, ...rest }) => {
157
+ if (antiClockwise)
158
+ return {
159
+ ...rest,
160
+ startAngle: endAngle - 180,
161
+ endAngle: startAngle - 180,
162
+ };
163
+ return { ...rest, startAngle, endAngle };
164
+ };
165
+
166
+ const getArc = (
167
+ radius,
168
+ {
169
+ padAngle = 0,
170
+ innerRadius = 0,
171
+ outerRadius = 1,
172
+ cornerRadius = 0,
173
+ startAngle = 0,
174
+ endAngle = 360,
175
+ ...rest
176
+ },
177
+ series
178
+ ) => ({
179
+ ...rest,
180
+ type: 'pie',
181
+ fieldName: series.fieldName,
182
+ displayName: series.displayName || rest.data.s,
183
+ series: series,
184
+ innerRadius: innerRadius * radius,
185
+ outerRadius: outerRadius * radius,
186
+ arc: arc()
187
+ .innerRadius(innerRadius * radius)
188
+ .outerRadius(outerRadius * radius)
189
+ .cornerRadius(cornerRadius)
190
+ .startAngle(startAngle)
191
+ .endAngle(endAngle)
192
+ .padAngle(padAngle),
193
+ });
194
+
195
+ const getCircleScale = ({ count, color, width, length } = tick, radius) => {
196
+ let data = [],
197
+ arcs = [],
198
+ centroids = [];
199
+ for (let i = 0; i < count; i++) {
200
+ data.push(1);
201
+ }
202
+ let scaleData = pie()(data);
203
+ scaleData.map((data) => {
204
+ let _arc = arc()
205
+ .innerRadius(radius + length / 2)
206
+ .outerRadius(radius + length / 2)
207
+ .startAngle(data.startAngle)
208
+ .endAngle(data.endAngle);
209
+ arcs.push(_arc());
210
+ centroids.push(_arc.centroid());
211
+ });
212
+ return (
213
+ <g>
214
+ {centroids.map((center, index) => {
215
+ let x = center[0],
216
+ y = center[1];
217
+ let rate = length / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
218
+ return (
219
+ <path
220
+ key={index}
221
+ d={`M${x},${y}l${x * rate},${y * rate}`}
222
+ strokeWidth={width}
223
+ stroke={color}
224
+ />
225
+ );
226
+ })}
227
+ </g>
228
+ );
229
+ };
230
+
231
+ const Component = memo(
232
+ ({
233
+ config: {
234
+ chart: {
235
+ dimension: {
236
+ chartDimension: { width, height },
237
+ },
238
+ label,
239
+ legend: { formatter = legendFormatter, ...legend },
240
+ margin: { marginLeft, marginTop },
241
+ },
242
+ fan: {
243
+ chart = defaultChart,
244
+ chart: { outerRadius = defaultChart.outerRadius, padAngle },
245
+ angle = defaultAngle,
246
+ stroke: { show, strokeWidth, color } = { show: false },
247
+ decorate,
248
+ decorate2,
249
+ categoryText,
250
+ outerDecorate,
251
+ current,
252
+ } = {},
253
+ order,
254
+ series,
255
+ animation: {
256
+ on,
257
+ current: {
258
+ heighten = 0,
259
+ opacity = 0,
260
+ width: radiusWidthAdd = 0,
261
+ color: animateColor,
262
+ textStyle: animateCTS,
263
+ gap = 0,
264
+ },
265
+ rotate = 0,
266
+ },
267
+ },
268
+ state: { currentIndex, trigger },
269
+ onEvent,
270
+ data = [],
271
+ }) => {
272
+ const prevIndex = useRef(null);
273
+ const { precision: legendPrecision } = legend.config.percent;
274
+ const {
275
+ id,
276
+ width: chartWidth,
277
+ height: chartHeight,
278
+ triggerOnRelative,
279
+ onEmit,
280
+ } = useContext(chartContext);
281
+
282
+ const [y, setY] = useState(1);
283
+ const radius = (Math.min(chartWidth, chartHeight) / 2) * outerRadius;
284
+
285
+ const arcsFunc = useMemo(() => {
286
+ const { startAngle = 0, endAngle = 360 } = getAngle(angle);
287
+ const arcsFunc = pie()
288
+ .startAngle((startAngle * PI) / 180)
289
+ .endAngle((endAngle * PI) / 180)
290
+ .value((d) => d.y);
291
+ return arcsFunc;
292
+ }, [angle]);
293
+
294
+ const arcs = useMemo(() => {
295
+ const _chart = Object.assign(defaultChart, chart);
296
+ const {
297
+ innerRadius,
298
+ outerRadius,
299
+ rose,
300
+ cornerRadius,
301
+ padAngle,
302
+ roseType,
303
+ } = _chart;
304
+ const _padAngle = (padAngle * Math.PI) / 180;
305
+
306
+ switch (order) {
307
+ case '':
308
+ arcsFunc.sort(null);
309
+ break;
310
+ case 'desc':
311
+ arcsFunc.sort((a, b) => b.y - a.y);
312
+ break;
313
+ case 'asc':
314
+ arcsFunc.sort((a, b) => a.y - b.y);
315
+ break;
316
+ }
317
+
318
+ const arcs = arcsFunc(data);
319
+ const legendDataWithPercent = getDataWithPercent(arcs, legendPrecision);
320
+ const _legendDataWithPercent = sortPie(legendDataWithPercent, order);
321
+
322
+ if (rose) {
323
+ const domain = extent(_legendDataWithPercent, (d) => d.value);
324
+ const roseRadius = getRoseRadius(_chart);
325
+ const scaler = scaleLinear().domain(domain).range([roseRadius, 1]);
326
+
327
+ const angle = (PI * 2) / _legendDataWithPercent.length;
328
+ return _legendDataWithPercent.map(
329
+ ({ startAngle, endAngle, ...arc }, index) => ({
330
+ ...arc,
331
+ id: id + '_linear_' + index,
332
+ startAngle: roseType == 'area' ? angle * index : startAngle,
333
+ endAngle: roseType == 'area' ? angle * (index + 1) : endAngle,
334
+ cornerRadius,
335
+ padAngle: _padAngle,
336
+ innerRadius,
337
+ outerRadius: scaler(arc.value),
338
+ })
339
+ );
340
+ }
341
+ return _legendDataWithPercent.map((arc, index) => ({
342
+ ...arc,
343
+ id: id + '_linear_' + index,
344
+ cornerRadius,
345
+ padAngle: _padAngle,
346
+ innerRadius,
347
+ outerRadius,
348
+ }));
349
+ }, [data, arcsFunc, chart, legendPrecision]);
350
+
351
+ const _arcs = useMemo(() => {
352
+ const seriesLength = series.size;
353
+ if (!seriesLength) return [];
354
+ const _series = [...series.values()];
355
+ return arcs.map((arc, index) => getArc(radius, arc, _series[index]));
356
+ }, [series, arcs, radius]);
357
+ const onClick = useCallback(
358
+ (e) =>
359
+ onEvent({
360
+ currentIndex: +e.currentTarget.dataset.index,
361
+ type: 'onClick',
362
+ }),
363
+ [onEvent]
364
+ );
365
+
366
+ const onMouseEnter = useCallback(
367
+ (e) =>
368
+ onEvent({
369
+ currentIndex: +e.currentTarget.dataset.index,
370
+ type: 'onMouseEnter',
371
+ }),
372
+ [onEvent]
373
+ );
374
+
375
+ const onMouseLeave = useCallback(
376
+ (e) =>
377
+ onEvent({
378
+ currentIndex: +e.currentTarget.dataset.index,
379
+ type: 'onMouseLeave',
380
+ }),
381
+ [onEvent]
382
+ );
383
+
384
+ useLayoutEffect(() => {
385
+ let animation;
386
+ if (!!on) {
387
+ animation = animate({
388
+ from: 0,
389
+ to: 1,
390
+ duration: 500,
391
+ ease: linear,
392
+ onPlay: () => {},
393
+ onUpdate: (v) => {
394
+ setY(v);
395
+ },
396
+ onComplete: () => {
397
+ const _data = arcs[+currentIndex].data;
398
+ triggerOnRelative(_data);
399
+ onEmit('carousel', _data);
400
+ },
401
+ });
402
+ } else {
403
+ if (currentIndex !== null && trigger === 'onClick') {
404
+ const _data = arcs[+currentIndex].data;
405
+ triggerOnRelative(_data);
406
+ onEmit(trigger, _data);
407
+ }
408
+ }
409
+ return () => {
410
+ prevIndex.current = currentIndex;
411
+ animation && animation.stop();
412
+ animation = null;
413
+ };
414
+ }, [
415
+ JSON.stringify(arcs),
416
+ on,
417
+ currentIndex,
418
+ trigger,
419
+ onEmit,
420
+ triggerOnRelative,
421
+ ]);
422
+
423
+ const halfChartWidth = chartWidth / 2;
424
+ const halfChartHeight = chartHeight / 2;
425
+
426
+ const rotate_ = decorate2
427
+ ? (-(arcs[+currentIndex].startAngle + arcs[+currentIndex].endAngle) *
428
+ 90) /
429
+ Math.PI +
430
+ rotate
431
+ : 0;
432
+ let maxRadius = 0;
433
+ _arcs.map((d) => {
434
+ maxRadius = Math.max(maxRadius, d.outerRadius);
435
+ });
436
+ let centerRadius = 0.5 * maxRadius + 0.5 * _arcs[0].innerRadius;
437
+ return outerDecorate ? (
438
+ <>
439
+ <ChartContainer //用于生成甜甜圈图,判断依据是外环装饰这个配置项(outerDecorate)
440
+ width={width}
441
+ height={height}
442
+ marginLeft={marginLeft}
443
+ marginTop={marginTop}
444
+ >
445
+ <g
446
+ style={{
447
+ transition: 'transform ease-in-out 0.3s',
448
+ transform:
449
+ 'translate(' +
450
+ halfChartWidth +
451
+ 'px, ' +
452
+ halfChartHeight +
453
+ 'px) rotate(' +
454
+ rotate_ +
455
+ 'deg)',
456
+ }}
457
+ >
458
+ {
459
+ //用于生成外环装饰的刻度
460
+ outerDecorate.tick.show &&
461
+ getCircleScale(outerDecorate.tick, maxRadius)
462
+ }
463
+ <circle //外环装饰
464
+ cx='0'
465
+ cy='0'
466
+ r={maxRadius + 2}
467
+ fill='none'
468
+ stroke={outerDecorate.color}
469
+ strokeWidth={outerDecorate.width}
470
+ />
471
+ {_arcs.map(
472
+ (
473
+ {
474
+ id,
475
+ value,
476
+ series,
477
+ arc,
478
+ innerRadius,
479
+ outerRadius,
480
+ },
481
+ index
482
+ ) => {
483
+ const arcWidth = outerRadius - innerRadius;
484
+ const path = arc
485
+ .innerRadius(centerRadius)
486
+ .outerRadius(centerRadius)(value);
487
+ const dashLength = Math.ceil(
488
+ (Math.PI * centerRadius * 2) / _arcs.length
489
+ );
490
+ const pie = getColorList(series.color);
491
+ return (
492
+ <Fragment key={index}>
493
+ <path
494
+ className={ringCss['inner-arc']}
495
+ style={{
496
+ strokeDasharray: `${dashLength},${2 * dashLength}`,
497
+ strokeDashoffset: dashLength,
498
+ animationDelay: `${index * 2000}ms`,
499
+ }}
500
+ data-index={index}
501
+ onClick={onClick}
502
+ onMouseEnter={onMouseEnter}
503
+ onMouseLeave={onMouseLeave}
504
+ d={path.split('L')[0]}
505
+ stroke={'url(#' + id + ')'}
506
+ strokeWidth={arcWidth}
507
+ fill='none'
508
+ />
509
+ <defs>
510
+ <LinearGradient
511
+ id={id}
512
+ colors={pie}
513
+ rotate={series.color.linear.angle + 180}
514
+ // gradientUnits='objectBoundingBox'
515
+ />
516
+ </defs>
517
+ </Fragment>
518
+ );
519
+ }
520
+ )}
521
+ {label && (
522
+ <RingLabel
523
+ config={{ ...label, maxRadius: maxRadius + 2 }}
524
+ arcs={_arcs}
525
+ />
526
+ )}
527
+ </g>
528
+ </ChartContainer>
529
+ <Legend {...legend} series={_arcs} formatter={formatter} />
530
+ </>
531
+ ) : (
532
+ <>
533
+ <ChartContainer
534
+ width={width}
535
+ height={height}
536
+ marginLeft={marginLeft}
537
+ marginTop={marginTop}
538
+ >
539
+ <g
540
+ style={{
541
+ transition: 'transform ease-in-out 0.3s',
542
+ transform:
543
+ 'translate(' +
544
+ halfChartWidth +
545
+ 'px, ' +
546
+ halfChartHeight +
547
+ 'px) rotate(' +
548
+ rotate_ +
549
+ 'deg)',
550
+ }}
551
+ >
552
+ {_arcs.map(
553
+ (
554
+ {
555
+ id,
556
+ value,
557
+ series,
558
+ arc,
559
+ innerRadius,
560
+ outerRadius,
561
+ index: dataIndex,
562
+ },
563
+ index
564
+ ) => {
565
+ const current = index == currentIndex;
566
+ const prev = index == prevIndex.current;
567
+ const offset = current ? y : prev ? 1 - y : 0;
568
+
569
+ const fillOpacity = animateColor
570
+ ? 1
571
+ : current
572
+ ? opacity / 100
573
+ : 1;
574
+ const deltaHeighten = offset * heighten;
575
+ const path = arc
576
+ .innerRadius(innerRadius + deltaHeighten)
577
+ .outerRadius(outerRadius + deltaHeighten)(value);
578
+ const pie = getColorList(series.color);
579
+ const currentPie = animateColor
580
+ ? getColorList(animateColor)
581
+ : getColorList(series.color);
582
+ let textPath = '',
583
+ categoryTextStyle = {};
584
+ if (categoryText && categoryText.show) {
585
+ //如果有类目文本,则需要计算文字路径
586
+ //let offsetWidth=decorate2.radiusWidth/2 + radiusWidthAdd/2; //当前文字需生成在装饰物内,故而半径需要减小
587
+ let textArc = arc
588
+ .innerRadius(
589
+ outerRadius + (current ? gap : categoryText.gap)
590
+ )
591
+ .outerRadius(
592
+ outerRadius + (current ? gap : categoryText.gap)
593
+ )(value);
594
+ let lastA = textArc.lastIndexOf('A');
595
+ textPath = textArc.slice(
596
+ 0,
597
+ lastA > 0 ? lastA : textArc.length
598
+ ); //文字路径
599
+ categoryTextStyle = current
600
+ ? animateCTS
601
+ : categoryText.textStyle; //这里把textstyle拿出来
602
+ }
603
+
604
+ return (
605
+ <Fragment key={index}>
606
+ <path
607
+ data-index={index}
608
+ onClick={onClick}
609
+ onMouseEnter={onMouseEnter}
610
+ onMouseLeave={onMouseLeave}
611
+ d={path}
612
+ stroke={show ? color : 'none'}
613
+ strokeWidth={show ? strokeWidth : '0'}
614
+ fill={'url(#' + id + ')'}
615
+ fillOpacity={fillOpacity}
616
+ />
617
+ {
618
+ //装饰物2,产生于每个弧的外部
619
+ decorate2 && decorate2.show && (
620
+ <path
621
+ data-index={index}
622
+ onClick={onClick}
623
+ onMouseEnter={onMouseEnter}
624
+ onMouseLeave={onMouseLeave}
625
+ d={arc
626
+ .innerRadius(outerRadius)
627
+ .outerRadius(
628
+ outerRadius +
629
+ decorate2.radiusWidth +
630
+ (current ? radiusWidthAdd : 0)
631
+ )(value)}
632
+ stroke={show ? color : 'none'}
633
+ strokeWidth={show ? strokeWidth : '0'}
634
+ fill={'url(#' + id + ')'}
635
+ fillOpacity={decorate2.opacity / 100}
636
+ />
637
+ )
638
+ }
639
+ {
640
+ //类目文本
641
+ categoryText && categoryText.show && (
642
+ <g>
643
+ <path
644
+ onClick={onClick}
645
+ onMouseEnter={onMouseEnter}
646
+ onMouseLeave={onMouseLeave}
647
+ id={id + '_text_' + index}
648
+ d={textPath}
649
+ fill='none'
650
+ stroke='none'
651
+ />
652
+ <text
653
+ textAnchor='middle'
654
+ style={{
655
+ ...categoryTextStyle,
656
+ fontWeight: categoryTextStyle.bold
657
+ ? 'bold'
658
+ : 'normal',
659
+ fontStyle: categoryTextStyle.italic
660
+ ? 'italic'
661
+ : 'normal',
662
+ pointerEvents: 'none',
663
+ }}
664
+ fill={categoryTextStyle.color}
665
+ >
666
+ <textPath
667
+ startOffset='50%'
668
+ href={'#' + id + '_text_' + index}
669
+ >
670
+ {_arcs[index].displayName ||
671
+ _arcs[index].fieldName}
672
+ </textPath>
673
+ </text>
674
+ </g>
675
+ )
676
+ }
677
+ <defs>
678
+ <LinearGradient
679
+ id={id}
680
+ colors={current ? currentPie : pie}
681
+ rotate={
682
+ current
683
+ ? animateColor
684
+ ? animateColor.linear.angle + 180
685
+ : series.color.linear.angle + 180
686
+ : series.color.linear.angle + 180
687
+ }
688
+ // gradientUnits='objectBoundingBox'
689
+ />
690
+ </defs>
691
+ </Fragment>
692
+ );
693
+ }
694
+ )}
695
+ {label && <Label config={label} arcs={_arcs} />}
696
+ {current && (
697
+ <g
698
+ fillOpacity={y}
699
+ style={{ transform: 'rotate(' + -rotate_ + 'deg)' }}
700
+ >
701
+ <Current
702
+ config={current}
703
+ width={width}
704
+ height={height}
705
+ data={_arcs}
706
+ currentIndex={+currentIndex}
707
+ />
708
+ </g>
709
+ )}
710
+ </g>
711
+ </ChartContainer>
712
+ {decorate && (
713
+ <ConicalGradient
714
+ width={width}
715
+ height={height}
716
+ centerX={halfChartWidth + marginLeft}
717
+ centerY={halfChartHeight + marginTop}
718
+ config={decorate}
719
+ arcs={_arcs}
720
+ radius={radius}
721
+ />
722
+ )}
723
+ <Legend {...legend} series={_arcs} formatter={formatter} />
724
+ </>
725
+ );
726
+ }
727
+ );
728
+
729
+ const Current = ({
730
+ config: {
731
+ show,
732
+ gap,
733
+ name: { show: showName, sameColor: nameColor, font: nameFont, textBreak },
734
+ percent: {
735
+ show: showPercent,
736
+ sameColor: percentColor,
737
+ font: percentFont,
738
+ precision,
739
+ translate: { x: translatePercentX, y: translatePercentY },
740
+ },
741
+ value: {
742
+ show: showValue,
743
+ sameColor: valueColor,
744
+ font: valueFont,
745
+ translate: { x: translateValueX, y: translateValueY },
746
+ suffix: {
747
+ show: showSuffix,
748
+ fontSize,
749
+ text,
750
+ translate: { x: translateSuffixX, y: translateSuffixY },
751
+ },
752
+ },
753
+ },
754
+ width,
755
+ height,
756
+ data,
757
+ currentIndex,
758
+ }) => {
759
+ const _data = useMemo(() => {
760
+ const legendDataWithPercent = getDataWithPercent(data, precision);
761
+ return sortPie(legendDataWithPercent, '');
762
+ }, [data, precision]);
763
+ const currentData = _data[currentIndex];
764
+
765
+ if (!currentData) return null;
766
+
767
+ const { displayName, fieldName, value, percent } = currentData;
768
+ let nameTemp = displayName ? displayName : fieldName; //类目名
769
+ let nameList = [];
770
+ if (textBreak) {
771
+ //如果限制了首行字符,则切割组件
772
+ while (nameTemp.length > textBreak) {
773
+ nameList.push(nameTemp.slice(0, textBreak));
774
+ nameTemp = nameTemp.slice(textBreak);
775
+ }
776
+ }
777
+ nameList.push(nameTemp);
778
+ let foreignStyle = {
779
+ //foreignObject标签样式,
780
+ width,
781
+ height,
782
+ transform: `translate(-${width / 2}px,-${height / 2}px)`,
783
+ pointerEvents: 'none',
784
+ },
785
+ boxStyle = {
786
+ //弹性盒子样式,用于当前值的上下居中对齐等
787
+ width,
788
+ height,
789
+ display: 'flex',
790
+ flexDirection: 'column',
791
+ justifyContent: 'center',
792
+ alignItems: 'center',
793
+ };
794
+ let seriesColor = currentData.series.color;
795
+ seriesColor =
796
+ seriesColor.type == 'pure'
797
+ ? seriesColor.pure
798
+ : seriesColor.linear.stops[0].color;
799
+ return (
800
+ show && (
801
+ <foreignObject style={foreignStyle}>
802
+ <div style={boxStyle}>
803
+ {
804
+ //类目名称
805
+ showName && (
806
+ <div
807
+ style={{
808
+ ...getFontStyle(nameFont),
809
+ margin: gap / 2 + 'px 0',
810
+ display: 'flex',
811
+ flexDirection: 'column',
812
+ alignItems: 'center',
813
+ color: nameColor ? seriesColor : nameFont.color,
814
+ }}
815
+ >
816
+ {nameList.map((d, i) => {
817
+ return <span key={i}>{d}</span>;
818
+ })}
819
+ </div>
820
+ )
821
+ }
822
+ {
823
+ //真实值
824
+ showValue && (
825
+ <span
826
+ style={{
827
+ ...getFontStyle(valueFont),
828
+ transform:
829
+ 'translate(' +
830
+ translateValueX +
831
+ 'px,' +
832
+ translateValueY +
833
+ 'px)',
834
+ margin: gap / 2 + 'px 0',
835
+ color: valueColor ? seriesColor : valueFont.color,
836
+ }}
837
+ >
838
+ {value}
839
+ {showSuffix && text && (
840
+ <span
841
+ style={{
842
+ transform:
843
+ 'translate(' +
844
+ translateSuffixX +
845
+ 'px,' +
846
+ translateSuffixY +
847
+ 'px)',
848
+ fontSize: fontSize,
849
+ }}
850
+ >
851
+ {text}
852
+ </span>
853
+ )}
854
+ </span>
855
+ )
856
+ }
857
+ {
858
+ //百分比值
859
+ showPercent && (
860
+ <span
861
+ style={{
862
+ transform:
863
+ 'translate(' +
864
+ translatePercentX +
865
+ 'px,' +
866
+ translatePercentY +
867
+ 'px)',
868
+ ...getFontStyle(percentFont),
869
+ margin: gap / 2 + 'px 0',
870
+ color: percentColor ? seriesColor : percentFont.color,
871
+ }}
872
+ >
873
+ {percent + '%'}
874
+ </span>
875
+ )
876
+ }
877
+ </div>
878
+ </foreignObject>
879
+ )
880
+ );
881
+ };
882
+
883
+ const Label = ({
884
+ config: {
885
+ maxRadius = 0,
886
+ lineLength,
887
+ lineColor,
888
+ distance,
889
+ mode,
890
+ show,
891
+ translate: { x: translateX, y: translateY },
892
+ name: { show: showName, font: nameFont },
893
+ value: {
894
+ show: showValue,
895
+ font: valueFont,
896
+ suffix: {
897
+ show: showSuffix,
898
+ text,
899
+ fontSize: suffixFontSize,
900
+ translate: { x: suffixTranslateX, y: suffixTranslateY },
901
+ },
902
+ sameColor: valueSameColor = false,
903
+ },
904
+ percent: {
905
+ show: showPercent,
906
+ font: percentFont,
907
+ precision,
908
+ sameColor: percentSameColor = false,
909
+ },
910
+ },
911
+ arcs,
912
+ animation,
913
+ }) => {
914
+ // const [labels, setLabels] = useState(null);
915
+ // const [opacity, setOpacity] = useState(0);
916
+
917
+ const _arcs = useMemo(
918
+ () => getDataWithPercent(arcs, precision),
919
+ [arcs, precision]
920
+ );
921
+ // useEffect(() => {
922
+ // if (labels) {
923
+ // const children = [...labels.children];
924
+ // const bbox = children.reduce(
925
+ // (prev, current) => {
926
+ // const { topRight, bottomRight, bottomLeft, topLeft } = prev;
927
+ // const { x, y, height } = current.getBBox();
928
+
929
+ // current._y1 = y;
930
+ // current._y2 = y + height;
931
+ // current._delta = 0;
932
+
933
+ // if (x > 0) {
934
+ // if (y > 0) {
935
+ // bottomRight.push(current);
936
+ // } else {
937
+ // topRight.push(current);
938
+ // }
939
+ // } else {
940
+ // if (y > 0) {
941
+ // bottomLeft.push(current);
942
+ // } else {
943
+ // topLeft.push(current);
944
+ // }
945
+ // }
946
+ // return prev;
947
+ // },
948
+ // {
949
+ // topRight: [],
950
+ // bottomRight: [],
951
+ // bottomLeft: [],
952
+ // topLeft: [],
953
+ // }
954
+ // );
955
+ // console.log('bbox: ', bbox);
956
+ // }
957
+ // }, [labels]);
958
+
959
+ return (
960
+ <g
961
+ // style={{ opacity }} ref={setLabels}
962
+ >
963
+ {_arcs.map(
964
+ (
965
+ {
966
+ series: {
967
+ color: {
968
+ type,
969
+ pure,
970
+ linear: { stops },
971
+ },
972
+ },
973
+ displayName,
974
+ value,
975
+ percent,
976
+ arc,
977
+ outerRadius,
978
+ index: actualIndex,
979
+ },
980
+ index
981
+ ) => {
982
+ const [x, y] = arc.centroid();
983
+ const midAngle = Math.atan2(y, x);
984
+
985
+ const [x1, y1] = getCoord(
986
+ midAngle,
987
+ maxRadius ? maxRadius : outerRadius
988
+ );
989
+
990
+ const radius = (maxRadius ? maxRadius : outerRadius) + distance;
991
+ const [x2, y2] = getCoord(midAngle, radius);
992
+
993
+ const direction = x2 < 0 ? -1 : 1;
994
+ const x3 = x2 + lineLength * direction;
995
+
996
+ const _x = x3 + (translateX + 6) * direction;
997
+
998
+ const _showName = showName && displayName;
999
+ const _showValue = showValue && (value || showSuffix);
1000
+
1001
+ return (
1002
+ show &&
1003
+ (_showName || showPercent || showValue) && (
1004
+ <g key={index}>
1005
+ <path
1006
+ className={animation ? ringCss['label-line'] : ''}
1007
+ style={{
1008
+ animationDelay: `${
1009
+ animation ? (actualIndex + 1) * 2000 - 800 : 0
1010
+ }ms`,
1011
+ }}
1012
+ d={
1013
+ 'M' +
1014
+ x1 +
1015
+ ', ' +
1016
+ y1 +
1017
+ 'L' +
1018
+ x2 +
1019
+ ', ' +
1020
+ y2 +
1021
+ 'L' +
1022
+ x3 +
1023
+ ', ' +
1024
+ y2
1025
+ }
1026
+ stroke={
1027
+ lineColor
1028
+ ? lineColor
1029
+ : type == 'pure'
1030
+ ? pure
1031
+ : stops[0].color
1032
+ }
1033
+ fill='none'
1034
+ />
1035
+ <text
1036
+ className={animation ? ringCss['label-text'] : ''}
1037
+ style={{
1038
+ animationDelay: `${
1039
+ animation ? (actualIndex + 1) * 2000 - 800 : 0
1040
+ }ms`,
1041
+ }}
1042
+ x={_x}
1043
+ y={y2 + translateY}
1044
+ dominantBaseline='middle'
1045
+ textAnchor={x3 >= 0 ? 'start' : 'end'}
1046
+ >
1047
+ {_showName && (
1048
+ <tspan style={getFontStyle(nameFont, 'svg')}>
1049
+ {displayName + (showValue || showPercent ? ':' : '')}
1050
+ </tspan>
1051
+ )}
1052
+ {showValue && (
1053
+ <>
1054
+ <tspan
1055
+ x={!!(_showName && mode == 'vertical') ? _x : ''}
1056
+ dx={valueDx(_showName, mode)}
1057
+ dy={!!(_showName && mode == 'vertical') ? '1.5em' : ''}
1058
+ style={getFontStyle(valueFont, 'svg')}
1059
+ >
1060
+ {value}
1061
+ </tspan>
1062
+ {showSuffix && (
1063
+ <tspan
1064
+ style={{
1065
+ ...getFontStyle(valueFont, 'svg'),
1066
+ fontSize: suffixFontSize,
1067
+ }}
1068
+ dx={suffixTranslateX}
1069
+ dy={suffixTranslateY}
1070
+ >
1071
+ {text}
1072
+ </tspan>
1073
+ )}
1074
+ </>
1075
+ )}
1076
+ {showPercent && (
1077
+ <tspan
1078
+ x={percentX(_showName, _showValue, mode, _x)}
1079
+ dx={percentDx(_showName, _showValue, mode)}
1080
+ dy={
1081
+ percentDy(_showName, _showValue, mode) +
1082
+ (_showValue && showSuffix ? suffixTranslateY * -1 : '')
1083
+ }
1084
+ style={getFontStyle(percentFont, 'svg')}
1085
+ >
1086
+ {(_showValue ? '(' : '') +
1087
+ percent +
1088
+ '%' +
1089
+ (_showValue ? ')' : '')}
1090
+ </tspan>
1091
+ )}
1092
+ </text>
1093
+ </g>
1094
+ )
1095
+ );
1096
+ }
1097
+ )}
1098
+ </g>
1099
+ );
1100
+ };
1101
+
1102
+ const RingLabel = ({
1103
+ config: {
1104
+ maxRadius = 0,
1105
+ lineLength,
1106
+ lineColor,
1107
+ distance,
1108
+ mode,
1109
+ show,
1110
+ translate: { x: translateX, y: translateY },
1111
+ name: { show: showName, font: nameFont },
1112
+ value: {
1113
+ show: showValue,
1114
+ font: valueFont,
1115
+ suffix: {
1116
+ show: showSuffix,
1117
+ text,
1118
+ fontSize: suffixFontSize,
1119
+ translate: { x: suffixTranslateX, y: suffixTranslateY },
1120
+ },
1121
+ sameColor: valueSameColor = false,
1122
+ },
1123
+ percent: {
1124
+ show: showPercent,
1125
+ font: percentFont,
1126
+ precision,
1127
+ sameColor: percentSameColor = false,
1128
+ },
1129
+ },
1130
+ arcs,
1131
+ }) => {
1132
+ const _arcs = useMemo(
1133
+ () => getDataWithPercent(arcs, precision),
1134
+ [arcs, precision]
1135
+ );
1136
+
1137
+ return (
1138
+ <g>
1139
+ {_arcs.map(
1140
+ (
1141
+ {
1142
+ series: {
1143
+ color: {
1144
+ type,
1145
+ pure,
1146
+ linear: { stops },
1147
+ },
1148
+ },
1149
+ data: realData,
1150
+ displayName,
1151
+ value,
1152
+ percent,
1153
+ arc,
1154
+ outerRadius,
1155
+ index: actualIndex,
1156
+ },
1157
+ index
1158
+ ) => {
1159
+ const [x, y] = arc.centroid();
1160
+
1161
+ const midAngle = Math.atan2(y, x);
1162
+
1163
+ const [x1, y1] = getCoord(
1164
+ midAngle,
1165
+ maxRadius ? maxRadius : outerRadius
1166
+ );
1167
+
1168
+ const radius = (maxRadius ? maxRadius : outerRadius) + distance;
1169
+ const [x2, y2] = getCoord(midAngle, radius);
1170
+
1171
+ const directionX = x2 < 0 ? -1 : 1;
1172
+ const directionY = y2 < 0 ? -1 : 1;
1173
+ const x3 = x2 + lineLength * directionX;
1174
+ const _x = x3 + (translateX + 6) * directionX;
1175
+
1176
+ const _showName = showName && displayName;
1177
+ const _showValue = showValue && (value || showSuffix);
1178
+
1179
+ return (
1180
+ show &&
1181
+ (_showName || showPercent || _showValue) && (
1182
+ <g key={index}>
1183
+ <path
1184
+ className={ringCss['label-line']}
1185
+ style={{
1186
+ animationDelay: `${(actualIndex + 1) * 2000 - 800}ms`,
1187
+ }}
1188
+ d={
1189
+ 'M' +
1190
+ x1 +
1191
+ ', ' +
1192
+ y1 +
1193
+ 'L' +
1194
+ x2 +
1195
+ ', ' +
1196
+ y2 +
1197
+ 'L' +
1198
+ x3 +
1199
+ ', ' +
1200
+ y2
1201
+ }
1202
+ stroke={
1203
+ lineColor
1204
+ ? lineColor
1205
+ : type == 'pure'
1206
+ ? pure
1207
+ : stops[0].color
1208
+ }
1209
+ fill='none'
1210
+ />
1211
+ <text
1212
+ className={ringCss['label-text']}
1213
+ style={{
1214
+ animationDelay: `${(actualIndex + 1) * 2000 - 800}ms`,
1215
+ }}
1216
+ x={mode == 'horizontal' ? _x : x2}
1217
+ y={y2 + translateY}
1218
+ dominantBaseline='middle'
1219
+ textAnchor={x3 >= 0 ? 'start' : 'end'}
1220
+ >
1221
+ {_showName && (
1222
+ <tspan
1223
+ dy={nameDy(_showValue, showPercent, mode, directionY)}
1224
+ style={getFontStyle(nameFont, 'svg')}
1225
+ >
1226
+ {displayName + (showValue || showPercent ? ':' : '')}
1227
+ </tspan>
1228
+ )}
1229
+ {_showValue && (
1230
+ <>
1231
+ <tspan
1232
+ x={_showName ? (mode == 'horizontal' ? '' : x2) : ''}
1233
+ dx={valueDx(_showName, mode)}
1234
+ dy={valueDy(_showName, mode, directionY)}
1235
+ style={getFontStyle(valueFont, 'svg')}
1236
+ >
1237
+ {realData.y}
1238
+ </tspan>
1239
+ {showSuffix && (
1240
+ <tspan
1241
+ style={{
1242
+ ...getFontStyle(valueFont, 'svg'),
1243
+ fontSize: suffixFontSize,
1244
+ }}
1245
+ dx={suffixTranslateX}
1246
+ dy={suffixTranslateY}
1247
+ >
1248
+ {text}
1249
+ </tspan>
1250
+ )}
1251
+ </>
1252
+ )}
1253
+ {showPercent && (
1254
+ <tspan
1255
+ x={
1256
+ _showName
1257
+ ? _showValue
1258
+ ? ''
1259
+ : mode == 'vertical'
1260
+ ? x2
1261
+ : ''
1262
+ : ''
1263
+ }
1264
+ dx={percentDx(_showName, _showValue, mode)}
1265
+ dy={
1266
+ percentDy_(_showName, _showValue, mode, directionY) +
1267
+ (_showValue && showSuffix ? suffixTranslateY * -1 : '')
1268
+ }
1269
+ style={getFontStyle(percentFont, 'svg')}
1270
+ >
1271
+ {(_showValue ? '(' : '') +
1272
+ percent +
1273
+ '%' +
1274
+ (_showValue ? ')' : '')}
1275
+ </tspan>
1276
+ )}
1277
+ </text>
1278
+ </g>
1279
+ )
1280
+ );
1281
+ }
1282
+ )}
1283
+ </g>
1284
+ );
1285
+ };
1286
+
1287
+ export default Mapping(Carousel(Component));