Search code examples
xmljasper-reportsireport

create dataset from a CSV field using XML as datasource


Is it possible to plot a CSV field inside an XML file with iReport/JasperReports?

I have an XML file, and one of its fields looks something like this:

<values>48,59,59,58,53,53,52,55,50,52,54,56,57,59,60,57,56,55,58,62</values>

How can I use that numbers to create a plot? Or how can I use that numbers to create a subdataset that can be used in a plot?


UPDATE: I've created a String[] variable, with the following variable expression:

<variableExpression><![CDATA[$F{values}.split(",")]]></variableExpression>

So now I have an array of Strings but I still can't find the way to plot it. Any ideas?
This guy also had this problem, nobody answered him: building a Chart with an Array


UPDATE2: now the String[] has been converted to a List with Arrays.asList($F{values}.split(",")). But I don't know what can I use as Category Expression in the chart series:

enter image description here


Solution

  • As @josefprochazka pointed the solution was to create a Custom DataSource and pass it to the subreport. But I'm not a JasperReports expert and creating a custom datasource took me a while. So here is the solution, with code & tutorials. I will divide it in 3 steps:

    1. You have to create a *.jar containing a class that implements JRDataSource (this will be your custom datasource)
    2. Add that jar to your Java project and/or iReport
    3. In iReport/jrxml create an instance of your custom datasource and pass it to your subreport

    So, the first step is not that hard once you know how to do it. I've used the following tutorial: http://community.jaspersoft.com/wiki/building-custom-datasource-yahoo-finance-data
    Basically what you have to do is implement two methods: getFieldValue(JRField f) and next(). Here is a complete example using 3 fields: http://pastebin.com/jX0wJVGi
    Once you finish that class, create a .jar with it and add it to your projects classpath. In iReport go to Tools->Options, select "Classpath" tab and add your jar there.
    Same in Eclipse if you are using JasperReports there too.

    Finally we are going to instantiate our custom DS in the report:

    <subreport>
        <reportElement uuid="52f64c56-b410-45a7-b391-828ef35e0103" positionType="Float" x="0" y="245" width="555" height="32"/>
        <dataSourceExpression><![CDATA[new com.dbelectronics.BandasCSVDatasource($F{csv_field1},$F{csv_field2})]]></dataSourceExpression>
        <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "your_subreport.jasper"]]></subreportExpression>
    </subreport>
    

    As you can see I've called BandasCSVDatasource with two parameters, each one containing a string with 20 numbers separated by comas.

    Well, now you just have to create the subreport and map the fields:

    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Verificacion" class="java.lang.String"/>
    <field name="Calibracion" class="java.lang.String"/>
    <field name="Banda" class="java.lang.String"/>
    

    Then add a chart and use the fields there: example chart series

    Hope it helps. And thanks again Josef.