Jump to content
We've recently updated our Privacy Statement, available here ×
  • Setting up chart customizer projects in Eclipse


    gdmoreno

    Introduction

    Chart customizers allow us to customize how charts get output in iReport and in JasperReports Server, specifically those charts that are based on the JFreeChart library.

    To customize a graph, a class must be written that implements the net.sf.jasperreports.engine.JRChartCustomizer interface. The only one method to implement is the customize method.

    Setting up the Eclipse project

    • Create a new Eclipse project, as you normally would
    • Create a lib directory, and add the necessary JAR files

    You'll need to add the JFreeChart JAR file, and the JasperReports JAR files, which you can copy from the JasperReports Server LIB directory (/jasperserver-pro/WEB-INF/lib): All the JAR files whose filenames start with "jasperreports", and jfreechart-1.0.12.jar; the exact JFreeChart version number will probably vary on your JasperReports Server installation.

    You'll also need to add jcommon-1.0.15.jar, which you can also find in JasperReports Server's LIB directory.

    Finally, you'll also need the AspectJ JAR files, which you can also find in the JasperReports Server LIB directory. Just add all the JAR files whose filenames start with the label "aspectj".

    • Make sure to add those JAR files to the library build path

    Creating a custom class

    You'll need to create a class that either implements the JRChartCustomizer interface, or that extends the JRAbstractChartCustomizer class. In the sample code below, the class implements the customize method so that every shape in the chart will be a circle.

    Creating the right shape and using the JFreeChart API is beyond the scope of this article; suffice it to say that there exist rich resources out on the web.

    package test.customizer; 
    
    import java.awt.*; 
    import java.awt.geom.*; 
    
    import org.aspectj.asm.internal.HandleProviderDelimiter; 
    import org.jfree.chart.*; 
    import org.jfree.chart.plot.*; 
    import org.jfree.chart.renderer.*; 
    import org.jfree.chart.renderer.category.CategoryItemRenderer; 
    import org.jfree.chart.renderer.xy.XYItemRenderer; 
    import org.jfree.data.category.CategoryDataset; 
    import org.jfree.data.general.PieDataset; 
    import org.jfree.data.xy.XYDataset; 
    import org.jfree.util.ShapeUtilities; 
    
    import net.sf.jasperreports.engine.JRAbstractChartCustomizer; 
    import net.sf.jasperreports.engine.JRChart; 
    
    public class ColorScaleCustomizer extends JRAbstractChartCustomizer { 
    
        public void customize(JFreeChart freeChart, JRChart jrChart) { 
            XYPlot plot = (XYPlot)freeChart.getPlot(); 
            XYItemRenderer renderer = (XYItemRenderer)plot.getRenderer(); 
            int seriesCount = plot.getSeriesCount(); 
            for (int i = 0; i < seriesCount; i++) { 
                Shape shape = new Ellipse2D.Double(0, 0, 5, 5); 
                renderer.setSeriesShape(i, shape); 
            }
        }
    }
    

    Create a JAR file

    When you're done creating the customizer classes, you'll need to export the Eclipse project as a JAR file. You can do this by right-clicking on the project name, choosing export, and going through the steps of creating a JAR file. You do not need to export project-specific files (like the .classpath and the .project files).

    Add the JAR file to the iReport classpath

    Once the JAR file has been exported out to your filesystem, you'll need to add it the iReport classpath. You do this in iReport by choosing "Tools -> Options" and then adding the JAR file to the classpath tab, as in the screenshot below. Ireport_classpath.png.889641c183af2d87336dfd15c5181571.png

     

    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...