JasperReport + iReport advanced report Design Practice

Source: Internet
Author: User
Tags crosstab

JasperReport + iReport advanced report Design Practice

The preface has always been an important and inaccessible part of many projects. However, the complexity and professionalism make it impossible for programmers to design their own report modules for the systems currently being built; even after the design, it may be unable to be applied to the next system due to reasons such as universality, which leads to the popularity of the Report class library/component in the market. A typical example is the crystal Report, which is almost everywhere. Some components dedicated to processing Chinese-style reports are also emerging in the software market. However, unfortunately, the vast majority of them charge fees-this is understandable. After all, people will survive. As a result, most programmers with insufficient budgets have turned their eyes to open-source software, and JasperReport will be the first one to come to their sights. However, open-source code does not mean that we can use it on the other hand. People's documents are also charged, so there are also a lot of JasperReport documents on the market, however, most of them focus on simple applications and basic operations. It does not provide good help for complex report design. This article focuses on the relatively complex report design (excluding images and page elements that do not have a great deal of use for office systems or processes), cross tabulation and other advanced report design solutions. For those basic operations, you will be left with your own experience. We believe that you can solve some practical problems for you.


1 iReport overview... 42.1. preparations... 51.1.1 Database JDBC Connection Datasource. 51.1.2 JavaBean Set Datasource. 72. Practical report design... 102.1 simple and complex table header report design... 122.2 cross tabulation design... 132.2.1 use the iReport Wizard to generate the cross tabulation architecture... 132.2.2 lazy solution... 163 conclusion... 17


