<script type="text/javascript">
// 路径配置
require.config({
paths : {
echarts : 'http://echarts.baidu.com/build/dist'
}
});
// 使用
require([ 'echarts', 'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
],
drewEcharts
);
function drewEcharts(ec) {
// 基于准备好的dom,初始化echarts图表
var myChart = ec.init(document.getElementById('main'));
var option = {
tooltip : {
show : true
},
legend : {
data : [ '人数' ]
},
xAxis : [ {
type : 'category',
data :(function(){
var arr=[];
$.ajax({
type : "post",
async : false, //同步执行
url : "bar.do",
data : {},
dataType : "json", //返回数据形式为json
success : function(result) {
if (result) {
/* alter("first"); */
for(var i=0;i<result.length;i++){
console.log(result[i].name);
arr.push(result[i].name);
}
}
},
error : function(errorMsg) {
alert("不好意思,图表请求数据失败啦!");
myChart.hideLoading();
}
})
return arr;
})()
} ],
yAxis : [ {
type : 'value'
} ],
series : [ {
"name" : "人数",
"type" : "bar",
"data" : (function(){
var arr=[];
$.ajax({
type : "post",
async : false, //同步执行
url : "bar.do",
data : {},
dataType : "json", //返回数据形式为json
success : function(result) {
if (result) {
for(var i=0;i<result.length;i++){
console.log(result[i].num);
arr.push(result[i].num);
}
}
},
error : function(errorMsg) {
alert("不好意思,大爷,图表请求数据失败啦!");
myChart.hideLoading();
}
})
return arr;
})()
} ]
};
// 为echarts对象加载数据
myChart.setOption(option);
}
Connection connection;
public Connection getConnection() {
try {
String name = "admin";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/todo_ssm";
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, name, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public List<barBean> listAll() {
ArrayList<barBean> list = new ArrayList<barBean>();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = this.getConnection().prepareStatement("SELECT * FROM records");
rs = pstmt.executeQuery();
while (rs.next()) {
barBean bar = new barBean();
bar.setName(rs.getString("name"));
bar.setSum(rs.getInt("sum"));
list.add(bar);
System.out.println("连接数据库成功");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
java代码如下,数据是获取到了,就是不显示图表
已经解决了,是javascript写错!