Search code examples
javachartsjasper-reportsdatasource

Jasper report with pie chart using Datasource from java is created blank


I am using Jasper Report with Pie charts and push the data using java code. Here is my java code

data1, data2, data3 are the objects of Data.java have some fields and its value.

ArrayList<Data> selectedDataList = new ArrayList<Data>();
selectedDataList.add(data1);
selectedDataList.add(data2);
selectedDataList.add(data3);

      URL in = CampaignReport.class.getResource("Report.jrxml");
      // Compile jrxml file.
      JasperReport jasperReport = JasperCompileManager.compileReport(in.getPath());

      //Parameters for report
      Map<String, Object> parameters = new HashMap<String, Object>();

      // DataSource
      // This is simple example, no database.
      // then using empty datasource.
      JRDataSource dataSource = new JREmptyDataSource();
      JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(selectedDataList);
      parameters.put("ItemDataSource", beanColDataSource);


      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
      // Make sure the output directory exists.
      File outDir = new File(folderUrl);
      outDir.mkdirs();

     JasperExportManager.exportReportToPdfFile(jasperPrint, fileName);

Here is the code of Report.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-01-30T17:53:54 -->
<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="template_Table" pageWidth="842" pageHeight="595" orientation="Landscape" whenNoDataType="NoPages" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1a0aa80b-dc07-4dca-bdbc-aa78eeb0d12b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <style name="Row" mode="Transparent" fontName="Calibri" pdfFontName="Times-Roman">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style mode="Opaque" backcolor="#F0EFEF"/>
        </conditionalStyle>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#D0EBF7">
        <box>
            <pen lineWidth="0.5" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.5" lineColor="#FFFFFF"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#FAFCFE">
        <box>
            <pen lineWidth="0.5" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.5" lineColor="#FFFFFF"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FAFCFE">
        <box>
            <pen lineWidth="0.5" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.5" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.5" lineColor="#FFFFFF"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#D5E3E2"/>
        </conditionalStyle>
    </style>
    <subDataset name="Dataset1" uuid="7a7c32a6-b47a-4839-afad-3aeaed61363e">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="subject" class="java.lang.String"/>
        <field name="marks" class="java.lang.Integer"/>
    </subDataset>
    <parameter name="ItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="subject" class="java.lang.String"/>
    <field name="marks" class="java.lang.Integer"/>
    <group name="Group1">
        <groupExpression><![CDATA[(int)($V{REPORT_COUNT}/15)]]></groupExpression>
    </group>
    <group name="Group2">
        <groupExpression><![CDATA[(int)($V{REPORT_COUNT}/5)]]></groupExpression>
    </group>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <detail>
        <band height="120" splitType="Stretch">
            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
            <componentElement>
                <reportElement stretchType="RelativeToTallestObject" mode="Transparent" x="0" y="0" width="800" height="120" forecolor="#FFFFFF" backcolor="#424242" uuid="7786bb05-503a-4b6a-991a-a7bcf3abad07">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.grid.JSSGridBagLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                    <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                    <property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
                    <property name="com.jaspersoft.studio.components.autoresize.next" value="true"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Dataset1" uuid="d3e1e03a-2c83-4436-9e50-79518bc3d337">
                        <dataSourceExpression><![CDATA[$P{ItemDataSource}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="52" uuid="dca8961f-ce2d-47ac-879c-b15847d170a9">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                        <jr:tableHeader style="Table 1_TH" height="70">
                            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                            <property name="com.jaspersoft.studio.unit.height" value="px"/>
                            <property name="com.jaspersoft.studio.unit.width" value="px"/>
                            <staticText>
                                <reportElement x="0" y="0" width="52" height="70" uuid="f60d1675-6fb6-4569-93ca-de32b3a8e861"/>
                                <box leftPadding="3"/>
                                <textElement textAlignment="Left" verticalAlignment="Middle" markup="html">
                                    <font fontName="Verdana" size="9"/>
                                </textElement>
                                <text><![CDATA[Subject]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table 1_TD" height="50">
                            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                            <property name="com.jaspersoft.studio.unit.height" value="px"/>
                            <property name="com.jaspersoft.studio.unit.width" value="px"/>
                            <textField>
                                <reportElement x="0" y="0" width="52" height="50" uuid="f1de366a-b36f-4200-9b1e-7a0009be5373"/>
                                <box leftPadding="6"/>
                                <textElement textAlignment="Left" verticalAlignment="Middle">
                                    <font fontName="Verdana"/>
                                </textElement>
                                <textFieldExpression><![CDATA[$F{subject}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>

                    <jr:column width="53" uuid="dca8961f-ce2d-47ac-879c-b15847d170a9">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                        <jr:tableHeader style="Table 1_TH" height="70">
                            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                            <property name="com.jaspersoft.studio.unit.height" value="px"/>
                            <property name="com.jaspersoft.studio.unit.width" value="px"/>
                            <staticText>
                                <reportElement x="0" y="0" width="53" height="70" uuid="f60d1675-6fb6-4569-93ca-de32b3a8e861"/>
                                <box leftPadding="3"/>
                                <textElement textAlignment="Left" verticalAlignment="Middle" markup="html">
                                    <font fontName="Verdana" size="9"/>
                                </textElement>
                                <text><![CDATA[Marks]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table 1_TD" height="50">
                            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                            <property name="com.jaspersoft.studio.unit.height" value="px"/>
                            <property name="com.jaspersoft.studio.unit.width" value="px"/>
                            <textField>
                                <reportElement x="0" y="0" width="53" height="50" uuid="f1de366a-b36f-4200-9b1e-7a0009be5373"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{marks}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>

                </jr:table>
            </componentElement>
        </band>
    </detail>
    <summary>
        <band height="440" splitType="Stretch">
            <pieChart>
                <chart evaluationTime="Report">
                    <reportElement x="10" y="40" width="270" height="300" uuid="08e2ac0f-0059-4037-b77e-521d78434987"/>
                    <chartTitle>
                        <titleExpression><![CDATA["My First JR Pie Chart"]]></titleExpression>
                    </chartTitle>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <pieDataset>
                    <keyExpression><![CDATA[$F{subject}]]></keyExpression>
                    <valueExpression><![CDATA[$F{marks}]]></valueExpression>
                </pieDataset>
                <piePlot>
                    <plot/>
                    <itemLabel/>
                </piePlot>
            </pieChart>
        </band>
    </summary>
</jasperReport>

Using these code chart is created with data but report (table) is blank does not show data.

What should I do ? Is something wrong in the code.


Solution

  • There is a problem of datasource, you are using same datasource for both the entity like table and chart.

    So, create two different datasource.

    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(selectedDataList);
          parameters.put("ItemDataSource", beanColDataSource);
    
    JRBeanCollectionDataSource chartColDataSource = new JRBeanCollectionDataSource(selectedDataList);
    
          JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, chartColDataSource);
    

    This how you can pass the values to jrmxl. Now both have the data access properly i.e. chart and table.