@easyv/charts 1.2.3 → 1.2.6
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.
- package/.babelrc +8 -8
- package/.husky/commit-msg +3 -3
- package/CHANGELOG.md +18 -18
- package/lib/components/AnimateData.js +11 -7
- package/lib/components/Axis.js +18 -14
- package/lib/components/Background.js +2 -2
- package/lib/components/Band.js +8 -4
- package/lib/components/BaseLine.js +76 -0
- package/lib/components/Brush.js +2 -2
- package/lib/components/Carousel.js +15 -8
- package/lib/components/CartesianChart.js +26 -7
- package/lib/components/Chart.js +9 -6
- package/lib/components/ChartContainer.js +8 -4
- package/lib/components/ConicalGradient.js +27 -23
- package/lib/components/ExtentData.js +7 -4
- package/lib/components/FilterData.js +7 -4
- package/lib/components/Indicator.js +2 -2
- package/lib/components/Label.js +2 -2
- package/lib/components/Legend.js +11 -5
- package/lib/components/Lighter.js +179 -179
- package/lib/components/Line.js +11 -5
- package/lib/components/LinearGradient.js +2 -2
- package/lib/components/Mapping.js +8 -5
- package/lib/components/Marquee.js +14 -14
- package/lib/components/PieChart.js +17 -8
- package/lib/components/StackData.js +7 -4
- package/lib/components/StereoBar.js +8 -4
- package/lib/components/TextOverflow.js +65 -0
- package/lib/components/Tooltip.js +2 -2
- package/lib/components/index.js +58 -50
- package/lib/css/index.module.css +41 -41
- package/lib/css/piechart.module.css +26 -26
- package/lib/element/ConicGradient.js +72 -72
- package/lib/formatter/legend.js +2 -2
- package/lib/hooks/index.js +14 -14
- package/lib/hooks/useAnimateData.js +7 -7
- package/lib/hooks/useAxes.js +7 -7
- package/lib/hooks/useCarouselAxisX.js +7 -7
- package/lib/hooks/useExtentData.js +8 -8
- package/lib/hooks/useFilterData.js +8 -8
- package/lib/hooks/useStackData.js +10 -8
- package/lib/hooks/useTooltip.js +10 -10
- package/lib/index.js +6 -2
- package/lib/utils/index.js +31 -8
- package/package.json +53 -51
- package/src/components/AnimateData.tsx +24 -24
- package/src/components/Axis.tsx +354 -354
- package/src/components/Background.tsx +45 -45
- package/src/components/Band.tsx +173 -173
- package/src/components/BaseLine.js +76 -0
- package/src/components/Brush.js +159 -159
- package/src/components/Carousel.tsx +144 -144
- package/src/components/CartesianChart.js +22 -5
- package/src/components/Chart.js +99 -99
- package/src/components/ChartContainer.tsx +63 -63
- package/src/components/ConicalGradient.js +258 -258
- package/src/components/ExtentData.js +17 -17
- package/src/components/FilterData.js +23 -23
- package/src/components/Indicator.js +13 -13
- package/src/components/Label.js +206 -206
- package/src/components/Legend.js +158 -158
- package/src/components/Lighter.jsx +173 -173
- package/src/components/Line.js +144 -144
- package/src/components/LinearGradient.js +29 -29
- package/src/components/Mapping.js +71 -71
- package/src/components/Marquee.js +93 -93
- package/src/components/PieChart.js +1288 -1288
- package/src/components/StackData.js +20 -20
- package/src/components/StereoBar.tsx +310 -310
- package/src/components/TextOverflow.js +51 -0
- package/src/components/Tooltip.js +169 -169
- package/src/components/index.js +55 -53
- package/src/context/index.js +2 -2
- package/src/css/index.module.css +41 -41
- package/src/css/piechart.module.css +26 -26
- package/src/element/ConicGradient.jsx +55 -55
- package/src/element/Line.tsx +33 -33
- package/src/element/index.ts +3 -3
- package/src/formatter/index.js +1 -1
- package/src/formatter/legend.js +90 -90
- package/src/hooks/index.js +17 -17
- package/src/hooks/useAnimateData.ts +67 -67
- package/src/hooks/useAxes.js +144 -144
- package/src/hooks/useCarouselAxisX.js +163 -163
- package/src/hooks/useExtentData.js +88 -88
- package/src/hooks/useFilterData.js +72 -72
- package/src/hooks/useStackData.js +100 -100
- package/src/hooks/useTooltip.ts +96 -96
- package/src/index.js +6 -6
- package/src/types/index.d.ts +67 -67
- package/src/utils/index.js +714 -696
- package/tsconfig.json +22 -22
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import * as d3 from 'd3';
|
|
2
|
-
|
|
3
|
-
//只支持在svg中使用
|
|
4
|
-
//在<clipPath>标签中绘制圆环路径,在<foreignObject>中使用conic-gradient来制作角向渐变
|
|
5
|
-
//然后在foreignObject中调用clipPath来剪切dom元素,实现环形渐变效果
|
|
6
|
-
export default function ConicGradient(props){
|
|
7
|
-
const {
|
|
8
|
-
gradientId, //渐变ID,每一个环形渐变都需要使用独有的id,防止大屏中存在多个环形渐变时,出现渐变混用的情况。
|
|
9
|
-
colorConfig={},
|
|
10
|
-
circleConfig={}
|
|
11
|
-
} = props,
|
|
12
|
-
{
|
|
13
|
-
colorType = "linear", //渐变类型
|
|
14
|
-
colors = [{offset:0,color:"rgba(255,0,0,0.5)"},
|
|
15
|
-
{offset:50,color:"rgba(0,255,0,0.5)"},
|
|
16
|
-
{offset:100,color:"rgba(0,0,255,0.5)"}] //渐变颜色
|
|
17
|
-
} = colorConfig,
|
|
18
|
-
{
|
|
19
|
-
innerRadius = 80, //圆环内半径
|
|
20
|
-
outerRadius = 100, //圆环外半径
|
|
21
|
-
startAngle = 0, //初始角度
|
|
22
|
-
endAngle = 360, //结束角度
|
|
23
|
-
cornerRadius = 0, //圆环两端的圆角
|
|
24
|
-
padAngle = 0 //圆环内边距,用于多个圆环之间的间距
|
|
25
|
-
} = circleConfig;
|
|
26
|
-
const arc = d3
|
|
27
|
-
.arc()
|
|
28
|
-
.innerRadius(innerRadius)
|
|
29
|
-
.outerRadius(outerRadius)
|
|
30
|
-
.startAngle((startAngle * Math.PI) / 180)
|
|
31
|
-
.endAngle((endAngle * Math.PI) / 180)
|
|
32
|
-
.cornerRadius(cornerRadius)
|
|
33
|
-
.padAngle(padAngle);
|
|
34
|
-
return <g>
|
|
35
|
-
<clipPath id={""+gradientId} style={{transform:`translate(50%,50%)`}}>
|
|
36
|
-
<path d={arc()}></path>
|
|
37
|
-
</clipPath>
|
|
38
|
-
<foreignObject style={{
|
|
39
|
-
width:"100%",
|
|
40
|
-
height:"100%",
|
|
41
|
-
transform:`translate(-50%,-50%)`,
|
|
42
|
-
background:colorType==="pure"?
|
|
43
|
-
colors : `conic-gradient(${colorsToString(colors)})`,
|
|
44
|
-
clipPath:`url(${'#'+gradientId})`
|
|
45
|
-
}}></foreignObject>
|
|
46
|
-
</g>
|
|
47
|
-
}
|
|
48
|
-
function colorsToString(colors){
|
|
49
|
-
let s="";
|
|
50
|
-
colors.sort((a,b)=>{
|
|
51
|
-
return a.offset-b.offset;
|
|
52
|
-
}).map(d=>{
|
|
53
|
-
s=s+d.color+" "+d.offset+"%,";
|
|
54
|
-
})
|
|
55
|
-
return s.slice(0,-1)
|
|
1
|
+
import * as d3 from 'd3';
|
|
2
|
+
|
|
3
|
+
//只支持在svg中使用
|
|
4
|
+
//在<clipPath>标签中绘制圆环路径,在<foreignObject>中使用conic-gradient来制作角向渐变
|
|
5
|
+
//然后在foreignObject中调用clipPath来剪切dom元素,实现环形渐变效果
|
|
6
|
+
export default function ConicGradient(props){
|
|
7
|
+
const {
|
|
8
|
+
gradientId, //渐变ID,每一个环形渐变都需要使用独有的id,防止大屏中存在多个环形渐变时,出现渐变混用的情况。
|
|
9
|
+
colorConfig={},
|
|
10
|
+
circleConfig={}
|
|
11
|
+
} = props,
|
|
12
|
+
{
|
|
13
|
+
colorType = "linear", //渐变类型
|
|
14
|
+
colors = [{offset:0,color:"rgba(255,0,0,0.5)"},
|
|
15
|
+
{offset:50,color:"rgba(0,255,0,0.5)"},
|
|
16
|
+
{offset:100,color:"rgba(0,0,255,0.5)"}] //渐变颜色
|
|
17
|
+
} = colorConfig,
|
|
18
|
+
{
|
|
19
|
+
innerRadius = 80, //圆环内半径
|
|
20
|
+
outerRadius = 100, //圆环外半径
|
|
21
|
+
startAngle = 0, //初始角度
|
|
22
|
+
endAngle = 360, //结束角度
|
|
23
|
+
cornerRadius = 0, //圆环两端的圆角
|
|
24
|
+
padAngle = 0 //圆环内边距,用于多个圆环之间的间距
|
|
25
|
+
} = circleConfig;
|
|
26
|
+
const arc = d3
|
|
27
|
+
.arc()
|
|
28
|
+
.innerRadius(innerRadius)
|
|
29
|
+
.outerRadius(outerRadius)
|
|
30
|
+
.startAngle((startAngle * Math.PI) / 180)
|
|
31
|
+
.endAngle((endAngle * Math.PI) / 180)
|
|
32
|
+
.cornerRadius(cornerRadius)
|
|
33
|
+
.padAngle(padAngle);
|
|
34
|
+
return <g>
|
|
35
|
+
<clipPath id={""+gradientId} style={{transform:`translate(50%,50%)`}}>
|
|
36
|
+
<path d={arc()}></path>
|
|
37
|
+
</clipPath>
|
|
38
|
+
<foreignObject style={{
|
|
39
|
+
width:"100%",
|
|
40
|
+
height:"100%",
|
|
41
|
+
transform:`translate(-50%,-50%)`,
|
|
42
|
+
background:colorType==="pure"?
|
|
43
|
+
colors : `conic-gradient(${colorsToString(colors)})`,
|
|
44
|
+
clipPath:`url(${'#'+gradientId})`
|
|
45
|
+
}}></foreignObject>
|
|
46
|
+
</g>
|
|
47
|
+
}
|
|
48
|
+
function colorsToString(colors){
|
|
49
|
+
let s="";
|
|
50
|
+
colors.sort((a,b)=>{
|
|
51
|
+
return a.offset-b.offset;
|
|
52
|
+
}).map(d=>{
|
|
53
|
+
s=s+d.color+" "+d.offset+"%,";
|
|
54
|
+
})
|
|
55
|
+
return s.slice(0,-1)
|
|
56
56
|
}
|
package/src/element/Line.tsx
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
|
|
3
|
-
type Props = {
|
|
4
|
-
config: ChartLine;
|
|
5
|
-
x1: number;
|
|
6
|
-
x2: number;
|
|
7
|
-
y1: number;
|
|
8
|
-
y2: number;
|
|
9
|
-
className: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export default ({
|
|
13
|
-
config: { show, color, lineWidth, strokeDasharray },
|
|
14
|
-
x1,
|
|
15
|
-
x2,
|
|
16
|
-
y1,
|
|
17
|
-
y2,
|
|
18
|
-
className,
|
|
19
|
-
}: Props) => {
|
|
20
|
-
if (!show) return null;
|
|
21
|
-
return (
|
|
22
|
-
<line
|
|
23
|
-
className={className}
|
|
24
|
-
x1={x1}
|
|
25
|
-
x2={x2}
|
|
26
|
-
y1={y1}
|
|
27
|
-
y2={y2}
|
|
28
|
-
stroke={color}
|
|
29
|
-
strokeWidth={lineWidth}
|
|
30
|
-
strokeDasharray={strokeDasharray}
|
|
31
|
-
/>
|
|
32
|
-
);
|
|
33
|
-
};
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type Props = {
|
|
4
|
+
config: ChartLine;
|
|
5
|
+
x1: number;
|
|
6
|
+
x2: number;
|
|
7
|
+
y1: number;
|
|
8
|
+
y2: number;
|
|
9
|
+
className: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default ({
|
|
13
|
+
config: { show, color, lineWidth, strokeDasharray },
|
|
14
|
+
x1,
|
|
15
|
+
x2,
|
|
16
|
+
y1,
|
|
17
|
+
y2,
|
|
18
|
+
className,
|
|
19
|
+
}: Props) => {
|
|
20
|
+
if (!show) return null;
|
|
21
|
+
return (
|
|
22
|
+
<line
|
|
23
|
+
className={className}
|
|
24
|
+
x1={x1}
|
|
25
|
+
x2={x2}
|
|
26
|
+
y1={y1}
|
|
27
|
+
y2={y2}
|
|
28
|
+
stroke={color}
|
|
29
|
+
strokeWidth={lineWidth}
|
|
30
|
+
strokeDasharray={strokeDasharray}
|
|
31
|
+
/>
|
|
32
|
+
);
|
|
33
|
+
};
|
package/src/element/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import Line from './Line';
|
|
2
|
-
|
|
3
|
-
export { Line };
|
|
1
|
+
import Line from './Line';
|
|
2
|
+
|
|
3
|
+
export { Line };
|
package/src/formatter/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './legend';
|
|
1
|
+
export * from './legend';
|
package/src/formatter/legend.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import css from '../css/index.module.css';
|
|
2
|
-
import { getFontStyle } from '../utils';
|
|
3
|
-
export const pieLegendFormatter = (
|
|
4
|
-
{ displayName, data, percent, series: { color: seriesColor, icon="" } },
|
|
5
|
-
{
|
|
6
|
-
name: { show: showName, font: nameFont },
|
|
7
|
-
value: {
|
|
8
|
-
show: showValue,
|
|
9
|
-
font: valueFont,
|
|
10
|
-
sameColor: valueSameColor,
|
|
11
|
-
gap: valueGap,
|
|
12
|
-
suffix: {
|
|
13
|
-
show: showSuffix,
|
|
14
|
-
text,
|
|
15
|
-
fontSize,
|
|
16
|
-
translate: { x, y },
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
percent: {
|
|
20
|
-
show: showPercent,
|
|
21
|
-
font: percentFont,
|
|
22
|
-
sameColor: percentSameColor,
|
|
23
|
-
gap: percentGap,
|
|
24
|
-
},
|
|
25
|
-
}
|
|
26
|
-
) => {
|
|
27
|
-
const {
|
|
28
|
-
type: seriesColorType,
|
|
29
|
-
pure,
|
|
30
|
-
linear: { stops },
|
|
31
|
-
} = seriesColor;
|
|
32
|
-
|
|
33
|
-
const _color = seriesColorType == 'pure' ? pure : stops[0].color;
|
|
34
|
-
return (
|
|
35
|
-
<>
|
|
36
|
-
{icon &&<span style={{ ...icon, marginRight: icon.marginRight }} />}
|
|
37
|
-
<span
|
|
38
|
-
className={
|
|
39
|
-
showName && showValue && showPercent
|
|
40
|
-
? css.showAllStyle
|
|
41
|
-
: css.notShowAllStyle
|
|
42
|
-
}
|
|
43
|
-
>
|
|
44
|
-
{showName && (
|
|
45
|
-
<span style={{ paddingLeft: 5, ...getFontStyle(nameFont), whiteSpace:"nowrap" }}>
|
|
46
|
-
{displayName}
|
|
47
|
-
</span>
|
|
48
|
-
)}
|
|
49
|
-
{showValue && (
|
|
50
|
-
<span
|
|
51
|
-
style={{
|
|
52
|
-
whiteSpace:"nowrap",
|
|
53
|
-
...getFontStyle(valueFont),
|
|
54
|
-
marginLeft: valueGap,
|
|
55
|
-
display: 'flex',
|
|
56
|
-
color: valueSameColor ? _color : valueFont.color,
|
|
57
|
-
alignItems: 'center',
|
|
58
|
-
justifyContent: 'flex-start',
|
|
59
|
-
}}
|
|
60
|
-
>
|
|
61
|
-
<span>{data.y}</span>
|
|
62
|
-
{showSuffix && (
|
|
63
|
-
<span
|
|
64
|
-
style={{
|
|
65
|
-
whiteSpace:"nowrap",
|
|
66
|
-
fontSize,
|
|
67
|
-
transform: 'translate3d(' + x + 'px, ' + y + 'px, 0px)',
|
|
68
|
-
}}
|
|
69
|
-
>
|
|
70
|
-
{text}
|
|
71
|
-
</span>
|
|
72
|
-
)}
|
|
73
|
-
</span>
|
|
74
|
-
)}
|
|
75
|
-
{showPercent && (
|
|
76
|
-
<span
|
|
77
|
-
style={{
|
|
78
|
-
whiteSpace:"nowrap",
|
|
79
|
-
...getFontStyle(percentFont),
|
|
80
|
-
marginLeft: percentGap,
|
|
81
|
-
color: percentSameColor ? _color : percentFont.color,
|
|
82
|
-
}}
|
|
83
|
-
>
|
|
84
|
-
{percent + '%'}
|
|
85
|
-
</span>
|
|
86
|
-
)}
|
|
87
|
-
</span>
|
|
88
|
-
</>
|
|
89
|
-
);
|
|
90
|
-
};
|
|
1
|
+
import css from '../css/index.module.css';
|
|
2
|
+
import { getFontStyle } from '../utils';
|
|
3
|
+
export const pieLegendFormatter = (
|
|
4
|
+
{ displayName, data, percent, series: { color: seriesColor, icon="" } },
|
|
5
|
+
{
|
|
6
|
+
name: { show: showName, font: nameFont },
|
|
7
|
+
value: {
|
|
8
|
+
show: showValue,
|
|
9
|
+
font: valueFont,
|
|
10
|
+
sameColor: valueSameColor,
|
|
11
|
+
gap: valueGap,
|
|
12
|
+
suffix: {
|
|
13
|
+
show: showSuffix,
|
|
14
|
+
text,
|
|
15
|
+
fontSize,
|
|
16
|
+
translate: { x, y },
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
percent: {
|
|
20
|
+
show: showPercent,
|
|
21
|
+
font: percentFont,
|
|
22
|
+
sameColor: percentSameColor,
|
|
23
|
+
gap: percentGap,
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
) => {
|
|
27
|
+
const {
|
|
28
|
+
type: seriesColorType,
|
|
29
|
+
pure,
|
|
30
|
+
linear: { stops },
|
|
31
|
+
} = seriesColor;
|
|
32
|
+
|
|
33
|
+
const _color = seriesColorType == 'pure' ? pure : stops[0].color;
|
|
34
|
+
return (
|
|
35
|
+
<>
|
|
36
|
+
{icon &&<span style={{ ...icon, marginRight: icon.marginRight }} />}
|
|
37
|
+
<span
|
|
38
|
+
className={
|
|
39
|
+
showName && showValue && showPercent
|
|
40
|
+
? css.showAllStyle
|
|
41
|
+
: css.notShowAllStyle
|
|
42
|
+
}
|
|
43
|
+
>
|
|
44
|
+
{showName && (
|
|
45
|
+
<span style={{ paddingLeft: 5, ...getFontStyle(nameFont), whiteSpace:"nowrap" }}>
|
|
46
|
+
{displayName}
|
|
47
|
+
</span>
|
|
48
|
+
)}
|
|
49
|
+
{showValue && (
|
|
50
|
+
<span
|
|
51
|
+
style={{
|
|
52
|
+
whiteSpace:"nowrap",
|
|
53
|
+
...getFontStyle(valueFont),
|
|
54
|
+
marginLeft: valueGap,
|
|
55
|
+
display: 'flex',
|
|
56
|
+
color: valueSameColor ? _color : valueFont.color,
|
|
57
|
+
alignItems: 'center',
|
|
58
|
+
justifyContent: 'flex-start',
|
|
59
|
+
}}
|
|
60
|
+
>
|
|
61
|
+
<span>{data.y}</span>
|
|
62
|
+
{showSuffix && (
|
|
63
|
+
<span
|
|
64
|
+
style={{
|
|
65
|
+
whiteSpace:"nowrap",
|
|
66
|
+
fontSize,
|
|
67
|
+
transform: 'translate3d(' + x + 'px, ' + y + 'px, 0px)',
|
|
68
|
+
}}
|
|
69
|
+
>
|
|
70
|
+
{text}
|
|
71
|
+
</span>
|
|
72
|
+
)}
|
|
73
|
+
</span>
|
|
74
|
+
)}
|
|
75
|
+
{showPercent && (
|
|
76
|
+
<span
|
|
77
|
+
style={{
|
|
78
|
+
whiteSpace:"nowrap",
|
|
79
|
+
...getFontStyle(percentFont),
|
|
80
|
+
marginLeft: percentGap,
|
|
81
|
+
color: percentSameColor ? _color : percentFont.color,
|
|
82
|
+
}}
|
|
83
|
+
>
|
|
84
|
+
{percent + '%'}
|
|
85
|
+
</span>
|
|
86
|
+
)}
|
|
87
|
+
</span>
|
|
88
|
+
</>
|
|
89
|
+
);
|
|
90
|
+
};
|
package/src/hooks/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import useFilterData from './useFilterData';
|
|
2
|
-
import useStackData from './useStackData';
|
|
3
|
-
import useAxes from './useAxes';
|
|
4
|
-
import useTooltip from './useTooltip';
|
|
5
|
-
import useAnimateData from './useAnimateData';
|
|
6
|
-
import useCarouselAxisX from './useCarouselAxisX';
|
|
7
|
-
import useExtentData from './useExtentData';
|
|
8
|
-
|
|
9
|
-
export {
|
|
10
|
-
useFilterData,
|
|
11
|
-
useStackData,
|
|
12
|
-
useAxes,
|
|
13
|
-
useTooltip,
|
|
14
|
-
useAnimateData,
|
|
15
|
-
useCarouselAxisX,
|
|
16
|
-
useExtentData,
|
|
17
|
-
};
|
|
1
|
+
import useFilterData from './useFilterData';
|
|
2
|
+
import useStackData from './useStackData';
|
|
3
|
+
import useAxes from './useAxes';
|
|
4
|
+
import useTooltip from './useTooltip';
|
|
5
|
+
import useAnimateData from './useAnimateData';
|
|
6
|
+
import useCarouselAxisX from './useCarouselAxisX';
|
|
7
|
+
import useExtentData from './useExtentData';
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
useFilterData,
|
|
11
|
+
useStackData,
|
|
12
|
+
useAxes,
|
|
13
|
+
useTooltip,
|
|
14
|
+
useAnimateData,
|
|
15
|
+
useCarouselAxisX,
|
|
16
|
+
useExtentData,
|
|
17
|
+
};
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
2
|
-
import { animate, linear } from 'popmotion';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 图表数据动画
|
|
6
|
-
* @param {Array} data data列表
|
|
7
|
-
* @param {boolean} dataAnimation 是否开启数据增长动画
|
|
8
|
-
* @returns 改变后的数据
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export default (data: DataType[], dataAnimation: DataAnimation) => {
|
|
12
|
-
const [animateData, setAnimateData] = useState<DataType[]>([]);
|
|
13
|
-
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
let animateRef: any = null;
|
|
16
|
-
if (
|
|
17
|
-
dataAnimation &&
|
|
18
|
-
dataAnimation.show &&
|
|
19
|
-
dataAnimation.duration &&
|
|
20
|
-
data.length
|
|
21
|
-
) {
|
|
22
|
-
animateRef = animate({
|
|
23
|
-
autoplay: true,
|
|
24
|
-
repeat: 0,
|
|
25
|
-
from: 0,
|
|
26
|
-
to: 1,
|
|
27
|
-
duration: dataAnimation.duration * 1000,
|
|
28
|
-
ease: linear,
|
|
29
|
-
onUpdate: (v: number) => {
|
|
30
|
-
setAnimateData((oldData: DataType[]) => {
|
|
31
|
-
const oldLength = oldData.length;
|
|
32
|
-
const newLength = data.length;
|
|
33
|
-
if (oldLength >= newLength) {
|
|
34
|
-
return oldData.map(({ y }: DataType, i: number) => {
|
|
35
|
-
const current: DataType = data[i] || { y: 0 };
|
|
36
|
-
const delta: number = current.y - y;
|
|
37
|
-
return {
|
|
38
|
-
...current,
|
|
39
|
-
y: Math.floor(y + delta * v),
|
|
40
|
-
};
|
|
41
|
-
});
|
|
42
|
-
} else {
|
|
43
|
-
return data.map((current: DataType, i: number) => {
|
|
44
|
-
const oldCurrent: DataType = oldData[i] || { y: 0 };
|
|
45
|
-
const delta = oldCurrent.y - current.y;
|
|
46
|
-
return {
|
|
47
|
-
...current,
|
|
48
|
-
y: Math.floor(oldCurrent.y + delta * v),
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
onComplete: () => {
|
|
55
|
-
setAnimateData(data);
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
} else {
|
|
59
|
-
setAnimateData(data);
|
|
60
|
-
}
|
|
61
|
-
return () => {
|
|
62
|
-
animateRef && animateRef.stop();
|
|
63
|
-
};
|
|
64
|
-
}, [data, dataAnimation]);
|
|
65
|
-
|
|
66
|
-
return animateData;
|
|
67
|
-
};
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { animate, linear } from 'popmotion';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 图表数据动画
|
|
6
|
+
* @param {Array} data data列表
|
|
7
|
+
* @param {boolean} dataAnimation 是否开启数据增长动画
|
|
8
|
+
* @returns 改变后的数据
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export default (data: DataType[], dataAnimation: DataAnimation) => {
|
|
12
|
+
const [animateData, setAnimateData] = useState<DataType[]>([]);
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
let animateRef: any = null;
|
|
16
|
+
if (
|
|
17
|
+
dataAnimation &&
|
|
18
|
+
dataAnimation.show &&
|
|
19
|
+
dataAnimation.duration &&
|
|
20
|
+
data.length
|
|
21
|
+
) {
|
|
22
|
+
animateRef = animate({
|
|
23
|
+
autoplay: true,
|
|
24
|
+
repeat: 0,
|
|
25
|
+
from: 0,
|
|
26
|
+
to: 1,
|
|
27
|
+
duration: dataAnimation.duration * 1000,
|
|
28
|
+
ease: linear,
|
|
29
|
+
onUpdate: (v: number) => {
|
|
30
|
+
setAnimateData((oldData: DataType[]) => {
|
|
31
|
+
const oldLength = oldData.length;
|
|
32
|
+
const newLength = data.length;
|
|
33
|
+
if (oldLength >= newLength) {
|
|
34
|
+
return oldData.map(({ y }: DataType, i: number) => {
|
|
35
|
+
const current: DataType = data[i] || { y: 0 };
|
|
36
|
+
const delta: number = current.y - y;
|
|
37
|
+
return {
|
|
38
|
+
...current,
|
|
39
|
+
y: Math.floor(y + delta * v),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
} else {
|
|
43
|
+
return data.map((current: DataType, i: number) => {
|
|
44
|
+
const oldCurrent: DataType = oldData[i] || { y: 0 };
|
|
45
|
+
const delta = oldCurrent.y - current.y;
|
|
46
|
+
return {
|
|
47
|
+
...current,
|
|
48
|
+
y: Math.floor(oldCurrent.y + delta * v),
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
onComplete: () => {
|
|
55
|
+
setAnimateData(data);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
} else {
|
|
59
|
+
setAnimateData(data);
|
|
60
|
+
}
|
|
61
|
+
return () => {
|
|
62
|
+
animateRef && animateRef.stop();
|
|
63
|
+
};
|
|
64
|
+
}, [data, dataAnimation]);
|
|
65
|
+
|
|
66
|
+
return animateData;
|
|
67
|
+
};
|