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 getJsonData() { List returnList = new ArrayList(); BarData barData = new BarData(); List barBaseData = getBarBaseData(); barData.setyAxis(barBaseData); List key = super.getXData(queryVo); List xAxis = new ArrayList(); 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 getBarBaseData() { List baseBars = new ArrayList(); BarBaseData barBasedata = null; if (queryVo.getyConstant().size() == 0) { // 二维查询条件为空,只查询一维所有数据 barBasedata = new BarBaseData(); barBasedata.setName(" "); List 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 data = getPlanerData(key); if (data.size() > 0) { barBasedata.setData(data); baseBars.add(barBasedata); } } } return baseBars; } /** * 根据二维的一个字段及值,查询该条件下一维数据 * * @param name * : 二维查询字段名 * @return */ @SuppressWarnings({ "rawtypes" }) public List getPlanerData(String value) { String sql = super.getSQLCondition(value, queryVo); log.info("二维图表查询sql:" + sql); ChartJDBCHelper jdbc = new ChartJDBCHelper(); RowSet rowSet = jdbc.queryData(sql); Map temp = new HashMap(); List 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 key = super.getXData(queryVo); List list = new ArrayList(); 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(); } return list; } }