Exemplo n.º 1
0
 /** 描述:把数据转换成json 时间:Oct 30, 2010 作者:童贝 参数: 返回值:String 抛出异常: */
 public String getTableToJson(MSLineChart chart) {
   StringBuffer result = new StringBuffer();
   // 记录集
   for (MSLineDataSet dataset : chart.getMSLineDataSets()) {
     List<Map> sortList = new ArrayList<Map>();
     List<MSLineSet> setList = dataset.getMSLineSets();
     int flag = 0;
     for (MSLineCategory category : chart.getMSLineCategories().getCategorys()) {
       Map<String, String> map = new HashMap<String, String>();
       String label = category.getLabel();
       MSLineSet set = setList.get(flag);
       String value = set.getValue();
       map.put(label, value);
       sortList.add(map);
       flag++;
     }
     result.append(JSONUtil.listToJson(sortList));
   }
   return result.toString();
 }
Exemplo n.º 2
0
  /**
   * 描述:生成图形 时间:2010-5-5 作者:童贝 参数: 返回值: 抛出异常:
   *
   * @throws Exception
   */
  public MSLineChart generateChart(String keyList, String item, String kid, String flflag)
      throws Exception {
    // 图形
    MSLineChart chart = new MSLineChart();
    chart.setCaption(this.getInameByKidAndItem(kid, item));
    chart.setSubcaption("");
    chart.setLabelStep("1");
    chart.setBaseFontSize("12");
    // 标题
    MSLineCategories mSLineCategories = new MSLineCategories();

    // 数据集
    MSLineDataSet dataSet = new MSLineDataSet();
    dataSet.setSeriesName("");
    String[] color = {"F1683C", "2AD62A"};
    dataSet.setColor(color[0]);
    dataSet.setAnchorBgColor(color[0]);
    dataSet.setAnchorBorderColor(color[0]);

    Connection conn = null;
    Statement state = null;
    ResultSet rs = null;
    // 组装查询条件
    Map<String, String> keyMap = new HashMap<String, String>();
    String[] keys = keyList.split("!");
    for (String key : keys) {
      String[] nameAndValue = key.split(",");
      if (nameAndValue.length == 2) {
        String name = nameAndValue[0];
        String value = nameAndValue[1];
        keyMap.put(name, value);
      }
    }
    try {
      StringBuffer keyWhere = new StringBuffer();
      StringBuffer field = new StringBuffer();

      String startNian = keyMap.get(TjInterFace.SNIAN);
      String startYue = keyMap.get(TjInterFace.SYUE);
      String startRi = keyMap.get(TjInterFace.SRI);
      String endNian = keyMap.get(TjInterFace.ENIAN);
      String endYue = keyMap.get(TjInterFace.EYUE);
      String endRi = keyMap.get(TjInterFace.ERI);
      // 日数据
      if ("1".equals(flflag)) {
        field.append(TjInterFace.NIAN + "," + TjInterFace.YUE + "," + TjInterFace.RI + "," + item);
        keyWhere.append(
            "((NIAN="
                + startNian
                + " and YUE="
                + startYue
                + " and RI>="
                + startRi
                + ") or (NIAN="
                + startNian
                + " and YUE>"
                + startYue
                + ") or (NIAN="
                + endNian
                + " and YUE<"
                + endYue
                + ") or (NIAN="
                + endNian
                + " and YUE="
                + endYue
                + " and RI<="
                + endRi
                + ")) ");
        // 月数据
      } else if ("2".equals(flflag)) {
        field.append(TjInterFace.NIAN + "," + TjInterFace.YUE + "," + item);
        keyWhere.append(
            "((NIAN="
                + startNian
                + " and YUE>="
                + startYue
                + ") or (NIAN>"
                + startNian
                + " and NIAN<"
                + endNian
                + ") or (NIAN="
                + endNian
                + " and YUE<="
                + endYue
                + "))");
        // 年数据
      } else if ("3".equals(flflag)) {
        field.append(TjInterFace.NIAN + "," + item);
        keyWhere.append("NIAN>=" + startNian + " and  NIAN<=" + endNian + "");
      }

      // 增加机组条件
      String jz = keyMap.get(TjInterFace.JZ);
      if (StringUtils.isNotEmpty(jz)) {
        keyWhere.append(" and " + TjInterFace.JZ + "='" + jz + "'");
      }
      // 查询语句
      String sql =
          "select " + field.toString() + " from DATA_" + kid + " where " + keyWhere.toString();

      // 存放查询出来的所有结果
      List<Map<String, String>> rowRecords = new ArrayList<Map<String, String>>();
      conn = this.dataSource.getConnection();
      state = conn.createStatement();
      rs = state.executeQuery(sql);
      ResultSetMetaData rsmeta = rs.getMetaData();
      while (rs.next()) {
        Map<String, String> map = new LinkedHashMap<String, String>();
        for (int i = 1; i <= rsmeta.getColumnCount(); i++) {
          Object obj = rs.getObject(i);
          map.put(rsmeta.getColumnName(i).toString(), (obj == null ? "" : obj.toString()));
        }
        rowRecords.add(map);
      }

      // 初始化起始时间到终止时间之间的每天的值
      List<Map<String, String>> allData = this.initQssjToZzsjData(keyMap, flflag, item);

      // 全部日期的值
      for (Map<String, String> itemMap : allData) {
        MSLineSet set = new MSLineSet();
        String nian = itemMap.get(TjInterFace.NIAN);
        String yue = itemMap.get(TjInterFace.YUE);
        String ri = itemMap.get(TjInterFace.RI);
        // 查询出来有记录的值
        for (Map<String, String> recordMap : rowRecords) {
          String reNian = recordMap.get(TjInterFace.NIAN);
          String reYue = recordMap.get(TjInterFace.YUE);
          String reRi = recordMap.get(TjInterFace.RI);
          if ("1".equals(flflag)) {
            if (nian.equals(reNian) && yue.equals(reYue) && ri.equals(reRi)) {
              itemMap.put(item, recordMap.get(item));
            }
          } else if ("2".equals(flflag)) {
            if (nian.equals(reNian) && yue.equals(reYue)) {
              itemMap.put(item, recordMap.get(item));
            }
          } else if ("3".equals(flflag)) {
            if (nian.equals(reNian)) {
              itemMap.put(item, recordMap.get(item));
            }
          }
        }
        MSLineCategory category = new MSLineCategory();
        if ("1".equals(flflag)) {
          category.setLabel(new Integer(ri).toString());
        } else if ("2".equals(flflag)) {
          category.setLabel(new Integer(yue).toString());
        } else if ("3".equals(flflag)) {
          category.setLabel(new Integer(nian).toString());
        }
        mSLineCategories.getCategorys().add(category);

        set.setValue(itemMap.get(item));
        dataSet.getMSLineSets().add(set);
      }

      chart.getMSLineDataSets().add(dataSet);

      // 下面公共的样式属性
      MSLineStyle style = new MSLineStyle();
      style.setName("captionFont");
      MSLineDefinition defin = new MSLineDefinition();
      defin.setMSLineStyle(style);

      MSLineApply apply = new MSLineApply();
      MSLineApplication applition = new MSLineApplication();
      applition.getMSLineApplys().add(apply);

      MSLineStyles styles = new MSLineStyles();
      styles.setMSLineApplication(applition);
      styles.setMSLineDefinition(defin);

      chart.setMSLineCategories(mSLineCategories);
      chart.setMSLineStyles(styles);
    } catch (Exception e) {
      throw e;
    } finally {
      if (rs != null) {
        rs.close();
      }
      if (state != null) {
        state.close();
      }
      if (conn != null) {
        conn.close();
      }
    }
    return chart;
  }