Jaspersoft BI Tools

iReport,Jaspersoft Design Studio,Jaspersoft Server, Schema Workbench,Analysis,Dashboards, Self Service BI, Domains,Topics,Ad-Hoc Reports, Jaspersoft ETL, Embedding BI,HTML-5 charts, Fusion Charts,JFree Charts,Drill down reports , Drill down Dashboards,Visualize.js, Performance Tuning, D3 Charts, Jaspersoft Customization

Wednesday, April 09, 2014

Tip - Converting String to Integer and performing conditions then converting back to Strings - Hours on X-axis scenario

Hi Guys,

This small tip might be useful for iReport developers.

Scenario :
Let us say 5,6,7,8,9,10,11,12,13,14,15,16,17 are the general hours in the string format on the category axis of a bar chart.[Category axis allows only strings in iReport]
If you want to display them as 5 A.M 6 A.M 7 A.M 8 A.M 9 A.M 10 A.M 11 A.M 12 P.M 1 P.M 2 P.M 3 P.M 4 P.M 5 P.M you need to convert the hours into Integer and have to do some comparison and then you have to convert back the integers to Strings.

Let us say your String field name is HOURS ( $F{HOURS}) , you need to write below expression for Category Expression 

Integer.parseInt($F{HOURS})>12 ? ((Integer.parseInt($F{HOURS})-12).toString()+" "+"P.M") : ((Integer.parseInt($F{HOURS})==12) ? ($F{HOURS}+" "+"P.M"):($F{HOURS}+" "+"A.M"))

Integer.parseInt - It will convert the strings to integer - It's not directly available in iReport methods hence you need to call it with it's class Integer.
.toString()  - This function will convert the Integer value to String - By default this method is available

Logic :
if hours>12 then (hours-12 )+P.M else if hours==12 then hours+P.M else hours+A.M


Sample output :


Note : I have no 12,13 from my query result in the above image. It's just a sample output.

:D :-) :P