for循环的代价

数据结构
思想前所未有的高度集中的一天 ,脑细胞死了不知几千万,就为了将图表的显示时间运行的快一点,其他理论啥都不说了 直接上今天上午和下午敲的两段代码

运行效果如下:
图片

(1)上午:
@Override
public PageTool getReportConfData(String objectId, String addr,
   String as_num, String time, String type, PageTool pageTool,
   String sizeStr) {
  long size = Long.parseLong(sizeStr);
  String countsql = BgpReportSql.getAllCount();
  Integer count = queryForInt(countsql);
  String sql = BgpReportSql.getReportSql(addr, as_num, time, type);
  //表格结果
  List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
  //折线图结果
  List<Map<String, Object>> lineList = new ArrayList<Map<String, Object>>();
  //类型结果
  List<Map<String, Object>> typeList = new ArrayList<Map<String, Object>>();
  String dataSql = BgpReportSql.getLineSql(addr, as_num, time, type, size);
  lineList = queryForList(dataSql);
  String typeSql = BgpReportSql.getAllType();
  typeList = queryForList(typeSql);
  // 获取总行数
  int totalCount = getTotalCount(sql);
  resultList = queryForList(sql);
  for (int i = 0; i < resultList.size(); i++) {
   Integer cntNum = Integer.parseInt(resultList.get(i).get("CNTNUM").toString());
   // 获取格式化对象
   NumberFormat nt = NumberFormat.getPercentInstance();
   // 设置百分数精确度2即保留两位小数
   nt.setMinimumFractionDigits(2);
   double dou = (double) cntNum / count;
   resultList.get(i).put("areNum", nt.format(dou).toString());
  }
 
  //开始计算横坐标
  String[] arrStr = time.split("-");
  long arr1 = Long.parseLong(arrStr[0].toString());
  long arr2 = Long.parseLong(arrStr[1].toString());
  long date = arr2 - arr1;
  // 时间分割
  long size_d = date / (size * 60) + 1;
  String[] categories = new String[(int) size_d];
  StringBuffer categoriebuff=new StringBuffer("[");
  for (int i = 0; i < size_d; i++) {
   categories[i] = DateUtil.getDate(Math.round(arr1 / (size * 60))* size * 60 + (size) * 60 * i);
   categoriebuff.append("'").append(DateUtil.getDate(Math.round(arr1 / (size * 60))* size * 60 + (size) * 60 * i)).append("'").append(",");
  }
 
  //横坐标
  String categorieStr=categoriebuff.substring(0,categoriebuff.length()-1)+"]";
  //计算每条线的格式数据
  StringBuffer entrysData = new StringBuffer("[");
  for (int j = 0; j < typeList.size(); j++) {
   StringBuffer itemData = new StringBuffer("{");
   itemData.append("name:").append("'").append(typeList.get(j).get("TEXT").toString())
     .append("',data:[");
   StringBuffer print = new StringBuffer();
   String idType = typeList.get(j).get("ID").toString();
   System.out.println("总数:"+lineList.size());
   for (int k = 0; k < lineList.size(); k++) {
    System.out.println("时间点数:"+categories.length);
    for (int i = 0; i < categories.length; i++) {
     String printTime = categories[i].toString();
     if (lineList.get(k).get("MSG_TYPE").toString().equals(idType)) {
      String itemDate = DateUtil.getDate(Long.parseLong(lineList.get(k).get("TT").toString()));
      if (itemDate.equals(printTime)) {
       if (StringUtils.isNotEmpty(lineList.get(k).get("CNTNUM").toString())) {
        print.append(lineList.get(k).get("CNTNUM").toString()).append(",");
       } else {
        print.append("0").append(",");
       }
      }
     }
    }
   }
   if(print.length()>1){
   itemData.append(print.substring(0, print.length() - 1)).append("]}").append(",");
   entrysData.append(itemData);
   }
  }
  StringBuffer lineData =new StringBuffer();
  if(entrysData.length()>1){
  //折线数据
   lineData.append(entrysData.substring(0,entrysData.length() - 1));
  }
  lineData.append("]");
  pageTool.setTotleCount(totalCount);
  pageTool.setDataList(resultList);
  pageTool.setCategories(categorieStr);
  pageTool.setSeries(lineData.toString());
  return pageTool;
}

