DIfference with chart and Legend colors

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jm.tinghir
Posts: 1
Joined: Tue Jan 29, 2008 10:20 am
Location: Paris, France

DIfference with chart and Legend colors

Post by jm.tinghir » Tue Jan 29, 2008 10:25 am

Hi,
I want to customize the colors in my stackedBarChart, because I don't like the default ones.

I managed to do it this way :

Code: Select all

JFreeChart chart = ChartFactory.createStackedBarChart("", "", "", resultDataSet, PlotOrientation.VERTICAL, true, true, false);
final CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = new CustomRenderer();
plot.setRenderer(renderer);

private class CustomRenderer extends StackedBarRenderer {
	private Paint[] colors;

	public CustomRenderer() {
		this.colors = new Paint[] {Color.green, Color.red, Color.blue, Color.yellow, Color.orange, Color.cyan};
	}

	public Paint getItemPaint(final int row, final int column) {
		return (this.colors[row % this.colors.length]);
	}
}
But my problem is that now, legend colors do not correspond to my custom colors.

Any idea to solve that? I can't find a LegendRenderer or something like that...

Thanks for your help.
Jean-Marie

hhenrion
Posts: 12
Joined: Fri Mar 07, 2008 9:56 am

Post by hhenrion » Tue Apr 29, 2008 4:00 pm

I have the same problem with a XYLineChart.
My legend has not the color that I have choosen for my series...

Does someone have a solution please?

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 Apr 29, 2008 4:26 pm

You'd normally override getItemPaint(int, int) if you wanted a different colour for each item in a series - and, in that case, the series doesn't have an identifying colour, so you probably shouldn't be displaying a legend.

If you just want to provide a custom colour for each series, you can either just call setSeriesPaint(int, Paint) on the renderer and specify the colour for each series in advance...or you could override lookupSeriesPaint(int) and bypass the per-series and default paint settings (the getLegendItems() method call lookupSeriesPaint() so the colour of the series will get picked up for the legend).

In your example above, it would be sufficient to add the following method override:

Code: Select all

           public Paint lookupSeriesPaint(int series) {
        	   return getItemPaint(series, 0);
           }
David Gilbert
JFreeChart Project Leader

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

hhenrion
Posts: 12
Joined: Fri Mar 07, 2008 9:56 am

Post by hhenrion » Wed Apr 30, 2008 3:43 pm

Thank you for your help.

I have tried before to add the method override you tell me : but it was looping... And blocking my interface...

So I have override my renderer instead of call setSeriesPaint(int, Paint) on the renderer.
It works fine.

Locked