2
votes

Following the answer from here : How to create multiple charts of same type but with different dataseries using JRBeanCollectionDatasource in Jasperreports

I managed to at least create charts with my nested list. My java object :

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

    //constructor + getter/setters

userLogins is the y coordinate value, the chart is a timeseries chart so the x coordinate is the date. poolId is the series in which the coordinates belong to.

I get the list of values I need for each series, and since there are multiple series, I have a list of a list that I pass to the parameter map to Jasper Reports.

My Jasper Reports :

Main jasper (the whole document is quite long so only the relevant part) :

<?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="test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fbaeab5e-6793-456e-9567-e0c5af982904">
    <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="0" y="0" width="550" 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>

$P{poolLogins} is a List<List<PoolUserLoginsVO>.

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"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="210" splitType="Stretch">
            <timeSeriesChart>
                <chart evaluationTime="Report">
                    <reportElement x="0" y="20" width="540" height="180" uuid="874eee5e-1e71-4870-906b-ef71ef91d274"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <timeSeriesDataset>
                    <timeSeries>
                        <seriesExpression><![CDATA[$F{poolId}]]></seriesExpression>
                        <timePeriodExpression><![CDATA[$F{date}]]></timePeriodExpression>
                        <valueExpression><![CDATA[$F{userLogins}.intValue()]]></valueExpression>
                    </timeSeries>
                </timeSeriesDataset>
                <timeSeriesPlot>
                    <plot/>
                    <timeAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </timeAxisFormat>
                    <valueAxisFormat>
                        <axisFormat labelColor="#000000" tickLabelColor="#000000" axisLineColor="#000000"/>
                    </valueAxisFormat>
                </timeSeriesPlot>
            </timeSeriesChart>
        </band>
    </detail>
</jasperReport>

The PDF and the charts successfully generate. But there are two problems :

1) I get like 30 pages of charts, even though there is only 4 series. Each series is duplicated exactly 30 times (the number of coordinates in each series). The generated charts are all correct though.

2) I don't want separate charts. I want all the series to be in one single chart, with different colors.

What am I doing wrong here?

Edit :

Main :

public class testmain {
    public static void main(String[] args) {
        int threshold = 30;
        List<List<PoolUserLoginsVO>> poolLogins = new ArrayList<>();
        List<PoolUserLoginsVO> userLogins = 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);
            userLogins.add(logins);
        }
        poolLogins.add(userLogins);

        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);

        JRBeanCollectionDataSource vmsJRBean = new JRBeanCollectionDataSource(poolLogins);
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("poolLogins", poolLogins);

        File file = new File("report.pdf");
        try {
            FileOutputStream fop = new FileOutputStream(file);
            JasperPrint jasperPrint = JasperFillManager.fillReport("test.jasper", parameters, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(jasperPrint, fop);
            fop.flush();
            fop.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
2

2 Answers

1
votes

You are ignoring what the detail band actually do in the sub_chart.jasper

The detail band will iterate over your datasource (record after record), creating a chart for every record and you don't want this; you need the datasource for your chart.

I have hard time testing your actual setup to check that everything else is ok, but a good guess is that it's enough that you move the chart to the summary band and then completely delete the detail band.

The code change in your sub_chart.jasper is:

<detail> --> <summary>

</detail> --> </summary>

0
votes

I finally fixed it! I shouldn't have followed the other question/answer that I linked in OP. That question I think was asking how to create multiple charts, and I wanted only one chart but with all the series combined together. I fixed the problem by not putting my data in a nested list. I just put all the data in a single 1 dimensional list; since each PoolUserLoginsVO already contains which poolId it belongs to, there was no point in having a nested list to begin with (unless I wanted multiple charts for each pool I guess). So I passed in a single list, then modified the main jasper report to pass straight to the sub_chart.jasper instead of sub_charts.jasper (I deleted sub_charts.jasper since it was no longer needed). The result?

enter image description here

Success!