echarts 3 動態顯示最近一天資料,以分為單位獲取資料點
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="./echarts.js"></script>
<script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
function randomData() {
now = new Date(+now + 60*1000);//下一個點獲取時新增的時間設定
value = value + Math.random() * 21 - 10;
return {
name: now.toString(),
value: [
now,
Math.round(value)
]
}
}
var data = [];
var now = new Date();
var value = Math.random() * 1000;
for (var i = 0; i < 24*60; i++) {//初始化有多少個點
data.push(randomData());
}
option = {
title: {
text: '動態資料 + 時間座標軸'
},
tooltip: {
trigger: 'axis',
formatter: function (params) {
params = params[0];
var date = new Date(params.name);
return date.getHours() + ':' + date.getMinutes() +" "+date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];//資料點現實的格式資料
},
axisPointer: {
animation: false
}
},
xAxis: {
type: 'time',
splitLine: {
show: false
}
},
yAxis: {
type: 'value',
boundaryGap: [0, '100%'],
splitLine: {
show: false
}
},
series: [{
name: '模擬資料',
type: 'line',
showSymbol: false,
hoverAnimation: false,
data: data
}]
};
setInterval(function () {
data.shift(); //資料不積累,來一個點,則不現實最後一個點,註釋則積累現實
data.push(randomData());
myChart.setOption({
series: [{
data: data
}]
});
}, 1000);
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
</script>
</body>
</html>