2

Just wondering if there would be a possibility to create such a Jasper report chart that would be a combination of many chart types?

In my case I would like to combine Bar Chart and XY Line on two different axis. I already have a following kind of bar chart ready:

enter image description here

Ideally I would be able to add one XY-line in the same chart as well, which would present the cumulative value of the sum of all hour types for each month.See picture at the end. I have a feeling that this is not possible, and maybe I just should create a separate chart for the XY-line? enter image description here

Alex K
  • 22,315
  • 19
  • 108
  • 236
Vka
  • 321
  • 1
  • 7
  • 19
  • Its unclear, what you are asking here?.. you can draw a line in the barchart if you like using customizer... did you see my link in the answer of last question. – Petter Friberg Mar 08 '16 at 13:29
  • I noticed the link, but I guess I had already skimmed it through in the past sometimes. If I remember right, that link demonstrated a situation where a simple direct line was added to the report. In my case I would like to add a XY-line + additional dedicated y-value scheme on the right side of the report. The data to this XY line would ideally be populated from the same ChartData as described here: http://stackoverflow.com/questions/35800235/how-to-populate-chart-data-with-javabeans-collection-dataset .I added image to describe the situation to the original question.Could customizer do this? – Vka Mar 08 '16 at 13:50
  • No you need multiAxisChart and to define each series separately I have posted an answer. Next time post your final solution first so we immediately can find the correct datasource for you implementation. However it has been fun to show both methods and now you know everything about charts in jasper-reports – Petter Friberg Mar 08 '16 at 16:14
  • I agree. I just tend to approach things bit by bit, like first creating the more simple use case ready, and after that trying to build on top of that. This time that just wasn't the ideal approach. And to be honest, I stared only that XY-line and did not even notice those "2nd axis"-values until much later so I guess this would have been the only path this time anyway... – Vka Mar 08 '16 at 16:27
  • np it was time to put some nice chart example on SO – Petter Friberg Mar 08 '16 at 16:57

1 Answers1

2

This is follow up on this question How to populate chart data with JavaBeans collection dataSet? (see my answer to understand further details if you like a simple bar chart and use the series expression dynamically)

To achieve a multi axis chart you should use <multiAxisChart> and it will become a little bit more complicated. We can not use the dynamic seriesExpression anymore and need to define each series manually, therefore I will use your original bean but still in a separated datasource.

Java bean

public class WorkingHours {

    private int month = 0;
    private double hoursNormal = 0;
    private double hoursTravel = 0;
    private double hoursOvertime = 0;
    private double hoursTotalCumulative = 0;

    public WorkingHours(int month, double hoursNormal, double hoursTravel, double hoursOvertime, double hoursTotalCumulative) {
        super();
        this.month = month;
        this.hoursNormal = hoursNormal;
        this.hoursTravel = hoursTravel;
        this.hoursOvertime = hoursOvertime;
        this.hoursTotalCumulative = hoursTotalCumulative;
    }
    //getter and setter
}

Fill with data (use your logic) and pass as java.util.List in parameter

List<WorkingHours> list = new ArrayList<WorkingHours>();
list.add(new WorkingHours(1, 2.3, 1.2, 2.1,4.1));
list.add(new WorkingHours(2, 5.3, 2.2,3, 9.1));
list.add(new WorkingHours(3, 3.1, 0.5, 2.0, 20.5));
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("CHART_DATA", list);

