Asynchronous data loading and updating (code cleanup)

The official website there is a small demo, simulation asynchronous data loading, I added a little bit annotations and notes (which includes loading animation)

code:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<title></title>
</head>
<body>
<div id="main" style="width: 1000px; height:500px; "> </ div> CB ({setTimeout (function () {// load simulation by asynchronous setTimeoutfunction fetchData (CB) {
<Script type =" text / JavaScript ">




categories: [ "Shirt", "sweater", "chiffon shirt", "pants", "shoes", "socks"],
Data: [. 5, 20 is, 36, 10, 10, 20 is]
});
}, 1000);
}
var myChart = echarts.init (document.getElementById ( 'main'));
// initial Option

myChart.showLoading ();
myChart.setOption ({
title: {
text: 'exemplary asynchronous data loading'
} ,
ToolTip: {},
Legend: {
Data: [ 'sales']
},
XAXIS: {
Data: []
},
YAXIS: {},
Series: [{
name: 'sales',
type: 'bar',
Data: []
}]
});
after data transfer // data
fetchData (function (data) {
myChart.hideLoading ();
myChart.setOption({
xAxis: {
// call categories of data values
data: data.categories
},
Series: [{
// The names to a respective series
name: 'sales',
// call data of data values
data: data.data
}]
});
});
</ Script>
</ body>
</ HTML>

 

There is also a dynamic update of the data Demo (not seen inside method), CV can be used directly

code:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<title></title>
</head>
<body>
<div id="main" style="width: 1000px; height: 500px;"></div>
<script type="text/javascript">
var base = +new Date(2014, 9, 3);
var oneDay = 24 * 3600 * 1000;
var date = [];

var data = [Math.random() * 150];
var now = new Date(base);

function addData(shift) {
now = [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/');
date.push(now);
data.push((Math.random() - 0.4) * 10 + data[data.length - 1]);

if (shift) {
date.shift();
data.shift();
}

now = new Date(+new Date(now) + oneDay);
}

for (var i = 1; i < 100; i++) {
addData();
}
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption( {
xAxis: {
type: 'category',
boundaryGap: false,
data: date
},
yAxis: {
boundaryGap: [0, '50%'],
type: 'value'
},
series: [
{
name:'成交',
type:'line',
smooth:true,
symbol: 'none',
stack: 'a',
areaStyle: {
normal: {}
},
data: data
}
]
});






setInterval(function () {
addData(true);
myChart.setOption({
xAxis: {
data: date
},
series: [{
name:'成交',
data: data
}]
});
}, 500);
</script>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/huchong-bk/p/11401477.html