Unable to control coloring of Line in LineAndShapeRenderer !

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
kanishk
Posts: 10
Joined: Mon May 01, 2006 2:57 am

Unable to control coloring of Line in LineAndShapeRenderer !

Post by kanishk » Mon Jul 17, 2006 8:06 am

Hello,
Given below is the code and image generated.
My problem is that I don't want the lines to be black but ONLY the data points. Any suggestions?
Also, is it possible to put the value above the plotted data points in the graph ?

Code: Select all

public class MyRenderer extends LineAndShapeRenderer {

	private Boolean[] anomaly;
	private static int count = 0;
	
	/**
	 * 
	 *
	 */
	public MyRenderer(Boolean[] anomaly) {
		
		super();
		this.anomaly = anomaly;
	}


	/**
	 * 
	 */
	public Paint getItemPaint(int series, int item) {

		
		
		if(anomaly[item].booleanValue())
			return Color.BLACK;
		
		return super.getItemPaint(series,item);
	}

}

Code: Select all

public class MyDrawingSupplier extends DefaultDrawingSupplier {

	private static Paint[] myPaints = { Color.RED, Color.BLUE, Color.GREEN,
			Color.MAGENTA };

	private int paintIndex = 0;

	public MyDrawingSupplier() {

		super();
	}

	/**
	 * Returns the next paint in the sequence.
	 * 
	 * @return The paint.
	 */
	public Paint getNextPaint() {
		Paint result = myPaints[paintIndex % myPaints.length];
		paintIndex++;
		return result;
	}

	/**
	 * Returns the next shape in the sequence.
	 * 
	 * @return The shape.
	 */
	public Shape getNextShape() {
		
		double size = 6.0;
        double delta = size / 2.0;
        
        // circle
        Shape result = new Ellipse2D.Double(-delta, -delta, size, size);
        
		return result;
	}

}
Image

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jul 18, 2006 11:12 am

There are a couple of methods in LineAndShapeRenderer:

setUseFillPaint(boolean);
setUseOutlinePaint(boolean);

Set these to true and the shapes will be filled with the fill paint and drawn with the outline paint. Then in your renderer, you can override the getItemFillPaint() method rather than getItemPaint() and the returned value will affect the shape only and not the lines.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

kanishk
Posts: 10
Joined: Mon May 01, 2006 2:57 am

Post by kanishk » Wed Jul 19, 2006 7:48 am

excellent !! that worked like a charm.
Thanks.

MattBallard
Posts: 29
Joined: Wed Jun 06, 2007 7:41 pm

Post by MattBallard » Fri Sep 07, 2007 6:03 pm

I am having an issue with a similar problem.

I just want my shapes to change color and my lines to be black.

Currently I have extended the LineAndShapeRenderer to this:

Code: Select all

public class LineAndColorShapeRenderer extends LineAndShapeRenderer {
    
    /** Creates a new instance of LineAndColorShapeRenderer */
    public LineAndColorShapeRenderer() {
        super();
    }
    
    public LineAndColorShapeRenderer(boolean lines, boolean shapes){
        super(lines, shapes);
    }
    
    public Paint getItemPaint(int series, int item) {
        
        if(series == 0){
            return Color.green.darker();
        }
           
      
      return super.getItemPaint(series,item);
   } 
    
}
I declare my LineAndColorShapeRenderer like this:

Code: Select all

LineAndShapeRenderer peRenderer = new LineAndColorShapeRenderer();
        peRenderer.setShape(hex,true);
//        peRenderer.setUseFillPaint(true);
//        peRenderer.setShapesFilled(true);
        
        if(aecvdm.isIsSummary()){
            peRenderer.setBaseLinesVisible(false);
        }else{
            peRenderer.setBaseLinesVisible(true);
        }
        
        peRenderer.setSeriesStroke(0 , new BasicStroke(.5f));
        plot.setRenderer(2,peRenderer);
What else do I need to do to keep my lines black?

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Post by John Matthews » Wed Sep 12, 2007 7:40 pm

Matt:

Another stumbling block: I sometimes forget that the series index in such methods is 0-based! For a LineAndShapeRenderer that's only used by one series, the index is always zero.

Code: Select all

plot.setRenderer(myDatasetIndex, myRenderer);
myRenderer.setSeriesOutlinePaint(0, Color.GREEN);
John

Locked