********************************************************************************************
下午:
  @Override
public PageTool getReportConfData(String objectId, String addr,
   String as_num, String time, String type, PageTool pageTool,
   String sizeStr) {
  long size = Long.parseLong(sizeStr);
  String countsql = BgpReportSql.getAllCount();
  Integer count = queryForInt(countsql);
  String sql = BgpReportSql.getReportSql(addr, as_num, time, type);
  // 表格结果
  List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
  // 折线图结果
  List<Map<String, Object>> lineList = new ArrayList<Map<String, Object>>();
  // 类型结果
  List<Map<String, Object>> typeList = new ArrayList<Map<String, Object>>();
  String dataSql = BgpReportSql
    .getLineSql(addr, as_num, time, type, size);
  lineList = queryForList(dataSql);
  String typeSql = BgpReportSql.getAllType();
  typeList = queryForList(typeSql);
  // 获取总行数
  int totalCount = getTotalCount(sql);
  resultList = queryForList(sql);
  for (int i = 0; i < resultList.size(); i++) {
   Integer cntNum = Integer.parseInt(resultList.get(i).get("CNTNUM")
     .toString());
   // 获取格式化对象
   NumberFormat nt = NumberFormat.getPercentInstance();
   // 设置百分数精确度2即保留两位小数
   nt.setMinimumFractionDigits(2);
   double dou = (double) cntNum / count;
   resultList.get(i).put("areNum", nt.format(dou).toString());
  }
  // 开始计算横坐标
  String[] arrStr = time.split("-");
  int arr1 = Integer.parseInt(arrStr[0].toString());
  int arr2 = Integer.parseInt(arrStr[1].toString());
  int date = arr2 - arr1;
  // 时间分割
  long size_d = date / (size * 60) + 1;
  HashMap<Integer, String> c_map=new HashMap<Integer, String>();
  for (int i = 0; i < size_d; i++) {
   int date_p=(int) ((arr1 / (size * 60))* size * 60 + (size) * 60 * i);
   c_map.put(date_p, "0");
  }
  String[] categories = new String[(int) size_d];
  StringBuffer categoriebuff = new StringBuffer("[");
  for (int i = 0; i < size_d; i++) {
   categories[i] = DateUtil.getDate(Math.round(arr1 / (size * 60))* size * 60 + (size) * 60 * i);
   categoriebuff.append("'").append(DateUtil.getDate(Math.round(arr1 / (size * 60))* size * 60 + (size) * 60 * i)).append("'").append(",");
  }
  // 横坐标
  String categorieStr = categoriebuff.substring(0,
    categoriebuff.length() - 1) + "]";
  // 计算每条线的格式数据
  StringBuffer entrysData = new StringBuffer("[");
  for (int j = 0; j < typeList.size(); j++) {
   StringBuffer itemData = new StringBuffer("{");
   itemData.append("name:").append("'")
     .append(typeList.get(j).get("TEXT").toString())
     .append("',data:[");
   String idType = typeList.get(j).get("ID").toString();
   for (int i = 0; i < lineList.size(); i++) {
    if (lineList.get(i).get("MSG_TYPE").toString().equals(idType)) {
     int c_time = Integer.parseInt(lineList.get(i).get("TT").toString());
     if (StringUtils.isNotEmpty(lineList.get(i).get("CNTNUM").toString())) {
      c_map.put(c_time,lineList.get(i).get("CNTNUM").toString());
     }
    }
   }
   StringBuffer porintStr = new StringBuffer();
   for (int i = 0; i <size_d; i++) {
    int date_p=(int) ((arr1 / (size * 60))* size * 60 + (size) * 60 * i);
    porintStr.append(c_map.get(date_p).toString()).append(",");
   }
   itemData.append(porintStr.substring(0, porintStr.length() - 1))
     .append("]}").append(",");
   entrysData.append(itemData);
  }
  StringBuffer lineData = new StringBuffer();
  // 折线数据
  lineData.append(entrysData.substring(0, entrysData.length() - 1));
  lineData.append("]");
  pageTool.setTotleCount(totalCount);
  pageTool.setDataList(resultList);
  pageTool.setCategories(categorieStr);
  pageTool.setSeries(lineData.toString());
  return pageTool;
}

你可能感兴趣的:(数据结构,UI,Ajax)