Jasper Reports Bar Chart Example

JasperReports is a powerful open-source reporting tool that allows you to create a wide range of reports, including charts and graphs. One of the most commonly used chart types in JasperReports is the bar chart, which is excellent for comparing data and displaying trends. Let's explore how to create a bar chart in JasperReports with an example.

bar chart with numbers, bars and other things to see in the diagram on it
bar chart with numbers, bars and other things to see in the diagram on it

Before we dive into the example, ensure you have the JasperReports library and its dependencies set up in your project. You'll also need to design your report using iReport or JasperReports Studio, or you can create the report using the JasperReports API programmatically.

Editable Bar Chart Template for Reports & Presentations
Editable Bar Chart Template for Reports & Presentations

Creating a Bar Chart in JasperReports

To create a bar chart in JasperReports, you'll need to use the chart component and configure it to display your data as bars. Here's a step-by-step guide using an example of a simple bar chart that displays sales data for different regions.

How to Make a Diverging Bar Chart in Tableau
How to Make a Diverging Bar Chart in Tableau

First, let's assume you have the following dataset for your report:

RegionSales
North50000
South35000
East45000
West60000
Bar Chart Templates
Bar Chart Templates

Setting Up the Chart Component

In your report design, add a chart component to the detail band. In the chart editor, select 'Bar' as the chart type. Then, configure the following properties:

  • Category axis: Set the category class to 'java.lang.String' and the category value to '$F{Region}'.
  • Value axis: Set the value class to 'java.lang.Integer' and the value expression to '$F{Sales}'.
  • Series: Set the series expression to '$F{Region}' and the category axis to 'Category'.
bar graph with tags for financial analysis
bar graph with tags for financial analysis

Customizing the Bar Chart Appearance

To make your bar chart more visually appealing, you can customize its appearance by modifying the following properties:

  • Bar width: Adjust the width of the bars to better suit your report's layout.
  • Bar spacing: Control the space between bars to improve readability.
  • Bar color: Set a specific color for the bars or use a color expression to apply different colors based on your data.
  • Show labels: Enable or disable the display of data labels on the bars.
Represent Data on a Bar Graph | Constructing Bar Graphs | Horizontal
Represent Data on a Bar Graph | Constructing Bar Graphs | Horizontal

Adding a Bar Chart Title and Legend

To provide context and improve the readability of your bar chart, add a title and a legend. Here's how to do it:

Blank Bar Graph Template - Free Printable PDF
Blank Bar Graph Template - Free Printable PDF
a bar chart showing the number of people in each region
a bar chart showing the number of people in each region
Elevate Your Presentations – Discover Our Business Report Template
Elevate Your Presentations – Discover Our Business Report Template
Board Report Template Visme
Board Report Template Visme
a bar chart showing the percentage of fruit stand sales
a bar chart showing the percentage of fruit stand sales
Data Drive Bar Chart for Toronto PPT - SlideModel
Data Drive Bar Chart for Toronto PPT - SlideModel
a bar chart showing the number of people in each country
a bar chart showing the number of people in each country
bar chart showing percentage of population in urban areas, 1950 - 2009 source world atlas
bar chart showing percentage of population in urban areas, 1950 - 2009 source world atlas
Bar Charts Google Slides theme and PowerPoint template
Bar Charts Google Slides theme and PowerPoint template
Multi Series Bar Chart
Multi Series Bar Chart
Bar Charts - Learn about this chart and tools to create it
Bar Charts - Learn about this chart and tools to create it
Anubhav Agarwal on Instagram: "10 types of bar chart 📊" Bar Chart, Bar, 10 Things, Instagram
Anubhav Agarwal on Instagram: "10 types of bar chart 📊" Bar Chart, Bar, 10 Things, Instagram
Percentage of People Aged 60 - 64 Who Were Still in The Workforce - Task 1 Bar Graph Band 9
Percentage of People Aged 60 - 64 Who Were Still in The Workforce - Task 1 Bar Graph Band 9
graphing lines worksheet for the bar chart templates with answers and examples
graphing lines worksheet for the bar chart templates with answers and examples
an info sheet with different types of trees and plants on it, including the number of leaves
an info sheet with different types of trees and plants on it, including the number of leaves
How to Write a Project Management Report (  Templates)
How to Write a Project Management Report ( Templates)
the project report is shown in blue and orange, with numbers on each side of it
the project report is shown in blue and orange, with numbers on each side of it
Blank Bar Graph Template - Free Printable PDF
Blank Bar Graph Template - Free Printable PDF
a bar chart showing the number of people in each region
a bar chart showing the number of people in each region
Download Monthly Sales Report Templates for Restaurants, Retail, and More
Download Monthly Sales Report Templates for Restaurants, Retail, and More

In the chart editor, add a title by setting the 'Title' property to 'Sales by Region'. You can also customize the title's font, size, and color. To add a legend, enable the 'Show legend' property and configure its position and layout as needed.

Positioning the Bar Chart in the Report

To ensure your bar chart fits well within your report, you can adjust its position and size using the chart component's properties. You can set the x and y coordinates, as well as the width and height, to place the chart exactly where you want it in the report.

Additionally, you can use the 'Stretch type' property to make the chart stretch to fit the available space in the report or keep its aspect ratio constant.

Exporting and Viewing the Report

Once you've designed your report with the bar chart, you can export it to various formats, such as PDF, HTML, or Excel, using the JasperReports API or the export options in iReport or JasperReports Studio. After exporting, open the generated report to view and interact with your bar chart.

Creating bar charts in JasperReports allows you to effectively communicate data trends and comparisons in your reports. By following this example, you can create engaging and informative bar charts to enhance your reports' visual appeal. Happy reporting!