Method of generating linechart report by Java program

Hello, everyone. Meet again. I'm Jun Quan.

iReport is generally a design stage tool. Used to design the layout and content of the report. The dynamic generation of reports needs to be realized by programs (after all, the data of reports is dynamic and the quantity is very large, so it is impossible to manually generate them one by one in the way of iReport Preview).

Use the previous article iReport5. 6.0 fabrication method of linechart(http://blog.csdn.net/hbsong75/article/details/39992475 )Generate After jasper document. The linechart report can be generated dynamically through the method of Java program.

The main elements of the report generated by the program are the following three points:

1. Ireport obtains data through parameters;

1) Add resultsList parameter to main report Parameters

This parameter is then used to receive the sub data set transmitted from the program's MAP;

2) Sub dataset configuration Parameter

Select the linechart element in the Summary band, right-click and select "CharData", and select the "Parameters" tab page in the pop-up dialog box. Here we will configure how to receive the data source of the sub data set from the program:

Click "Add" to Add parameters:

Configure according to the blue number in the figure above. The key to this step is: $p {report_parameters_map} Get ("resultsList"), when transferring the key value of the data source in the later program, the "resultsList" must be consistent with the key value here, otherwise the transferred data will not be obtained.

After configuration, click "compile report" to generate the. jasper file again.

2. Call several static methods of JasperRunManager, such as runReportToPdf, runReportToHtmlFile, etc;

Demonstrate the sample program, such as the following: (TestBean.java refer to the 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) {           List<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 is a test report");        try {            byte[] reportStream = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,new JREmptyDataSource());            FileOutputStream fw = new FileOutputStream(reportPdfFile);            fw.write(reportStream);            fw.close();        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

3. Associate the data source through the Parameters of iReport

Can see. resultsList is the type of JRBeanCollectionDataSource, and parameters are used in the program Put ("resultsList", resultsList), which transfers the TestBean data set required by linechart; Correspondingly, $p {report_parameters_map} in iReport's configuration Get ("resultsList") gets the dataset.

Finally, run the test program and generate testlinereport under D:\workspace\jasperreport\linecharReport folder Pdf file. Open the PDF file and see that it is consistent with the Preview in iReport (the same as the test data):

Publisher: full stack programmer, stack length, please indicate the source for Reprint: https://javaforall.cn/115334.html Original link: https://javaforall.cn

Added by Seraph on Thu, 17 Feb 2022 07:49:29 +0200