Jasper Reports - Thermometer Report

Below is the code of 'ThermoBean', which holds the data for 'Thermometer Report':

package com.chetty.reporting.beans;

public class ThermoBean {
private String title;
private double value;
private double rangeMin;
private double rangeMax;
private double firstSubRangeMin;
private double firstSubRangeMax;
private double secondSubRangeMin;
private double secondSubRangeMax;
private double thirdSubRangeMin;
private double thirdSubRangeMax;

public void setTitle(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void setValue(double value) {
this.value = value;
}

public double getValue() {
return value;
}

public void setRangeMin(double rangeMin) {
this.rangeMin = rangeMin;
}

public double getRangeMin() {
return rangeMin;
}

public void setRangeMax(double rangeMax) {
this.rangeMax = rangeMax;
}

public double getRangeMax() {
return rangeMax;
}

public void setFirstSubRangeMin(double firstSubRangeMin) {
this.firstSubRangeMin = firstSubRangeMin;
}

public double getFirstSubRangeMin() {
return firstSubRangeMin;
}

public void setFirstSubRangeMax(double firstSubRangeMax) {
this.firstSubRangeMax = firstSubRangeMax;
}

public double getFirstSubRangeMax() {
return firstSubRangeMax;
}

public void setSecondSubRangeMin(double secondSubRangeMin) {
this.secondSubRangeMin = secondSubRangeMin;
}

public double getSecondSubRangeMin() {
return secondSubRangeMin;
}

public void setSecondSubRangeMax(double secondSubRangeMax) {
this.secondSubRangeMax = secondSubRangeMax;
}

public double getSecondSubRangeMax() {
return secondSubRangeMax;
}

public void setThirdSubRangeMin(double thirdSubRangeMin) {
this.thirdSubRangeMin = thirdSubRangeMin;
}

public double getThirdSubRangeMin() {
return thirdSubRangeMin;
}

public void setThirdSubRangeMax(double thirdSubRangeMax) {
this.thirdSubRangeMax = thirdSubRangeMax;
}

public double getThirdSubRangeMax() {
return thirdSubRangeMax;
}
}


And below is the class file with business logic, used to generate a collection of 'ThermoBean' objects, which is passed on to the Jasper reporting engine, to dynamically generate the Thermometer report:

package com.chetty.reporting.business;

import java.util.ArrayList;

import com.chetty.reporting.beans.ThermoBean;

public class ThermoBeanMaker {
public ArrayList getThermoData() {
ArrayList thermoBeanList = new ArrayList();

thermoBeanList.add(create(-10.0, -40.0, 50.0, -40.0, 0.0, 0.0, 15.0, 15.0, 50.0));

return thermoBeanList;
}

private ThermoBean create(double value, double rangeMin, double rangeMax, double firstSubRangeMin, 
double firstSubRangeMax, double secondSubRangeMin, double secondSubRangeMax, 
double thirdSubRangeMin, double thirdSubRangeMax) {
ThermoBean thermoBean = new ThermoBean();

thermoBean.setTitle("Germany - Temparature Report");
thermoBean.setValue(value);
thermoBean.setRangeMin(rangeMin);
thermoBean.setRangeMax(rangeMax);
thermoBean.setFirstSubRangeMin(firstSubRangeMin);
thermoBean.setFirstSubRangeMax(firstSubRangeMax);
thermoBean.setSecondSubRangeMin(secondSubRangeMin);
thermoBean.setSecondSubRangeMax(secondSubRangeMax);
thermoBean.setThirdSubRangeMin(thirdSubRangeMin);
thermoBean.setThirdSubRangeMax(thirdSubRangeMax);

return thermoBean;
}
}


And below is the main class, that generates the report:

package com.chetty.reporting.engine;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.chetty.reporting.beans.GanttChartBean;
import com.chetty.reporting.beans.PieChartBean;
import com.chetty.reporting.beans.ThermoBean;
import com.chetty.reporting.business.GanttChartBeanMaker;
import com.chetty.reporting.business.PieChartBeanMaker;
import com.chetty.reporting.business.ThermoBeanMaker;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class Reporter {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream ("reports/thermochart.jrxml");

ThermoBeanMaker thermoBeanMaker = new ThermoBeanMaker();
ArrayList thermoBeanList = thermoBeanMaker.getThermoData();

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(thermoBeanList);

Map parameters = new HashMap();

JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "c:/reports/thermochart.pdf"); 
}
}


When you run the above class, the following report is generated:

Thermometer Report

You can find the report design file, here: thermochart.jrxml

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.