Recently, I came around a problem in which I had a line chart with data spanning over few months (yes, it had data in x-axis known as category axis) and the dates on the category axis were overlapping each other so that it had almost become a black strip at the bottom of the chart. In my search for reducing the date labels on the category axis without compromising the precision of the data, I came across time-series-chart which was built specifically to address this issue. Time series chart uses DateAxis instead of CategoryAxis for plotting x-axis. I will show you how we can use time series chart which automatically adjusts the amount of the tick labels (dates printed on the category axis for each data) on the chart. I’ll use XML data source file for the sake of simplicity and better granularity in this article. So, here’s the XML:

<date class="sql-date">2013-10-15</date>
<categorySeriesValue key="cs1901" value="57.0"/>
<date class="sql-date">2013-10-16</date>
<categorySeriesValue key="cs1901" value="59.9"/>
<!-- more data -->

And to interpret this XML in the jasper report we need to tell the jasper that we are using XML data source which should be interpreted using x-path and we do this by putting the following snippet in the jrxml:

<subDataset name="timeSeriesDataset" uuid="02091a0f-5ed3-4c13-9c7f-e48f1c72064c">
<field name="date" class="java.lang.String">
<field name="cs1901" class="java.lang.Integer">
<queryString language="xPath">

This snippet is also declaring the fields and mapping them to the XML elements and for this mapping we are using x-path. And finally, the jrxml snippet for the time series chart would be:

<chart evaluationTime="Report">
<reportElement x="0" y="0" width="0" height="0" backcolor="#FFFFFF" uuid="4252bcf1-43b7-49a3-b614-a556f3d90a96"/>
<chartTitle color="#000000"/>
<chartSubtitle color="#000000"/>
<chartLegend textColor="#000000" backgroundColor="#FFFFFF"/>
<datasetRun subDataset="timeSeriesDataset" uuid="c464fcb5-f019-4f72-b565-5785a15c8ead">
<seriesExpression><![CDATA["Observed Temperature"]]></seriesExpression>
<timePeriodExpression><![CDATA[(new SimpleDateFormat("yyyy-MM-dd").parse($F{date}))]]></timePeriodExpression>
<timeSeriesPlot isShowShapes="false">
<plot labelRotation="-45.0">
<seriesColor seriesOrder="0" color="#4F81BD"/>
<axisFormat verticalTickLabels="false"/>

Here, seriesExpression and valueExpression elements are normal which you’ll find in other charts as well but the interesting element is timePeriodExpression element which takes java.util.Date value and is used by DateAxis. And DateAxis automatically adjusts the amount of tick labels (date labels) so that all the labels are visible on the chart. And when you run this using the ireport, you’ll get an output like:

As it’s evident from the image, I have used a XML with a lot of data for demonstration purpose. I hope, this saves others’ valuable time.