1 iReport OverviewJasperReport is an open-source report solution provided by JasperSoft. With JasperReport, you can easily customize, design, and generate various reports required by the project. Like JasperReport, iReport is also an open source project on Sourceforg. It is mainly used to reduce the difficulty of using JasperReport and provide users with a visual report design tool. Currently, the iReport version is 1.2.7. Each version of iReport matches the latest JasperReport, And you can manually set the JasperReport compatibility with the user so that nostalgic users can use the latest iReport without any difficulty: due to space limitations, I cannot record every detail of the iReport operation in this article. Instead, I can only briefly introduce the involved parts, such as what are the elements of each report in the report, what are the attributes? What are the basic concepts of iReport fields (Filed), parameters (parameter), and variables (variable)? If you want to go into details, you have two options: 1. I am looking for official documents (charged). The versions of documents circulating on the Internet are relatively low, but they can also be used as a reference. If you want to translate the JasperReport user documents, you can send me an email. 2. Do it yourself. 2.1. PreparationsThe purpose of a report is to display data, whether it is a simple query result or some statistical information. Therefore, the first step is to set the data source or database connection in the iReport so as to dynamically obtain data from the database. In the menu-> Connection and Datasource, we can set the data provision method to be used. IReport provides us with a variety of options: Generally, we have two options. One is to select the data source of the database link. In this way, we can directly write SQL query statements in the report design, so that the report can automatically obtain the required data during the runtime to load the report without additional work. However, in this way, the flexibility of some programs will inevitably be lost. For example, when the query statement or database connection needs to be modified, We have to re-enter the corresponding content and compile the report. So I usually use the second method in my work, that is, using a Collection (Collection or Array) of JavaBean to act as a data source. The following describes the Two Connection Methods: 1.1.1Database JDBC Connection DatasourceAssume that the Database Connection I want to use is MySQL, so we need to select the "Database JDBC Connection" method: we can establish a Database Connection based on our actual situation. It is worth mentioning that iReport provides us with almost all of the JDBC drivers we will encounter in actual projects, whether you use MySQL, DB2, Orcale, hsqldb, or cloudscape, it is very convenient. After setting the connection, you can enter the query statements required for filling in the report in "edit-> REPORT query. If you set it correctly, after you enter an SQL statement, iReport will automatically display the attributes of the table you want to use: for example, attributes in the "ky_kyxtbmb" table in my database are automatically displayed below after I fill in the SQL statement. This is very convenient for simple reports. However, if the SQL query or database connection needs to be changed, you need to re-enter the SQL or database connection information and compile the report design. This is obviously convenient after the flexibility is lost. So I usually use the second method. 1.1.2JavaBean Set DatasourceClick "Data => connection/Data Source => create data source", as shown in ). Select JavaBean set datasource for the data source type. In the options, Factory Class is the Factory Class used to generate the Bean data source. It contains at least one method called createBeanCollection (other names can also be called ). The factory class is used to provide an array or collection class as the data source for the report. The report engine reads the data at runtime and loads it into the corresponding fields of the report. When iReport is used for testing, iReport will use the reflection function to create an invisible factory class at runtime and call its static data source generation method to generate the data source, finally, the report engine is responsible for filling. It should be noted that every Field in the JavaBean should correspond to a Field in the report design, which we will soon see. Then, we can see that when iReport specifies the static method for generating the data source, it does not provide us with the function of setting method parameters, that is to say, if your method requires parameters, you cannot use iReport for testing. However, this will not have a big impact. We can test it in the program. Next, we need to set classpath in "option-> classpath" so that iReport can find the factory class we define and the corresponding JavaBean class. Finally, we can enter the fully qualified name of JavaBean in the Class Name text box of "data> REPORT query> JavaBean data source" to obtain the field name of the JavaBean. Select "Add Selected Fields" to convert these Fields into "Fields" in the report, so we can use them through "$ F {Field name}" in the report design. The following is an example of using a JavaBean array as the Data source: public class SRDataSourceFactory {/*********************************** * ************************************ generate a lab data Source for age structure of Personnel * @ return JRDataSource ***************************** **************************************** * **/public static JRDataSource createILabMemberInfoDS () throws Exception {JRBeanCollectionDataSource ds = null; ArrayList beans = createILabMemberInfo Collection (); ds = new JRBeanCollectionDataSource (beans); return ds;} public static ArrayList createILabMemberInfoCollection () throws Exception {Connection conn = DBConnection. getConnection (); // obtain the database connection ArrayList <ILabMemberInfoBean> beans = new ArrayList <ILabMemberInfoBean> (); ILabPaperIndexedBean newBean; /*** @ todo the code below is mainly to fill in the data into newBean, and then put newBean into the array beans * This is not detailed here. */...... Return beans ;} 2. Practical report DesignIn practical applications, many reports are used to display the results of statistical queries on database information. Therefore, these reports are not simple tables, but reports with complex headers, or the number of items in the table header is also a dynamic cross tabulation. For the first type of reports, although the table header is complex, the report framework is static. It only takes some time to design statistical query statements and Use JavaBean as the data source, during the runtime, the database can dynamically read data and load it into the report. Therefore, I call it "simple". For specific examples, see Table 2.1 between 1 and 2.1 Between 2. The second type of report is much more complex than the previous one, such as Table 2.1 limit 3. This type of table is mostly used to display the statistical query results, and the number of columns can be known at runtime. This requires special support from the report tool, and JasperReport provides us with the "Limited" capability to support such cross tables. In Table 2.1, listing 4 is even more complex. Not only are columns dynamically generated, but each column is a composite header. However, the second layer (that is, the "Number of projects" and "funding" layer) of the composite header comes from different attributes, this is beyond the scope of current JasperReport and iReport capabilities (I will explain the specific reasons below for not doing so ). But we still have a solution: we can limit the number of columns in the table (this will become a simple report with a fixed header ); or simply, we can only use the API of JaperReport to dynamically generate reports using programs. However, this is obviously very complicated and troublesome. It is beyond the scope of this article and will not be detailed here. Next we will look at how to design the reports with JasperReport and iReport capabilities. Chart 2.1 complex Table 1 simple report of complex table header (1) Chart 2.1 complex Table 2 simple report of complex table header (2) Chart 2.1 complex Table 3 simple cross table chart 2.1 complex cross table 2.1 simple and complex table header report DesignMy example report design shows the following results in the designer: This report structure is very simple and does not use advanced technologies such as Group, Subreport, and cross tabulation, it is only to show that complicated headers are not "complicated ". The purpose of this report is to display several database tables for statistical query results. As long as you set the parameters (Parameter) and fields (Field) correctly during preparation ), enter the correct query statement to obtain the desired result. 2.2 cross tabulation DesignCrosstab-Cross Tabulation is a table that contains the total content of rows and columns. It is mostly used to display statistical results and is very common in work. JasperReport started to support this feature in JasperReport1.1. However, it is still immature and cannot complete more complex operations, as shown in Table 2.1 limit 4. But it is better than nothing. BIRT will only support cross tabulation in the next version. Next let's take a look at how to generate a report design with statistics for rows and columns like chart 2.1, and 3, and a box in the lower right corner shows the total value. 2.2.1 use the iReport Wizard to generate a cross tabulation ArchitectureFirst, we generate a new report and place a crosstab chart in its summery band. Then, the iReport will show the crosstab chart generation Wizard to help us set the structure of the crosstab chart. According to the figure 2.1 "3" structure, we can see that the row in the table is the name of the city to which the plane is flying, and the name of the flight class is listed. Total in each column indicates the number of flights of a certain plane in each city, while total in each line indicates the number of all flights in a city. The bottom right corner of the table shows the total number of flights in all cities. We can see that the iReport wizard provides us with two Row groups by default. For example, we extend figure 2.1 to 3 to show the flight status of each city in each State, you need to set Row Group1 to the attribute corresponding to the State (province), and set Row Group 2 to the attribute corresponding to the city. Similarly, we can set two Column groups. However, we must note that such a configuration cannot implement a structure such as Table 2.1 limit 4. Because the section of Column Group2 in Table 2.1 Listing 4 is not taken from an attribute, JasperReport cannot organize it in a grid. Therefore, we can only implement a report with one Group (one dimension for each row and column) but a single attribute, however, a complex structure similar to 2.1 limit 4 cannot be generated. Finally, if necessary, we can add a new row/column Group to the Properties tab of the cross tabulation after the cross tabulation wizard ends, it is not limited to the two provided in the iReport wizard. In the Detail Field section of the table, we can also select three options as shown in. This option is used to indicate how JasperReport performs operations. Finally, we can define the configuration of total, that is, whether to add the total number of rows, whether to add the total number of columns, and whether to display table lines. In this example, we all need. Now, after the basic skeleton of the table is generated, we can see the following in the designer: Then we can write the following query statement in the query window, we didn't use any statistical query statement, and JasperReport could automatically perform Statistical Computation for us and enter the results in the specified position. How can this problem be achieved? Careful people may have seen a problem. How do we write variable V in the text box of report design instead of the commonly used field F? This is the reason. JasperReport uses a "Measure", that is, the attribute we define in the Detail Field, according to its operation type (including Average, Count, First, Highest, Lowest, Nothing, standardDeviation, Sum and Variance .) to calculate the number of rows/columns and store the results in internal variables (these variables are invisible in the iReport variable viewer ), and use these variables to display the statistical results. However, we can clearly see that there are still many problems with such function settings. For example, what if our statistical query cannot be expressed by only one query statement? Even if a query can be used to represent the statistics, we still have no way to implement a structure such as Table 2.1 limit 4. Therefore, we can only use the JasperReportAPI to dynamically generate the JRXML file of the report to achieve the desired flexibility. This is the panacea of JasperReport, but it only requires more effort. Before JasperReport and IReport become more powerful, we can only use existing tools for some limited work. 2.2.2 solutions for lazinessFor a cross-tabulation, the difficulty lies in that the report content is a statistical query, and all desired data can be obtained without a query statement, and the table columns cannot be known during the design period, in addition, you cannot set a query as mentioned in the preparation work. However, you can also easily extract all the data required by the report to a temporary table (or a data structure supported by other JasperReport data sources, for example, JavaBean) and then fill in the data in the report one by one. This method is both practical and simple for reports such as Table 2.1 commit 2, but this method is not suitable for Dynamic cross-tables such as Table 2.1 commit 3. So there is nothing to say about the last case. We must use the Crosstab function of JasperReport. The above is all about the use of JasperReport + iReport for report design. Due to the length limit, I can only try to explain some of the most common problems on the internet, explaining what JasperReport can do, what is it that it cannot be done or is difficult to do, and it also briefly explains how to do it. I hope you can provide some help when making reports. 3 conclusionIntroduction: Xue Di, a graduate student from Heilongjiang University. He is currently working at the Information Technology Research Institute of Heilongjiang University and is engaged in research on sensor networks and mobile databases. He is particularly interested in Java technology. You can contact him through the jxuedi@gmail.com.

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.