写好的图表页面,加上动态数据后页面变成了空白
前台jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="height:400px"></div>
<!-- ECharts单文件引入 -->
<script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
<script type="text/javascript">
// 路径配置
require.config({
paths: {
echarts: 'http://echarts.baidu.com/build/dist'
}
});
// 使用
require(
[
'echarts',
'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
],
function (ec) {
// 基于准备好的dom,初始化echarts图表
var myChart = ec.init(document.getElementById('main'));
var option = {
title:{
show:true,
text:'这是一个柱状图'
},
toolbox:{
show:true,
//添加页面上的按钮
feature:{
dataView:{
show:true
},
restore:{
show:true
},
dataZoom:{
show:true
},
saveAsImage:{
show:true
}
}
},
tooltip: {
show: true
},
legend: {
data:['年龄']
},
xAxis : [
{
type : 'category',
data : []
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'年龄',
type:"bar",
data:[]
}
]
};
var names = [];
var ages = [];
$.ajax({
type:"post",
async:true,
url:"findAll",
data:{},
dataType:"json",
success:function (result) {
$.each(result,function (i,p) {
names[i]=p['names'];
ages[i]={'name':p['names'],'age':p['names']};
});
myChart.setOption({
xAxis:{
data:names
},
series:{
name:'年龄',
data:ages
}
});
}
});
}
);
</script>
</body>
后台controller层
package com.springboot.controller;
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* Created by LYJ on 2017/9/12.
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
@ResponseBody
public List<User> findAll(){
List<User> userList = userService.findAll();
System.err.println(userList);
// JSONArray json = JSONArray.fromObject(userList);
// response.getWriter().write("json");
return userList;
}
@RequestMapping("/zzt")
public String zzt(){
return "zzt";
}
}