BarPlanarChart.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.planarChart.common.ChartJDBCHelper;
  9. import com.planarChart.vo.BarBaseData;
  10. import com.planarChart.vo.BarData;
  11. import com.planarChart.vo.PlanarChartData;
  12. import com.planarChart.vo.QueryPlanarVo;
  13. public class BarPlanarChart extends PlanarChart{
  14. private final static Logger log = Logger.getLogger(BarPlanarChart.class);
  15. private QueryPlanarVo queryVo = null;
  16. public BarPlanarChart(QueryPlanarVo vo) {
  17. this.queryVo = vo;
  18. }
  19. /**
  20. * 最后需要的数据格式: xAxis: {categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  21. * 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] series: [{ name: 'Tokyo', data:
  22. * [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1,
  23. * 95.6, 54.4]
  24. *
  25. * }, { name: 'New York', data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0,
  26. * 104.3, 91.2, 83.5, 106.6, 92.3]
  27. *
  28. * }]
  29. */
  30. @Override
  31. public List<PlanarChartData> getJsonData() {
  32. List<PlanarChartData> returnList = new ArrayList<PlanarChartData>();
  33. BarData barData = new BarData();
  34. List<BarBaseData> barBaseData = getBarBaseData();
  35. barData.setyAxis(barBaseData);
  36. List<String> key = super.getXData(queryVo);
  37. List<String> xAxis = new ArrayList<String>();
  38. for (int i = 0; i < key.size(); i++) {
  39. xAxis.add(queryVo.getxConstant().get(key.get(i)));
  40. }
  41. barData.setxAxis(xAxis);
  42. returnList.add(barData);
  43. return returnList;
  44. }
  45. /**
  46. * 获取二维数据
  47. *
  48. * @return
  49. */
  50. public List<BarBaseData> getBarBaseData() {
  51. List<BarBaseData> baseBars = new ArrayList<BarBaseData>();
  52. BarBaseData barBasedata = null;
  53. if (queryVo.getyConstant().size() == 0) { // 二维查询条件为空,只查询一维所有数据
  54. barBasedata = new BarBaseData();
  55. barBasedata.setName(" ");
  56. List<Integer> data = getPlanerData("");
  57. if (data.size() > 0) {
  58. barBasedata.setData(data);
  59. baseBars.add(barBasedata);
  60. }
  61. } else {
  62. for (String key : queryVo.getyConstant().keySet()) {
  63. barBasedata = new BarBaseData();
  64. String value = queryVo.getyConstant().get(key);
  65. barBasedata.setName(value);
  66. List<Integer> data = getPlanerData(key);
  67. if (data.size() > 0) {
  68. barBasedata.setData(data);
  69. baseBars.add(barBasedata);
  70. }
  71. }
  72. }
  73. return baseBars;
  74. }
  75. /**
  76. * 根据二维的一个字段及值,查询该条件下一维数据
  77. *
  78. * @param name
  79. * : 二维查询字段名
  80. * @return
  81. */
  82. @SuppressWarnings({ "rawtypes" })
  83. public List<Integer> getPlanerData(String value) {
  84. String sql = super.getSQLCondition(value, queryVo);
  85. log.info("二维图表查询sql:" + sql);
  86. ChartJDBCHelper jdbc = new ChartJDBCHelper();
  87. RowSet rowSet = jdbc.queryData(sql);
  88. Map<String, Integer> temp = new HashMap<String, Integer>();
  89. List<Map> rows = rowSet.getRows();
  90. for (Map m : rows) {
  91. for (Object key : m.keySet()) {
  92. String k = String.valueOf(key);
  93. String v = String.valueOf(m.get(k));
  94. if (queryVo.getxAxis().equalsIgnoreCase(String.valueOf(k))) {// 取常量值
  95. if (null == temp.get(v)) {
  96. temp.put(v, 1);
  97. } else {
  98. temp.put(v, temp.get(v) + 1);
  99. }
  100. }
  101. }
  102. }
  103. List<String> key = super.getXData(queryVo);
  104. List<Integer> list = new ArrayList<Integer>();
  105. for (int i = 0; i < key.size(); i++) {
  106. if (null == temp.get(key.get(i))) {
  107. list.add(new Integer(0));
  108. } else {
  109. list.add(temp.get(key.get(i)));
  110. }
  111. }
  112. int sum = 0;
  113. for (int m : list) {
  114. sum = sum + m;
  115. }
  116. if (sum == 0) {// 如果所有值为空
  117. return new ArrayList<Integer>();
  118. }
  119. return list;
  120. }
  121. }