The report (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="working_hours" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1a12c021-57e2-4482-a273-56cbd3f78a17">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <subDataset name="chartDataSet" uuid="119b7f0e-01ef-4e2b-b628-d76f51e83768">
        <field name="month" class="java.lang.Integer"/>
        <field name="hoursNormal" class="java.lang.Double"/>
        <field name="hoursTravel" class="java.lang.Double"/>
        <field name="hoursOvertime" class="java.lang.Double"/>
        <field name="hoursTotalCumulative" class="java.lang.Double"/>
    </subDataset>
    <parameter name="CHART_DATA" class="java.util.List" isForPrompting="false"/>
    <summary>
        <band height="282" splitType="Stretch">
            <multiAxisChart>
                <chart evaluationTime="Report">
                    <reportElement x="62" y="17" width="419" height="235" uuid="8a16251e-8c1a-4384-8487-9be8f6c274e5"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend position="Right"/>
                </chart>
                <multiAxisPlot>
                    <plot/>
                    <axis position="rightOrBottom">
                        <lineChart>
                            <chart evaluationTime="Report">
                                <reportElement positionType="Float" x="0" y="25" width="270" height="175" backcolor="#FFFFFF" uuid="4a755d76-1350-4921-a0be-20ae9e485e12"/>
                                <chartTitle color="#000000"/>
                                <chartSubtitle color="#000000"/>
                                <chartLegend textColor="#000000" backgroundColor="#FFFFFF" position="Right"/>
                            </chart>
                            <categoryDataset>
                                <dataset>
                                    <datasetRun subDataset="chartDataSet" uuid="abec2dce-b670-4e84-b71f-469d954dbcb5">
                                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{CHART_DATA})]]></dataSourceExpression>
                                    </datasetRun>
                                </dataset>
                                <categorySeries>
                                    <seriesExpression><![CDATA["Total Cum"]]></seriesExpression>
                                    <categoryExpression><![CDATA[$F{month}]]></categoryExpression>
                                    <valueExpression><![CDATA[$F{hoursTotalCumulative}]]></valueExpression>
                                </categorySeries>
                            </categoryDataset>
                            <linePlot isShowLines="true" isShowShapes="true">
                                <plot>
                                    <seriesColor seriesOrder="0" color="#9900CC"/>
                                </plot>
                                <categoryAxisFormat>
                                    <axisFormat/>
                                </categoryAxisFormat>
                                <valueAxisLabelExpression><![CDATA["Total Cum"]]></valueAxisLabelExpression>
                                <valueAxisFormat>
                                    <axisFormat labelColor="#000000" tickLabelColor="#000000" tickLabelMask="#,##0" axisLineColor="#000000"/>
                                </valueAxisFormat>
                            </linePlot>
                        </lineChart>
                    </axis>
                    <axis>
                        <barChart>
                            <chart evaluationTime="Report">
                                <reportElement x="0" y="0" width="0" height="0" backcolor="#FFFFFF" uuid="723abd06-b593-422a-b679-043084525a8c"/>
                                <chartTitle color="#000000"/>
                                <chartSubtitle color="#000000"/>
                                <chartLegend textColor="#000000" backgroundColor="#FFFFFF" position="Right"/>
                            </chart>
                            <categoryDataset>
                                <dataset>
                                    <datasetRun subDataset="chartDataSet" uuid="abec2dce-b670-4e84-b71f-469d954dbcb5">
                                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{CHART_DATA})]]></dataSourceExpression>
                                    </datasetRun>
                                </dataset>
                                <categorySeries>
                                    <seriesExpression><![CDATA["Normal"]]></seriesExpression>
                                    <categoryExpression><![CDATA[$F{month}]]></categoryExpression>
                                    <valueExpression><![CDATA[$F{hoursNormal}]]></valueExpression>
                                </categorySeries>
                                <categorySeries>
                                    <seriesExpression><![CDATA["Travel"]]></seriesExpression>
                                    <categoryExpression><![CDATA[$F{month}]]></categoryExpression>
                                    <valueExpression><![CDATA[$F{hoursTravel}]]></valueExpression>
                                </categorySeries>
                                <categorySeries>
                                    <seriesExpression><![CDATA["Overtime"]]></seriesExpression>
                                    <categoryExpression><![CDATA[$F{month}]]></categoryExpression>
                                    <valueExpression><![CDATA[$F{hoursOvertime}]]></valueExpression>
                                </categorySeries>
                            </categoryDataset>
                            <barPlot>
                                <plot/>
                                <itemLabel/>
                                <categoryAxisFormat>
                                    <axisFormat/>
                                </categoryAxisFormat>
                                <valueAxisLabelExpression><![CDATA["h"]]></valueAxisLabelExpression>
                                <valueAxisFormat>
                                    <axisFormat labelColor="#000000" tickLabelColor="#000000" tickLabelMask="#,##0" axisLineColor="#000000"/>
                                </valueAxisFormat>
                            </barPlot>
                        </barChart>
                    </axis>
                </multiAxisPlot>
            </multiAxisChart>
        </band>
    </summary>
</jasperReport>

Key points:

We use <multiAxisChart> with 2 <axis> on one the <lineChart> on the other <barChart>, for the <barChart> we define every <categorySeries> separately.

Output

Result

For more demos on chart see this: Jasper Reports Chart Samples

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Thanks for the very nice answer! I noticed though a really annoying feature of in Jasper charts. It seems that at least in this multi axis chart it is not possible to use $R{message.keys} to localize the series texts. For example if I replaced <![CDATA["Travel"]]> to <![CDATA[$R{lworkinghours.table.column.header.normalhours}]]>, Jasper will throw an "Caused by: net.sf.jasperreports.engine.JRRuntimeException: Category series name is null." error. Seems like it's not able to read the text before creating the chart so the value is still null. – Vka Mar 10 '16 at 13:03
  • @Vka, I can't reproduce this, it works fine for me I did a simple test with $R{test} in my bundle define in jasperReport I had test=Hello and it worked just fine. – Petter Friberg Mar 10 '16 at 13:41
  • Well,I guess I'm forced to localize the messages before passing the data to chart, so that the messages will be in the right format in the dataset already. – Vka Mar 10 '16 at 13:42
  • OK, maybe there's something wrong with my environment then. – Vka Mar 10 '16 at 13:44
  • I see something strange here R{lw is the l correct?, try to put it into a textField – Petter Friberg Mar 10 '16 at 13:44
  • @vka Furthermore you can use this on the jasper report tag... whenResourceMissingType="Key", so you understand where the problem is.. – Petter Friberg Mar 10 '16 at 13:46
  • Oh, that's a typo clearly, but I did it only for this forum:) In my code I was using correct key, I triple-checked. Funny thing is that very same $R-reference is working for subtitle in the chart, but after I copy pasted it for testing purposes to seriesexpression, system started to throw these errors. – Vka Mar 10 '16 at 13:49