JasperReports bubble chart
last modified February 12, 2024
JasperReports bubble chart tutorial shows how to create a bubble chart in JasperReports library.
JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV.
Bubble chart is a variation of a scatter chart with an additional dimension. Instead of data points, we use with bubbles. The first two dimensions are visualized as coordinates. The size of the bubbles represents the additional dimension.
JasperReports bubble chart example
In the following example, we place a bubble chart into a report; the chart displays products.
<?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"> <queryString language="SQL"> <![CDATA[SELECT name, price, quantity, market_share FROM products]]> </queryString> <field name="name"/> <field name="price" class="java.lang.Integer"/> <field name="quantity" class="java.lang.Integer"/> <field name="market_share" class="java.lang.Integer"/> <summary> <band height="450" splitType="Stretch"> <bubbleChart> <chart evaluationTime="Report"> <reportElement x="20" y="20" width="450" height="400"/> <chartTitle position="Top"> <titleExpression><![CDATA["Products"]]></titleExpression> </chartTitle> <chartSubtitle/> <chartLegend/> </chart> <xyzDataset> <dataset/> <xyzSeries> <seriesExpression><![CDATA[$F{name}]]></seriesExpression> <xValueExpression><![CDATA[$F{quantity}]]></xValueExpression> <yValueExpression><![CDATA[$F{price}]]></yValueExpression> <zValueExpression><![CDATA[$F{market_share} * 5]]></zValueExpression> </xyzSeries> </xyzDataset> <bubblePlot> <plot/> <xAxisLabelExpression><![CDATA["Quantity"]]></xAxisLabelExpression> <xAxisFormat> <axisFormat/> </xAxisFormat> <yAxisLabelExpression><![CDATA["Price"]]></yAxisLabelExpression> <yAxisFormat> <axisFormat/> </yAxisFormat> <domainAxisMinValueExpression><![CDATA[0]]></domainAxisMinValueExpression> <domainAxisMaxValueExpression><![CDATA[80]]></domainAxisMaxValueExpression> <rangeAxisMinValueExpression><![CDATA[0]]></rangeAxisMinValueExpression> <rangeAxisMaxValueExpression><![CDATA[1000]]></rangeAxisMaxValueExpression> </bubblePlot> </bubbleChart> </band> </summary> </jasperReport>
The bubble chart uses the bubbleChart
tag.
<queryString language="SQL"> <![CDATA[SELECT name, price, quantity, market_share FROM products]]> </queryString>
The SQL query selects four columns.
<field name="name"/> <field name="price" class="java.lang.Integer"/> <field name="quantity" class="java.lang.Integer"/> <field name="market_share" class="java.lang.Integer"/>
The fields are mapped to the columns.
<xyzDataset> <dataset/> <xyzSeries> <seriesExpression><![CDATA[$F{name}]]></seriesExpression> <xValueExpression><![CDATA[$F{quantity}]]></xValueExpression> <yValueExpression><![CDATA[$F{price}]]></yValueExpression> <zValueExpression><![CDATA[$F{market_share} * 5]]></zValueExpression> </xyzSeries> </xyzDataset>
The $F{name}
is used for the labels. The $F{quantity}
is placed on the x-axis while the $F{price}
on the y-axis.
The $F{market_share}
is used for the z-coordinates; it represents
the bubbles. The scale is manually adjusted by multiplying the values with 5.
<domainAxisMinValueExpression><![CDATA[0]]></domainAxisMinValueExpression> <domainAxisMaxValueExpression><![CDATA[80]]></domainAxisMaxValueExpression> <rangeAxisMinValueExpression><![CDATA[0]]></rangeAxisMinValueExpression> <rangeAxisMaxValueExpression><![CDATA[1000]]></rangeAxisMaxValueExpression>
We adjust the axis values.
package com.zetcode @GrabConfig(systemClassLoader=true) @Grab(group='net.sf.jasperreports', module='jasperreports', version='6.21.0') @Grab(group='com.github.librepdf', module='openpdf', version='1.3.39') @Grab(group='com.h2database', module='h2', version='1.4.200') import net.sf.jasperreports.engine.JasperCompileManager import net.sf.jasperreports.engine.JasperExportManager import net.sf.jasperreports.engine.JasperFillManager import groovy.sql.Sql def xmlFile = "report.xml" def jrReport = JasperCompileManager.compileReport(xmlFile) def createTable = ''' CREATE TABLE products(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), price INT, quantity INT, market_share INT); INSERT INTO products(name, price, quantity, market_share) VALUES('Product A', 642, 50, 23); INSERT INTO products(name, price, quantity, market_share) VALUES('Product B', 540, 23, 47); INSERT INTO products(name, price, quantity, market_share) VALUES('Product C', 188, 19, 30); ''' def url = "jdbc:h2:mem:" Sql.withInstance(url) { sql -> sql.execute(createTable) def params = [:] def jPrint = JasperFillManager.fillReport(jrReport, params, sql.connection) JasperExportManager.exportReportToPdfFile(jPrint, "report.pdf") }
This is the Groovy code to generate the report. We create the data in the in-memory H2 database.
In this article we have used a bubble chart in our report created with JasperReports library.