XY Line Graph with Java — Jasper Report

Shashika Chamod
5 min readJun 30, 2021

--

Jasper Report is the most popular reporting tool supplemented with Java. This single article is for those who have experienced java and most importantly some knowledge in Jasper Reporting as well. Here, I will present a common guide to drawing an XY line graph with any number of (x,y) like pairs. With this article, I try to fill a small gap in this area where you could not find end-to-end guidance for this particular topic even though Jasper reports are there for around 20 years.

Templating Graph with JasperSoft

JasperSoft is the official software released by the Jasper Report development team. I am going to implement the graph template with this software as this provides a user-friendly interface for report template designing and hides the complexities behind it. If you are more familiar with writing your own ‘.jrxml’ template file with a text editor, you can also follow that.

First, you have to define two attributes.

  1. ‘Dataset’ for your graph
  2. A ‘Parameter’ to access/put external data

Dataset

Create Dataset — JasperSoft

When you are creating the dataset, unless you use a dataset directly from a database use an empty type dataset.

After naming the dataset with an appropriate name, then you can define the field names and their types. You can define any number of field names, but essentially there must be at least the following fields (for a single XY graph).

  • x-parameter field (floating point/ integer/ categorical)
  • y-parameter field(floating point/ integer/ categorical)
  • graph name (string)
Fied Name and Type

Parameter

This should be defined as a parameter outside the previously defined dataset which means it should be a report parameter, not a dataset parameter. This parameter is the data pass entry point to the report.

Create Parameter

Select the class of the parameter as ‘java.util.List’.

Parameter Name and Type

Now you have set up the dataset, fields, and a parameter to acquire the data from the java program.

Setting up XY Graph

It’s time to add your XY-line graph to the appropriate section in the report.

Selection of XY -line graph

By double-clicking on the chart element on your report template, edit the variables (dataset, fields..) as follows.

Graph Variable Configuration

The JRDatasource expression is as follows.

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{CHART_DATA})

Here, as you can see the parameter name ‘CHART_DATA’ is assigned as the DataSource variable.

When you save and compile this, the .jrxml file would look like this.

<xyLineChart>
<chart evaluationTime="Report">
<reportElement x="170" y="20" width="200" height="200"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset resetType="Report">
<datasetRun subDataset="temp_dataset">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{CHART_DATA})]]></dataSourceExpression>
</datasetRun>
</dataset>
<xySeries>
<seriesExpression><![CDATA[$F{seriesName}]]></seriesExpression>
<xValueExpression><![CDATA[$F{xValue}]]></xValueExpression>
<yValueExpression><![CDATA[$F{yValue}]]></yValueExpression>
</xySeries>
</xyDataset>
<linePlot>
<plot/>
<categoryAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
</valueAxisFormat>
</linePlot>
</xyLineChart>

Yes! Now, we have come to the end of report templating with JasperSoft. Next, let’s see how we are going to integrate this into the java program to generate the actual report.

Java Program

As the first step, you need to copy the generated report.jrxml file into a folder in your java program. Here, we use the standard ‘Resource’ (Maven/Gradle) folder as the file location.

Coordinate Bean Class

To keep (x,y) pairs, we create a Java bean class. Recall that, we have set the Jasper Report data source as a bean data source.

package com.jendo.service.report_generator;

public class Coordinate {

private double yValue;
private double xValue;
private String seriesName;

public Coordinate(double yValue, int xValue, String seriesName) {
this.yValue = yValue;
this.xValue = xValue;
this.seriesName = seriesName;
}

public String getSeriesName() {
return seriesName;
}

public void setSeriesName(String seriesName) {
this.seriesName = seriesName;
}

public double getyValue() {
return yValue;
}

public void setyValue(double yValue) {
this.yValue = yValue;
}

public int getxValue() {
return xValue;
}

public void setxValue(int xValue) {
this.xValue = xValue;
}
}

As you can observe, the bean class has variable names which are the same as the field names defined in the Jasper report template.

Demo

A quick code to generate an (x,y) Coordinate set. Here, instead of the example, we can use any floating-point/ integer or categorical data according to your application

double[] d = {1, 2, 3, 4, 5, 6, 7, 8, 9};
List<Coordinate> xyData = new ArrayList<>();

for (int j = 0; j < d.length; j++) {
xyData.add(new Coordinate(d[j], j, "xy chart"));
}

Output dataset is as follows.

(1,1, “xy chart”), (2,2, “xy chart”),(3,3, “xy chart”)….

Now, we are a few steps behind the final scene. Let’s get into it. To pass the coordinate data into the Jasper report template, we use Java Map object. There we define the key “CHART_DATA” matching with the report parameter name.

InputStream inputReportStream = FileOperations.getFileFromResourceAsStream("report.jrxml");JasperReport jasperReport = JasperCompileManager.compileReport(inputReportStream);Map<String, Object> parameters = new HashMap<>();
parameters.put("CHART_DATA", xyData);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());OutputStream output = new FileOutputStream(new File("src/main/resources/reports/demo_report.pdf"));JasperExportManager.exportReportToPdfStream(jasperPrint, output);
output.close();

Done!

Let’s see how our chart looks like in the report.

XY-Line Chart Generated by the Program

In this short article, I tried to illustrate a method that I found with a struggle by picking up bits and pieces from here and there. Hopefully, it would help some of you who too are struggling to generate a Jasper report XY line graph.

Enjoy coding !!

--

--