Jump to content
We've recently updated our Privacy Statement, available here ×
  • Chart Customizer Examples for JFreeCharts in JasperReports – Legends, Parameters, Ticks and Gridlines


    In the previous post on chart customizing in JasperReports I gave a short tutorial on the implementation and usage of chart customizer classes based on an example for enabling and configuring minor ticks. Chart customizer classes can leverage the full functionality of the JFreeCharts library, so the customizing possibilities are almost limitless.

    In this post I’ll show a few examples for JFreeCharts customization in JasperReports by using chart customizer classes. This first part of the overview is limited to the topics gridlines, ticks, parameter passing and legends. All details discussed in this article are related to JFreeCharts, so they can be applied both in the community edition as well in the professional or enterprise editions of JasperReports, iReport Designer and Jaspersoft Studio.

    Ticks and Gridlines

    Ticks displayed on the y or x axis are closely related to gridlines, since the number of ticks define the number of gridlines (if visible). Chart properties and chart themes enable only limited configuration for ticks and gridlines. The examples below show several customizations which can be only implemented by chart customizer classes.

    Changing the Number of Gridlines

    JasperReports embeds the JFreeCharts library, automatically computes the optimal number of horizontal and vertical gridlines (and resp. the optimal number of ticks on the y and x axis) for a chart. If gridlines should appear more or less dense than automatically computed, a chart customizer class can be used to change the number of ticks and gridlines to be displayed. Following screenshot depicts a chart where the number of gridlines (and ticks) has been set to the fixed value of 5.

    http://blog.itransparent.de/wp-content/uploads/2013/11/XYYaxisChart1-300x176.png

    Cofiguration of the Ticks on the Date Axis

    The previous example shows months values on the x axis. Although the date format can be set in iReport Designer and Jaspersoft Studio, the number of ticks on the date axis is still computed automatically by JFreeCharts which doesn’t necessarily result in a tick for each month. Chart customizer classes enable the detailed configuration of the ticks for a date axis. E.g. the chart in the previous example can be configured in a way that the date axis sets a tick for each month. The resulting chart will look like as follows:

    http://blog.itransparent.de/wp-content/uploads/2013/11/XYYaxisChart11-300x176.png

    It’s also possible to set the number of repetitions per unit on the date axis (e.g. you could repeat each year 4 times on the x axis).

    Enabling a Zeroline

    In some cases the readability of a chart is much improved by displaying a zeroline. One solution might be to generate a pseudo-line in a line chart with zero values only. The drawback of this solution is that this pseudo-line will always be displayed, even if there are no negative values. The JFreeCharts library natively supports zerolines, which are displayed only if there are values present above and below or close to the zero region. The zeroline can only be enabled by implementing a chart customizer class. It’s also possible to configure the line to specific needs, e.g. the following example shows a red dotted zeroline.

    http://blog.itransparent.de/wp-content/uploads/2013/11/XYZeroLineChart-300x176.png

    Mapping Multiple Charts to the Same Y Axis in MultiAxis Charts

    MultiAxis charts can be used either for displaying different type of values in the same chart (e.g. rain, temperature and hours of sunshine) or for displaying similar type of values using different appearances (e.g. displaying the values for each region as lines and displaying the country average as area). The latter requires the mapping of all charts to the same y axis. This can be achieved by chart customizer classes. Following example uses three different renderers for the different series. Each series is a separate chart in a MultiAxis chart. A chart customizer class has been applied for mapping all three charts to the same y axis.

    http://blog.itransparent.de/wp-content/uploads/2013/11/XYRenderingMultiAxisChart-300x176.png

    Changing the Chart Appearance by Setting Report Parameters / Input Controls

    Another advantage of chart customizer classes compared to chart properties or chart themes is that the appearance of a chart can be manipulated based on the values of report parameters. All customizations implemented using chart customizer classes can be extended so that they can be controlled by report parameters.

    Following screenshots show the usage of report parameters with chart customizer classes. In this example the number of ticks on the y axis can be set via report parameters (or input controls in the JasperReports Server).

    http://blog.itransparent.de/wp-content/uploads/2013/11/XYYAxisChart10-300x196.png

    When changing the value from 10 to 4, only 4 ticks (and 4 gridlines) are generated on the y axis of the chart.

    http://blog.itransparent.de/wp-content/uploads/2013/11/XYYAxisChart4-300x190.png

    Legends

    Chart properties and chart themes enable only limited customizations for legends. If for example a legend should be displayed vertically and without borders, it can only be achieved by a chart customizer class. The style of a legend can be customized very flexibly using chart customizer classes. Following example shows a legend without borders and in vertical arrangement.

    http://blog.itransparent.de/wp-content/uploads/2013/11/TimeSeriesChart_Legend-300x171.png

    Conclusion

    In this post I showed several examples for customizing JFreeCharts in JasperReports by using customizer classes. The examples covered the topics of legends, parameters, ticks and gridlines. In the upcoming post I’ll show further examples for customizing JFreeCharts in JasperReports using chart customizer classes, covering the topics of markers and rendering.

    Download our free example chart customizing project for Jaspersoft Studio: ChartCustomizerBlog.zip


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...