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 ;
}