I want to create Variable count in JasperReport - jasper-reports

In my report, I have two values of column "Status" are: Y/N.
I want to count "Y" in "Status".
I created a variable $V{count_y} with Variable Expression : String.valueOf($F{status})=="Y" but $V{count_y} count all record (include Y and N).
<variable name="count_y" class="java.lang.Integer" calculation="Count">
<variableExpression><![CDATA[String.valueOf($F{status})=="Y"]]></variableExpression>
</variable>
Now, how can i count?
Thanks !

Create variable like this
<field name="status" class="java.lang.String"/>
<variable name="countY" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[$F{status}.equals("Y") ? 1 : 0]]></variableExpression>
</variable>
and put $V{countY} into Summary band

Related

Jasper Reports - calculation of total using CalculationEnum.System

I have 3 columns (obtained from the query along with the data)
| A | B | C |
| 5 | 15 | 20 |
| 15| 25 | 40 |
The net total for these columns are to be calculated as below :-
20 40 0.5 (i.e , the total of C = total of A / total of B) . How do I get the total calculated as required in jasper reports using java .
For calculating the total of column 'C' , I have set the calculation enum constant to be calculationEnum.SYSTEM . On setting the expression to be
expression.setText("new Double($F{" +A + "/" +B +"}.doubleValue())"); , it shows that the corresponding field , new Double($F{A/B}.doubleValue()) does not exist . To eliminate this , I have added the field in the list of field columns . But then it shows that the column for the corresponding field doesnot exist .
Is what I have done until now correct ? Or is there any better way in which I can get the total to be calculated as required .
Field A and B need to be of class="java.lang.Number" or one of its sub classes as java.lang.Double
To get the sum of them the expression is:
$F{A}.doubleValue() + $F{B}.doubleValue()
To calculate the SUM($F{A}) / SUM($F{B}) you need to use two variables
<variable name="sumOfA" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{A}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<variable name="sumOfB" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{B}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
And then in the textField do the division, remember to set evaluationTime="Report" on the textField so that the variables have been calculated before displaying.
<textField evaluationTime="Report">
<reportElement x="208" y="17" width="100" height="20" uuid="414e6633-58c5-4081-a04b-fe3973f29d96"/>
<textFieldExpression><![CDATA[$V{A}.doubleValue()/$V{B}.doubleValue()]]></textFieldExpression>
</textField>
Create two variables, one for sum of A and one for sum of B.
And then add a text field with the following expression:
($V{sumOfA}/$V{sumOfB})

Variable value not getting printed in Detail band

I am working on a jasper report which uses following query:
select * from user_type
columns:
user_type
visit_date
sessions
user_type have values either "New Visitor" or "returning Visitor".
To get count of sessions of "New Visitor" I am creating a variable as:
<variable name="new_visitor" class="java.lang.Long" calculation="Sum">
<variableExpression><![CDATA[$F{user_type}.equals("New Visitor") ? $F{sessions} : 0]]></variableExpression>
</variable>
But when I print value of this variable in detail band or summary band its printing only zero.
Can someone please tell me what I am doing wrong here?

How to split chart into multiple pages like cross tab in JasperReports Server?

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.
in the report inspector right click on the report and select Add Report Group. Follow the wizard, set a name (e.g. splitter) and select Group by the following expression and enter $V{REPORT_COUNT} - 1 - ( ($V{REPORT_COUNT} - 1) % 15 ). Click next and select Add the group footer.
Drag and drop a chart element from the Palette into the report designer on the group footer band. Select Bar Chart, follow the wizard:
Choose Main report dataset, Click Next.
Change series expression to what you need (e.g. "Sample"), Click Next.
Set category expression to $F{cat}, value expression to $F{val}. Click Finish. The wizard closes and the chart is now configured in the report.
Right click on the chart, click on Chart Data. In the Dataset panel set Reset type to Group, select as Reset Group your group (e.g. splitter). Click close.
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>
You can use Groups to to this and add the condition at the time of adding group.
$V{REPORT_COUNT} - 1 - ( ($V{REPORT_COUNT} - 1) % 10 )

Jasper Reports: page x of y within one record

