jfreechart完整使用(4)- DrawImageUtil.java

DrawImageUtil.java 提供方法为外部调用
/**
	 * 创建堆积柱状图<br>
	 * 1.查询数据<br>
	 * 2.解析数据<br>
	 * 3.创建图片<br>
	 * @param sqlKey sql语句的key值<br>
	 * @param map 查询条件<br>
	 * @param entity Chart对象 (x轴名称、y轴名称、标题、文件名)<br>
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 * @throws IOException 
	 * @return byte[] 图片流数据
	 */
	public List createStackedBarChart(String sqlKey,Map map,ChartEntity entity) 
		throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException{
		// 查询数据
		BaseDao dao = ObjectRegistry.getBaseDao() ;
		List list = dao.listByDynamicSQL(sqlKey, map) ;
		
		// 解析数据
		Map result = new HashMap() ;
		double [][]data = new double[][]{} ;
		String[] rowKeys = new String[]{} ;
		String[] columnKeys = new String[]{} ;
		
		if(list != null && list.size() > 0){
			result = this.parseDataForBar(list) ;
			data = result.get("data")!=null ? (double[][]) result.get("data") : new double[][]{} ;
			rowKeys = result.get("rowKeys")!=null ? (String[]) result.get("rowKeys") : new String[]{} ;
			columnKeys = result.get("columnKeys")!=null ? (String[]) result.get("columnKeys") : new String[]{} ;
		}
		
		// 创建图片
        return service.createStackedBarChart(data, rowKeys, columnKeys,entity) ;
	}
	
	
	/**
	 * 创建折线图<br>
	 * 1.获取数据<br>
	 * 2.解析数据<br>
	 * 3.创建图片<br>
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IOException 
	 * @return byte[] 图片流数据
	 */
	public List createLine(String sqlKey,Map map,ChartEntity entity) 
		throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException{
		
		BaseDao dao = ObjectRegistry.getBaseDao() ;
		List list = dao.listByDynamicSQL(sqlKey, map) ;
		Map result = new HashMap() ;
		
		double [][]data = new double[][]{} ;
		String[] rowKeys = new String[]{} ;
		String[] columnKeys = new String[]{} ;
		
		if(list != null && list.size() > 0){
			result = this.parseDataForLine(list) ;
			data = result.get("data")!=null ? (double[][]) result.get("data") : new double[][]{} ;
			rowKeys = result.get("rowKeys")!=null ? (String[]) result.get("rowKeys") : new String[]{} ;
			columnKeys = result.get("columnKeys")!=null ? (String[]) result.get("columnKeys") : new String[]{} ;
		}
		
	    return service.createLineAndShapeChart(data,rowKeys,columnKeys,entity) ;
	}
	
	/**
	 * 解析数据
	 * @param list 要解析的数据
	 * @return 包含rowKeys,columnKeys,data的Map对象
	 */
	public Map parseDataForBar(List list){
		Map result = new HashMap() ;
		int size = list.size() ;
		List columnKeys = new ArrayList() ;
		List rowKeys = new ArrayList() ;
		// 获取行
		if(list != null && list.size() > 0){
			rowKeys = this.getRowKeys((Map)list.get(0)) ;
		}
		// 获取列
		for(int i=0 ; list!=null && i<list.size() ; i++){
			Map map = (Map)list.get(i) ;
			String columnName = ""+map.get("COLUMNNAME") ;
			columnKeys.add(columnName) ; // 添加列
		}
		// 获取数据
		double [][] data = new double[rowKeys.size()][size]  ;
		for(int i=0 ; i<rowKeys.size() ; i++){
			for(int j=0 ; j<columnKeys.size() ; j++){
				Map map = (Map)list.get(j) ;
				double value = Double.parseDouble(map.get("VALUE"+(i+1))!=null?map.get("VALUE"+(i+1)).toString():"0") ;
				data[i][j] = value ;
			}
		}
		
		result.put("columnKeys", this.parseArray(columnKeys.toArray()));
		result.put("rowKeys", this.parseArray(rowKeys.toArray())) ;
		result.put("data", data) ;
		return result ;
	}
	
	
	/**
	 * 为折线图解析数据
	 * @param list 数据
	 * @return 包含rowKeys,columnKeys,data的Map对象
	 */
	public Map parseDataForLine(List list){
		Map result = new HashMap() ;
		int size = list.size() ;
		List columnKeys = new ArrayList() ;
		List rowKeys = new ArrayList() ;
		//获取行
		if(list != null && list.size() > 0){
			rowKeys = this.getRowKeys((Map)list.get(0)) ;
		}
		// 获取列
		for(int i=0 ; list!=null && i<list.size() ; i++){
			Map map = (Map)list.get(i) ;
			String columnName = ""+map.get("COLUMNNAME") ;
			columnKeys.add(columnName) ; // 添加列
		}
		// 具体数据
		double [][] data = new double[rowKeys.size()][size]  ;
		for(int i=0 ; i<rowKeys.size() ; i++){
			for(int j=0 ; j<columnKeys.size() ; j++){
				Map map = (Map)list.get(j) ;
				double value = Double.parseDouble(map.get("VALUE"+(i+1))!=null?map.get("VALUE"+(i+1)).toString():"0") ;
				data[i][j] = value ;
			}
		}
		result.put("columnKeys", this.parseArray(columnKeys.toArray()));
		result.put("rowKeys", this.parseArray(rowKeys.toArray())) ;
		result.put("data", data) ;
		return result ;
	}
	
	/**
	 * 解析数据
	 * 将Object[]转换为String[] 
	 * @param obj Object 数组
	 * @return String 数组
	 */
	private String[] parseArray(Object[] obj){
		String [] str = new String[obj.length] ;
		for(int i=0 ; i<obj.length ; i++){
			str[i] = obj[i].toString() ;
		}
		return str ;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值