前端之家收集整理的这篇文章主要介绍了
ECharts异步获取数据,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="static/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="static/js/echarts.js"></script>
</head>
<body>
<div id="main" style="width: 1000px;height:400px;"></div>
<script> var myChart = echarts.init(document.getElementById('main')); $.get("json_test").done(function(obj){ var datas = JSON.parse(obj); console.log(datas.series[1]); var datanam=[]; for(var i=0;i<datas.series.length;i++){ datanam.push(datas.series[i].name); } myChart.setOption({ title: { text: datas.title },tooltip: {},legend: { data:datanam },xAxis: { data: datas.xName },yAxis: {},series: datas.series }); }) </script>
</body>
</html>
后台代码
@ResponseBody
@RequestMapping("/json_test")
public Object thisIsTest(){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/Administrator/Desktop/test.json"),"utf-8"));
try {
StringBuilder sb = new StringBuilder();
String data = null;
while ((data = br.readLine())!=null) {
sb.append(data);
}
System.out.println(sb.toString());
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}