123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package com.planarChart;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.log4j.Logger;
- import com.formaction.vo.RowSet;
- import com.planarChart.common.ChartJDBCHelper;
- import com.planarChart.vo.BarBaseData;
- import com.planarChart.vo.BarData;
- import com.planarChart.vo.PlanarChartData;
- import com.planarChart.vo.QueryPlanarVo;
- public class BarPlanarChart extends PlanarChart{
- private final static Logger log = Logger.getLogger(BarPlanarChart.class);
- private QueryPlanarVo queryVo = null;
- public BarPlanarChart(QueryPlanarVo vo) {
- this.queryVo = vo;
- }
- /**
- * 最后需要的数据格式: xAxis: {categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
- * 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] series: [{ name: 'Tokyo', data:
- * [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1,
- * 95.6, 54.4]
- *
- * }, { name: 'New York', data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0,
- * 104.3, 91.2, 83.5, 106.6, 92.3]
- *
- * }]
- */
- @Override
- public List<PlanarChartData> getJsonData() {
- List<PlanarChartData> returnList = new ArrayList<PlanarChartData>();
- BarData barData = new BarData();
- List<BarBaseData> barBaseData = getBarBaseData();
- barData.setyAxis(barBaseData);
- List<String> key = super.getXData(queryVo);
- List<String> xAxis = new ArrayList<String>();
- for (int i = 0; i < key.size(); i++) {
- xAxis.add(queryVo.getxConstant().get(key.get(i)));
- }
- barData.setxAxis(xAxis);
- returnList.add(barData);
- return returnList;
- }
- /**
- * 获取二维数据
- *
- * @return
- */
- public List<BarBaseData> getBarBaseData() {
- List<BarBaseData> baseBars = new ArrayList<BarBaseData>();
- BarBaseData barBasedata = null;
- if (queryVo.getyConstant().size() == 0) { // 二维查询条件为空,只查询一维所有数据
- barBasedata = new BarBaseData();
- barBasedata.setName(" ");
- List<Integer> data = getPlanerData("");
- if (data.size() > 0) {
- barBasedata.setData(data);
- baseBars.add(barBasedata);
- }
- } else {
- for (String key : queryVo.getyConstant().keySet()) {
- barBasedata = new BarBaseData();
- String value = queryVo.getyConstant().get(key);
- barBasedata.setName(value);
- List<Integer> data = getPlanerData(key);
- if (data.size() > 0) {
- barBasedata.setData(data);
- baseBars.add(barBasedata);
- }
- }
- }
- return baseBars;
- }
- /**
- * 根据二维的一个字段及值,查询该条件下一维数据
- *
- * @param name
- * : 二维查询字段名
- * @return
- */
- @SuppressWarnings({ "rawtypes" })
- public List<Integer> getPlanerData(String value) {
- String sql = super.getSQLCondition(value, queryVo);
- log.info("二维图表查询sql:" + sql);
- ChartJDBCHelper jdbc = new ChartJDBCHelper();
- RowSet rowSet = jdbc.queryData(sql);
- Map<String, Integer> temp = new HashMap<String, Integer>();
- List<Map> rows = rowSet.getRows();
- for (Map m : rows) {
- for (Object key : m.keySet()) {
- String k = String.valueOf(key);
- String v = String.valueOf(m.get(k));
- if (queryVo.getxAxis().equalsIgnoreCase(String.valueOf(k))) {// 取常量值
- if (null == temp.get(v)) {
- temp.put(v, 1);
- } else {
- temp.put(v, temp.get(v) + 1);
- }
- }
- }
- }
- List<String> key = super.getXData(queryVo);
- List<Integer> list = new ArrayList<Integer>();
- for (int i = 0; i < key.size(); i++) {
- if (null == temp.get(key.get(i))) {
- list.add(new Integer(0));
- } else {
- list.add(temp.get(key.get(i)));
- }
- }
- int sum = 0;
- for (int m : list) {
- sum = sum + m;
- }
- if (sum == 0) {// 如果所有值为空
- return new ArrayList<Integer>();
- }
- return list;
- }
- }
|