What is JasperReports? JasperReports is a powerful open source reporting tool that has the ability to deliver rich content onto the screen, to the printer or into PDF, HTML, XLS, CSV and XML files. It is entirely written in Java and can be used in a variety of Java enabled applications to generate dynamic content. Its main purpose is to help creating page oriented, ready to print documents in a simple and flexible manner.

Some useful JasperReports tutorials JasperReports organizes data retrieved from a relational database through JDBC according to the report design defined in an XML file. In order to fill a report with data, the report design must be compiled first. The compilation of the XML file representing the report design is performed by the compileReport() method exposed by the net.sf.jasperreports.engine.JasperManager class. Through compilation, the report design is loaded into a report design object that is then serialized and stored on disk (net.sf.jasperreports.engine.JasperReport). This serialized object is then used when the application wants to fill the specified report design with data. In fact, the compilation of a report design implies the compilation of all Java expressions defined in the XML file representing the report design. Various verifications are made at compilation time, to check the report design consistency. The result is a ready to fill report design that will be then used to generate documents on different sets of data. In order to fill a report design, one can use the fillReportXXX() methods exposed by the net.sf.jasperreports.engine.JasperManager class. Those methods receive as a parameter the report design object, or a file representing the specified report design object, in a serialized form, and also a JDBC connection to the database from where to retrieve the data to fill the report. The result is an object that represents the ready to print document (net.sf.jasperreports.engine.JasperPrint) and can be stored onto the disk, in a serialized form, for later use, or can be delivered to the printer, to the screen or can be transformed into a PDF, HTML, XLS, CSV or XML document.
Official JasperReports tutorial 1. Introduction 2. Report Designs 3. Parameters 4. Data Source 5. Fields 6. Expressions 7. Variables 8. Report Sections 9. Frames 10. Groups 11. Fonts and Unicode Support 12. Styles 13. Scriptlets 14. Subreports 15. I18n 16. Datasets 17. Charts 18. Crosstabs
JasperReports tutorial JasperReports' report is defined in XML files, which by convention have an extension name of jrxml. A typical jrxml file contains the following elements: <jasperReport> - the root element. <title> - its contents are printed only once at the beginning of the report <pageHeader> - its contents are printed at the beginning of every page in the report. <detail> - contains the body of the report. <pageFooter> - its contents are printed at the bottom of every page in the report. <band> - defines a report section, all of the above elements contain a band element as its only child element. Below is a JasperReports example - Create a JasperDesign instance
Instances of JasperDesign represent the raw material, which the JasperReports library uses for report generating purposes. Such instances are obtained after the XML report design files are parsed by the library internal XML parsing procedures, JasperDesign jasperDesign = new JasperDesign (); jasperDesign.setName("SampleReport"); jasperDesign.setPageWidth(550); jasperDesign.setPageHeight(800); jasperDesign.setColumnWidth(515); jasperDesign.setColumnSpacing(0); jasperDesign.setLeftMargin(40); jasperDesign.setRightMargin(40); jasperDesign.setTopMargin(50); jasperDesign.setBottomMargin(50); - Create a JRDesignStyle instance
Set the JasperReports style JRDesignStyle normalStyle = new JRDesignStyle(); normalStyle.setName("Arial_Normal"); normalStyle.setDefault(true); normalStyle.setFontName("Arial"); normalStyle.setFontSize(12); normalStyle.setPdfFontName("Helvetica"); normalStyle.setPdfEncoding("Cp1252"); normalStyle.setPdfEmbedded(false); jasperDesign.addStyle(normalStyle); JRDesignStyle boldStyle = new JRDesignStyle(); boldStyle.setName("Arial_Bold"); boldStyle.setFontName("Arial"); boldStyle.setFontSize(12); boldStyle.setBold(true); boldStyle.setPdfFontName("Helvetica-Bold"); boldStyle.setPdfEncoding("Cp1252"); boldStyle.setPdfEmbedded(false); jasperDesign.addStyle(boldStyle); JRDesignStyle italicStyle = new JRDesignStyle(); italicStyle.setName("Arial_Italic"); italicStyle.setFontName("Arial"); italicStyle.setFontSize(12); italicStyle.setItalic(true); italicStyle.setPdfFontName("Helvetica-Oblique"); italicStyle.setPdfEncoding("Cp1252"); italicStyle.setPdfEmbedded(false); jasperDesign.addStyle(italicStyle //Parameters JRDesignParameter parameter = new JRDesignParameter(); parameter.setName("ReportTitle"); parameter.setValueClass(java.lang.String.class); jasperDesign.addParameter(parameter); parameter = new JRDesignParameter(); parameter.setName("OrderByClause"); parameter.setValueClass(java.lang.String.class); jasperDesign.addParameter(parameter); //Query JRDesignQuery query = new JRDesignQuery(); query.setText("SELECT * FROM Address $P!{OrderByClause}"); jasperDesign.setQuery(query); //Fields JRDesignField field = new JRDesignField(); field.setName("Id"); field.setValueClass(java.lang.Integer.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("FirstName"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("LastName"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("Street"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); field = new JRDesignField(); field.setName("City"); field.setValueClass(java.lang.String.class); jasperDesign.addField(field); //Variables JRDesignVariable variable = new JRDesignVariable(); variable.setName("CityNumber"); variable.setValueClass(java.lang.Integer.class); variable.setResetType(JRVariable.RESET_TYPE_GROUP); JRDesignGroup group = new JRDesignGroup(); group.setName("CityGroup"); variable.setResetGroup(group); variable.setCalculation(JRVariable.CALCULATION_SYSTEM); JRDesignExpression expression = new JRDesignExpression(); expression.setValueClass(java.lang.Integer.class); expression.setText("($V{CityNumber} != null)?(new Integer($V{CityNumber}.intValue() + 1)):(new Integer(1))"); variable.setInitialValueExpression(expression); jasperDesign.addVariable(variable); variable = new JRDesignVariable(); variable.setName("AllCities"); variable.setValueClass(java.lang.String.class); variable.setResetType(JRVariable.RESET_TYPE_REPORT); variable.setCalculation(JRVariable.CALCULATION_SYSTEM); jasperDesign.addVariable(variable); //Groups group.setMinHeightToStartNewPage(60); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$F{City}"); group.setExpression(expression); JRDesignBand band = new JRDesignBand(); band.setHeight(20); JRDesignRectangle rectangle = new JRDesignRectangle(); rectangle.setX(0); rectangle.setY(4); rectangle.setWidth(515); rectangle.setHeight(15); rectangle.setForecolor(new Color(0xC0, 0xC0, 0xC0)); rectangle.setBackcolor(new Color(0xC0, 0xC0, 0xC0)); band.addElement(rectangle); JRDesignTextField textField = new JRDesignTextField(); textField.setX(0); textField.setY(4); textField.setWidth(515); textField.setHeight(15); textField.setBackcolor(new Color(0xC0, 0xC0, 0xC0)); textField.setMode(JRElement.MODE_OPAQUE); textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT); textField.setStyle(boldStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("\" \" + String.valueOf($V{CityNumber}) + \". \" + String.valueOf($F{City})"); textField.setExpression(expression); band.addElement(textField); JRDesignLine line = new JRDesignLine(); line.setX(0); line.setY(19); line.setWidth(515); line.setHeight(0); band.addElement(line); group.setGroupHeader(band); band = new JRDesignBand(); band.setHeight(20); line = new JRDesignLine(); line.setX(0); line.setY(-1); line.setWidth(515); line.setHeight(0); band.addElement(line); JRDesignStaticText staticText = new JRDesignStaticText(); staticText.setX(400); staticText.setY(0); staticText.setWidth(60); staticText.setHeight(15); staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT); staticText.setStyle(boldStyle); staticText.setText("Count : "); band.addElement(staticText); textField = new JRDesignTextField(); textField.setX(460); textField.setY(0); textField.setWidth(30); textField.setHeight(15); textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT); textField.setStyle(boldStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.Integer.class); expression.setText("$V{CityGroup_COUNT}"); textField.setExpression(expression); band.addElement(textField); group.setGroupFooter(band); jasperDesign.addGroup(group); //Title band = new JRDesignBand(); band.setHeight(50); line = new JRDesignLine(); line.setX(0); line.setY(0); line.setWidth(515); line.setHeight(0); band.addElement(line); textField = new JRDesignTextField(); textField.setBlankWhenNull(true); textField.setX(0); textField.setY(10); textField.setWidth(515); textField.setHeight(30); textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER); textField.setStyle(normalStyle); textField.setFontSize(22); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$P{ReportTitle}"); textField.setExpression(expression); band.addElement(textField); jasperDesign.setTitle(band); //Page header band = new JRDesignBand(); band.setHeight(20); rectangle = new JRDesignRectangle(); rectangle.setX(0); rectangle.setY(5); rectangle.setWidth(515); rectangle.setHeight(15); rectangle.setForecolor(new Color(0x33, 0x33, 0x33)); rectangle.setBackcolor(new Color(0x33, 0x33, 0x33)); band.addElement(rectangle); staticText = new JRDesignStaticText(); staticText.setX(0); staticText.setY(5); staticText.setWidth(55); staticText.setHeight(15); staticText.setForecolor(Color.white); staticText.setBackcolor(new Color(0x33, 0x33, 0x33)); staticText.setMode(JRElement.MODE_OPAQUE); staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER); staticText.setStyle(boldStyle); staticText.setText("ID"); band.addElement(staticText); staticText = new JRDesignStaticText(); staticText.setX(55); staticText.setY(5); staticText.setWidth(205); staticText.setHeight(15); staticText.setForecolor(Color.white); staticText.setBackcolor(new Color(0x33, 0x33, 0x33)); staticText.setMode(JRElement.MODE_OPAQUE); staticText.setStyle(boldStyle); staticText.setText("Name"); band.addElement(staticText); staticText = new JRDesignStaticText(); staticText.setX(260); staticText.setY(5); staticText.setWidth(255); staticText.setHeight(15); staticText.setForecolor(Color.white); staticText.setBackcolor(new Color(0x33, 0x33, 0x33)); staticText.setMode(JRElement.MODE_OPAQUE); staticText.setStyle(boldStyle); staticText.setText("Street"); band.addElement(staticText); jasperDesign.setPageHeader(band); //Column header band = new JRDesignBand(); jasperDesign.setColumnHeader(band); //Detail band = new JRDesignBand(); band.setHeight(20); textField = new JRDesignTextField(); textField.setX(0); textField.setY(4); textField.setWidth(50); textField.setHeight(15); textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT); textField.setStyle(normalStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.Integer.class); expression.setText("$F{Id}"); textField.setExpression(expression); band.addElement(textField); textField = new JRDesignTextField(); textField.setStretchWithOverflow(true); textField.setX(55); textField.setY(4); textField.setWidth(200); textField.setHeight(15); textField.setPositionType(JRElement.POSITION_TYPE_FLOAT); textField.setStyle(normalStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$F{FirstName} + \" \" + $F{LastName}"); textField.setExpression(expression); band.addElement(textField); textField = new JRDesignTextField(); textField.setStretchWithOverflow(true); textField.setX(260); textField.setY(4); textField.setWidth(255); textField.setHeight(15); textField.setPositionType(JRElement.POSITION_TYPE_FLOAT); textField.setStyle(normalStyle); expression = new JRDesignExpression(); expression.setValueClass(java.lang.String.class); expression.setText("$F{Street}"); textField.setExpression(expression); band.addElement(textField); line = new JRDesignLine(); line.setX(0); line.setY(19); line.setWidth(515); line.setHeight(0); line.setForecolor(new Color(0x80, 0x80, 0x80)); line.setPositionType(JRElement.POSITION_TYPE_FLOAT); band.addElement(line); jasperDesign.setDetail(band); //Column footer band = new JRDesignBand(); jasperDesign.setColumnFooter(band); //Page footer band = new JRDesignBand(); jasperDesign.setPageFooter(band); //Summary band = new JRDesignBand(); jasperDesign.setSummary(band)
How to indert charts into JasperReports JasperReports now has built-in support for charts. There is a new chart component ready to use like we already had images, text fields, subreports and other elements. This greatly simplifies the way charts are included inside reports, because previously the user had to completely rely on scriptlets in order to gather the chart data and render the chart using an image element in the report template. Now with the new chart component, the user only has to make the desired visual settings to it and define the expressions that will help the engine built-up the chart dataset in a incremental fashion during the iteration through the report data source. When including and configuring a chart component, there are three entities involved: - the overall chart component; - the chart dataset (groups chart data related settings); - the chart plot (groups visual settings related to the way the chart items are rendered); JasperReports currently supports the following types of charts: Pie, Pie 3D, Bar, Bar 3D, XY Bar, Stacked Bar, Stacked Bar 3D, Line, XY Line, Area, XY Area, Scatter Plot, Bubble, Time series, High Low Open Close, Candlestick. These types of charts use several types of datasets (each type of chart works with certain types of datasets): Pie Dataset, Category Dataset, XY Dataset, Time Series, Time Period Values, XYZ Dataset, High Low Dataset. For all charts we can configure the following: - border around all sides - background color - title - title position (top, left, bottom, right) - title font - title color - subtitle - subtitle color - subtitle font - show/hide legend - plot area background color - plot area background transparency (alpha) - plot area foreground transparency (alpha) - plot orientation (vertical, horizontal) - axis labels For all datasets we can configure: - increment type (detail, column, page, group, report) - increment group - reset type (none, column, page, group, report) - reset group Specific settings by chart type: Pie 3D - depth factor Bar, XY Bar, Stacked Bar - hide/show labels - hide/show tick marks - hide/show tick labels Bar 3D, Stacked Bar 3D - hide/show labels - x offset (3D effect) - y offset (3D effect) Line, XY Line, Scatter Plot, Time series - hide/show lines - hide/show shapes Bubble - scale type (both axes, domain axis, range axis) High Low Open Close - hide/show close ticks - hide/show open ticks Candlestick - hide/show volume JasperReports uses the JFreeChart library to render the charts and details about how to use this functionality can be found in the supplied "charts" sample
iReport is visual report builder for JasperReportsiReport is a powerful, intuitive and easy to use visual report builder/designer for JasperReports written in 100% pure java. This tool allows users to visually edit complex reports with charts, images, subreports,.... iReport is integrated with JFreeChart, one of the most diffused OpenSource chart library for java. The data to print can be retrieved through several ways including multiple JDBC connections, TableModels, JavaBeans, XML, etc...
|