在进行折线图展示时 查出的数据中 日期不一定是连续的 则需进行日期填充数据补0
先定义一个方法用来查询出 开始日期和结束日期之间的时间 再将查询出的数据和时间做遍历塞值
日期查询代码如下:
public List<String> findDates(String dBegin, String dEnd,String type){ List<String> lDate = new ArrayList<String>(); try{ SimpleDateFormat sd = null; int value = 0; Date begin = null; Date end = null; if("day".equals(type)){ sd = new SimpleDateFormat("yyyyMMdd"); value = Calendar.DAY_OF_MONTH; begin = sd.parse(dBegin); end = sd.parse(dEnd); }else if("month".equals(type)){ sd = new SimpleDateFormat("yyyyMM"); value = Calendar.MONTH; begin = sd.parse(dBegin); end = sd.parse(dEnd); } lDate.add(sd.format(begin)); Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calBegin.setTime(begin); Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calEnd.setTime(end); // 测试此日期是否在指定日期之后 while (end.after(calBegin.getTime())){ // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(value, 1); lDate.add(sd.format(calBegin.getTime())); } }catch(Exception e){ } return lDate; }
当有时间的数据后 便可进行遍历塞值
//查询出 时间和数据 List<Map> result = abilityCallMapper.abilityCallCountTotal(param); //获取到前台传入的开始日期,结束日期和日期类型 List<String> dates = findDates(param.get("startDate").toString(), param.get("endDate").toString(),param.get("type").toString()); //遍历塞值 boolean exists=false; for (int i=0;i<dates.size();i++) { exists=false; for (Map abilityDetail : result) { if (dates.get(i).equals((abilityDetail.get("cDate")))) { exists=true; break; } } if(!exists){ final String date=dates.get(i); Map df = new HashMap(); df.put("ctotal",0); df.put("cDate",date); result.add(i,df); } }