How to create multiple charts of the same type, but with different data using JRBeanCollectionDatasource in Jasperreports

I need to create multiple XY line charts with a different dataset using the same chart report template, and I must also use JRBeanCollectionDatasource for it.

Requirements:

1) Must be done using JRBeanCollectionDatasource.

2) You must use the same chart report template to create multiple charts.

3) The number of graphs is not fixed (here I have a problem with the names for the report parameter in java). Because in ReportParametersMap they can only have a unique key name.

Java:

Coordinates.java

private Number series;
private Number xCoordinate;
private Number yCoordinate;
//Getters & Setters

GenerateReport.java

, -. XYChartDataSource (java.util.List), , , new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{XYChartDataSource}) .

Subreport XYChartDataSource (java.util.List) (, xCoordinate, yCoordinate) MainDataset ( )

List<List<Coordinates>> allchartData = new ArrayList<>();
List<Coordinates> chartData = new ArrayList<>();

chartData.add(new Coordinates(2.08, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.08, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.08, xCoordinate, yCoordinate));
allchartData.add(chartData);
.
.
.
chartData.add(new Coordinates(2.12, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.12, xCoordinate, yCoordinate));
chartData.add(new Coordinates(2.12, xCoordinate, yCoordinate));
allchartData.add(chartData);
.
.
.


for (int i = 0; i < baselineChartData.size(); i++) {
            parameters.put("XYChartDataSource", allchartData.get(i));
    }

main_report_book.jrxml

<parameter name="XYChartDataSource" class="java.util.List"/>

        <part uuid="5e668430-9acd-4835-be21-f4e2902ce33d">
            <p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
                <subreportParameter name="REPORT_DATA_SOURCE">
                    <subreportParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{XYChartDataSource})]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"/sub_chart.jasper"]]></subreportExpression>
            </p:subreportPart>
        </part>

sub_chart.jrxml

<parameter name="XYChartDataSource" class="java.util.List"/>
<field name="xCoordinate" class="java.lang.Double"/>
<field name="yCoordinate" class="java.lang.Double"/>
<field name="series" class="java.lang.Double"/>
    <summary>
        <band height="405">
            <xyLineChart>
                <chart evaluationTime="Report" bookmarkLevel="1">
                    <reportElement x="30" y="98" width="525" height="230" uuid="627d87d6-b675-409c-accb-b2bb3ffb9c80">
                        <property name="net.sf.jasperreports.chart.domain.axis.tick.interval" value="1"/>
                    </reportElement>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend position="Right"/>
                </chart>
                <xyDataset>
                    <xySeries autoSort="true">
                        <seriesExpression><![CDATA[$F{series}]]></seriesExpression>
                        <xValueExpression><![CDATA[$F{xCoordinate}]]></xValueExpression>
                        <yValueExpression><![CDATA[$F{yCoordinate}]]></yValueExpression>

                    </xySeries>
                </xyDataset>
                <linePlot isShowShapes="false">
                    <plot/>
                    <categoryAxisFormat>
                        <axisFormat/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </linePlot>
            </xyLineChart>






            </textField>
        </band>
    </summary>

: , .

enter image description here

:

( ), PDF:

chart1

enter image description here

chart2

enter image description here

+4
1

:

for (int i = 0; i < baselineChartData.size(); i++) {
        parameters.put("XYChartDataSource", allchartData.get(i));
}

"XYChartDataSource" List, , . Map.put(-, V ))

parameters.put("XYChartDataSource", allchartData);

List<Coordinates>

- , List<List<Coordinates>> .

  • List<List<Coordinates>> allchartData (sub_charts.jrxml)

  • _THIS, List<Coordinates> (, List<List<Coordinates>>)

  • sub_chart.jrxml $F{_THIS}

sub_charts.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<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="sub_charts" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bc8c76ba-0b85-4522-bf67-4c62ae87202b">
    <field name="_THIS" class="java.util.List">
        <fieldDescription>_THIS</fieldDescription>
    </field>
    <detail>
        <band height="63" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="0" width="550" height="60" uuid="b0e761bf-fe02-4a0a-bafb-32d6831b7a13"/>
                 <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{_THIS})]]></dataSourceExpression>
                 <subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"/sub_chart.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

main_report_book.jrxml.

<subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"/sub_charts.jasper"]]></subreportExpression>

OP Dhruvil Thaker .

graphical representation

+6

Source: https://habr.com/ru/post/1654434/


All Articles