FusionCharts报表——StackedColumn图工具类

StackedColumn图工具类:

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

/**
 * 
<chart  bgColor='FFFFFF'  caption='手术' shownames='1' showvalues='0'   showSum='1' decimals='0' overlapColumns='0'>
<categories>
<category label='心内监护室' />
<category label='口腔科一病区' />
<category label='眼科一病区' />
<category label='心研所监护室' />
<category label='中医肛肠病区' />
</categories>
<dataset seriesName='进行中'  showValues='0'>
<set value='5' />
<set value='4' />
<set value='3' />
<set value='2' />
<set value='1' />
</dataset>
<dataset seriesName='已完成'  showValues='0'>
<set value='1' />
<set value='1' />
<set value='2' />
<set value='0' />
<set value='1' />
</dataset>
<dataset seriesName='预约'  showValues='0'>
<set value='3' />
<set value='2' />
<set value='1' />
<set value='1' />
<set value='1' />
</dataset>
</chart>
 * 
 * 
 * 
 */
public class StackedColumnChartHelper {
    private String caption;// 标题
    private String clickURL;
    private List<DateSet> dataSets = new ArrayList<DateSet>();
    private Set<String> categoryList = new LinkedHashSet();

    public DateSet createDataSet(){
        return new DateSet();
    }
    public class DateSet {
        private String seriesName;
        private StringBuilder setString = new StringBuilder();

        public void addSetItem(Number value, String color) {
            if(StringUtils.isBlank(color)){
                setString.append("<set value='" + value.toString() + "' showValues='0'/>");
            }else{
                setString.append("<set value='" + value.toString() + "' color='"
                        + color + "' showValues='0'/>");
            }
        }

        public String getSeriesName() {
            return seriesName;
        }

        public void setSeriesName(String seriesName) {
            this.seriesName = seriesName;
        }

        @Override
        public String toString() {
            return "<dataset seriesName='" + seriesName + "'>" + setString
                    + "</dataset>";
        }
    }

    public void addDateSet(DateSet dataSet) {
        dataSets.add(dataSet);
    }

    public void addCategory(String category) {
        categoryList.add(category);
    }

    public String createChartXmlData() {
        StringBuilder builder = new StringBuilder();
        builder.append("<chart showValues='1'  bgColor='FFFFFF'  caption='"+caption+"' ");
                
        if (StringUtils.isNotBlank(clickURL)) {
            builder.append("clickURL='" + clickURL + "' ");
        }
        builder.append("shownames='1' showvalues='0'   showSum='1' decimals='0' overlapColumns='0'>");
        StringBuilder categoriesBuilder = new StringBuilder("<categories>");
        for (String category : categoryList) {
            categoriesBuilder.append("<category label='" + category + "' />");
        }
        categoriesBuilder.append("</categories>");
        builder.append(categoriesBuilder);

        StringBuilder datasetBuilder = new StringBuilder();
        for (DateSet dataSet : dataSets) {
            datasetBuilder.append(dataSet.toString());
        }
        builder.append(datasetBuilder);
        builder.append("</chart>");

        return builder.toString();
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getClickURL() {
        return clickURL;
    }

    public void setClickURL(String clickURL) {
        this.clickURL = clickURL;
    }

}

使用方法:

private String getStackedColumnChartXml(List<WaterAndPowerVo> ls,String caption){
        try {
            String clickUrl = "";
            StackedColumnChartHelper helper=new StackedColumnChartHelper();
            helper.setCaption(caption);
            helper.setClickURL(clickUrl);
            StackedColumnChartHelper.DateSet dataSet1=helper.createDataSet();
            StackedColumnChartHelper.DateSet dataSet2=helper.createDataSet();
            dataSet1.setSeriesName("水费");
            dataSet2.setSeriesName("电费");
            helper.addDateSet(dataSet1);
            helper.addDateSet(dataSet2);
            for(WaterAndPowerVo waterAndPowerVo:ls){
                helper.addCategory(waterAndPowerVo.getMonth());
                dataSet1.addSetItem(waterAndPowerVo.getWaterPrice(), "");
                dataSet2.addSetItem(waterAndPowerVo.getPowerPrice(), "");
            }
            String xmlData = helper.createChartXmlData();
            return xmlData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

效果图:

posted @ 2013-04-28 18:26  人生如若初见  阅读(484)  评论(0编辑  收藏  举报