Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
tony_mistro
Posts: 3
Joined: Thu Aug 27, 2015 10:41 am
antibot: No, of course not.

Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by tony_mistro » Thu Aug 27, 2015 10:56 am

Hello all

I am having some trouble getting some chart modifications to give me the required output in Jaspersoft. I am using JaspersoftReportsServer 5.6.1 which bundles JFreeCharts 1.0.12.

I have created the following chart customizer which has three purposes:

1. To remove the legend border (this is working as expected)
2. To create a difference fill between the first 2 series in my XY chart (this is partially working)
3. To set the first 2 series lines as dashed rather than solid (this is not working at all)

The code I have created is as follows:

Code: Select all

package bpiave.customizer; 
 

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;

import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;


public class BPIAveSuite implements  net.sf.jasperreports.engine.JRChartCustomizer{

    @Override
    public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart) 
    {
    	
        XYPlot plot = (XYPlot) chart.getPlot();
        XYItemRenderer XYlineAndShapeRenderer =(XYLineAndShapeRenderer) plot.getRenderer();
        Stroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
       XYlineAndShapeRenderer.setBaseStroke(dashed);
        XYlineAndShapeRenderer.setBaseItemLabelsVisible(false);
       XYlineAndShapeRenderer.setSeriesStroke(
             0, new BasicStroke(
                 1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                 1.0f, new float[] {4.0f, 2.0f}, 0.0f
             )
         );
       
      XYlineAndShapeRenderer.setBaseStroke(dashed);
       XYlineAndShapeRenderer.setBaseItemLabelsVisible(false);
      XYlineAndShapeRenderer.setSeriesStroke(
            1, new BasicStroke(
                1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                1.0f, new float[] {4.0f, 2.0f}, 0.0f
            )
        );
        
      XYPlot plot1 = (XYPlot) chart.getPlot();
      XYDifferenceRenderer r = new XYDifferenceRenderer(Color.lightGray,
              Color.lightGray, false);
      r.setRoundXCoordinates(true);
      plot1.setRenderer(r);
     
      if(chart.getLegend()!=null)
      {
       chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);	
      }      
    } 
    
       
    }
The chart renders as follows:

Image

Any help to resolve these issues would be very gratefully accepted - I have burnt a couple of days trying to figure this out so far without stumbling on a solution.

Thanks in advance.

Tony

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by paradoxoff » Thu Aug 27, 2015 7:32 pm

Code: Select all

       XYlineAndShapeRenderer.setBaseStroke(dashed);
        XYlineAndShapeRenderer.setBaseItemLabelsVisible(false);
       XYlineAndShapeRenderer.setSeriesStroke(
             0, new BasicStroke(
                 1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                 1.0f, new float[] {4.0f, 2.0f}, 0.0f
             )
         );
This code block appears twice in your snippet. Is that an artifact from pasting your code to the forum, or does your code really look like that?

Code: Select all

    public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart) 
    {
    	
        XYItemRenderer XYlineAndShapeRenderer =(XYLineAndShapeRenderer) plot.getRenderer();
      XYDifferenceRenderer r = new XYDifferenceRenderer(Color.lightGray,
              Color.lightGray, false);
      r.setRoundXCoordinates(true);
      plot1.setRenderer(r);
Here you are retrieving the renderer from the plot and modify its appearance. Then, you create a new renderer object and assign that to the plot. The old renderer, that you have just modified, is overwritten.
Solution: do your modifications not on "XYlineAndShapeRenderer" (which is a confusing name for a variable because it is very similar to a JFreeChart class. When I read your code, I was indeed wondering for a moment what static methods you are calling), but on the "r" instead.

tony_mistro
Posts: 3
Joined: Thu Aug 27, 2015 10:41 am
antibot: No, of course not.

Re: Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by tony_mistro » Tue Sep 01, 2015 12:02 am

Hi paradoxoff, and thanks for your reply.

I think this is beginning to become a bit clearer to me. I am a bit of a java novice (well, a lot of actually!), so please excuse what will seem to you fairly stupid questions :)

I have modified my code based on your suggestions, but I know that I have a gap in my knowledge as to how to make the 2 required changes to the plot. I have a setRenderer at the end of my code which I know will only do half the job.

Is there a way to make the two required modifications to the plot?

Code: Select all

package bpiave.customizer; 
 

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;

import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;


