Search code examples
jasper-reportsjfreechart

How to set multiple Renderers on a single XYDataSet in jaspersoft reports customizer for jFreeChart


We have a jfreechart in jaspersoft reports community edition where we want to apply two renderers to the same DataSet. The approach we are currently using is not working as expected.

Our current approach is as follows where we attempt to copy the DataSet from index 0 into index 1 and then set a renderer for each index.

xyplot.setDataset( 1, xyplot.getDataset(0) );
xyplot.setRenderer( 1, XYLineAndShapeRenderer_DashedLines );
xyplot.setRenderer( 0, xYDifferenceRenderer_GrayBand ); 

We don't get any errors, but the line is not dashed and we do get the gray band but it is not drawn correctly.

However when we comment out one or the other, they work fine on their own.

It kinda feels like the second one overwrites the first one.

Is this the right approach to setting multiple renderers on a single DataSet and if so what are we doing wrong?

Or should a different approach be taken and if so what is it?


Solution

  • Ok, so here is what finally solved it. I was attempting to use two renderers, one for the grey band and one for the dashed lines, but I only needed to use one.

    So the final code ended up being:

    package gprCustomizer;
    
    import org.jfree.chart.JFreeChart;
    import net.sf.jasperreports.engine.JRChart;
    import net.sf.jasperreports.engine.JRChartCustomizer;
    import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
    import java.awt.BasicStroke;
    import org.jfree.chart.plot.XYPlot;
    import java.awt.Color;
    
    public class GPRCustomizations implements JRChartCustomizer {
        public void customize(JFreeChart chart, JRChart jrChart) {
            // Get Plot
            XYPlot plot = (XYPlot)chart.getPlot();
            // Apply Gray Band Style
            XYDifferenceRenderer xYDifRnd_GrayBand = new XYDifferenceRenderer();
            xYDifRnd_GrayBand.setNegativePaint(Color.lightGray);
            xYDifRnd_GrayBand.setPositivePaint(Color.lightGray);
            xYDifRnd_GrayBand.setShapesVisible(false);
            xYDifRnd_GrayBand.setRoundXCoordinates(true);
            // Apply Dashed Style to Series 0,1
            xYDifRnd_GrayBand.setSeriesStroke(0, 
                new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {6.0f, 6.0f}, 0.0f
                )
            );
            xYDifRnd_GrayBand.setSeriesStroke(1, 
                new BasicStroke(
                    2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {6.0f, 6.0f}, 0.0f
                )
            );
            plot.setRenderer(xYDifRnd_GrayBand);
            // Remove Borders from Legend
            if(chart.getLegend() != null)
            {
                chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);   
            }
        }
    }
    

    Which produced the expected outcome of the grey band and the dashed lines either side:

    Final Outcome