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

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值