echarts 实现曲线图,并在曲线上添加光标从开始到结束位置循环移动, 如下图
![](https://img-blog.csdnimg.cn/img_convert/7988fa50dcd5786e09e9731bab55cbfa.gif)
echarts本身有lines的effect可以实现光点移动的效果,但是只能是折线点,不能实现曲线光点移动效果;
实现思路: 画两条一样的曲线,一条是实际展示的曲线,另一条是移动的,根据渐变色实现,开始和结尾都设置为透明色,而中间很短的一部分设置为光点色,然后不断修改光标的位置,这样就形成了一条不断移动的光点;
配置如下:
let i = 1;
let step = 0.03;
let data = [
[502.84, 205.97, 332.79, 281.55, 398.35, 214.02],
[1502.84, 1205.97, 1332.79, 1281.55, 1398.35, 1214.02]
];
let a = [ {
name: "注册总量",
type: "line",
smooth: 0.27,
lineStyle: {
width: 4
},
data: [502.84, 205.97, 332.79, 281.55, 398.35, 214.02],
animation: false
},
{
name: "注册总量",
type: "line",
lineStyle: {
width: 4
},
smooth: 0.27,
data: [1502.84, 1205.97, 1332.79, 1281.55, 1398.35, 1214.02],
animation: false
}]
let dataIndex = 0;
setInterval(() => {
i++;
let startPre = step * (i-1);
let start = step * i;
let end = start + step;
let endSuffix = end + step;
if (endSuffix >= 1) {
endSuffix = 1;
i = 0;
}
// console.log(startPre, start, end, endSuffix);
let lines = {
name: "注册总量",
type: "line",
smooth: 0.27,
data: data[dataIndex],
animation: false,
lineStyle: {
width: 4,
color: {
type: "linear",
colorStops: [
{
offset: startPre,
color: "transparent"
},
{
offset: start,
color: "red"
},
{
offset: end,
color: "red"
},
{
offset: endSuffix,
color: "transparent"
}
]
}
}
};
a[2] = (lines)
if (endSuffix >= 1) {
dataIndex++;
if (dataIndex >= data.length) {
dataIndex = 0;
}
}
option = {
xAxis: {
type: "category",
data: ["A", "B", "C", "D", "E", "F"]
},
yAxis: {
type: "value"
},
series: a
};
myChart.setOption(option);
}, 100);