How to generate linechart report using Java program

Source: Internet
Author: User
Tags java reference

How to generate linechart report using Java program

IReport is usually a tool in the design stage. It is used to design the layout and content of a report. The dynamic generation of a report requires a program (after all, the report data is dynamic and the number of reports is large, you cannot use the iReport Preview method to manually generate one by one ). Using the previous iReport5.6.0linechart production method (http://blog.csdn.net/hbsong75/article/details/39992475) to generate the. jasper document, you can use the Java program to dynamically generate the linechart report.

The main elements for generating reports are as follows:

1. parameport obtains data through parameters;

1) added the resultsList parameter in the main report Parameters.


This parameter is used to receive child datasets from the MAP of the program;

2) Configure Parameter for the subDataSet (subDataSet)

In Summary band, right-click the linechart element and select "CharData". In the displayed dialog box, select the "Parameters" tab, here we will configure how to receive the data source of the Child dataset from the program:

Click "Add" to Add parameters:

The key to this step is: $ P {REPORT_PARAMETERS_MAP }. get ("resultsList"), where "resultsList" is required to pass the key value of the data source in subsequent programs, which must be consistent with the key value here, otherwise, the transmitted data cannot be obtained.

After the configuration is complete, click "CompileReport" to regenerate the. jasper file.

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

The example 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) {           List
 
   testBeans = new ArrayList
  
   ();        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
   
     parameters=new HashMap
    
     ();        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 iReport Parameters

As you can see, resultsList is of the JRBeanCollectionDataSource type, and parameters is used in the program. put ("resultsList", resultsList), transmits the TestBean data set required by linechart. Correspondingly, $ P {REPORT_PARAMETERS_MAP} In iReport configuration }. get ("resultsList") obtains the dataset.

Finally, run the test program and you will find that the testlinereporthistory file is generated under the D: \ workspace \ jasperreport \ linecharreportdirectory. Open the PDF file and you will see that the file is consistent with the Preview in the iReport (the test data is the same ):

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.