PiePlanarChart.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.planarChart;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.log4j.Logger;
  7. import com.formaction.vo.RowSet;
  8. import com.persistence.service.PersistenceFactory;
  9. import com.persistence.service.SysPersistence;
  10. import com.persistence.service.exception.PersistenceException;
  11. import com.planarChart.common.ChartJDBCHelper;
  12. import com.planarChart.vo.PieBaseData;
  13. import com.planarChart.vo.PieData;
  14. import com.planarChart.vo.PieDataLabels;
  15. import com.planarChart.vo.PlanarChartData;
  16. import com.planarChart.vo.QueryPlanarVo;
  17. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  18. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  19. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  20. public class PiePlanarChart extends PlanarChart{
  21. private QueryPlanarVo queryVo = null;
  22. private final static Logger log = Logger.getLogger(PiePlanarChart.class);
  23. public PiePlanarChart(QueryPlanarVo vo) {
  24. this.queryVo = vo;
  25. }
  26. /**
  27. * 最后需要的数据格式: series: [{ type: 'pie', name: '2008', //二维数据 size: '45%',
  28. * innerSize: '20%', data: [ { name: 'Firefox', y: 44.2, color: '#4572A7' },
  29. * //在二维条件下所有一维分组数据 { name: 'IE', y: 46.6, color: '#AA4643' }, { name:
  30. * 'Chrome', y: 3.1, color: '#89A54E' }, { name: 'Safari', y: 2.7, color:
  31. * '#80699B' }, { name: 'Opera', y: 2.3, color: '#3D96AE' }, { name:
  32. * 'Mozilla', y: 0.4, color: '#DB843D' } ], dataLabels: { enabled: false }
  33. * },{...}]
  34. */
  35. @Override
  36. public List<PlanarChartData> getJsonData() {
  37. List<PlanarChartData> returnList = new ArrayList<PlanarChartData>();
  38. PieData piedata = null;
  39. PieDataLabels dataLabel = null;
  40. if (queryVo.getyConstant().size() == 0) { // 二维查询条件为空,只查询一维所有数据
  41. dataLabel = new PieDataLabels();
  42. dataLabel.setColor("#000000");
  43. dataLabel.setConnectorColor("#000000");
  44. dataLabel.setEnabled(true);
  45. piedata = new PieData();
  46. piedata.setInnerSize("45%");
  47. piedata.setSize("80%");
  48. piedata.setType("pie");
  49. piedata.setName(queryVo.getyConstant().get(queryVo.getyAxis()));
  50. List<PieBaseData> data = getPieBaseData("");
  51. if (data.size() > 0) {
  52. piedata.setData(data);
  53. piedata.setName(" ");
  54. piedata.setDataLabels(dataLabel);
  55. returnList.add(piedata);
  56. }
  57. } else {
  58. Map<String, String> map = getYData(); // 数据表中所有(distinct)的二维数据
  59. /**
  60. * 有一个这样的问题,查询到二维distinct后的数据,循环这些数据作为条件查询一维数据,
  61. * 有时查询结果为空,也就是说,有的圈为空,这样导致结果有的圈很小或没有提示, 解决办法是:先根据二维数据查询一维数据,确定圈的个数
  62. */
  63. int m = 0; // 二维的圈数
  64. List<String> xdata = new ArrayList<String>(); // 根据二维条件存在的一维数据的键
  65. for (String key : map.keySet()) {
  66. List<PieBaseData> data = getPieBaseData(key);
  67. if (data.size() > 0) {
  68. m++;
  69. xdata.add(key);
  70. }
  71. }
  72. int width = 0; // 饼图每个圈的宽度
  73. if (0 != m) {
  74. width = 80 / m;
  75. }
  76. int index = 0;
  77. for (String key : xdata) {
  78. piedata = new PieData();
  79. piedata.setInnerSize(String.valueOf(10 + index * width) + "%");
  80. piedata.setSize(String.valueOf(10 + (index + 1) * width) + "%");
  81. index++;
  82. dataLabel = new PieDataLabels();
  83. dataLabel.setColor("#000000");
  84. dataLabel.setConnectorColor("#000000");
  85. dataLabel.setEnabled(false);
  86. if (index == m) {
  87. dataLabel.setEnabled(true); // 设置是否显示饼图提示信息
  88. }
  89. piedata.setType("pie");
  90. piedata.setName(queryVo.getyConstant().get(key));
  91. List<PieBaseData> data = getPieBaseData(key);
  92. if (data.size() > 0) {
  93. piedata.setData(data);
  94. piedata.setDataLabels(dataLabel);
  95. returnList.add(piedata);
  96. }
  97. }
  98. }
  99. return returnList;
  100. }
  101. /**
  102. * 从表数据获取二维存在的所有数据
  103. *
  104. * @return
  105. */
  106. public Map<String, String> getYData() {
  107. Map<String, String> map = new HashMap<String, String>();
  108. int classId = Integer.valueOf(queryVo.getClassid());
  109. SysModel sysmodel = ModelFactory.getSysmodel();
  110. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  111. MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId);
  112. String tableName = mdpClass.getName();
  113. String sql = "select distinct " + queryVo.getyAxis() + " from " + tableName + " where "
  114. + queryVo.getCondition() + " and " + queryVo.getyAxis() + " is not null ";
  115. log.info("取得一维常量中的数据sql:" + sql);
  116. try {
  117. ArrayList<String[]> result = persistence.getSearchResult(classId, sql);
  118. for (String[] str : result) {
  119. map.put(str[0], str[0]);
  120. }
  121. } catch (PersistenceException e) {
  122. e.printStackTrace();
  123. }
  124. return map;
  125. }
  126. @SuppressWarnings({ "rawtypes" })
  127. public List<PieBaseData> getPieBaseData(String value) {
  128. String sql = super.getSQLCondition(value, queryVo);
  129. log.info("二维图表查询sql:" + sql);
  130. ChartJDBCHelper jdbc = new ChartJDBCHelper();
  131. RowSet rowSet = jdbc.queryData(sql);
  132. Map<String, Integer> temp = new HashMap<String, Integer>();
  133. List<Map> rows = rowSet.getRows();
  134. for (Map m : rows) {
  135. for (Object key : m.keySet()) {
  136. String k = String.valueOf(key);
  137. String v = String.valueOf(m.get(k));
  138. if (queryVo.getxAxis().equalsIgnoreCase(String.valueOf(k))) {// 取常量值
  139. if (null == temp.get(v)) {
  140. temp.put(v, 1);
  141. } else {
  142. temp.put(v, temp.get(v) + 1);
  143. }
  144. }
  145. }
  146. }
  147. List<String> key = super.getXData(queryVo);
  148. PieBaseData pieBasedata = null;
  149. List<PieBaseData> list = new ArrayList<PieBaseData>();
  150. for (int i = 0; i < key.size(); i++) {
  151. pieBasedata = new PieBaseData();
  152. pieBasedata.setName(queryVo.getxConstant().get(key.get(i)));
  153. pieBasedata.setColor(queryVo.getColor().get(key.get(i)));
  154. if (null != temp.get(key.get(i))) {
  155. pieBasedata.setY(temp.get(key.get(i)));
  156. list.add(pieBasedata);
  157. }
  158. }
  159. return list;
  160. }
  161. }