Thursday, 25 May 2017

How to Create Pie Chart Using Jasper Soft Tibbco 6.0



JasperReports offers built-in support for several chart types .

Pie Chart is one of most conman chart . pie chart .jrxml file contain following element


Pie Dataset

A pie dataset is characterized by the following expressions −
  • <keyExpression> − represents the categories that will make up the slices in the pie chart. This expression can return any java.lang.Comparable object.
  • <labelExpression> − if this expression is missing, the chart will display default labels for each slice in the pie chart. Use this expression, which returns java.lang.String values, to customize the item labels for the pie chart.
  • <valueExpression> − produces the values that correspond to each category/key in the dataset. Values are always java.lang.Number objects.

in order to create pie chart following file 

1. MarkBean.java
2 .PieChart.java
3. piechart.jrxml

MarkBean.java

package in.jk;

public class MarkBean {

private String subName;
private Integer marks;

public String getSubName() {
return subName;
}
public void setSubName(String subName) {
this.subName = subName;
}


public Integer getMarks() {
return marks;
}
public void setMarks(Integer marks) {
this.marks = marks;
}


piechart.jrxml

Create jasper jrxml with two field same name property name  as MarkBean.java class  
these filed represent the keyExpresion and valueExpression .

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0  -->
<!-- 2017-05-25T11:34:31 -->
<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="piechart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0e00e387-4fc8-4e4c-8530-c43819c8f52d">
<queryString>

<![CDATA[]]>
</queryString>
<field name="subName" class="java.lang.String"/>
<field name="marks" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="236" splitType="Stretch">
<pieChart>
<chart evaluationTime="Report">
<reportElement x="160" y="10" width="200" height="200" uuid="a48c06bd-4731-453d-b8bd-c83b6cad5c30"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{subName}]]></keyExpression>
<valueExpression><![CDATA[$F{marks}]]></valueExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>




PieChart.java

package in.jk;

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

import net.sf.jasperreports.engine.JRException;
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;

public class PieChart {
public static void main(String[] args) throws FileNotFoundException, JRException {
MarkBean bean = new MarkBean();
bean.setSubName("Hindi");
bean.setMarks(50);
   MarkBean bean1 = new MarkBean();
bean1.setSubName("English");
bean1.setMarks(60);
   MarkBean bean2 = new MarkBean();
bean2.setSubName("Math");
bean2.setMarks(40);
List<MarkBean> list = new ArrayList();
list.add(bean);
list.add(bean1);
list.add(bean2);
InputStream inputStream = new                     FileInputStream("C:/Users/preeti_mehta/JaspersoftWorkspace/MyReports/piechart.jrxml");
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(list);

Map parameters = new HashMap();
     
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint,"D:/jk.pdf");
     
     System.out.println("Printed ......");
     
}

}


Final output for Pie Chart 






No comments:

Post a Comment