PlanarChart.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.planarChart;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.log4j.Logger;
  8. import com.formaction.Utils;
  9. import com.persistence.service.PersistenceFactory;
  10. import com.persistence.service.SysPersistence;
  11. import com.persistence.service.exception.PersistenceException;
  12. import com.planarChart.vo.PlanarChartData;
  13. import com.planarChart.vo.QueryPlanarVo;
  14. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  15. import com.sysmodel.datamodel.xmlmodel.Reference;
  16. import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
  17. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  18. import com.sysmodel.datamodel.xmlmodel.able.MdpConstant;
  19. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  20. public abstract class PlanarChart{
  21. public final static int PIE = 1;
  22. public final static int LINE = 2;
  23. public final static int BAR = 3;
  24. public final static String[] color = new String[] { "#4572A7", "#AA4643", "#89A54E",
  25. "#80699B", "#3D96AE", "#FFCC33", "#FFCC99", "#996633", "#DB843D", "#003333", "#99CC66",
  26. "#CCCC99", "#6699CC", "#330000", "#009966", "#00CC99", "#CCCCCC", "#99CC33", "#3399CC",
  27. "#66CC33", "#99CC99", "#999933", "#FFCC99", "#CCCC99", "#FF9933", "#6699CC", "#66CCCC",
  28. "#33CCCC", "#66CC66", "#6699FF", "#00CCCC", "#66FF66", "#FFCCFF", "#FFCC66", "#CCFF99",
  29. "#996666", "#6699CC", "#66CCCC", "#33CCCC", "#0099FF" };
  30. private static SysModel sysmodel = ModelFactory.getSysmodel();
  31. private final static Logger log = Logger.getLogger(PlanarChart.class);
  32. /**
  33. * 返回给前台的JSON数据,在子类中实现自己的数据格式
  34. *
  35. * @return
  36. */
  37. public abstract List<PlanarChartData> getJsonData();
  38. public static QueryPlanarVo getQueryVo(String params) {
  39. QueryPlanarVo queryVo = new QueryPlanarVo();
  40. String condition = Utils.getParameter("condition", params);
  41. String xAxis = Utils.getParameter("xAxis", params);
  42. String yAxis = Utils.getParameter("yAxis", params) == null ? "" : Utils.getParameter(
  43. "yAxis", params);
  44. String type = Utils.getParameter("type", params);
  45. String classid = Utils.getParameter("classid", params);
  46. queryVo.setClassid(classid);
  47. queryVo.setCondition(condition);
  48. queryVo.setxAxis(xAxis);
  49. queryVo.setyAxis(yAxis);
  50. queryVo.setType(type);
  51. queryVo = initConstant(queryVo);
  52. return queryVo;
  53. }
  54. private static QueryPlanarVo initConstant(QueryPlanarVo vo) {
  55. int classid = Integer.parseInt(vo.getClassid());
  56. MdpClass mdpClass = sysmodel.getMdpClassByClassID(classid);
  57. String referenceTable = null;
  58. // 设置一维属性值
  59. MdpAttribute atttrbute = mdpClass.getMdpAttributeByName(vo.getxAxis());
  60. Reference reference = atttrbute.getReference();// 取得子集
  61. referenceTable = reference.getReferenceTable();// 获取相关联的常量表
  62. MdpConstant mdpConstant = sysmodel.getMdpConstantByName(referenceTable);// 读取某一常量的具体数据
  63. List<String[]> nodeList = mdpConstant.getAllNode();// 这是全部的常量值
  64. Map<String, String> transConstant = new HashMap<String, String>();
  65. for (String[] ConstantValue : nodeList) {
  66. transConstant.put(ConstantValue[0], ConstantValue[1]);
  67. }
  68. vo.setxConstant(transConstant);
  69. /**
  70. * 设置二维属性值 二维属性值可能不选,也可能值为no,
  71. */
  72. if (!"".equals(vo.getyAxis()) && !"no".equals(vo.getyAxis())) {
  73. MdpAttribute yatttrbute = mdpClass.getMdpAttributeByName(vo.getyAxis());
  74. Reference yreference = yatttrbute.getReference();// 取得子集
  75. referenceTable = yreference.getReferenceTable();// 获取相关联的常量表
  76. MdpConstant ymdpConstant = sysmodel.getMdpConstantByName(referenceTable);// 读取某一常量的具体数据
  77. List<String[]> ynodeList = ymdpConstant.getAllNode();// 这是全部的常量值
  78. Map<String, String> ytransConstant = new HashMap<String, String>();
  79. for (String[] ConstantValue : ynodeList) {
  80. ytransConstant.put(ConstantValue[0], ConstantValue[1]);
  81. }
  82. vo.setyConstant(ytransConstant);
  83. } else {
  84. Map<String, String> ytransConstant = new HashMap<String, String>();
  85. vo.setyConstant(ytransConstant);
  86. }
  87. // 设置饼图属性的颜色
  88. Map<String, String> colour = new HashMap<String, String>();
  89. int i = 0;
  90. for (String key : vo.getxConstant().keySet()) {
  91. if (i >= color.length) {
  92. i -= 8;// 这种情况很少出现
  93. }
  94. colour.put(key, color[i]);
  95. i++;
  96. }
  97. vo.setColor(colour);
  98. return vo;
  99. }
  100. /**
  101. * 根据二维的一个条件值,查询一维的所有数据
  102. *
  103. * @param key
  104. * : 二维的一个值
  105. * @return
  106. */
  107. public String getSQLCondition(String value, QueryPlanarVo queryVo) {
  108. int classId = Integer.valueOf(queryVo.getClassid());
  109. SysModel sysmodel = ModelFactory.getSysmodel();
  110. MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId);
  111. String tableName = mdpClass.getName();
  112. StringBuffer sql = new StringBuffer();
  113. sql.append("select ");
  114. sql.append(queryVo.getxAxis());
  115. sql.append(" from ");
  116. sql.append(tableName);
  117. sql.append(" where ");
  118. sql.append(queryVo.getCondition());
  119. if (!"".equals(value)) { // 二维查询属性不为空
  120. sql.append(" and ");
  121. sql.append(queryVo.getyAxis() + " = '" + value + "'");
  122. sql.append(" and ");
  123. sql.append(queryVo.getxAxis() + " is not null");
  124. }
  125. return sql.toString();
  126. }
  127. /**
  128. * 取得一维常量中的数据
  129. *
  130. * @return
  131. */
  132. public ArrayList<String> getXData(QueryPlanarVo queryVo) {
  133. ArrayList<String> list = new ArrayList<String>();
  134. int classId = Integer.valueOf(queryVo.getClassid());
  135. SysModel sysmodel = ModelFactory.getSysmodel();
  136. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  137. MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId);
  138. String tableName = mdpClass.getName();
  139. String sql = "select distinct " + queryVo.getxAxis() + " from " + tableName + " where "
  140. + queryVo.getCondition() + " and " + queryVo.getxAxis() + " is not null";
  141. log.info("取得一维常量中的数据sql:" + sql);
  142. try {
  143. ArrayList<String[]> result = persistence.getSearchResult(classId, sql);
  144. for (String[] str : result) {
  145. list.add(str[0]);
  146. }
  147. } catch (PersistenceException e) {
  148. e.printStackTrace();
  149. }
  150. Collections.sort(list);
  151. return list;
  152. }
  153. }