JasperReport Development Guide and SampleWord格式.docx
《JasperReport Development Guide and SampleWord格式.docx》由会员分享,可在线阅读,更多相关《JasperReport Development Guide and SampleWord格式.docx(16页珍藏版)》请在冰点文库上搜索。
importjava.util.Map;
importorg.jfree.chart.ChartFactory;
importorg.jfree.chart.JFreeChart;
importorg.jfree.chart.plot.PiePlot3D;
importorg.jfree.data.general.DefaultPieDataset;
importmon.util.SpringContextUtils;
importcom.achievo.module.training.domain.AdminDistrict;
importcom.achievo.module.training.service.IAdminDistrictService;
importnet.sf.jasperreports.engine.JRDataSource;
importnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
importnet.sf.jasperreports.renderers.JFreeChartRenderer;
publicclassReportTestActionextendsReportSupport{
IAdminDistrictServiceadminDistrictService=(IAdminDistrictService)SpringContextUtils.getBean("
adminDistrictService"
);
@Override
publicvoidaddSubReport(JasperReportDataSourceAdapterjrda){
}
//初始化数据
publicJRDataSourceinitJRDataSource(){
//取业务数据
List<
reportRows=adminDistrictService.getAll();
if(null==reportRows){
reportRows=newArrayList<
();
}
JRBeanCollectionDataSourcedataSource=newJRBeanCollectionDataSource(
reportRows);
returndataSource;
publicvoidprepareParam(Mapparam){
reportName="
report_test"
;
ReportTestAction继承于ReportSupport,ReportSupport封装了公用的功能,代码如下:
importjava.awt.Graphics2D;
importjava.awt.geom.Rectangle2D;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.HashMap;
importorg.jfree.chart.plot.PlotOrientation;
importorg.jfree.data.category.DefaultCategoryDataset;
importorg.jfree.data.xy.XYSeriesCollection;
importmon.util.Struts2Utils;
importcom.achievo.framework.server.web.action.AbstractBaseAction;
publicabstractclassReportSupportextendsAbstractBaseAction{
StringreportName;
Stringpath;
publicstaticfinalStringREPORT_PATH="
com/achievo/module/training/action"
publicStringgetPath(){
returnpath;
publicvoidsetPath(Stringpath){
this.path=path;
publicStringgetReportName(){
returnreportName;
publicvoidsetReportName(StringreportName){
this.reportName=reportName;
publicabstractvoidprepareParam(Mapparam);
publicabstractvoidaddSubReport(JasperReportDataSourceAdapterjrda);
//生成报表文件到前台
publicStringshowPdfReport(){
path=this.getClass().getClassLoader().getResource(
REPORT_PATH).getPath();
/**Parameters*/
Mapparams=newHashMap();
params.put("
sub_dir"
path);
prepareParam(params);
ReportDataSourcerds=newReportDataSource();
//Templatefilepath
rds.setReportTemplatePath(path+"
/"
+reportName
+"
.jrxml"
//Compiledfilepath
rds.setJasperReportPath(path+"
+reportName
.jasper"
//Parameters
rds.setParams(params);
//JRDataSource
rds.setJRDataSource(initJRDataSource());
//OutPutFilename
rds.setOutPutFileName(reportName+"
.pdf"
//Compileandfillvaluetothereport
JasperReportDataSourceAdapterjrda=newJasperReportDataSourceAdapter(
rds);
//Exportreportconstructor
Reportreport=newJasperReportObject();
try{
//添加子报表(如果有)
addSubReport(jrda);
//以PDF方式导出
report.export(Struts2Utils.getRequest(),
Struts2Utils.getResponse(),jrda.getJaspertPrint(),
Struts2Utils.getResponse().getOutputStream(),
Report.REPORT_EXPORT_PDF,rds.getOutPutFileName());
}catch(ReportExceptione){
e.printStackTrace();
}catch(IOExceptione){
returnnull;
publicabstractJRDataSourceinitJRDataSource();
BufferedImageextractImage(JFreeChartchart,intwidth,intheight){
BufferedImageimg=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphics2Dg2=img.createGraphics();
chart.draw(g2,newRectangle2D.Double(0,0,width,height));
g2.dispose();
returnimg;
JFreeChartcreateChart(XYSeriesCollectiondataSet){
JFreeChartjfreechart=ChartFactory.createXYBarChart(null,"
"
false,
"
dataSet,PlotOrientation.HORIZONTAL,false,false,false);
returnjfreechart;
ReportSupport的实现需要用到下列类:
JasperReportDataSourceAdapter,JasperReportObject,Report,ReportDataSource,ReportException。
map为提供给报表的参数,如日期范围等。
struts.xml配置如下:
<
actionname="
showReport"
class="
com.achievo.module.training.action.ReportTestAction"
method="
showPdfReport"
/>
三、利用iReport开发报表
1.新建数据源
打开iReport,点击下图像插头一样的ReportDatasources,弹出对象框中New,选择"
JavaBeanssetdatasource"
后Next,输入Name为"
demo"
后Save,如下两图:
2.增加类路径
点击“工具”“选项”,选择classpath标签页,点击AddFolder,将项目类路径加入,如下图路径为:
C:
\Users\linli\Documents\workspace\JSReportDemo\build\classes
3.新建报表
点击createanewreport
OpenthisTemplate"
后,输入报表名这里是demoReport后下一步,继续完成。
4.读取bean属性
点击Preview字样右边的按钮设置数据源,在弹出框中选择"
JavaBeanDatasource"
标签页,在Classname中输入类名,点击"
Readattributes"
会列出类中所有属性。
选中所需的属性,点击对话框中间的"
Addselectedfield(s)"
增加到报表字段中。
6.设计报表
将左侧ReportInspector中Fields下刚加入的字段托到报表设计器中,设计完的报表如下图:
7.运行报表
将demoReport.jxml文件复制到项目,启动容器,浏览器下查看报表如下:
四,利用JFreeChart画图
方式一,利用IReport的charttool控件:
将chart拖到报表中,选择类型,如pie:
点击右键,选择ChartData菜单,
需要设置Keyexpression,Valueexpression,Labelexpression,其中$F{disId},$F{name}均来自于数据源,效果如下:
方式二,利用Scriptlet,
创建一个Java类,如JChartReportDemo,继承于JRDefaultScriptlet,重写afterReportInit,代码如下:
importnet.sf.jasperreports.engine.JRDefaultScriptlet;
importnet.sf.jasperreports.engine.JRScriptletException;
publicclassJChartReportDemoextendsJRDefaultScriptlet{
publicvoidafterReportInit()throwsJRScriptletException{
//createpiechartdataset
DefaultPieDatasetdateset=newDefaultPieDataset();
//setdatasetvalue
dateset.setValue("
Chinese"
108);
Math"
110);
English"
74);
ScienceDepartment"
226);
/**//*
*createjfreeChartobject
*thefirstparameterispiecharttitle
*thesecendparameterisdatasetofpiechart
*thethreeparameterisbooleanvalue,createchartnote
*thefourparameterisbooleanvalue,it'
satooltipofmovemouseon
*configurecharttogenerateURLs,It'
sgetaPiePlot3Dobject
*
*/
JFreeChartfreeChart=ChartFactory.createPieChart3D("
ReportPieChart"
dateset,true,true,false);
PiePlot3Dplot3D=(PiePlot3D)freeChart.getPlot();
plot3D.setNoDataMessage("
Nodatatodisplay"
//setvariable"
jfreeChart"
value
this.setVariableValue("
newJFreeChartRenderer(freeChart));
将该Scriptlet加入报表,方法如下:
在Scriptlets中添加刚才编写的JChartReportDemo;
在报表加入一个Image控件,
设置它的属性:
其中ImageExpression设置为$V{jfreeChart},ExpressionClass设为net.sf.jasperreports.engine.JRRenderable。
保存报表,运行效果如下:
方式三,利用参数或JavaBean属性传递数据
在参数中加入如下代码:
@Override
DefaultPieDatasetdateset=newDefaultPieDataset();
param.put("
pfreeChart"
将报表Image控件的ImageExpression设为$P{pfreeChart}。
运行效果如下: