ZetCode

JasperReports line chart

last modified February 12, 2024

In this article we show how to create a line chart in JasperReports library.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV.

Line chart is a type of chart that displays values which change over time. In this chart a series of (x, y) values are drawn and connected with lines.

JasperReports line chart example

In the following example, we place a line chart into a report; the chart shows temperaturs over time.

report.xml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
            http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
        name="report" pageWidth="595" pageHeight="842" columnWidth="555"
        leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">

    <field name="id" class="java.lang.Long"/>
    <field name="date"/>
    <field name="val" class="java.lang.Double"/>

    <summary>
        <band height="450" splitType="Stretch">
            <lineChart>
                <chart evaluationTime="Report">
                    <reportElement x="20" y="20" width="540" height="400"/>
                    <chartTitle position="Top">
                        <titleExpression><![CDATA["Line chart"]]></titleExpression>
                    </chartTitle>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <dataset/>
                    <categorySeries>
                        <seriesExpression><![CDATA["Temperatures"]]></seriesExpression>
                        <categoryExpression><![CDATA[ $F{date} ]]></categoryExpression>
                        <valueExpression><![CDATA[ $F{val} ]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <linePlot>
                    <plot/>
                    <categoryAxisLabelExpression><![CDATA["Date"]]></categoryAxisLabelExpression>
                    <categoryAxisFormat>
                        <axisFormat/>
                    </categoryAxisFormat>
                    <valueAxisLabelExpression><![CDATA["Temperature"]]></valueAxisLabelExpression>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </linePlot>
            </lineChart>
        </band>
    </summary>
</jasperReport>

The line chart uses the lineChart tag.

<field name="id" class="java.lang.Long"/>
<field name="date"/>
<field name="val" class="java.lang.Double"/>

We have three fields mapped to the datasource. In the chart, we use two of them: date and val.

<chartTitle position="Top">
    <titleExpression><![CDATA["Line chart"]]></titleExpression>
</chartTitle>

The title of the chart is set with chartTitle.

<categoryDataset>
    <dataset/>
    <categorySeries>
        <seriesExpression><![CDATA["Temperatures"]]></seriesExpression>
        <categoryExpression><![CDATA[ $F{date} ]]></categoryExpression>
        <valueExpression><![CDATA[ $F{val} ]]></valueExpression>
    </categorySeries>
</categoryDataset>

The $F{date} is used for the x-axis and the $F{val} for the y-axis.

<linePlot>
    <plot/>
    <categoryAxisLabelExpression><![CDATA["Date"]]></categoryAxisLabelExpression>
    <categoryAxisFormat>
        <axisFormat/>
    </categoryAxisFormat>
    <valueAxisLabelExpression><![CDATA["Temperature"]]></valueAxisLabelExpression>
    <valueAxisFormat>
        <axisFormat/>
    </valueAxisFormat>
</linePlot>

In the linePlot, we can do some formatting of the chart; we add the labels for the axes.

report.gvy
package com.zetcode

@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.21.0')
@Grab(group='com.github.librepdf', module='openpdf', version='1.3.39')

import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import groovy.transform.Immutable

@Immutable
class Temp {
    long id;
    String date;
    double val;
}

def data = [
    new Temp(1L, "Jan 1", -7.3),
    new Temp(2L, "Jan 10", -3.4),
    new Temp(3L, "Jan 12", -5.0),
    new Temp(4L, "Jan 20", -0.9),
    new Temp(5L, "Jan 30", -2.2),
    new Temp(6L, "Feb 1", 4.8),
    new Temp(7L, "Feb 2", 5.1),
    new Temp(9L, "Feb 5", -1.9),
    new Temp(10L, "Feb 8", 0),
    new Temp(11L, "Feb 12", 2.6)
]


def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)

def ds = new JRBeanCollectionDataSource(data)

def params = [:]
def jPrint = JasperFillManager.fillReport(jrReport, params, ds)

JasperExportManager.exportReportToPdfFile(jPrint, "report.pdf")

This is the Groovy code to generate the report.

def data = [
    new Temp(1L, "Jan 1", -7.3),
    new Temp(2L, "Jan 10", -3.4),
    new Temp(3L, "Jan 12", -5.0),
    new Temp(4L, "Jan 20", -0.9),
    new Temp(5L, "Jan 30", -2.2),
    new Temp(6L, "Feb 1", 4.8),
    new Temp(7L, "Feb 2", 5.1),
    new Temp(9L, "Feb 5", -1.9),
    new Temp(10L, "Feb 8", 0),
    new Temp(11L, "Feb 12", 2.6)
]

This is the data displayed in the line chart.

def ds = new JRBeanCollectionDataSource(data)

We pass the list of data to the JRBeanCollectionDataSource.

def jPrint = JasperFillManager.fillReport(jrReport, params, ds)

The datasource is passed to the JasperFillManager.fillReport.

In this article we have created a line chart with JasperReports library.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all JasperReports tutorials.