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


    In the previous 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 this post I’ll cover the topics of markers and rendering. 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.

    Marker

    The readability of charts can be improved by adding horizontal and vertical marker lines or marker intervals to emphasize important or critical areas. In JasperReports markers can be configured by using chart customizer classes. The following examples show the use of marker lines and marker intervals in JasperReports.

    Displaying Marker Lines

    Marker lines can be added to a chart both in horizontal and in vertical orientation. The position of the marker, its style and color can all be configured to meet any needs. Following example shows two horizontal marker lines which depict the highest and the lowest value in the chart.

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

    In many cases it’s not the minimum and maximum values that are required but rather marker lines showing the 20% and 80% values. Because a chart customizer class has access to the lowest and highest value of the chart and markers can be positioned at any value, the 20/80% marker lines can be easily implemented using a chart customizer class. Following example shows the 20/80% marker lines.

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

    Displaying a Marker Interval

    In addition to marker lines it is also possible to emphasize regions of a chart by displaying horizontal or vertical marker intervals. Similarly to marker lines, the appearance, position and style of marker intervals can be configured. For displaying a marker interval in JasperReports charts a chart customizer class is required. Following example shows a marker interval for the 20/80% area of the chart.

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

    Rendering of Series

    Chart properties and chart themes only allow to set the colors for the series. Chart customizer classes also enable to set the line style, fill or renderer for a series. Following examples show some customizations for the rendering of series.

    Changing the Line Style for a Series

    Defining the line styles of a chart in iReport Designer or Jaspersoft Studio via chart properties or chart themes is unfortunately limited to a few border lines of the charts. Style settings for series are limited to colors only. Using chart customizer classes in JasperReports the line style for series can be changed. This is especially required in b/w print, because different line styles are necessary for creating readable charts. Following example shows lines for a series as dashed, solid and custom styled lines. Also the line thickness can be adjusted by using chart customizer classes.

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

    Changing the Renderer for a Series

    Next to changing the line style for a series it’s also possible to change the appearance of a series by setting a different renderer for it. The JFreeCharts library offers a wide range of renderers, much more than the number of renderers configurable in JasperReports via the chart properties or chart themes. For example when displaying monthly values in a chart the default connecting lines don’t make any sense, since there are no in-between values. It would be more useful to render monthly values as steps. Following example shows the monthly values using steps instead of the default lines. The according renderer has been changed in the chart customizer class accordingly. Line styles can also be applied when using the step visualization.

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

    By combining chart customizer classes and multi axis charts it’s also possible to combine different renderers in the same chart.

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

    Patterned Bar Charts

    Similarly to line charts it might also be necessary for bar charts not only to set the colors of the series but also define a texture or pattern. Especially when printing the reports in b/w patterns help differentiating between the series. Following example shows a bar chart with a selection of striped patterns. In general there are no limitations for the textures to be used in bar charts.

    http://blog.itransparent.de/wp-content/uploads/2013/11/stripedbars-300x128.png

    Setting the Color for a Series Based on its Name / Setting the Color for a Bar Based on its X or Y Value

    Imagine the scenario of a single report or several reports where different data for the same categories (series) is depicted in several charts. It would be preferable to always use the same color for the same category during the whole report or reports (e.g. lines and bars for Saxony should always be yellow and for Bavaria the color should be blue). It can be solved by setting the series colors using the chart properties or chart themes but if in a chart some series are missing or the series are scrambled, the colors will be mixed up. A more useful implementation would be the use of a chart customizer class. Chart customizer classes can access the dataset of the chart and therefore also know the categories (and series names). Based on this information a mapping can be defined in the chart customizer class which always sets the color for a series with the same name to the same color.

    A related problem is the coloring of single bars in the same series based on the value of the bar on the x or y axis. For example by using chart customizer classes all bars can be colored green which surpass a given value. The limit can be computed dynamically or be given as a report parameter from the user. Following example shows a bar chart with a single series where bars surpassing a given limit are colored green (the limit was set to 80% of the maximum value).

    http://blog.itransparent.de/wp-content/uploads/2013/11/ColoredBars2-300x162.png

    Using chart customizer classes it’s also possible to change the color of a bar in a series to e.g. green if the bar belongs to a category value e.g. “March”. Following example shows this scenario. Of course the name of the bar to be filled differently can be also set by a report parameter.

    http://blog.itransparent.de/wp-content/uploads/2013/11/ColoredBars-300x162.png

    Reading the Design Information from a Design-File oder Design-Database

    Chart customizer classes are regular Java classes, so when implementing the customize method there are no limitations. It’s also possible to use implementations from other Java libraries. This also allows us to retrieve all design information from a design file or from a design database. All examples presented in this and the previous posts can be adapted to work with a design database.

    Conclusion

    In this post I presented several examples for customizing JFreeCharts in JasperReports by using customizer classes. The examples covered the topics of markers and rendering. In the end I also discussed the possibility of reading all design information from a design file or database – instead of hard-coding the design values in the code of the chart customizer classes.

    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...