JasperReports creating multiple chart series with nested list dynamically

0

I am trying to create a single time series chart with multiple data series dynamically. I am passing in a nested list (List<List<PoolUserLoginsVO>>). I have managed to print the charts, but they're all separate charts for each series instead of a single chart containing all the series. 

PoolUserLoginsVO : 

public class PoolUserLoginsVO {
    private int userLogins;
    private String poolId;
    private Date date;

Main jasper :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final 
     using JasperReports Library version 6.10.0-unknown  -->
<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="Blank_Letter" pageWidth="612" pageHeight="792" columnWidth="555"
              leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
              uuid="a609ce20-8f08-441e-a001-43aa907ee923">
    <property name="com.jaspersoft.studio.data.defaultdataadapter"
              value="One Empty Record"/>
    <parameter name="poolLogins" class="java.util.List"/>
    <queryString><![CDATA[]]></queryString>
    <background><band splitType="Stretch"/></background>
    <detail>
        <band height="223">
            <subreport>
                <reportElement x="6" y="10" width="561" height="200"
                               uuid="9f8a2d4a-1d90-4f88-a08c-1ea2604360c5"/>
                <dataSourceExpression>
                    <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{poolLogins})]]>
                </dataSourceExpression>
                <subreportExpression>
                    <![CDATA["sub_charts.jasper"]]>
                </subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

sub_charts.jasper :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final
     using JasperReports Library version 6.10.0-unknown  -->
<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><![CDATA[_THIS]]></fieldDescription>
    </field>
    <detail>
        <band height="237" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="20" width="540" height="200"
                               uuid="1ec1f733-ffee-46f3-859c-f774e5277d19"/>
                <dataSourceExpression>
                    <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{_THIS})]]>
                </dataSourceExpression>
                <subreportExpression>
                    <![CDATA["sub_chart.jasper"]]>
                </subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

sub_chart.jasper :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final
     using JasperReports Library version 6.10.0-unknown  -->
<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_chart" pageWidth="595" pageHeight="842" columnWidth="555"
              leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
              uuid="4ce41625-c60b-4759-acbe-5fe12a013fb2">
    <queryString><![CDATA[]]></queryString>
    <field name="userLogins" class="java.lang.Integer"/>
    <field name="poolId" class="java.lang.String"/>
    <field name="date" class="java.util.Date"/>
    <summary>
        <band height="210" splitType="Stretch">
            <timeSeriesChart>
                <chart evaluationTime="Report">
                    <reportElement isPrintRepeatedValues="false"
                                   x="0" y="20" width="540" height="180"
                                   uuid="874eee5e-1e71-4870-906b-ef71ef91d274">
                        <property name="net.sf.jasperreports.chart.domain.axis.tick.interval"
                                  value="1"/>
                    </reportElement>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <timeSeriesDataset>
                    <timeSeries>
                        <seriesExpression>
                            <![CDATA[$F{poolId}]]>
                        </seriesExpression>
                        <timePeriodExpression>
                            <![CDATA[$F{date}]]>
                        </timePeriodExpression>
                        <valueExpression>
                            <![CDATA[$F{userLogins}]]>
                        </valueExpression>
                    </timeSeries>
                </timeSeriesDataset>
                <timeSeriesPlot>
                    <plot/>
                    <timeAxisFormat>
                        <axisFormat/>
                    </timeAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </timeSeriesPlot>
            </timeSeriesChart>
        </band>
    </summary>
</jasperReport>

How I am populating the list :

List<List<PoolUserLoginsVO>> poolLogins = new ArrayList<>();
List<PoolUserLoginsVO> userLogins4 = new ArrayList<>();
 
    for (int i = 0; i < threshold; ++i) {
        LocalDate localDate = LocalDate.now().minusDays(threshold - i - 1);
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        PoolUserLoginsVO logins = new PoolUserLoginsVO(i, "pool1", date);
        userLogins4.add(logins);
    }
 
    poolLogins.add(userLogins4);
 
    List<PoolUserLoginsVO> userLogins2 = new ArrayList<>();
 
    for (int i = 0; i < threshold; ++i) {
        LocalDate localDate = LocalDate.now().minusDays(threshold - i - 1);
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        PoolUserLoginsVO logins = new PoolUserLoginsVO(1, "pool2", date);
        userLogins2.add(logins);
    }
 
    poolLogins.add(userLogins2);
 
    List<PoolUserLoginsVO> userLogins3 = new ArrayList<>();
 
    for (int i = threshold; i > 0; --i) {
        LocalDate localDate = LocalDate.now().minusDays(threshold - i - 1);
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        PoolUserLoginsVO logins = new PoolUserLoginsVO(i, "pool3", date);
        userLogins3.add(logins);
    }
 
    poolLogins.add(userLogins3);

Can someone help me combine the charts into a single time series chart with different colors for each series?

johnk419's picture
Joined: Oct 21 2019 - 10:44pm
Last seen: 3 weeks 5 days ago

0 Answers:

No answers yet
Feedback
randomness