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.persistence.service.PersistenceFactory; import com.persistence.service.SysPersistence; import com.persistence.service.exception.PersistenceException; import com.planarChart.common.ChartJDBCHelper; import com.planarChart.vo.PieBaseData; import com.planarChart.vo.PieData; import com.planarChart.vo.PieDataLabels; import com.planarChart.vo.PlanarChartData; import com.planarChart.vo.QueryPlanarVo; import com.sysmodel.datamodel.xmlmodel.ModelFactory; import com.sysmodel.datamodel.xmlmodel.able.MdpClass; import com.sysmodel.datamodel.xmlmodel.able.SysModel; public class PiePlanarChart extends PlanarChart{ private QueryPlanarVo queryVo = null; private final static Logger log = Logger.getLogger(PiePlanarChart.class); public PiePlanarChart(QueryPlanarVo vo) { this.queryVo = vo; } /** * 最后需要的数据格式: series: [{ type: 'pie', name: '2008', //二维数据 size: '45%', * innerSize: '20%', data: [ { name: 'Firefox', y: 44.2, color: '#4572A7' }, * //在二维条件下所有一维分组数据 { name: 'IE', y: 46.6, color: '#AA4643' }, { name: * 'Chrome', y: 3.1, color: '#89A54E' }, { name: 'Safari', y: 2.7, color: * '#80699B' }, { name: 'Opera', y: 2.3, color: '#3D96AE' }, { name: * 'Mozilla', y: 0.4, color: '#DB843D' } ], dataLabels: { enabled: false } * },{...}] */ @Override public List getJsonData() { List returnList = new ArrayList(); PieData piedata = null; PieDataLabels dataLabel = null; if (queryVo.getyConstant().size() == 0) { // 二维查询条件为空,只查询一维所有数据 dataLabel = new PieDataLabels(); dataLabel.setColor("#000000"); dataLabel.setConnectorColor("#000000"); dataLabel.setEnabled(true); piedata = new PieData(); piedata.setInnerSize("45%"); piedata.setSize("80%"); piedata.setType("pie"); piedata.setName(queryVo.getyConstant().get(queryVo.getyAxis())); List data = getPieBaseData(""); if (data.size() > 0) { piedata.setData(data); piedata.setName(" "); piedata.setDataLabels(dataLabel); returnList.add(piedata); } } else { Map map = getYData(); // 数据表中所有(distinct)的二维数据 /** * 有一个这样的问题,查询到二维distinct后的数据,循环这些数据作为条件查询一维数据, * 有时查询结果为空,也就是说,有的圈为空,这样导致结果有的圈很小或没有提示, 解决办法是:先根据二维数据查询一维数据,确定圈的个数 */ int m = 0; // 二维的圈数 List xdata = new ArrayList(); // 根据二维条件存在的一维数据的键 for (String key : map.keySet()) { List data = getPieBaseData(key); if (data.size() > 0) { m++; xdata.add(key); } } int width = 0; // 饼图每个圈的宽度 if (0 != m) { width = 80 / m; } int index = 0; for (String key : xdata) { piedata = new PieData(); piedata.setInnerSize(String.valueOf(10 + index * width) + "%"); piedata.setSize(String.valueOf(10 + (index + 1) * width) + "%"); index++; dataLabel = new PieDataLabels(); dataLabel.setColor("#000000"); dataLabel.setConnectorColor("#000000"); dataLabel.setEnabled(false); if (index == m) { dataLabel.setEnabled(true); // 设置是否显示饼图提示信息 } piedata.setType("pie"); piedata.setName(queryVo.getyConstant().get(key)); List data = getPieBaseData(key); if (data.size() > 0) { piedata.setData(data); piedata.setDataLabels(dataLabel); returnList.add(piedata); } } } return returnList; } /** * 从表数据获取二维存在的所有数据 * * @return */ public Map getYData() { Map map = new HashMap(); int classId = Integer.valueOf(queryVo.getClassid()); SysModel sysmodel = ModelFactory.getSysmodel(); SysPersistence persistence = PersistenceFactory.getInstance(sysmodel); MdpClass mdpClass = sysmodel.getMdpClassByClassID(classId); String tableName = mdpClass.getName(); String sql = "select distinct " + queryVo.getyAxis() + " from " + tableName + " where " + queryVo.getCondition() + " and " + queryVo.getyAxis() + " is not null "; log.info("取得一维常量中的数据sql:" + sql); try { ArrayList result = persistence.getSearchResult(classId, sql); for (String[] str : result) { map.put(str[0], str[0]); } } catch (PersistenceException e) { e.printStackTrace(); } return map; } @SuppressWarnings({ "rawtypes" }) public List getPieBaseData(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); PieBaseData pieBasedata = null; List list = new ArrayList(); for (int i = 0; i < key.size(); i++) { pieBasedata = new PieBaseData(); pieBasedata.setName(queryVo.getxConstant().get(key.get(i))); pieBasedata.setColor(queryVo.getColor().get(key.get(i))); if (null != temp.get(key.get(i))) { pieBasedata.setY(temp.get(key.get(i))); list.add(pieBasedata); } } return list; } }