API直接生成v4统计图
客户需求:
之前遇到过客户希望后台直接获取到润乾生成统计图,当时给客户解决的方法是通过先在润乾报表中设置统计图,后台运算后,获取到单元格的值,转化成ImageValue对象。
实现准备:
1:了解清楚客户需求的统计图类型,并查询api获取到对应的byte值
2:获取分类轴的值,转化成数组
3:获取系列名称,转化成数组
4:获取系列值,转化成数组
5:获取标题,转化成数组
代码:
package com.api;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import com.raq.dm.Sequence;
import com.runqian.report4.model.expression.graph.ExtGraphCategory;
import com.runqian.report4.model.expression.graph.ExtGraphProperty;
import com.runqian.report4.model.expression.graph.ExtGraphSery;
import com.runqian.report4.model.expression.graph.StatisticGraph;
import com.runqian.report4.usermodel.Palette;
import com.runqian.report4.usermodel.graph.GraphProperty;
import com.runqian.report4.usermodel.graph.ImageValue;
public class V4Graph {
//通过参数决定返回那个统计图,1<=参数<=20
byte[]img=null;
public byte[] getGraps(int num) {
try{
byte[]types={1,14,12,2,7,13,20,22,21,10,15,8,9,5,6,4,17,16,11,29};
String[]cats=new String[]{“Apr”,”Feb”,”Jan”,”Jun”,”Mar”,”May”};
String[]sers=new String[]{“煤“,”石油“,”沼气“};
int[]vals=new int[]{23,20,10,21,10,5,21,12,3,26,6,5,23,19,4,20,15,8};
String[] tips = new String[]{“a”,”b”,”c”,”a”,”b”,”c”,”a”,”b”,”c”,”a”,”b”,”c”,”a”,”b”,”c”,”a”,”b”,”c”};
GraphProperty graphProp=new GraphProperty();
graphProp.setImageFormat(GraphProperty.IMAGE_PNG);
graphProp.setType(types[num-1]);
ExtGraphProperty extProp = new ExtGraphProperty(graphProp);
ArrayList categories = new ArrayList();
ExtGraphCategory graphCat = null;
for(int c=0;c<cats.length;c++){
graphCat=new ExtGraphCategory();
graphCat.setName(cats[c]);
ArrayList series = new ArrayList();
for(int s=0;s<sers.length;s++){
ExtGraphSery graphSery = new ExtGraphSery();
graphSery.setName(sers[s]);
graphSery.setValue(vals[(c*3+s)]);
graphSery.setTips(tips[c*3+s]);
series.add(graphSery);
graphCat.setSeries(series);
}
categories.add(graphCat);
}
extProp.setCategories(categories);
// 设置调色板
extProp.setPalette(Palette.getDefaultPalette());
// 计算统计图
StatisticGraph sg = new StatisticGraph(extProp, 360, 220); // 三个参数分别为:统计图属性,宽度,高度
ImageValue value = sg.calc();
img = value.getValue();
}
catch (Throwable e) {
e.printStackTrace();
}
return img;
}
}
总结:
例子中循环遍历,生成了20个统计图,可根据客户具体需求更改代码。