echart 后台返回多条数据处理

业务需求 : 如图  显示每年的能源使用的情况 

echart 后台返回多条数据处理_第1张图片

后台代码 直接上 业务层:

    

Map, Object> map = new HashMap<>();

        List xAxis = new ArrayList<>();// x坐标需要值
        List manyList = new ArrayList<>();// 返回一对多的数据
        
        List 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 many = new HashMap<>();
                many.put("name", m.getMaterialName() + "(" + m.getMaterialUnit() + ")");

                List 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
          });
},
   
});

    


你可能感兴趣的:(json,数据,echarts,一对多)