1.jsp中引入jquery.js ecahrt.js ,js部分代码如下
var myChart = echarts.init(document.getElementById('main'));
//显示标题,图例和空的坐标轴myChart.setOption({
title: {
text: '异步数据加载示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: []
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: []
}]
});
//异步加载数据
$.get('${pageContext.request.contextPath}/echart',{},function (d){
var data = eval("("+d+")");
// 填入数据
myChart.setOption({
xAxis: {
data: data.categories
},
series: [{
// 根据名字对应到相应的系列
name: '销量',
data: data.values
}]
});
});
2.后台返回json格式数据
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String[] categories = {"鞋", "衬衫", "外套", "牛仔裤"};
Integer[] values = {80, 50, 75, 100};
Map<String, Object> json = new HashMap<String, Object>();
json.put("categories", categories);
json.put("values", values);
JsonUtils.writeJson(json, req, resp);
}
3.效果图