FusionCharts报表——饼图工具类
饼图工具类:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; /**<chart palette="4" decimals="0" * enableSmartLabels="1" enableRotation="0" * bgColor="99CCFF,FFFFFF" * bgAlpha="40,100" bgRatio="0,100" * bgAngle="360" showBorder="1" startingAngle="70"> <set label="France" value="17"/> <set label="India" value="12"/> <set label="Brazil" value="18"/> <set label="USA" value="8" isSliced="1"/> <set label="Australia" value="10" isSliced="1"/> <set label="Japan" value="16" isSliced="1"/> <set label="England" value="11"/> <set label="Nigeria" value="12"/> <set label="Italy" value="8"/> <set label="China" value="10"/> <set label="Canada" value="19"/> <set label="Germany" value="15"/> </chart> * * */ public class PipChartHelper { private String caption;// 标题 private String palette="1"; private String decimals="1";// 保留1位小数,四舍五入 private String enableSmartLabels="1";//是否启用 智能标签 private String enableRotation="0";//开启旋转 private String bgColor="FFFFFF";// 图表背景色,6位16进制颜色值 private String bgAlpha="40,100";// 背景透明度 private String bgRatio="0,100";//背景比例 private String bgAngle="360";//背景角度 private String showBorder="0"; private String startingAngle="70";//起始角度 private String showLegend="1"; //是否显示图例 private String legendPosition="right";//图例位置 private String legendIconScale ="2";//图例图标大小 private String interactiveLegend ="1";//图例是否可交互 private String showPercentValues = "1";//是否显示百分比,注,showValue必须为1,当显示了百分比后,value就不能显示了 private String showLabels="1";//是否显示标签 private String clickURL; private List<Map<String,String>> dataSet=new ArrayList<Map<String,String>>(); public void addDataItem(String label,Number value){ Map<String,String> itemMap=new HashMap<String,String>(); itemMap.put("label", label); itemMap.put("value", value.toString()); dataSet.add(itemMap); } public String createChartXmlData(){ StringBuilder builder=new StringBuilder(); builder.append("<chart showValues='1' palette='"+ChartColorsHelper.getRandomPalette()+"' "); if(StringUtils.isNotBlank(clickURL)){ builder.append("clickURL='"+clickURL+"' "); } builder.append("showLabels='"+showLabels+"' "); builder.append("showPercentValues='"+showPercentValues+"' "); builder.append("showLegend='"+showLegend+"' "); builder.append("legendPosition='"+legendPosition+"' "); builder.append("legendIconScale='"+legendIconScale+"' "); builder.append("interactiveLegend='"+interactiveLegend+"' "); builder.append("decimals='"+decimals+"' "); builder.append("enableSmartLabels='"+enableRotation+"' "); builder.append("enableRotation='"+enableRotation+"' "); builder.append("caption='"+caption+"' "); builder.append("bgColor='"+bgColor+"' "); builder.append("bgAlpha='"+bgAlpha+"' "); builder.append("bgRatio='"+bgRatio+"' "); builder.append("bgAngle='"+bgAngle+"' "); builder.append("showBorder='"+showBorder+"' "); builder.append("startingAngle='"+startingAngle+"'> "); for (int i = 0; i < dataSet.size(); i++) { Map<String, String> map=dataSet.get(i); if(i==3){ //把默认的有点 红色 的颜色替换成其他颜色 builder.append("<set color='BCF0DA' label='"+map.get("label")+"' value='"+map.get("value")+"'/>"); }else{ builder.append("<set label='"+map.get("label")+"' value='"+map.get("value")+"'/>"); } } builder.append("</chart>"); return builder.toString(); } public String getCaption() { return caption; } public void setCaption(String caption) { this.caption = caption; } public String getPalette() { return palette; } public void setPalette(String palette) { this.palette = palette; } public String getDecimals() { return decimals; } public void setDecimals(String decimals) { this.decimals = decimals; } public String getEnableSmartLabels() { return enableSmartLabels; } public void setEnableSmartLabels(String enableSmartLabels) { this.enableSmartLabels = enableSmartLabels; } public String getEnableRotation() { return enableRotation; } public void setEnableRotation(String enableRotation) { this.enableRotation = enableRotation; } public String getBgColor() { return bgColor; } public void setBgColor(String bgColor) { this.bgColor = bgColor; } public String getBgAlpha() { return bgAlpha; } public void setBgAlpha(String bgAlpha) { this.bgAlpha = bgAlpha; } public String getBgRatio() { return bgRatio; } public void setBgRatio(String bgRatio) { this.bgRatio = bgRatio; } public String getBgAngle() { return bgAngle; } public void setBgAngle(String bgAngle) { this.bgAngle = bgAngle; } public String getShowBorder() { return showBorder; } public void setShowBorder(String showBorder) { this.showBorder = showBorder; } public String getStartingAngle() { return startingAngle; } public void setStartingAngle(String startingAngle) { this.startingAngle = startingAngle; } public String getClickURL() { return clickURL; } public void setClickURL(String clickURL) { this.clickURL = clickURL; } }
使用方法
public String getPipChartXml(List ls,String caption) { try { String clickUrl = ""; PipChartHelper helper=new PipChartHelper(); helper.setCaption(caption); helper.setClickURL(clickUrl); for(int i = 0 ; i < ls.size() ; i++){ Object[] str = (Object[]) ls.get(i); String a = (String)str[0];//部门 Double b = (Double)str[1];//水费 helper.addDataItem(a, b); } String xmlData = helper.createChartXmlData(); return xmlData; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
效果图: