业务需求 : 如图 显示每年的能源使用的情况
后台代码 直接上 业务层:
Map<String, Object> map = new HashMap<>(); List<String> xAxis = new ArrayList<>();// x坐标需要值 List<Object> manyList = new ArrayList<>();// 返回一对多的数据 List<Material> eqList = indexDao.getMaterialenery(); if (eqList.size() > 0) { String time = DateUtil.currentDatetime("YYYY"); for (int i = 3; i >=0; i--) {// 填充x坐标 xAxis.add(String.valueOf(Integer.valueOf(time) - i)); } for (Material m : eqList) { Map<String, Object> many = new HashMap<>(); many.put("name", m.getMaterialName() + "(" + m.getMaterialUnit() + ")"); List<BigDecimal> blist = new ArrayList<>(); String eqId = getMaterial(m.getMaterialId()); for (int i = 3; i >=0; i--) { String Ytime = String.valueOf(Integer.valueOf(time) - i); if (StringUtil.isRealString(eqId)) { BigDecimal energyP = BigDecimal.valueOf(0); for (int e = 1; e <= 12; e++) { String Tname = "'dataday_" + Ytime + (String.valueOf(e).length() == 1 ? "0" + String.valueOf(e) : String.valueOf(e)) + "'"; if (indexDao.gettablename(Tname) > 0) { energyP = ArithMath.add(indexDao.getAmountThat(Tname.replace("'", ""), eqId), energyP); } } blist.add(energyP); } else { blist.add(BigDecimal.valueOf(0)); } } many.put("data", blist); manyList.add(many); } } map.put("elName", xAxis); map.put("enerydata", manyList);
前端代码:
初始化的echarts 省略... 以下是解析数据的方法
$.ajax({ url : ctx + "/rest/index/indexelectricJson", type : "GET", contentType: "application/json", data:{}, async:true, // dataType:"JSONP", // jsonpCallback: 'GetAllYearPower', //回调函数名 success : function(data) { var enlist=data.enerydata; var elegend=[]; var eseries =[]; if(enlist.length>0){ for(var i in enlist){ elegend.push({name:enlist[i]['name'],icon: 'circle'}); eseries.push({ name: enlist[i]['name'], type: 'bar', data: enlist[i]['data'] }); } } myChart2.hideLoading();// 隐藏动态加载 myChart2.setOption({// 能力管控 xAxis:{data:data.elName}, legend:{ data: elegend }, series:eseries });
}, });