You will often need to display your report data in the form of a graph or a chart. JasperReports allows you to use graphs and charts in your report.
In this recipe, you will learn how to present a summary of your data as a graph.
Refer to the installPostgreSQL.txt
file included in the source code download for this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code for this chapter also includes a file named copyFeederDataIntoPGS.txt
, which helps you to create a database named jasperdb8
and two tables named Feeder
and Transformer
. The text file will also tell you how to copy sample data for this recipe into the tables.
The following steps will help you learn how to present data trends by using a graph at the end of your report:
Feeder_Trend.jrxml
file from the Task4
folder of the source code for this chapter. The Designer tab of iReport shows that the report contains two groups named Feeder and Month. You will find a set of four components in the Feeder Group Header 1 and Month Group Header 1 sections. These four components together form a two-column table, as shown in the following screenshot: $F{FeederID}
expression will appear in the upper portion of the dialog, as shown. Click the Apply button, and the Expression editor dialog will disappear. $F{FeederID}
expression will appear under Series expression. Now click the button beside Category expression, as shown next. Another Expression editor dialog will appear. ($F{Date}.getMonth()+1)+"/"+($F{Date}.getYear()+1900)
expression in the upper portion of the Expression editor dialog. Click the Apply button; the Expression editor dialog will disappear and you will be back to the previous dialog. You will notice the $F{Date}.getMonth()+"/"+$F{Date}.getYear()
expression appearing under Category expression. $V{Bill_1}
expression will appear in the upper portion of the dialog. Click the Apply button; the Expression editor dialog will disappear and you will be back to the previous dialog.In this recipe, you have designed a graph to summarize and show trends in the data of your report. In order to draw this graph, you have done the following four tasks:
$F{FeederID}
as your Series expression. This specifies that you want to use FeederID to draw one complete trend (each feeder with a different colored line). JasperReports automatically included a legend to describe which color corresponds to which feeder. The legend is shown in the following screenshot: ($F{Date}.getMonth()+1)+"/"+($F{Date}.getYear()+1900)
as Category expression. This specifies what should be displayed along the x-axis of the graph. In your case, month/year are shown along the x-axis of the graph. $V{Bill_1}
as Value expression. The Bill_1 variable stores the sum of monthly bills of all transformers connected to a feeder. JasperReports automatically displays a scale for values along the y-axis of the graph.The following figure shows all components of the graph mapped to their respective expressions:
JasperReports allows you to use different types of charts. In this recipe, you have designed a line graph using the Chart component of JasperReports. You can use the same Chart component in a very similar fashion to build a bar graph. You just have to select a bar graph (as shown encircled next) instead of the line chart in step 4 of the recipe.
All the other steps of the recipe will be the same, and you will end up with the following preview in the last step:
18.191.74.66