Thursday, February 13, 2014

Conditional Styles in crosstab

Conditional Styles 

Apply different formats/sytles to  fields based on different conditions . This can be done in two ways.

1.Using conditiona style
<style 
name="Background_Style"
isDefault="false"
>

<conditionalStyle>
<conditionExpression><![CDATA[new Boolean("($F{forecast}"=="true")]]></conditionExpression>
<style 
isDefault="false"
mode="Transparent"
backcolor="red"
>
</style>
</conditionalStyle>
</style>

use this style name in cellcontents of crosstab

<cellContents mode="Opaque" style="Background_Style">


2. Use condition statement


<bucketExpression class="java.lang.String">

<![CDATA[($F{forecast}=="true")?
"<style  mode='Transparent' backcolor='#1ABDC9' >"+ $F{monthName}.toString()+"</style>"
:
"<style  mode='Transparent' backcolor='#D4D00F' >"+ $F{monthName}.toString()+"</style>"]]>

</bucketExpression>




To apply conditional style

Create a style above and add the name in style property of crosstab or any other object.



Thursday, January 23, 2014

Ireport chart from java

Even though it is simple to create chart from java code , lack of tutorial make it difficult to do ..

Here I use java map to pass values .

List<Map<String, Object>> chartMapList = null;

Inside looping
                chartMap = new HashMap<String, Object>();
                chartMap.put(FIELD_TYPE, key); //x axis
chartMap.put(FIELD_MONTH, monthKey); //y axis
chartMap.put(FIELD_DIRECT_HOURS, dataMap.get(FIELD_DIRECT_HOURS)); //values
chartMap.put(FIELD_AVAILABLE_HOURS, dataMap.get(FIELD_AVAILABLE_HOURS)); //values
chartMapList.add(chartMap);

Then add map list to the main map

parameters.put("ChartDS", new JRMapCollectionDataSource(chartMapList))

Pass to jasper
jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,new JREmptyDataSource());

OK above is the java part .

Let us see iReport part
Step 1:
1a.Drag chart control into you ireport and select the type of chart you want

I take line chart.

1b.Add a parameter say "ChartDS"


1c.Give parameter class type : net.sf.jasperreports.engine.JRDataSource

1d.Default value expression : $P{REPORT_PARAMETERS_MAP}.get( "ChartDS" )



Step 2
2a. Create a sub dataset

2b.Create corresponding fields

3. Setting chart properties
3a.Take chart properties and select chart data tab.
3b. Clcik the tab Connection/Data Source Exp. Give the value $P{ChartDS}


4.Click the Detail section

4a . Select Add button to add the values

similarly give another axis too.

Once finish , compile and run ...Here you got your beautiful chart ...

Generate odt report with list input - iReport

                byte odtByte[] = (byte[])null;
ByteArrayOutputStream byArrOutputStr = null;
try
{

//pdfByte = JasperExportManager.exportReportToPdf(jasperprint);
byArrOutputStr = new ByteArrayOutputStream();

JROdtExporter exporter = new JROdtExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,list);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byArrOutputStr);
exporter.exportReport();
odtByte = byArrOutputStr.toByteArray();
} catch(JRException e) {
logger.error("Error in creating ODT file : "+e);
} catch(Exception e) {
logger.error("Error in creating ODT file : "+e);
} finally {
//to close resource
try {
byArrOutputStr.close();
} catch (IOException e) {
// Ignored;
}
}

Monday, December 30, 2013

Simple Print from java

Printing from java code
Initialize

        JasperPrint jasperPrint = null;



JRXML file processing

JasperCompileManager.compileReportToFile(application.getRealPath("/reports/"+reportTemplate));
File reportFile = new File(application.getRealPath("/reports/"+reportRealPath));
if (!reportFile.exists())
throw new JRRuntimeException("File " + reportRealPath + " not found. The report design must be compiled first.");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());



Get all your Report's parameters

Map<String, Object> parameters = new HashMap<String, Object>();
jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,new JREmptyDataSource());


Assign output view

request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jp);
printDocumentToScreen(response, jp, viewType, reportType2);


private void printDocumentToScreen(HttpServletResponse response, JasperPrint jp, String reportView, String reportType) throws IOException{

byte bytes[] = null;
if(reportView.equalsIgnoreCase(VIEW_OPTION_HTML))
bytes = GenerateHTML.generateHTML(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_PDF))
bytes = GeneratePDF.generatePDF(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_XLS))
bytes = GenerateXLS.generateXLS(jp);
else if(reportView.equalsIgnoreCase(VIEW_OPTION_OTD))
bytes = GenerateODT.generateODT(jp);

response.setContentType("application/xls/csv/html/pdf/txt/html");
response.setHeader("Content-Disposition", "attachment;filename="+reportType+"."+reportView);
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes,0,bytes.length);
response.getOutputStream().flush();
}



Write you new about JREmptyDataSource() in another post.

These are the basic how to pass values from java to iReport

Friday, June 21, 2013

What is iReport , jasper Report , jfreechart How they are related ?

At first lot of doubts arise in my mind

iReport?

iReport is a graphical/visual tool for creating jasper reports.Design the report drag and drop do necessary changes ....s. Dynamic content is created using Parameters, Fields and  Variables . A XML file is created with extension jrxml for you.

What I found in their site is
iReport is a visual report designer for JasperReports. The library is a report engine that can be integrated in your open or commercial application to generate the reports designed with iReport, display them on screen or export them in a final format like PDF, OpenOffice, DOCX and many others. Alternatively, you can stream the result through a web application or send the final document directly to a printer. JasperReports is in some way the core of iReport.

JasperReports
Engine that takes the XML file and forms a report out of that file using the data source specified in the XML file.

jfreechart 
Library for generating charts like bar,line,pie etc.

Thursday, June 20, 2013

Introduction

When I start my report generation with iReport I totally fed up. Being a starter, there is not much online help or tutorials. Take a lot of time in the beginning .After familiar I thought of starting this blog for fresh people like me.

Here I am planning to write whatever I know  with my little time after work and family.