123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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<PlanarChartData> getJsonData() {
- List<PlanarChartData> returnList = new ArrayList<PlanarChartData>();
- 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<PieBaseData> data = getPieBaseData("");
- if (data.size() > 0) {
- piedata.setData(data);
- piedata.setName(" ");
- piedata.setDataLabels(dataLabel);
- returnList.add(piedata);
- }
- } else {
- Map<String, String> map = getYData(); // 数据表中所有(distinct)的二维数据
- /**
- * 有一个这样的问题,查询到二维distinct后的数据,循环这些数据作为条件查询一维数据,
- * 有时查询结果为空,也就是说,有的圈为空,这样导致结果有的圈很小或没有提示, 解决办法是:先根据二维数据查询一维数据,确定圈的个数
- */
- int m = 0; // 二维的圈数
- List<String> xdata = new ArrayList<String>(); // 根据二维条件存在的一维数据的键
- for (String key : map.keySet()) {
- List<PieBaseData> 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<PieBaseData> data = getPieBaseData(key);
- if (data.size() > 0) {
- piedata.setData(data);
- piedata.setDataLabels(dataLabel);
- returnList.add(piedata);
- }
- }
- }
- return returnList;
- }
- /**
- * 从表数据获取二维存在的所有数据
- *
- * @return
- */
- public Map<String, String> getYData() {
- Map<String, String> map = new HashMap<String, String>();
- 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<String[]> 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<PieBaseData> getPieBaseData(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);
- PieBaseData pieBasedata = null;
- List<PieBaseData> list = new ArrayList<PieBaseData>();
- 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;
- }
- }
|