Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to use Customizer Class to change the behavior of Multi Axis chart


    akonkin
    • Version: v5.0.0 Product: iReport Designer

    In this article I want to describe how to customize the standard behavior of Multi Axis chart.

    Example 1:

    We have a Multi Axis chart which in turn contains a stacked area chart and a couple of line charts in the order that is shown on the picture below:

    [toc]

    charts_disposition.png.1039d4d12a5d9ab86b49d3fc294c4f06.png

    You may ask - so what? What kind of issue may cause such disposition of the sub charts in Multi Axis chart?

    Lets execute this report and check the results of this execution:

    overlapping_1.png.a9947b4a960ab3748d0aec9656fb446c.png

    As you can see from the picture below some parts of Stacked Area chart overlap the Line charts, but you can still clearly interpret the information that was shown in the chart.

    Example 2:

    Stacked area charts are filled in with the values that are very close to the maximum values and they fill almost all the space in the chart:

    overlapping_2.png.05cd2d79e351b953ddfb4653efca8caa.png

    Such visualization of the data is not so convenient becuse it's not possible to analyze the information that was represented with the Line Charts.

    What can we do to change the behavior of the Line Charts in this situation?

    One of the possible solutions is to write the Java code that implements the Customizer class that should change the behavior of the Line Charts(I mean show them infront of Stacked Area chart).

    Below I quote the source code of such class (MultiAxisOrderCustomizer.java) which was kindly provided to me by one of our leading developers:

    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.DatasetRenderingOrder;
    import org.jfree.chart.plot.Plot;
    import org.jfree.chart.plot.XYPlot;
    
    import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
    import net.sf.jasperreports.engine.JRChart;
    
    public class MultiAxisOrderCustomizer extends JRAbstractChartCustomizer {
        @Override
        public void customize(JFreeChart chart, JRChart jasperChart) {
            Plot plot = chart.getPlot();
            if (plot instanceof CategoryPlot) {
                ((CategoryPlot) plot).setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
            } else if (plot instanceof XYPlot) {
                ((XYPlot) plot).setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
            }
        }
    }
    

    To use this Customizer Class in the report

    1. give the path to this class in iReport designer

    2. fill in the name of this class in the Multi Axis chart/Customizer Class property

      iReport_setup.png.fc8b8dc8936b938e14c2d850decdbfed.png

    Let's check the behavior of the charts after the we have applied the adjustmens that should integrate the MultiAxisOrderCustomizer in our report:

    report_runtime.png.bf8297085d5fd3186576eeb750e4edad.png

    In the picture above the two line charts are shown in front of Stacked Area chart.

    How to compile the source code (Windows 7)

    1. Download Java JDK from the Oracle site

    2. Run the windows console by executing the cmd command

    3. In the windows command line, define the JAVA_HOME variable. You can do this by executing the command that I quote below

      set JAVA_HOME=c:<path to Java JDK>java
      
    4. Add JAVA_HOME variable to the windows PATH variable

      set PATH=%JAVA_HOME%bin
      
    5. Compile the MultiAxisOrderCustomizer (to compile this file you need two Java libraries: jfreechart.jar and jasperreports.jar)

      In my test environment was installed JasperReports Server v5.0. This distribution contains both libraries in its lib folder.

      I gave the path to these libraries in javac compiler via the -classpath key as shown below:

      javac -classpath C:jaspersjrs_500apache-tomcatwebappsjasperserver-proWEB-INFlibjfreechart-1.0.12.jar;C:jaspersjrs_500apache-tomcatwebappsjasperserver-proWEB-INFlibjasperreports-5.0.0.jar MultiAxisOrderCustomizer.java
      

      console_output.png.202f97dacca12770a1069d6d30ce8c35.png

      If everything is ok you should find MultiAxisOrderCustomizer.class file in the same directory with MultiAxisOrderCustomizer.java file.

    Please find in the attachment, files.zip:

    • MultipleAxisChartReport.jrxml report (this report is based on Sugarcrm database that is supplied with JasperReports Server)
    • MultiAxisOrderCustomizer.java
    • MultiAxisOrderCustomizer.class
    • jfreechart-1.0.12.jar
    • jasperreports-5.0.0.jar

    Thats all. Thank you for your attention to this article :).

    overlapping_2.png.a2eacfdee2fdb55d5db27b401f82ff30.png

    console_output.png.c799c15455d86d158ee5366a6db4cd23.png

    files.zip


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...