ireport is usually a design phase of the tool, used to design the layout and content of the report, the dynamic generation of reports need to implement the program (after all, the report data is dynamic, the number is many, it is not possible to use ireport preview of the way one by hand to generate). Using the previous Ireport5.6.0linechart production method (http://blog.csdn.net/hbsong75/article/details/ 39992475) once the. Jasper document is generated, the Linechart report can be generated dynamically by means of a Java program.
The main features of the program generating report are the following three points:
1. IReport access to data through parameters;
1) Main report parameters add resultslist parameter
This parameter is then used to receive the sub-datasets passed from the program's map;
2) Sub-dataset (subdataset) configuration parameter
Select the Linechart element in the summary band, right-select "Chardata" and select "Parameters" tab in the popup dialog, where we will configure the data source to receive the child DataSet from the program:
Click "Add" to add the parameters:
The key to this step is to configure the blue number number in order: $P {report_parameters_map}.get ("resultslist"), where the key value of the data source is passed in a later program "Resultslist", Be consistent with the key value here, otherwise you will not get the data passed.
After the configuration is complete, click "Compilereport" to regenerate the. jasper file.
2. Call Jasperrunmanager Several static methods, such as runreporttopdf,runreporttohtmlfile, etc.;
The sample program is as follows: (Testbean.java reference above)
Package Com.report.linechart; Import Java.io.file;import java.io.fileoutputstream;import Java.util.arraylist;import Java.util.HashMap;import Java.util.list;import Java.util.Map; Import Net.sf.jasperreports.engine.jremptydatasource;import Net.sf.jasperreports.engine.jasperrunmanager;import Net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; public class Testreportgenerator {/** * @param args */public static void main (string[] args) {L ist<testbean> Testbeans = new arraylist<testbean> (); Testbean tb1 = new Testbean (); Tb1.setcat ("1"); Tb1.setvalue (1); Testbeans.add (TB1); Testbean TB2 = new Testbean (); Tb2.setcat ("2"); Tb2.setvalue (2); Testbeans.add (TB2); File Reportfile = new file ("D:\\workspace\\jasperreport\\linecharreport\\testlinereport.jasper"); File Reportpdffile = new file ("D:\\workspace\\jasperreport\\linecharreport\\testlinereport.pdf"); Map<string,oBject> parameters=new hashmap<string,object> (); Jrbeancollectiondatasource resultslist = new Jrbeancollectiondatasource (Testbeans); Parameters.put ("Resultslist", resultslist); Parameters.put ("Reporttitle", "This was a test report"); try {byte[] Reportstream = Jasperrunmanager.runreporttopdf (Reportfile.getpath (), Parameters,new JREmptyDataSour CE ()); FileOutputStream fw = new FileOutputStream (reportpdffile); Fw.write (Reportstream); Fw.close (); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); } }}
3. Parameters associated data source via ireport
As you can see, resultslist is the Jrbeancollectiondatasource type, in which the program passes through Parameters.put ("Resultslist", Resultslist), The Testbean data collection that Linechart needs to be used is passed, and ireport's configuration $p{report_parameters_map}.get ("Resultslist") gets the dataset.
The final execution of the test program can be found in the D:\workspace\jasperreport\ The Testlinereport.pdf file is generated under the Linecharreport directory, open the PDF file and see the same as preview in ireport (test data):
How Java programs generate Linechart report