I have report with multiple records, where one record is consisting of 1-5 pages. How to display "page x of y", where x is a number of page for actual record and y is total pages for actual record ? I have something like below for x variable (reset on new record, incremet by page), but it doesn't work (on each page x have 1 value):
<variable name="x" class="java.lang.Integer" resetType="Group" resetGroup="report_count" incrementType="Page" calculation="Count">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[new Integer(1)]]></initialValueExpression>
</variable>
<!-- group by record -->
<group name="report_count" isStartNewPage="true">
<groupExpression><![CDATA[$V{REPORT_COUNT}]]></groupExpression>
</group>
<textField evaluationTime="Now" evaluationGroup="report_count">
<reportElement x="141" y="5" width="156" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{x}+" of"]]></textFieldExpression>
</textField>
The problem is that calculation="count" does not do what you are expecting it to. It returns the count of non-null values that your variableExpression returns. As your variableExpression only returns a single value, the variable is always set to 1.
An easy solution is to set the calculation type to "Nothing", and the variableExpression to $V{x}+1
i.e:
<variable name="x" class="java.lang.Integer" resetType="Group" resetGroup="report_count" incrementType="Page" calculation="Nothing">
<variableExpression><![CDATA[$V{x} + 1]]></variableExpression>
<initialValueExpression><![CDATA[new Integer(1)]]></initialValueExpression>
</variable>
Edit: Alternative solution
The group tag can have the attribute isResetPageNumber. When set to true it will reset the built-in variable PAGE_NUMBER at the start of every group. As you are already grouping by each record, I think this should give you the effect you are looking for.

How can I increment a variable with value of another variable in JasperReports?

I need to make a grand total of the items I'm counting in a subReport. To do that, I think I need to add the value of that variable to another variable for each iteration, or "increment" it by that value. The subReport gets called for each group, and I get a total for that group. I need to add the variable values, rather than database columns/fields.
I'm receiving an integer returnValue from the subReport, which is itself the count of rows in the sub-report. I want to get the grand total, since that subReport is called multiple times for the different results (each for a GROUP) from my main SQL query. I want to add up all the results, but I'm getting a null value. I tried adding an operation to the subReport as a new returnValue and choosing Sum as the operation, but that also yielded a null.
<variable name="itemCount" class="java.lang.Integer" resetType="None"/>
<variable name="grandCount"
class="java.lang.Integer"
incrementType="Group"
incrementGroup="ITEM_BUNDLE">
<variableExpression><![CDATA[$V{itemCount}]]></variableExpression>
</variable>
...
<returnValue subreportVariable="countItems" toVariable="itemCount"/>
Add attribute calculation="Sum" to variable name="grandCount"
or pass grandCount to subreport as parameter
<subreportParameter name="grandCount">
<subreportParameterExpression><![CDATA[$P{grandCount}]]></subreportParameterExpression>
</subreportParameter>
in subreport declare variable countItems with initialValue of parameter grantCount
<variable name="countItems" .... >
<variableExpression><![CDATA[$P{itemCount} + $P{grandCount}]]></variableExpression>
<initialValueExpression><![CDATA[$P{grandCount}]]></initialValueExpression>
</variable>
and return
<returnValue subreportVariable="countItems" toVariable="grandCount" calculation="Sum"/>
Im not exactly shure how to write it in JRXML since i use iReport.
In iReport, i create a new Variable, with class type "Integer", and calculation type "System"
The calculation type is important here.
In the variable expression, you will need something like $V{grandCount} = $V{grandCount} + $V{itemCount}
NOTE: JasperReports render band by band, so you wont be able to use the grandCount variable in a band before the subreport band.
Hope im not too late
You might try to increment your variable (I named it totalSum) only when the band (group) is equal to the one on which the subreport is. For this you would need a field in the report to give you the current band (group).
<variable name="totalSum"
class="java.lang.Integer"
resetType="Report"
incrementType="Group"
incrementGroup="ITEM_BUNDLE"
calculation="Nothing">
<variableExpression>
<![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : $V{totalSum}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
I'm not sure if this works, I don't have the context to test it. But you might also try a second solution - with three variables. For example, you keep the value returned from the subreport (let's say returnValue) in a variable and you use another two variables to hold the sum - one until the subreport is called (let's say partialSum) and the second to store the sum between the returnValue and the partialSum. Let's call it totalSum. Then you would have something like this for totalSum:
<variable name="totalSum"
class="java.lang.Integer"
resetType="Report"
incrementType="Group"
incrementGroup="ITEM_BUNDLE"
calculation="Nothing">
<variableExpression>
<![CDATA[$V{returnValue} + $V{partialSum}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
For partialSum, you'll have something like this:
<variable name="partialSum"
class="java.lang.Integer"
resetType="Report"
calculation="Sum"
incrementType="None">
<variableExpression>
<![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : new Integer(0)]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
I hope this helps a bit. It would be easier to make all these settings from iRport directly on the report you want to use.