I want to split a horizontal Bar chart into multiple pages, as the Category Expressions dataset is huge around 200, which is resulting the graph to be in an unreadable format.
I want something like, the Category Expression dataset to be distributed in blocks of 15, as to display first 15 categories in page1, and so on. I was wondering, if its doable in JasperReports Server using iReport Professional 4.5.0.
You can utilize report groups to achieve this. Open your report in iReport. The report query is the main report query, in my sample I will refer to two fields: cat
as category and val
as value.
$V{REPORT_COUNT} - 1 - ( ($V{REPORT_COUNT} - 1) % 15 )
. Click next and select Add the group footer."Sample"
), Click Next. $F{cat}
, value expression to $F{val}
. Click Finish. The wizard closes and the chart is now configured in the report.Recommendation 1: Select the chart and scroll in the Properties panel to Range Axis Max Value Expression, set the max value for your chart there. This will ensure that all charts will have the same scale. In the attached sample I have set it statically to 100, it'd be also good to provide the max value via database query.
Recommendation 2: when you run the report you might notice that the last bar chart might look different as JasperReports/jfreechart scales the width of the bars depending on the number of categories. To have a static width for the bar charts you'd need to create a chart customizer which sets a fixed width to a single bar.
I have attached a sample output where i set the splitter value to 3 on a sample dataset.
I also attach the JRXML for further reference. Difference to the description above is that instead of using a static value I defined a parameter to define the number of elements in a single bar chart.
<?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="report5" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="271b22ae-bc2f-4da1-a499-e41a8f4252b2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="splitBy" class="java.lang.Integer">
<defaultValueExpression><![CDATA[3]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[select val, cat from (
select 15 as val, "A" as cat
union select 65 as val, "B" as cat
union select 34 as val, "C" as cat
union select 99 as val, "D" as cat
union select 67 as val, "E" as cat
union select 23 as val, "F" as cat
union select 76 as val, "G" as cat
union select 23 as val, "H" as cat
union select 56 as val, "I" as cat
union select 11 as val, "J" as cat
union select 23 as val, "K" as cat
union select 5 as val, "L" as cat
union select 11 as val, "M" as cat
union select 15 as val, "N" as cat
union select 12 as val, "O" as cat
union select 13 as val, "P" as cat
) tbl group by cat
]]>
</queryString>
<field name="val" class="java.lang.Long"/>
<field name="cat" class="java.lang.String"/>
<group name="splitter">
<groupExpression><![CDATA[$V{REPORT_COUNT} - 1 - ( ($V{REPORT_COUNT} - 1) % $P{splitBy} ) ]]></groupExpression>
<groupFooter>
<band height="154">
<barChart>
<chart>
<reportElement uuid="e046c83e-11c5-4f3a-adfb-a540024400f5" x="0" y="0" width="555" height="154"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<dataset resetType="Group" resetGroup="splitter"/>
<categorySeries>
<seriesExpression><![CDATA["Sample"]]></seriesExpression>
<categoryExpression><![CDATA[$F{cat}]]></categoryExpression>
<valueExpression><![CDATA[$F{val}]]></valueExpression>
</categorySeries>
</categoryDataset>
<barPlot>
<plot/>
<itemLabel/>
<rangeAxisMaxValueExpression><![CDATA[100]]></rangeAxisMaxValueExpression>
</barPlot>
</barChart>
</band>
</groupFooter>
</group>
</jasperReport>