ireport is generally a tool in the design phase. Used to design the layout and content of the report. The dynamic generation of the report needs to be implemented by the program (after all, the data of the report is dynamic, the number is very large, it is impossible to ireport preview the way one by one hand to generate).
After generating the. Jasper document using the previous Ireport5.6.0linechart Authoring Method (http://blog.csdn.net/hbsong75/article/details/39992475). It is possible to generate Linechart reports dynamically by means of Java programs.
The main features of the program-generated report are the following three points:
1. IReport access to data through parameters;
1) Main report parameters add Resultslist Parameters
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 summary band, right-select "Chardata" and select "Parameters" tab in the dialog box that pops up. Here we will configure the data source to receive the child datasets from the program:
Click "Add" to add the number of references:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvagjzb25nnzu=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Follow the blue number numbers and configure them sequentially. The key to this step is: $P {report_parameters_map}.get ("resultslist"), in which the key value of the data source is passed in the subsequent program, "Resultslist", which is consistent with the key value here, Otherwise, the data passed is not available.
Once configured, click "Compilereport" and generate the. jasper file again.
2. Call Jasperrunmanager Several static methods, such as runreporttopdf,runreporttohtmlfile, etc.;
The Demo sample program is as follows: (Testbean.java)
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
Can see. Resultslist is the Jrbeancollectiondatasource type, in which the program passes through Parameters.put ("Resultslist", Resultslist), The Testbean data collection to be used by the Linechart is passed, and the data set is obtained by $p{report_parameters_map}.get ("Resultslist") in the ireport configuration.
Finally run the test program, can be issued today D:\workspace\jasperreport\linecharReport folder generated testlinereport.pdf file. Open the PDF file and see the same as preview in ireport (test data):
How Java programs generate Linechart report