1.完整例子
javascript:
var script = document.createElement('script');
script.type = "text/javascript";
script.src ="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js";
document.head.appendChild(script);
var randomInteger = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
var dataX1 = [];
var dataX2 = [];
var dataX3 = [];
var dataY1 = [];
var options = {
title: {
text: '实时曲线',
subtext: '',
left: '5%',
top: '2%',
textStyle: {
color: "rgba(204,187,225,0.5)"
}
},
backgroundColor: "#1A1835",
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
textStyle: {
color: "#fff"
}
}
},
grid: {
borderWidth: 0,
top: 110,
bottom: 95,
textStyle: {
color: "#fff",
fontSize: 30
}
},
xAxis: {
name: '时间',
type: 'category',
boundaryGap: false,
axisLine: {
lineStyle: {
color: "rgba(204,187,225,0.5)"
}
},
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
interval: 0,
rotate: 40
},
data: dataY1
},
yAxis: {
type: 'value',
scale: true,
name: '平均值',
splitLine: {
show: false
},
axisLine: {
lineStyle: {
color: "rgba(204,187,225,0.5)"
}
}
},
series: [{
name: "qx1",
type: 'line',
smooth: true,
symbolSize: 10,
symbol: 'circle',
itemStyle: {
color: "#6f7de3"
},
data: dataX1
}, {
name: "qx2",
type: 'line',
smooth: true,
symbolSize: 10,
symbol: 'circle',
itemStyle: {
color: "#c257F6"
},
data: dataX2
}, {
name: "qx3",
type: 'line',
smooth: true,
symbolSize: 10,
symbol: 'circle',
itemStyle: {
color: "#42bcbc"
},
data: dataX3
}],
legend: {
x: '30%',
top: '2%',
textStyle: {
color: '#90979c',
fontSize: 20
},
data: ['qx1', 'qx2', 'qx3']
},
calculable: true
};
function updateChart() {
var now = new Date();
var formattedDate = now.toISOString().replace(/T/, ' ').replace(/\..+/, '');
dataX1.push(randomInteger(1, 100));
dataX2.push(randomInteger(1, 100));
dataX3.push(randomInteger(1, 100));
dataY1.push(formattedDate);
if (dataX1.length > 10) {
dataX1.shift();
dataX2.shift();
dataX3.shift();
dataY1.shift();
}
options.xAxis.data = dataY1;
options.series[0].data = dataX1;
options.series[1].data = dataX2;
options.series[2].data = dataX3;
myChart.setOption(options);
}
var myChart = null;
setTimeout(()=>{
var dom = $('[data-label=test]').get(0);
myChart = echarts.init(dom);
myChart.setOption(options);
},1000);
setInterval(updateChart, 1000);
2.注意事项
(1)想要在全局使用echats中的方法和属性,则必须把echarts在定时器setTImeout中初始化
(2)想要定时器每次更新数据时,只显示选择的图列则,必须使用myChart.setOption(options);,反之,则必须使用myChart.setOption(options,true);
(3)在axure中不能使用注释
文章详细描述了如何在JavaScript中通过ECharts库创建一个实时更新的曲线图表,包括如何在定时器中初始化ECharts、更新数据和控制图列显示。同时提及了在特定环境(如Axure)中的注意事项。

2063

被折叠的 条评论
为什么被折叠?



