alternating colors on one dataset in XYPlot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Weenskii
Posts: 4
Joined: Thu Feb 03, 2005 7:12 am

alternating colors on one dataset in XYPlot

Post by Weenskii » Thu Feb 03, 2005 7:34 am

I was wondering if it were possible to color-code different sections of a single XYPlot based on certain conditions in the application, i.e. the y-values of the plot.

For example: if there are 10 data points in the line chart dataset, I would like to have the sections between points 2 and 4 to be one color, and the have the sections between points 6 and 7 be another color, with everything else be in a third color.

*--*--*--*--*--*--*--*--*--*

Is this possible? I've looked into the Renderer classes, but have come up empty. If it turns out that this isnt possible, had anyone found a workaround to achieve this some other way? Any help is greatly appreciated. thanks!

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Thu Feb 03, 2005 8:40 am

I don't think there is an easy way to do it. Usually there is one color per series. Perhaps you have to extend the renderer.

mallupappi

multi colored charts

Post by mallupappi » Thu Feb 03, 2005 10:34 pm

So far the only method i have been able to come up with is independently charting each color on the same axis. if the data points are set up correctly there should be no empty spaces. Anyone with a better idea let me know too, thanks.

JeffKirby
Posts: 1
Joined: Fri Feb 04, 2005 2:22 am

Post by JeffKirby » Fri Feb 04, 2005 2:27 am

Quite Simple actually. I needed to do a similar thing for my Histogram, so
i extended the XYBarRenderer and implemented the method getItemPaint() as well as some modification methods.

here is my example.

All you have to do is swap the renderer when you create your chart


XYPlot xyPlot = jfreechart.getXYPlot();
SelectionRenderer newRend = new SelectionRenderer();

xyPlot.setRenderer( newRend);

Then set your selection. Your problem is easier ,since it woud be a constant if for the color range..



public class SelectionRenderer extends XYBarRenderer {

private int selectedRow;
private int selectedColumn;
private boolean selectionMade=false;
private Paint selectionPaint = Color.blue;


public void setSelection(int row,int column){
selectedRow = row;
selectedColumn=column;
selectionMade=true;
}

public void setSelected(boolean toggle){
selectionMade=toggle;
}

/* (non-Javadoc)
* @see org.jfree.chart.renderer.AbstractRenderer#getItemPaint(int, int)
*/
public Paint getItemPaint(int row, int column) {
if(selectionMade && row==selectedRow && column==selectedColumn ){
return selectionPaint;
}
return super.getItemPaint(row, column);
}
}



hope it helped :)

oacis
Posts: 101
Joined: Fri Jan 07, 2005 5:57 am
Location: Australia, Sydney

Post by oacis » Fri Feb 04, 2005 3:18 am

Yet another tangent...

If you are looking to highlight values on a graph, you may wish to use an IntervalMarker.

Although this will obviously colour the entire interval.

Weenskii
Posts: 4
Joined: Thu Feb 03, 2005 7:12 am

Post by Weenskii » Tue Feb 08, 2005 7:00 am

thank you all for your help. I'm now quite sure that I can override the AbstractRender.getItemPaint to create this behavior. For more info check out the javadoc:

http://www.jfree.org/jfreechart/javadoc ... nt,%20int)

Thanks again for your input!

tony01
Posts: 5
Joined: Tue Aug 02, 2005 12:44 pm

Post by tony01 » Fri Aug 05, 2005 9:38 am

JeffKirby wrote:
public class SelectionRenderer extends XYBarRenderer {}
Hi,JeffKirby

I use your code , and change it to extends XYLineAndShapeRenderer

but happend some thing....

the method names setSelection() just work ONE time

It's means I used like this:

Code: Select all

newRender.setSelection(0,3);//doesn't work
newRender.setSelection(0,4);//It's work
Could you tell me WHY??

I'm really have no idea .....................

Thank you

anto
Posts: 2
Joined: Sat Dec 17, 2005 12:03 am

Post by anto » Thu Dec 29, 2005 7:26 pm

tony01 wrote:
JeffKirby wrote:
public class SelectionRenderer extends XYBarRenderer {}
Hi,JeffKirby

I use your code , and change it to extends XYLineAndShapeRenderer

but happend some thing....

the method names setSelection() just work ONE time

It's means I used like this:

Code: Select all

newRender.setSelection(0,3);//doesn't work
newRender.setSelection(0,4);//It's work
Could you tell me WHY??

I'm really have no idea .....................

Thank you
I got the same probleme...the selection works only one time!!!!

Locked