@easyv/charts 1.2.8 → 1.2.11
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/Axis.js +10 -10
- package/lib/components/Background.js +2 -2
- package/lib/components/Carousel.js +2 -2
- package/lib/components/ConicalGradient.js +27 -22
- package/lib/components/Indicator.js +2 -2
- package/lib/components/Lighter.js +179 -179
- package/lib/components/LinearGradient.js +2 -2
- package/lib/components/Marquee.js +3 -3
- package/lib/components/PieChart.js +7 -2
- package/lib/components/TextOverflow.js +3 -3
- 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/hooks/useAnimateData.js +5 -5
- package/lib/hooks/useAxes.js +5 -5
- package/lib/hooks/useCarouselAxisX.js +5 -5
- package/lib/hooks/useExtentData.js +6 -7
- package/lib/hooks/useFilterData.js +5 -5
- package/lib/hooks/useStackData.js +5 -5
- package/lib/hooks/useTooltip.js +10 -10
- package/lib/utils/index.js +1 -1
- package/package.json +53 -53
- 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 +83 -83
- package/src/components/Brush.js +159 -159
- package/src/components/Carousel.tsx +144 -144
- 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 +145 -145
- 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 +1278 -1277
- package/src/components/StackData.js +20 -20
- package/src/components/StereoBar.tsx +310 -310
- package/src/components/TextOverflow.js +51 -51
- package/src/components/Tooltip.js +169 -169
- package/src/components/index.js +55 -55
- 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 -714
- package/tsconfig.json +22 -22
package/src/components/Line.js
CHANGED
|
@@ -1,145 +1,145 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 折线图
|
|
3
|
-
*/
|
|
4
|
-
import React, { memo, useMemo } from 'react';
|
|
5
|
-
import { line as d3Line, area as d3Area, curveCatmullRom, curveMonotoneX } from 'd3v7';
|
|
6
|
-
import { getColorList } from '../utils';
|
|
7
|
-
import { Lighter, LinearGradient } from '.';
|
|
8
|
-
|
|
9
|
-
const defined = (d) => d.data.y != null;
|
|
10
|
-
const getLineData = (data, connectNulls) =>
|
|
11
|
-
connectNulls
|
|
12
|
-
? data.map(({ data, ...item }) => ({
|
|
13
|
-
...item,
|
|
14
|
-
data: { ...data, y: +data.y },
|
|
15
|
-
}))
|
|
16
|
-
: data;
|
|
17
|
-
|
|
18
|
-
const Area = ({
|
|
19
|
-
data,
|
|
20
|
-
config,
|
|
21
|
-
config: {
|
|
22
|
-
id,
|
|
23
|
-
fill,
|
|
24
|
-
type,
|
|
25
|
-
url,
|
|
26
|
-
opacity,
|
|
27
|
-
size: { width: patternW, height: patternH },
|
|
28
|
-
curve,
|
|
29
|
-
tension
|
|
30
|
-
},
|
|
31
|
-
xScaler,
|
|
32
|
-
yScaler,
|
|
33
|
-
}) => {
|
|
34
|
-
const [height] = yScaler.range();
|
|
35
|
-
const area = useMemo(() => getColorList(fill), [fill]);
|
|
36
|
-
|
|
37
|
-
const areaGen = useMemo(() => {
|
|
38
|
-
const areaGen = d3Area()
|
|
39
|
-
.x(({ data: { x } }) => xScaler(x))
|
|
40
|
-
.y1(({ data: { y } }) => yScaler(y))
|
|
41
|
-
// .y0(height)
|
|
42
|
-
.y0(({})=>yScaler(0))
|
|
43
|
-
.defined(defined);
|
|
44
|
-
curve && areaGen.curve(curveCatmullRom.alpha(tension));
|
|
45
|
-
curve && areaGen.curve(curveMonotoneX);
|
|
46
|
-
return areaGen;
|
|
47
|
-
}, [xScaler, yScaler, curve, tension]);
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<>
|
|
51
|
-
<path d={areaGen(data)} stroke='none' fill={'url(#' + id + ')'} />
|
|
52
|
-
<defs>
|
|
53
|
-
{type && type == 'pattern' ? (
|
|
54
|
-
<pattern id={id} patternUnits="userSpaceOnUse" width={patternW} height={patternH}>
|
|
55
|
-
{url && <image opacity={opacity} width={patternW} height={patternH} xlinkHref={window.appConfig.ASSETS_URL + url} />}
|
|
56
|
-
</pattern>
|
|
57
|
-
) : (
|
|
58
|
-
<LinearGradient id={id} colors={area} rotate={0} />
|
|
59
|
-
)}
|
|
60
|
-
</defs>
|
|
61
|
-
</>
|
|
62
|
-
);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export default memo(
|
|
66
|
-
({
|
|
67
|
-
type,
|
|
68
|
-
config: {
|
|
69
|
-
line: {
|
|
70
|
-
type: lineType,
|
|
71
|
-
lineWidth,
|
|
72
|
-
curve,
|
|
73
|
-
tension,
|
|
74
|
-
connectNulls,
|
|
75
|
-
lighter,
|
|
76
|
-
},
|
|
77
|
-
// icon,
|
|
78
|
-
// label,
|
|
79
|
-
},
|
|
80
|
-
line: { id: lineId, stroke },
|
|
81
|
-
area,
|
|
82
|
-
data,
|
|
83
|
-
xAxis: { scaler: xScaler, direction },
|
|
84
|
-
yAxis: { scaler: yScaler },
|
|
85
|
-
}) => {
|
|
86
|
-
if (!data.length) return null;
|
|
87
|
-
const ticks = xScaler.domain();
|
|
88
|
-
|
|
89
|
-
const sortData = useMemo(() => {
|
|
90
|
-
const usefulData = data.filter(
|
|
91
|
-
({ data: { x } }) => ticks.indexOf(x) > -1
|
|
92
|
-
);
|
|
93
|
-
return usefulData.sort(
|
|
94
|
-
({ data: { x: a } }, { data: { x: b } }) =>
|
|
95
|
-
ticks.indexOf(a) - ticks.indexOf(b)
|
|
96
|
-
);
|
|
97
|
-
}, [data, ticks]);
|
|
98
|
-
|
|
99
|
-
const _data = useMemo(
|
|
100
|
-
() => getLineData(sortData, connectNulls),
|
|
101
|
-
[sortData, connectNulls]
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
const lineGen = useMemo(() => {
|
|
105
|
-
const isVertical = direction === 'vertical';
|
|
106
|
-
|
|
107
|
-
let lineGen = (
|
|
108
|
-
isVertical
|
|
109
|
-
? d3Line()
|
|
110
|
-
.y(({ data: { x } }) => xScaler(x))
|
|
111
|
-
.x(({ data: { y } }) => yScaler(y))
|
|
112
|
-
: d3Line()
|
|
113
|
-
.x(({ data: { x } }) => xScaler(x))
|
|
114
|
-
.y(({ data: { y } }) => yScaler(y))
|
|
115
|
-
).defined(defined);
|
|
116
|
-
curve && lineGen.curve(curveCatmullRom.alpha(tension));
|
|
117
|
-
curve && lineGen.curve(curveMonotoneX);
|
|
118
|
-
return lineGen;
|
|
119
|
-
}, [direction, xScaler, yScaler, tension, curve]);
|
|
120
|
-
|
|
121
|
-
const path = lineGen(_data);
|
|
122
|
-
|
|
123
|
-
const showLighter = lighter && lighter.show;
|
|
124
|
-
return (
|
|
125
|
-
<g className='__easyv-line'>
|
|
126
|
-
<path
|
|
127
|
-
d={path}
|
|
128
|
-
stroke={stroke}
|
|
129
|
-
fill='none'
|
|
130
|
-
strokeDasharray={lineType === 'dash' ? '3 3' : null}
|
|
131
|
-
strokeWidth={lineWidth}
|
|
132
|
-
/>
|
|
133
|
-
{type == 'area' && (
|
|
134
|
-
<Area
|
|
135
|
-
data={_data}
|
|
136
|
-
config={{ ...area, curve, tension }}
|
|
137
|
-
xScaler={xScaler}
|
|
138
|
-
yScaler={yScaler}
|
|
139
|
-
/>
|
|
140
|
-
)}
|
|
141
|
-
{showLighter && <Lighter path={path} config={lighter} />}
|
|
142
|
-
</g>
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
);
|
|
1
|
+
/**
|
|
2
|
+
* 折线图
|
|
3
|
+
*/
|
|
4
|
+
import React, { memo, useMemo } from 'react';
|
|
5
|
+
import { line as d3Line, area as d3Area, curveCatmullRom, curveMonotoneX } from 'd3v7';
|
|
6
|
+
import { getColorList } from '../utils';
|
|
7
|
+
import { Lighter, LinearGradient } from '.';
|
|
8
|
+
|
|
9
|
+
const defined = (d) => d.data.y != null;
|
|
10
|
+
const getLineData = (data, connectNulls) =>
|
|
11
|
+
connectNulls
|
|
12
|
+
? data.map(({ data, ...item }) => ({
|
|
13
|
+
...item,
|
|
14
|
+
data: { ...data, y: +data.y },
|
|
15
|
+
}))
|
|
16
|
+
: data;
|
|
17
|
+
|
|
18
|
+
const Area = ({
|
|
19
|
+
data,
|
|
20
|
+
config,
|
|
21
|
+
config: {
|
|
22
|
+
id,
|
|
23
|
+
fill,
|
|
24
|
+
type,
|
|
25
|
+
url,
|
|
26
|
+
opacity,
|
|
27
|
+
size: { width: patternW, height: patternH },
|
|
28
|
+
curve,
|
|
29
|
+
tension
|
|
30
|
+
},
|
|
31
|
+
xScaler,
|
|
32
|
+
yScaler,
|
|
33
|
+
}) => {
|
|
34
|
+
const [height] = yScaler.range();
|
|
35
|
+
const area = useMemo(() => getColorList(fill), [fill]);
|
|
36
|
+
|
|
37
|
+
const areaGen = useMemo(() => {
|
|
38
|
+
const areaGen = d3Area()
|
|
39
|
+
.x(({ data: { x } }) => xScaler(x))
|
|
40
|
+
.y1(({ data: { y } }) => yScaler(y))
|
|
41
|
+
// .y0(height)
|
|
42
|
+
.y0(({})=>yScaler(0))
|
|
43
|
+
.defined(defined);
|
|
44
|
+
curve && areaGen.curve(curveCatmullRom.alpha(tension));
|
|
45
|
+
curve && areaGen.curve(curveMonotoneX);
|
|
46
|
+
return areaGen;
|
|
47
|
+
}, [xScaler, yScaler, curve, tension]);
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<>
|
|
51
|
+
<path d={areaGen(data)} stroke='none' fill={'url(#' + id + ')'} />
|
|
52
|
+
<defs>
|
|
53
|
+
{type && type == 'pattern' ? (
|
|
54
|
+
<pattern id={id} patternUnits="userSpaceOnUse" width={patternW} height={patternH}>
|
|
55
|
+
{url && <image opacity={opacity} width={patternW} height={patternH} xlinkHref={window.appConfig.ASSETS_URL + url} />}
|
|
56
|
+
</pattern>
|
|
57
|
+
) : (
|
|
58
|
+
<LinearGradient id={id} colors={area} rotate={0} />
|
|
59
|
+
)}
|
|
60
|
+
</defs>
|
|
61
|
+
</>
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default memo(
|
|
66
|
+
({
|
|
67
|
+
type,
|
|
68
|
+
config: {
|
|
69
|
+
line: {
|
|
70
|
+
type: lineType,
|
|
71
|
+
lineWidth,
|
|
72
|
+
curve,
|
|
73
|
+
tension,
|
|
74
|
+
connectNulls,
|
|
75
|
+
lighter,
|
|
76
|
+
},
|
|
77
|
+
// icon,
|
|
78
|
+
// label,
|
|
79
|
+
},
|
|
80
|
+
line: { id: lineId, stroke },
|
|
81
|
+
area,
|
|
82
|
+
data,
|
|
83
|
+
xAxis: { scaler: xScaler, direction },
|
|
84
|
+
yAxis: { scaler: yScaler },
|
|
85
|
+
}) => {
|
|
86
|
+
if (!data.length) return null;
|
|
87
|
+
const ticks = xScaler.domain();
|
|
88
|
+
|
|
89
|
+
const sortData = useMemo(() => {
|
|
90
|
+
const usefulData = data.filter(
|
|
91
|
+
({ data: { x } }) => ticks.indexOf(x) > -1
|
|
92
|
+
);
|
|
93
|
+
return usefulData.sort(
|
|
94
|
+
({ data: { x: a } }, { data: { x: b } }) =>
|
|
95
|
+
ticks.indexOf(a) - ticks.indexOf(b)
|
|
96
|
+
);
|
|
97
|
+
}, [data, ticks]);
|
|
98
|
+
|
|
99
|
+
const _data = useMemo(
|
|
100
|
+
() => getLineData(sortData, connectNulls),
|
|
101
|
+
[sortData, connectNulls]
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const lineGen = useMemo(() => {
|
|
105
|
+
const isVertical = direction === 'vertical';
|
|
106
|
+
|
|
107
|
+
let lineGen = (
|
|
108
|
+
isVertical
|
|
109
|
+
? d3Line()
|
|
110
|
+
.y(({ data: { x } }) => xScaler(x))
|
|
111
|
+
.x(({ data: { y } }) => yScaler(y))
|
|
112
|
+
: d3Line()
|
|
113
|
+
.x(({ data: { x } }) => xScaler(x))
|
|
114
|
+
.y(({ data: { y } }) => yScaler(y))
|
|
115
|
+
).defined(defined);
|
|
116
|
+
curve && lineGen.curve(curveCatmullRom.alpha(tension));
|
|
117
|
+
curve && lineGen.curve(curveMonotoneX);
|
|
118
|
+
return lineGen;
|
|
119
|
+
}, [direction, xScaler, yScaler, tension, curve]);
|
|
120
|
+
|
|
121
|
+
const path = lineGen(_data);
|
|
122
|
+
|
|
123
|
+
const showLighter = lighter && lighter.show;
|
|
124
|
+
return (
|
|
125
|
+
<g className='__easyv-line'>
|
|
126
|
+
<path
|
|
127
|
+
d={path}
|
|
128
|
+
stroke={stroke}
|
|
129
|
+
fill='none'
|
|
130
|
+
strokeDasharray={lineType === 'dash' ? '3 3' : null}
|
|
131
|
+
strokeWidth={lineWidth}
|
|
132
|
+
/>
|
|
133
|
+
{type == 'area' && (
|
|
134
|
+
<Area
|
|
135
|
+
data={_data}
|
|
136
|
+
config={{ ...area, curve, tension }}
|
|
137
|
+
xScaler={xScaler}
|
|
138
|
+
yScaler={yScaler}
|
|
139
|
+
/>
|
|
140
|
+
)}
|
|
141
|
+
{showLighter && <Lighter path={path} config={lighter} />}
|
|
142
|
+
</g>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
);
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* svg渐变滤镜
|
|
3
|
-
*/
|
|
4
|
-
export default ({
|
|
5
|
-
id,
|
|
6
|
-
colors,
|
|
7
|
-
rotate = 0,
|
|
8
|
-
gradientUnits,
|
|
9
|
-
position: [x1, y1, x2, y2] = [0, 0, 0, 1],
|
|
10
|
-
}) => (
|
|
11
|
-
<linearGradient
|
|
12
|
-
x1={x1}
|
|
13
|
-
y1={y1}
|
|
14
|
-
x2={x2}
|
|
15
|
-
y2={y2}
|
|
16
|
-
id={id}
|
|
17
|
-
gradientUnits={gradientUnits}
|
|
18
|
-
gradientTransform={'rotate(' + rotate + ', .5, .5)'}
|
|
19
|
-
>
|
|
20
|
-
{colors.map(({ offset, color, stopOpacity = 1 }, index) => (
|
|
21
|
-
<stop
|
|
22
|
-
key={index}
|
|
23
|
-
offset={offset}
|
|
24
|
-
stopColor={color}
|
|
25
|
-
stopOpacity={stopOpacity}
|
|
26
|
-
/>
|
|
27
|
-
))}
|
|
28
|
-
</linearGradient>
|
|
29
|
-
);
|
|
1
|
+
/**
|
|
2
|
+
* svg渐变滤镜
|
|
3
|
+
*/
|
|
4
|
+
export default ({
|
|
5
|
+
id,
|
|
6
|
+
colors,
|
|
7
|
+
rotate = 0,
|
|
8
|
+
gradientUnits,
|
|
9
|
+
position: [x1, y1, x2, y2] = [0, 0, 0, 1],
|
|
10
|
+
}) => (
|
|
11
|
+
<linearGradient
|
|
12
|
+
x1={x1}
|
|
13
|
+
y1={y1}
|
|
14
|
+
x2={x2}
|
|
15
|
+
y2={y2}
|
|
16
|
+
id={id}
|
|
17
|
+
gradientUnits={gradientUnits}
|
|
18
|
+
gradientTransform={'rotate(' + rotate + ', .5, .5)'}
|
|
19
|
+
>
|
|
20
|
+
{colors.map(({ offset, color, stopOpacity = 1 }, index) => (
|
|
21
|
+
<stop
|
|
22
|
+
key={index}
|
|
23
|
+
offset={offset}
|
|
24
|
+
stopColor={color}
|
|
25
|
+
stopOpacity={stopOpacity}
|
|
26
|
+
/>
|
|
27
|
+
))}
|
|
28
|
+
</linearGradient>
|
|
29
|
+
);
|
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import { memo, useMemo } from 'react';
|
|
2
|
-
import { group } from 'd3v7';
|
|
3
|
-
|
|
4
|
-
const groupBySeriesData = (data) => {
|
|
5
|
-
const groupBySeries = group(data, (d) => d.s);
|
|
6
|
-
const tmp = [];
|
|
7
|
-
for (let [key, value] of groupBySeries.entries()) {
|
|
8
|
-
tmp.push({ key, value });
|
|
9
|
-
}
|
|
10
|
-
return tmp;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const isValidData = (data) =>
|
|
14
|
-
!!(
|
|
15
|
-
Array.isArray(data) &&
|
|
16
|
-
data.filter(
|
|
17
|
-
(obj) =>
|
|
18
|
-
Object.prototype.toString.call(obj) === '[object Object]' &&
|
|
19
|
-
Object.keys(obj).length !== 0
|
|
20
|
-
).length > 0
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
export default (Component) =>
|
|
24
|
-
memo(({ config: { series, ...config }, data: originData, ...rest }) => {
|
|
25
|
-
const data = useMemo(() => {
|
|
26
|
-
if (!isValidData(originData)) return [];
|
|
27
|
-
return groupBySeriesData(originData);
|
|
28
|
-
}, [originData]);
|
|
29
|
-
|
|
30
|
-
const _series = useMemo(() => {
|
|
31
|
-
if (!!data.length) {
|
|
32
|
-
const seriesLength = series.length;
|
|
33
|
-
const noName = series.every(({ fieldName }) => !fieldName);
|
|
34
|
-
const tmp = new Map();
|
|
35
|
-
if (!!noName) {
|
|
36
|
-
data.forEach(({ key }, index) => {
|
|
37
|
-
const current = series[index % seriesLength];
|
|
38
|
-
const name = key || '';
|
|
39
|
-
tmp.set(name, {
|
|
40
|
-
...current,
|
|
41
|
-
name,
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
return tmp;
|
|
45
|
-
}
|
|
46
|
-
const lastSeries = series[seriesLength - 1];
|
|
47
|
-
data.forEach(({ key }) => {
|
|
48
|
-
const current = series.find(({ fieldName }) => fieldName == key);
|
|
49
|
-
const result = current || lastSeries;
|
|
50
|
-
const name =
|
|
51
|
-
(current ? current.fieldName || current.displayName : key) || '';
|
|
52
|
-
tmp.set(name, {
|
|
53
|
-
...result,
|
|
54
|
-
name,
|
|
55
|
-
displayName: current ? current.displayName : '',
|
|
56
|
-
fieldName: current ? current.fieldName : '',
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
return tmp;
|
|
60
|
-
}
|
|
61
|
-
}, [series, data]);
|
|
62
|
-
return (
|
|
63
|
-
!!data.length && (
|
|
64
|
-
<Component
|
|
65
|
-
{...rest}
|
|
66
|
-
data={originData}
|
|
67
|
-
config={{ ...config, series: _series }}
|
|
68
|
-
/>
|
|
69
|
-
)
|
|
70
|
-
);
|
|
71
|
-
});
|
|
1
|
+
import { memo, useMemo } from 'react';
|
|
2
|
+
import { group } from 'd3v7';
|
|
3
|
+
|
|
4
|
+
const groupBySeriesData = (data) => {
|
|
5
|
+
const groupBySeries = group(data, (d) => d.s);
|
|
6
|
+
const tmp = [];
|
|
7
|
+
for (let [key, value] of groupBySeries.entries()) {
|
|
8
|
+
tmp.push({ key, value });
|
|
9
|
+
}
|
|
10
|
+
return tmp;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const isValidData = (data) =>
|
|
14
|
+
!!(
|
|
15
|
+
Array.isArray(data) &&
|
|
16
|
+
data.filter(
|
|
17
|
+
(obj) =>
|
|
18
|
+
Object.prototype.toString.call(obj) === '[object Object]' &&
|
|
19
|
+
Object.keys(obj).length !== 0
|
|
20
|
+
).length > 0
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
export default (Component) =>
|
|
24
|
+
memo(({ config: { series, ...config }, data: originData, ...rest }) => {
|
|
25
|
+
const data = useMemo(() => {
|
|
26
|
+
if (!isValidData(originData)) return [];
|
|
27
|
+
return groupBySeriesData(originData);
|
|
28
|
+
}, [originData]);
|
|
29
|
+
|
|
30
|
+
const _series = useMemo(() => {
|
|
31
|
+
if (!!data.length) {
|
|
32
|
+
const seriesLength = series.length;
|
|
33
|
+
const noName = series.every(({ fieldName }) => !fieldName);
|
|
34
|
+
const tmp = new Map();
|
|
35
|
+
if (!!noName) {
|
|
36
|
+
data.forEach(({ key }, index) => {
|
|
37
|
+
const current = series[index % seriesLength];
|
|
38
|
+
const name = key || '';
|
|
39
|
+
tmp.set(name, {
|
|
40
|
+
...current,
|
|
41
|
+
name,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
return tmp;
|
|
45
|
+
}
|
|
46
|
+
const lastSeries = series[seriesLength - 1];
|
|
47
|
+
data.forEach(({ key }) => {
|
|
48
|
+
const current = series.find(({ fieldName }) => fieldName == key);
|
|
49
|
+
const result = current || lastSeries;
|
|
50
|
+
const name =
|
|
51
|
+
(current ? current.fieldName || current.displayName : key) || '';
|
|
52
|
+
tmp.set(name, {
|
|
53
|
+
...result,
|
|
54
|
+
name,
|
|
55
|
+
displayName: current ? current.displayName : '',
|
|
56
|
+
fieldName: current ? current.fieldName : '',
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
return tmp;
|
|
60
|
+
}
|
|
61
|
+
}, [series, data]);
|
|
62
|
+
return (
|
|
63
|
+
!!data.length && (
|
|
64
|
+
<Component
|
|
65
|
+
{...rest}
|
|
66
|
+
data={originData}
|
|
67
|
+
config={{ ...config, series: _series }}
|
|
68
|
+
/>
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
});
|
|
@@ -1,93 +1,93 @@
|
|
|
1
|
-
import { useState, useEffect, memo, useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 文字跑马灯组件
|
|
5
|
-
* eg: <Marquee value={文本内容} speed={跑马灯速度} style={额外样式}></Marquee>
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export default memo((props) => {
|
|
9
|
-
const {
|
|
10
|
-
value, // 文本
|
|
11
|
-
style, // 样式
|
|
12
|
-
speed = 5, // 动画速度
|
|
13
|
-
} = props;
|
|
14
|
-
|
|
15
|
-
const dom = useRef(null);
|
|
16
|
-
const content = useRef(null);
|
|
17
|
-
const duration = useRef(1);
|
|
18
|
-
//dom和content盒子对象,避免speed变化时重新获取盒子对象
|
|
19
|
-
const domRect = useRef(null);
|
|
20
|
-
const contentRect = useRef(null);
|
|
21
|
-
|
|
22
|
-
const [textList, setTextList] = useState([value]);
|
|
23
|
-
|
|
24
|
-
//文本变化时,需要重新计算dom和content的宽高
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
const dom_ = dom.current;
|
|
27
|
-
domRect.current = dom_.getBoundingClientRect();
|
|
28
|
-
let content_ = content.current;
|
|
29
|
-
contentRect.current = content_.getBoundingClientRect();
|
|
30
|
-
if (domRect.current.width < contentRect.current.width) {
|
|
31
|
-
setTextList([value, value]);
|
|
32
|
-
}
|
|
33
|
-
}, [value]);
|
|
34
|
-
//value和speed变化时,直接更新duration
|
|
35
|
-
useEffect(() => {
|
|
36
|
-
if (domRect.current.width < contentRect.current.width) {
|
|
37
|
-
duration.current =
|
|
38
|
-
(contentRect.current.width / domRect.current.width / speed) * 10;
|
|
39
|
-
}
|
|
40
|
-
}, [speed]);
|
|
41
|
-
//在head标签中注入keyframes动画
|
|
42
|
-
useEffect(() => {
|
|
43
|
-
let md_marquee = document.getElementById('md_marquee'); //先查找是否已经存在md_marquee
|
|
44
|
-
if (!md_marquee) {
|
|
45
|
-
//不存在时再注入style标签
|
|
46
|
-
const keyframes = `@keyframes md_marquee {0%{
|
|
47
|
-
transform: translate(0,0);
|
|
48
|
-
}
|
|
49
|
-
100%{
|
|
50
|
-
transform: translate(-100%,0);
|
|
51
|
-
}}`;
|
|
52
|
-
const style = document.createElement('style');
|
|
53
|
-
style.id = 'md_marquee';
|
|
54
|
-
style.innerHTML = keyframes;
|
|
55
|
-
// 将style样式存放到head标签
|
|
56
|
-
document.getElementsByTagName('head')[0].appendChild(style);
|
|
57
|
-
}
|
|
58
|
-
}, []);
|
|
59
|
-
return (
|
|
60
|
-
<span
|
|
61
|
-
style={{
|
|
62
|
-
width: '100%',
|
|
63
|
-
...style,
|
|
64
|
-
display: 'flex',
|
|
65
|
-
whiteSpace: 'nowrap',
|
|
66
|
-
overflow: 'hidden',
|
|
67
|
-
}}
|
|
68
|
-
ref={dom}
|
|
69
|
-
>
|
|
70
|
-
{textList.map((item, itemIndex) => {
|
|
71
|
-
return (
|
|
72
|
-
<span
|
|
73
|
-
key={itemIndex}
|
|
74
|
-
style={
|
|
75
|
-
textList.length == 2
|
|
76
|
-
? {
|
|
77
|
-
//如果文本数量为2,则需要滚动
|
|
78
|
-
animationName: 'md_marquee',
|
|
79
|
-
animationTimingFunction: 'linear',
|
|
80
|
-
animationIterationCount: 'infinite',
|
|
81
|
-
animationDuration: duration.current + 's',
|
|
82
|
-
}
|
|
83
|
-
: {}
|
|
84
|
-
}
|
|
85
|
-
ref={itemIndex == 0 ? content : null}
|
|
86
|
-
>
|
|
87
|
-
{item}
|
|
88
|
-
</span>
|
|
89
|
-
);
|
|
90
|
-
})}
|
|
91
|
-
</span>
|
|
92
|
-
);
|
|
93
|
-
});
|
|
1
|
+
import { useState, useEffect, memo, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 文字跑马灯组件
|
|
5
|
+
* eg: <Marquee value={文本内容} speed={跑马灯速度} style={额外样式}></Marquee>
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export default memo((props) => {
|
|
9
|
+
const {
|
|
10
|
+
value, // 文本
|
|
11
|
+
style, // 样式
|
|
12
|
+
speed = 5, // 动画速度
|
|
13
|
+
} = props;
|
|
14
|
+
|
|
15
|
+
const dom = useRef(null);
|
|
16
|
+
const content = useRef(null);
|
|
17
|
+
const duration = useRef(1);
|
|
18
|
+
//dom和content盒子对象,避免speed变化时重新获取盒子对象
|
|
19
|
+
const domRect = useRef(null);
|
|
20
|
+
const contentRect = useRef(null);
|
|
21
|
+
|
|
22
|
+
const [textList, setTextList] = useState([value]);
|
|
23
|
+
|
|
24
|
+
//文本变化时,需要重新计算dom和content的宽高
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const dom_ = dom.current;
|
|
27
|
+
domRect.current = dom_.getBoundingClientRect();
|
|
28
|
+
let content_ = content.current;
|
|
29
|
+
contentRect.current = content_.getBoundingClientRect();
|
|
30
|
+
if (domRect.current.width < contentRect.current.width) {
|
|
31
|
+
setTextList([value, value]);
|
|
32
|
+
}
|
|
33
|
+
}, [value]);
|
|
34
|
+
//value和speed变化时,直接更新duration
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (domRect.current.width < contentRect.current.width) {
|
|
37
|
+
duration.current =
|
|
38
|
+
(contentRect.current.width / domRect.current.width / speed) * 10;
|
|
39
|
+
}
|
|
40
|
+
}, [speed]);
|
|
41
|
+
//在head标签中注入keyframes动画
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
let md_marquee = document.getElementById('md_marquee'); //先查找是否已经存在md_marquee
|
|
44
|
+
if (!md_marquee) {
|
|
45
|
+
//不存在时再注入style标签
|
|
46
|
+
const keyframes = `@keyframes md_marquee {0%{
|
|
47
|
+
transform: translate(0,0);
|
|
48
|
+
}
|
|
49
|
+
100%{
|
|
50
|
+
transform: translate(-100%,0);
|
|
51
|
+
}}`;
|
|
52
|
+
const style = document.createElement('style');
|
|
53
|
+
style.id = 'md_marquee';
|
|
54
|
+
style.innerHTML = keyframes;
|
|
55
|
+
// 将style样式存放到head标签
|
|
56
|
+
document.getElementsByTagName('head')[0].appendChild(style);
|
|
57
|
+
}
|
|
58
|
+
}, []);
|
|
59
|
+
return (
|
|
60
|
+
<span
|
|
61
|
+
style={{
|
|
62
|
+
width: '100%',
|
|
63
|
+
...style,
|
|
64
|
+
display: 'flex',
|
|
65
|
+
whiteSpace: 'nowrap',
|
|
66
|
+
overflow: 'hidden',
|
|
67
|
+
}}
|
|
68
|
+
ref={dom}
|
|
69
|
+
>
|
|
70
|
+
{textList.map((item, itemIndex) => {
|
|
71
|
+
return (
|
|
72
|
+
<span
|
|
73
|
+
key={itemIndex}
|
|
74
|
+
style={
|
|
75
|
+
textList.length == 2
|
|
76
|
+
? {
|
|
77
|
+
//如果文本数量为2,则需要滚动
|
|
78
|
+
animationName: 'md_marquee',
|
|
79
|
+
animationTimingFunction: 'linear',
|
|
80
|
+
animationIterationCount: 'infinite',
|
|
81
|
+
animationDuration: duration.current + 's',
|
|
82
|
+
}
|
|
83
|
+
: {}
|
|
84
|
+
}
|
|
85
|
+
ref={itemIndex == 0 ? content : null}
|
|
86
|
+
>
|
|
87
|
+
{item}
|
|
88
|
+
</span>
|
|
89
|
+
);
|
|
90
|
+
})}
|
|
91
|
+
</span>
|
|
92
|
+
);
|
|
93
|
+
});
|