alternating colors on one dataset in XYPlot
alternating colors on one dataset in XYPlot
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!
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!
multi colored charts
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.
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
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

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!
http://www.jfree.org/jfreechart/javadoc ... nt,%20int)
Thanks again for your input!
Hi,JeffKirbyJeffKirby wrote:
public class SelectionRenderer extends XYBarRenderer {}
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
I'm really have no idea .....................
Thank you
I got the same probleme...the selection works only one time!!!!tony01 wrote:Hi,JeffKirbyJeffKirby wrote:
public class SelectionRenderer extends XYBarRenderer {}
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:Could you tell me WHY??Code: Select all
newRender.setSelection(0,3);//doesn't work newRender.setSelection(0,4);//It's work
I'm really have no idea .....................
Thank you