public class BPIAveSuite implements  net.sf.jasperreports.engine.JRChartCustomizer{

    @Override
    public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart) 
    {
    	
        XYPlot plot = (XYPlot) chart.getPlot();
        XYItemRenderer DashedLine =(XYLineAndShapeRenderer) plot.getRenderer();
        Stroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
        DashedLine.setBaseStroke(dashed);
        DashedLine.setBaseItemLabelsVisible(false);
        DashedLine.setSeriesStroke(0, new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] {4.0f, 2.0f}, 0.0f));
        XYDifferenceRenderer GrayBand = new XYDifferenceRenderer(Color.lightGray, Color.lightGray, false);        
        GrayBand.setRoundXCoordinates(true);             
        plot.setRenderer(GrayBand);       
        
      if(chart.getLegend()!=null)
      {
       chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);	
      }      
    } 
    
    }

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by paradoxoff » Tue Sep 01, 2015 11:49 am

Essentially, you have repeated the bug from above: you retreive the renderer from the plot, then you change it, then you create a new renderer that overwrites the one that you have just changed.
Try this in your customize method:

Code: Select all

        public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart)
        {
           
            XYPlot plot = (XYPlot) chart.getPlot();
            XYDifferenceRenderer GrayBand = new XYDifferenceRenderer(Color.lightGray, Color.lightGray, false);       
            GrayBand.setRoundXCoordinates(true);             
            Stroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
            GrayBand.setBaseStroke(dashed);
            GrayBand.setBaseItemLabelsVisible(false);
            GrayBand.setSeriesStroke(0, new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] {4.0f, 2.0f}, 0.0f));
            plot.setRenderer(GrayBand);       
           
          if(chart.getLegend()!=null)
          {
           chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);   
          }     
        }

tony_mistro
Posts: 3
Joined: Thu Aug 27, 2015 10:41 am
antibot: No, of course not.

Re: Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by tony_mistro » Wed Sep 02, 2015 1:54 am

Hi paradoxoff

Thanks so much for your help, but I'm afraid that didn't change any output behaviours.

When looking at the problem again, and doing some further reading, it seemed to me that the problem centred around trying to use more than one renderer on the same dataset.

I found the following thread http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=29883 which seemed to be pretty close to the problem I was having, and modified the solution to the following:

Code: Select all

package bpiave.customizer; 
 

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Stroke;

import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;


public class BPIAveSuite implements  net.sf.jasperreports.engine.JRChartCustomizer{

    @Override
    public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart) 
    {
    	
        XYPlot xyplot = (XYPlot) chart.getPlot();
        XYItemRenderer DashedLines = new XYLineAndShapeRenderer();
        Stroke dashed =  new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
        DashedLines.setBaseStroke(dashed);
        DashedLines.setBaseItemLabelsVisible(false);
        DashedLines.setSeriesStroke(
             0, new BasicStroke(
                 1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                 1.0f, new float[] {4.0f, 2.0f}, 0.0f
             )
         );
        
         xyplot.setDataset( 1, xyplot.getDataset(0) );
         XYDifferenceRenderer GrayBand = new XYDifferenceRenderer(Color.lightGray,
              Color.lightGray, false);
         GrayBand.setRoundXCoordinates(true);
        xyplot.setRenderer( 1, DashedLines );
        xyplot.setRenderer( 0, GrayBand );
     
      if(chart.getLegend()!=null)
      {
       chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);	
      }      
    } 
    
       
    }
Again, unfortunately, I have seen no change to the outputted graph.

Any advice gratefully received. :)

Cheers

Tony

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by paradoxoff » Wed Sep 02, 2015 4:59 pm

Can you show an image of the actual output?
If the output does not change compared to a whatever reference chart, you can try to show your chart in a JFrame. if that looks ok, maybe a wrong chart gets picked up in your Jasper environment. I have never worked with jasper reports, so I don´t know whether this scenario is possible.

codeowl
Posts: 1
Joined: Sun Oct 18, 2015 11:17 pm
antibot: No, of course not.

Re: Help with Customizer for Jaspersoft (JFreeChart 1.0.12)

Post by codeowl » Sun Oct 18, 2015 11:31 pm

In case it helps someone else see the final solution here:
http://stackoverflow.com/questions/3313 ... rts-custom

paradoxoff, thanks for your input, much appreciated.

Regards,

Scott

Locked