@easyv/charts 1.7.35 → 1.7.37
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/CHANGELOG.md +18 -18
- package/commitlint.config.js +1 -1
- package/lib/components/Background.js +2 -2
- package/lib/components/Band.js +2 -2
- package/lib/components/Brush.js +2 -2
- package/lib/components/Chart.js +2 -2
- package/lib/components/ChartContainer.js +2 -2
- package/lib/components/ConicalGradient.js +21 -21
- package/lib/components/Control.js +5 -1
- package/lib/components/ExtentData.js +2 -2
- package/lib/components/Indicator.js +2 -2
- package/lib/components/Label.js +2 -2
- package/lib/components/Legend.js +38 -6
- package/lib/components/Lighter.js +2 -2
- package/lib/components/Line.js +2 -2
- package/lib/components/LinearGradient.js +2 -2
- package/lib/components/StereoBar.js +2 -2
- package/lib/css/index.module.css +39 -42
- package/lib/css/piechart.module.css +26 -26
- package/lib/formatter/legend.js +55 -50
- package/lib/hooks/useAnimateData.js +6 -6
- package/lib/hooks/useFilterData.js +5 -5
- package/lib/hooks/useStackData.js +5 -5
- package/lib/hooks/useTooltip.js +11 -11
- package/package.json +54 -54
- package/src/components/Background.tsx +61 -61
- package/src/components/Band.tsx +334 -334
- package/src/components/Brush.js +159 -159
- package/src/components/Chart.js +157 -157
- package/src/components/ChartContainer.tsx +71 -71
- package/src/components/ConicalGradient.js +258 -258
- package/src/components/Control.jsx +242 -242
- package/src/components/ExtentData.js +18 -18
- package/src/components/Indicator.js +61 -61
- package/src/components/Label.js +262 -262
- package/src/components/Legend.js +289 -267
- package/src/components/Lighter.jsx +173 -173
- package/src/components/Line.js +153 -153
- package/src/components/LinearGradient.js +29 -29
- package/src/components/PieTooltip.jsx +160 -160
- package/src/components/SplitText.tsx +70 -70
- package/src/components/StereoBar.tsx +307 -307
- package/src/components/index.js +61 -61
- package/src/context/index.js +2 -2
- package/src/css/index.module.css +39 -42
- 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 +122 -115
- package/src/hooks/index.js +20 -20
- package/src/hooks/useAnimateData.ts +68 -68
- package/src/hooks/useFilterData.js +77 -77
- package/src/hooks/useStackData.js +140 -140
- package/src/hooks/useTooltip.ts +103 -103
- package/src/index.js +6 -6
- package/src/types/index.d.ts +68 -68
- package/src/utils/index.js +812 -812
- package/tsconfig.json +23 -23
|
@@ -1,173 +1,173 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 区域图发光跟踪路径
|
|
3
|
-
*/
|
|
4
|
-
import React, {
|
|
5
|
-
useState,
|
|
6
|
-
useEffect,
|
|
7
|
-
useRef,
|
|
8
|
-
useCallback,
|
|
9
|
-
useMemo,
|
|
10
|
-
} from 'react';
|
|
11
|
-
import { scaleLinear } from 'd3v7';
|
|
12
|
-
import { getColorList } from '../utils';
|
|
13
|
-
import { svgPathProperties } from 'svg-path-properties';
|
|
14
|
-
|
|
15
|
-
export default ({
|
|
16
|
-
path: d,
|
|
17
|
-
config,
|
|
18
|
-
config: { length: curveLength, width, fill, unitStep },
|
|
19
|
-
}) => {
|
|
20
|
-
const pointIndexRef = useRef(0);
|
|
21
|
-
const raf = useRef(null);
|
|
22
|
-
const [points, setPoints] = useState([]);
|
|
23
|
-
const [lighter, setLighter] = useState(null);
|
|
24
|
-
const pointLength = points.length;
|
|
25
|
-
const totalLength = getLength(pointLength, curveLength);
|
|
26
|
-
const easingFunc = easing.linear;
|
|
27
|
-
|
|
28
|
-
const interpolateList = useMemo(
|
|
29
|
-
() => getColorsInterpolate(getColorList(fill)),
|
|
30
|
-
[fill]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
const drawLigher = useCallback(() => {
|
|
34
|
-
const STEP = 2;
|
|
35
|
-
const LOOP = false;
|
|
36
|
-
if (lighter && lighter.children) {
|
|
37
|
-
const currentPointLength = pointIndexRef.current;
|
|
38
|
-
const pointIndex =
|
|
39
|
-
currentPointLength >= totalLength
|
|
40
|
-
? LOOP
|
|
41
|
-
? curveLength
|
|
42
|
-
: 0
|
|
43
|
-
: currentPointLength +
|
|
44
|
-
unitStep +
|
|
45
|
-
Math.floor(easingFunc(pointIndexRef.current / totalLength) * STEP);
|
|
46
|
-
|
|
47
|
-
const overstep = pointIndex - pointLength;
|
|
48
|
-
const startIndex = getPointIndex(pointIndex, pointLength, curveLength);
|
|
49
|
-
const endIndex = getPointIndex(
|
|
50
|
-
pointIndex + curveLength,
|
|
51
|
-
pointLength,
|
|
52
|
-
curveLength
|
|
53
|
-
);
|
|
54
|
-
const overstepPoints =
|
|
55
|
-
LOOP && overstep > 0 ? points.slice(0, overstep) : [];
|
|
56
|
-
drawCircle(
|
|
57
|
-
Array.from(lighter.children),
|
|
58
|
-
[...points.slice(startIndex, endIndex), ...overstepPoints],
|
|
59
|
-
interpolateList
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
pointIndexRef.current = pointIndex;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
raf.current = requestAnimationFrame(drawLigher);
|
|
66
|
-
}, [lighter, totalLength, curveLength, unitStep, points, interpolateList]);
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
const path = new svgPathProperties(d);
|
|
69
|
-
const totalLength = path.getTotalLength();
|
|
70
|
-
const points = [];
|
|
71
|
-
for (let i = 0; i < totalLength; i++) {
|
|
72
|
-
const { x, y } = path.getPointAtLength(i);
|
|
73
|
-
points.push({
|
|
74
|
-
index: i,
|
|
75
|
-
x,
|
|
76
|
-
y,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
setPoints(points);
|
|
80
|
-
}, [d, config]);
|
|
81
|
-
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
raf.current = requestAnimationFrame(drawLigher);
|
|
84
|
-
return () => {
|
|
85
|
-
cancelAnimationFrame(raf.current);
|
|
86
|
-
};
|
|
87
|
-
}, [drawLigher]);
|
|
88
|
-
return (
|
|
89
|
-
<g ref={setLighter}>
|
|
90
|
-
{points.map(({ x, y }, index) => (
|
|
91
|
-
<circle cx={x} cy={y} key={index} r={width} fill='none' />
|
|
92
|
-
))}
|
|
93
|
-
</g>
|
|
94
|
-
);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const drawCircle = (children, points, interpolateList) => {
|
|
98
|
-
const pointLength = points.length;
|
|
99
|
-
// const [startPoint] = points;
|
|
100
|
-
// const startIndex = startPoint ? startPoint.index : 0;
|
|
101
|
-
children.forEach((child, index) => {
|
|
102
|
-
const pointIndex = points.findIndex(({ index: i }) => i == index);
|
|
103
|
-
child.setAttribute(
|
|
104
|
-
'fill',
|
|
105
|
-
pointIndex > -1
|
|
106
|
-
? getColor(
|
|
107
|
-
pointIndex / pointLength, //TODO: 颜色插值尚需完善
|
|
108
|
-
interpolateList
|
|
109
|
-
)
|
|
110
|
-
: 'none'
|
|
111
|
-
);
|
|
112
|
-
});
|
|
113
|
-
};
|
|
114
|
-
const getColor = (x, interpolateList) => {
|
|
115
|
-
return interpolateList(x);
|
|
116
|
-
};
|
|
117
|
-
const getLength = (pointLength, length) => {
|
|
118
|
-
if (length > pointLength) {
|
|
119
|
-
return pointLength * 2;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return pointLength + length;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const getColorsInterpolate = (colors) => {
|
|
126
|
-
const length = colors.length;
|
|
127
|
-
if (!!(length < 2))
|
|
128
|
-
return scaleLinear()
|
|
129
|
-
.domain([0, 1])
|
|
130
|
-
.range([colors[0].color || '#000', colors[0].color || '#000']);
|
|
131
|
-
const linear = scaleLinear()
|
|
132
|
-
.domain(colors.map((d) => d.offset))
|
|
133
|
-
.range(colors.map((d) => d.color));
|
|
134
|
-
return linear;
|
|
135
|
-
};
|
|
136
|
-
const easing = {
|
|
137
|
-
linear() {
|
|
138
|
-
return 0;
|
|
139
|
-
},
|
|
140
|
-
|
|
141
|
-
easeIn(k) {
|
|
142
|
-
return Math.pow(k, 3);
|
|
143
|
-
},
|
|
144
|
-
|
|
145
|
-
easeOut(k) {
|
|
146
|
-
return 1 - easing.easeIn(1 - k);
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
easeInOut(k) {
|
|
150
|
-
return k < 0.5
|
|
151
|
-
? easing.easeIn(k * 2) / 2
|
|
152
|
-
: 1 - easing.easeIn(2 - 2 * k) / 2;
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
easeOutIn(k) {
|
|
156
|
-
return k < 0.5
|
|
157
|
-
? 0.5 * (1 - easing.easeIn(1 - 2 * k))
|
|
158
|
-
: 0.5 * easing.easeIn(k * 2 - 1) + 0.5;
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
const getPointIndex = (pointIndex, pointLength, curveLength) => {
|
|
162
|
-
let index = pointIndex - curveLength;
|
|
163
|
-
|
|
164
|
-
if (index < 0) {
|
|
165
|
-
return 0;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (index >= pointLength) {
|
|
169
|
-
return pointLength - 1;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return index;
|
|
173
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 区域图发光跟踪路径
|
|
3
|
+
*/
|
|
4
|
+
import React, {
|
|
5
|
+
useState,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useCallback,
|
|
9
|
+
useMemo,
|
|
10
|
+
} from 'react';
|
|
11
|
+
import { scaleLinear } from 'd3v7';
|
|
12
|
+
import { getColorList } from '../utils';
|
|
13
|
+
import { svgPathProperties } from 'svg-path-properties';
|
|
14
|
+
|
|
15
|
+
export default ({
|
|
16
|
+
path: d,
|
|
17
|
+
config,
|
|
18
|
+
config: { length: curveLength, width, fill, unitStep },
|
|
19
|
+
}) => {
|
|
20
|
+
const pointIndexRef = useRef(0);
|
|
21
|
+
const raf = useRef(null);
|
|
22
|
+
const [points, setPoints] = useState([]);
|
|
23
|
+
const [lighter, setLighter] = useState(null);
|
|
24
|
+
const pointLength = points.length;
|
|
25
|
+
const totalLength = getLength(pointLength, curveLength);
|
|
26
|
+
const easingFunc = easing.linear;
|
|
27
|
+
|
|
28
|
+
const interpolateList = useMemo(
|
|
29
|
+
() => getColorsInterpolate(getColorList(fill)),
|
|
30
|
+
[fill]
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const drawLigher = useCallback(() => {
|
|
34
|
+
const STEP = 2;
|
|
35
|
+
const LOOP = false;
|
|
36
|
+
if (lighter && lighter.children) {
|
|
37
|
+
const currentPointLength = pointIndexRef.current;
|
|
38
|
+
const pointIndex =
|
|
39
|
+
currentPointLength >= totalLength
|
|
40
|
+
? LOOP
|
|
41
|
+
? curveLength
|
|
42
|
+
: 0
|
|
43
|
+
: currentPointLength +
|
|
44
|
+
unitStep +
|
|
45
|
+
Math.floor(easingFunc(pointIndexRef.current / totalLength) * STEP);
|
|
46
|
+
|
|
47
|
+
const overstep = pointIndex - pointLength;
|
|
48
|
+
const startIndex = getPointIndex(pointIndex, pointLength, curveLength);
|
|
49
|
+
const endIndex = getPointIndex(
|
|
50
|
+
pointIndex + curveLength,
|
|
51
|
+
pointLength,
|
|
52
|
+
curveLength
|
|
53
|
+
);
|
|
54
|
+
const overstepPoints =
|
|
55
|
+
LOOP && overstep > 0 ? points.slice(0, overstep) : [];
|
|
56
|
+
drawCircle(
|
|
57
|
+
Array.from(lighter.children),
|
|
58
|
+
[...points.slice(startIndex, endIndex), ...overstepPoints],
|
|
59
|
+
interpolateList
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
pointIndexRef.current = pointIndex;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
raf.current = requestAnimationFrame(drawLigher);
|
|
66
|
+
}, [lighter, totalLength, curveLength, unitStep, points, interpolateList]);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
const path = new svgPathProperties(d);
|
|
69
|
+
const totalLength = path.getTotalLength();
|
|
70
|
+
const points = [];
|
|
71
|
+
for (let i = 0; i < totalLength; i++) {
|
|
72
|
+
const { x, y } = path.getPointAtLength(i);
|
|
73
|
+
points.push({
|
|
74
|
+
index: i,
|
|
75
|
+
x,
|
|
76
|
+
y,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
setPoints(points);
|
|
80
|
+
}, [d, config]);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
raf.current = requestAnimationFrame(drawLigher);
|
|
84
|
+
return () => {
|
|
85
|
+
cancelAnimationFrame(raf.current);
|
|
86
|
+
};
|
|
87
|
+
}, [drawLigher]);
|
|
88
|
+
return (
|
|
89
|
+
<g ref={setLighter}>
|
|
90
|
+
{points.map(({ x, y }, index) => (
|
|
91
|
+
<circle cx={x} cy={y} key={index} r={width} fill='none' />
|
|
92
|
+
))}
|
|
93
|
+
</g>
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const drawCircle = (children, points, interpolateList) => {
|
|
98
|
+
const pointLength = points.length;
|
|
99
|
+
// const [startPoint] = points;
|
|
100
|
+
// const startIndex = startPoint ? startPoint.index : 0;
|
|
101
|
+
children.forEach((child, index) => {
|
|
102
|
+
const pointIndex = points.findIndex(({ index: i }) => i == index);
|
|
103
|
+
child.setAttribute(
|
|
104
|
+
'fill',
|
|
105
|
+
pointIndex > -1
|
|
106
|
+
? getColor(
|
|
107
|
+
pointIndex / pointLength, //TODO: 颜色插值尚需完善
|
|
108
|
+
interpolateList
|
|
109
|
+
)
|
|
110
|
+
: 'none'
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
const getColor = (x, interpolateList) => {
|
|
115
|
+
return interpolateList(x);
|
|
116
|
+
};
|
|
117
|
+
const getLength = (pointLength, length) => {
|
|
118
|
+
if (length > pointLength) {
|
|
119
|
+
return pointLength * 2;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return pointLength + length;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const getColorsInterpolate = (colors) => {
|
|
126
|
+
const length = colors.length;
|
|
127
|
+
if (!!(length < 2))
|
|
128
|
+
return scaleLinear()
|
|
129
|
+
.domain([0, 1])
|
|
130
|
+
.range([colors[0].color || '#000', colors[0].color || '#000']);
|
|
131
|
+
const linear = scaleLinear()
|
|
132
|
+
.domain(colors.map((d) => d.offset))
|
|
133
|
+
.range(colors.map((d) => d.color));
|
|
134
|
+
return linear;
|
|
135
|
+
};
|
|
136
|
+
const easing = {
|
|
137
|
+
linear() {
|
|
138
|
+
return 0;
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
easeIn(k) {
|
|
142
|
+
return Math.pow(k, 3);
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
easeOut(k) {
|
|
146
|
+
return 1 - easing.easeIn(1 - k);
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
easeInOut(k) {
|
|
150
|
+
return k < 0.5
|
|
151
|
+
? easing.easeIn(k * 2) / 2
|
|
152
|
+
: 1 - easing.easeIn(2 - 2 * k) / 2;
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
easeOutIn(k) {
|
|
156
|
+
return k < 0.5
|
|
157
|
+
? 0.5 * (1 - easing.easeIn(1 - 2 * k))
|
|
158
|
+
: 0.5 * easing.easeIn(k * 2 - 1) + 0.5;
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
const getPointIndex = (pointIndex, pointLength, curveLength) => {
|
|
162
|
+
let index = pointIndex - curveLength;
|
|
163
|
+
|
|
164
|
+
if (index < 0) {
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (index >= pointLength) {
|
|
169
|
+
return pointLength - 1;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return index;
|
|
173
|
+
};
|
package/src/components/Line.js
CHANGED
|
@@ -1,153 +1,153 @@
|
|
|
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
|
-
return data.flatMap(d=>{
|
|
12
|
-
const y = d.data.y;
|
|
13
|
-
return isNaN(y)?
|
|
14
|
-
connectNulls?
|
|
15
|
-
[]:
|
|
16
|
-
{...d,data:{...d.data,y:null}}:
|
|
17
|
-
d
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const Area = ({
|
|
23
|
-
data,
|
|
24
|
-
config,
|
|
25
|
-
config: {
|
|
26
|
-
id,
|
|
27
|
-
fill,
|
|
28
|
-
type,
|
|
29
|
-
url,
|
|
30
|
-
opacity,
|
|
31
|
-
size: { width: patternW, height: patternH },
|
|
32
|
-
curve,
|
|
33
|
-
tension
|
|
34
|
-
},
|
|
35
|
-
xScaler,
|
|
36
|
-
yScaler,
|
|
37
|
-
}) => {
|
|
38
|
-
const [height] = yScaler.range();
|
|
39
|
-
const area = useMemo(() => getColorList(fill), [fill]);
|
|
40
|
-
|
|
41
|
-
const areaGen = useMemo(() => {
|
|
42
|
-
const areaGen = d3Area()
|
|
43
|
-
.x(({ data: { x } }) => xScaler(x))
|
|
44
|
-
.y1(({ data: { y } }) => yScaler(y))
|
|
45
|
-
.y0(({})=>yScaler(0))
|
|
46
|
-
.defined(defined);
|
|
47
|
-
curve && areaGen.curve(curveCatmullRom.alpha(tension));
|
|
48
|
-
curve && areaGen.curve(curveMonotoneX);
|
|
49
|
-
return areaGen;
|
|
50
|
-
}, [xScaler, yScaler, curve, tension]);
|
|
51
|
-
|
|
52
|
-
return (
|
|
53
|
-
<>
|
|
54
|
-
<path d={areaGen(data)} style={{pointerEvents:"none"}} stroke='none' fill={'url(#' + id + ')'} />
|
|
55
|
-
<defs>
|
|
56
|
-
{type && type == 'pattern' ? (
|
|
57
|
-
<pattern id={id} patternUnits="userSpaceOnUse" width={patternW} height={patternH}>
|
|
58
|
-
{url && <image opacity={opacity} width={patternW} height={patternH} xlinkHref={window.appConfig.ASSETS_URL + url} />}
|
|
59
|
-
</pattern>
|
|
60
|
-
) : (
|
|
61
|
-
<LinearGradient id={id} colors={area} rotate={0} />
|
|
62
|
-
)}
|
|
63
|
-
</defs>
|
|
64
|
-
</>
|
|
65
|
-
);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
export default memo(
|
|
69
|
-
({
|
|
70
|
-
type,
|
|
71
|
-
config: {
|
|
72
|
-
line: {
|
|
73
|
-
type: lineType,
|
|
74
|
-
lineWidth,
|
|
75
|
-
curve,
|
|
76
|
-
tension,
|
|
77
|
-
connectNulls,
|
|
78
|
-
lineShadow,
|
|
79
|
-
lighter,
|
|
80
|
-
},
|
|
81
|
-
// icon,
|
|
82
|
-
// label,
|
|
83
|
-
},
|
|
84
|
-
line: { id: lineId, stroke },
|
|
85
|
-
area,
|
|
86
|
-
data,
|
|
87
|
-
xAxis: { scaler: xScaler, direction },
|
|
88
|
-
yAxis: { scaler: yScaler },
|
|
89
|
-
}) => {
|
|
90
|
-
if (!data.length) return null;
|
|
91
|
-
const ticks = xScaler.domain();
|
|
92
|
-
|
|
93
|
-
const sortData = useMemo(() => {
|
|
94
|
-
const usefulData = data.filter(
|
|
95
|
-
({ data: { x } }) => ticks.indexOf(x) > -1
|
|
96
|
-
);
|
|
97
|
-
return usefulData.sort(
|
|
98
|
-
({ data: { x: a } }, { data: { x: b } }) =>
|
|
99
|
-
ticks.indexOf(a) - ticks.indexOf(b)
|
|
100
|
-
);
|
|
101
|
-
}, [data, ticks]);
|
|
102
|
-
|
|
103
|
-
const _data = useMemo(
|
|
104
|
-
() => getLineData(sortData, connectNulls),
|
|
105
|
-
[sortData, connectNulls]
|
|
106
|
-
);
|
|
107
|
-
const lineGen = useMemo(() => {
|
|
108
|
-
const isVertical = direction === 'vertical';
|
|
109
|
-
|
|
110
|
-
let lineGen = (
|
|
111
|
-
isVertical
|
|
112
|
-
? d3Line()
|
|
113
|
-
.y(({ data: { x } }) => xScaler(x))
|
|
114
|
-
.x(({ data: { y } }) => yScaler(y))
|
|
115
|
-
: d3Line()
|
|
116
|
-
.x(({ data: { x } }) => xScaler(x))
|
|
117
|
-
.y(({ data: { y } }) => yScaler(y))
|
|
118
|
-
).defined(defined);
|
|
119
|
-
curve && lineGen.curve(curveCatmullRom.alpha(tension));
|
|
120
|
-
curve && lineGen.curve(curveMonotoneX);
|
|
121
|
-
return lineGen;
|
|
122
|
-
}, [direction, xScaler, yScaler, tension, curve]);
|
|
123
|
-
|
|
124
|
-
const path = lineGen(_data);
|
|
125
|
-
const showLighter = lighter && lighter.show;
|
|
126
|
-
const show = lineShadow && lineShadow.show;
|
|
127
|
-
const shadow = lineShadow && lineShadow.shadow;
|
|
128
|
-
return (
|
|
129
|
-
<g className='__easyv-line'>
|
|
130
|
-
<path
|
|
131
|
-
d={path}
|
|
132
|
-
stroke={stroke}
|
|
133
|
-
style={{
|
|
134
|
-
filter:show?`drop-shadow(${shadow.hShadow}px ${shadow.vShadow}px ${shadow.blur}px ${shadow.color})`:"none",
|
|
135
|
-
pointerEvents:"none"
|
|
136
|
-
}}
|
|
137
|
-
fill='none'
|
|
138
|
-
strokeDasharray={lineType === 'dash' ? '3 3' : null}
|
|
139
|
-
strokeWidth={lineWidth}
|
|
140
|
-
/>
|
|
141
|
-
{type == 'area' && (
|
|
142
|
-
<Area
|
|
143
|
-
data={_data}
|
|
144
|
-
config={{ ...area, curve, tension }}
|
|
145
|
-
xScaler={xScaler}
|
|
146
|
-
yScaler={yScaler}
|
|
147
|
-
/>
|
|
148
|
-
)}
|
|
149
|
-
{showLighter && <Lighter path={path} config={lighter} />}
|
|
150
|
-
</g>
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
);
|
|
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
|
+
return data.flatMap(d=>{
|
|
12
|
+
const y = d.data.y;
|
|
13
|
+
return isNaN(y)?
|
|
14
|
+
connectNulls?
|
|
15
|
+
[]:
|
|
16
|
+
{...d,data:{...d.data,y:null}}:
|
|
17
|
+
d
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const Area = ({
|
|
23
|
+
data,
|
|
24
|
+
config,
|
|
25
|
+
config: {
|
|
26
|
+
id,
|
|
27
|
+
fill,
|
|
28
|
+
type,
|
|
29
|
+
url,
|
|
30
|
+
opacity,
|
|
31
|
+
size: { width: patternW, height: patternH },
|
|
32
|
+
curve,
|
|
33
|
+
tension
|
|
34
|
+
},
|
|
35
|
+
xScaler,
|
|
36
|
+
yScaler,
|
|
37
|
+
}) => {
|
|
38
|
+
const [height] = yScaler.range();
|
|
39
|
+
const area = useMemo(() => getColorList(fill), [fill]);
|
|
40
|
+
|
|
41
|
+
const areaGen = useMemo(() => {
|
|
42
|
+
const areaGen = d3Area()
|
|
43
|
+
.x(({ data: { x } }) => xScaler(x))
|
|
44
|
+
.y1(({ data: { y } }) => yScaler(y))
|
|
45
|
+
.y0(({})=>yScaler(0))
|
|
46
|
+
.defined(defined);
|
|
47
|
+
curve && areaGen.curve(curveCatmullRom.alpha(tension));
|
|
48
|
+
curve && areaGen.curve(curveMonotoneX);
|
|
49
|
+
return areaGen;
|
|
50
|
+
}, [xScaler, yScaler, curve, tension]);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<>
|
|
54
|
+
<path d={areaGen(data)} style={{pointerEvents:"none"}} stroke='none' fill={'url(#' + id + ')'} />
|
|
55
|
+
<defs>
|
|
56
|
+
{type && type == 'pattern' ? (
|
|
57
|
+
<pattern id={id} patternUnits="userSpaceOnUse" width={patternW} height={patternH}>
|
|
58
|
+
{url && <image opacity={opacity} width={patternW} height={patternH} xlinkHref={window.appConfig.ASSETS_URL + url} />}
|
|
59
|
+
</pattern>
|
|
60
|
+
) : (
|
|
61
|
+
<LinearGradient id={id} colors={area} rotate={0} />
|
|
62
|
+
)}
|
|
63
|
+
</defs>
|
|
64
|
+
</>
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export default memo(
|
|
69
|
+
({
|
|
70
|
+
type,
|
|
71
|
+
config: {
|
|
72
|
+
line: {
|
|
73
|
+
type: lineType,
|
|
74
|
+
lineWidth,
|
|
75
|
+
curve,
|
|
76
|
+
tension,
|
|
77
|
+
connectNulls,
|
|
78
|
+
lineShadow,
|
|
79
|
+
lighter,
|
|
80
|
+
},
|
|
81
|
+
// icon,
|
|
82
|
+
// label,
|
|
83
|
+
},
|
|
84
|
+
line: { id: lineId, stroke },
|
|
85
|
+
area,
|
|
86
|
+
data,
|
|
87
|
+
xAxis: { scaler: xScaler, direction },
|
|
88
|
+
yAxis: { scaler: yScaler },
|
|
89
|
+
}) => {
|
|
90
|
+
if (!data.length) return null;
|
|
91
|
+
const ticks = xScaler.domain();
|
|
92
|
+
|
|
93
|
+
const sortData = useMemo(() => {
|
|
94
|
+
const usefulData = data.filter(
|
|
95
|
+
({ data: { x } }) => ticks.indexOf(x) > -1
|
|
96
|
+
);
|
|
97
|
+
return usefulData.sort(
|
|
98
|
+
({ data: { x: a } }, { data: { x: b } }) =>
|
|
99
|
+
ticks.indexOf(a) - ticks.indexOf(b)
|
|
100
|
+
);
|
|
101
|
+
}, [data, ticks]);
|
|
102
|
+
|
|
103
|
+
const _data = useMemo(
|
|
104
|
+
() => getLineData(sortData, connectNulls),
|
|
105
|
+
[sortData, connectNulls]
|
|
106
|
+
);
|
|
107
|
+
const lineGen = useMemo(() => {
|
|
108
|
+
const isVertical = direction === 'vertical';
|
|
109
|
+
|
|
110
|
+
let lineGen = (
|
|
111
|
+
isVertical
|
|
112
|
+
? d3Line()
|
|
113
|
+
.y(({ data: { x } }) => xScaler(x))
|
|
114
|
+
.x(({ data: { y } }) => yScaler(y))
|
|
115
|
+
: d3Line()
|
|
116
|
+
.x(({ data: { x } }) => xScaler(x))
|
|
117
|
+
.y(({ data: { y } }) => yScaler(y))
|
|
118
|
+
).defined(defined);
|
|
119
|
+
curve && lineGen.curve(curveCatmullRom.alpha(tension));
|
|
120
|
+
curve && lineGen.curve(curveMonotoneX);
|
|
121
|
+
return lineGen;
|
|
122
|
+
}, [direction, xScaler, yScaler, tension, curve]);
|
|
123
|
+
|
|
124
|
+
const path = lineGen(_data);
|
|
125
|
+
const showLighter = lighter && lighter.show;
|
|
126
|
+
const show = lineShadow && lineShadow.show;
|
|
127
|
+
const shadow = lineShadow && lineShadow.shadow;
|
|
128
|
+
return (
|
|
129
|
+
<g className='__easyv-line'>
|
|
130
|
+
<path
|
|
131
|
+
d={path}
|
|
132
|
+
stroke={stroke}
|
|
133
|
+
style={{
|
|
134
|
+
filter:show?`drop-shadow(${shadow.hShadow}px ${shadow.vShadow}px ${shadow.blur}px ${shadow.color})`:"none",
|
|
135
|
+
pointerEvents:"none"
|
|
136
|
+
}}
|
|
137
|
+
fill='none'
|
|
138
|
+
strokeDasharray={lineType === 'dash' ? '3 3' : null}
|
|
139
|
+
strokeWidth={lineWidth}
|
|
140
|
+
/>
|
|
141
|
+
{type == 'area' && (
|
|
142
|
+
<Area
|
|
143
|
+
data={_data}
|
|
144
|
+
config={{ ...area, curve, tension }}
|
|
145
|
+
xScaler={xScaler}
|
|
146
|
+
yScaler={yScaler}
|
|
147
|
+
/>
|
|
148
|
+
)}
|
|
149
|
+
{showLighter && <Lighter path={path} config={lighter} />}
|
|
150
|
+
</g>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
);
|