Colour Selection

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gaurav kathotia

Colour Selection

Post by gaurav kathotia » Sun Apr 28, 2002 7:27 am

hi
Is it possible to select the colour to plot bars while calling
chartFactory.createVerticalXYBarChart()? or any other chart for that matter?

Is it possible to choose during a run of any dynamic chart?

Thanks
Gaurav Kathotia

David Gilbert

Re: Colour Selection

Post by David Gilbert » Mon Apr 29, 2002 5:24 am

The bar colors are set on a per series basis using the setSeriesPaint(...) method in the Plot class. You can change these colors at any point after the chart is created.

Regards,

DG.

gaurav kathotia

Re: Colour Selection

Post by gaurav kathotia » Mon Apr 29, 2002 6:29 am

thanks DG
But is there is any example or any demo program that shows colour selection or change ? I am getting confused with the java.awt.Paint
interface or how to go about it?
Thanks in advance
Gaurav Kathotia

Mudit Wahal

Re: Colour Selection

Post by Mudit Wahal » Mon Apr 29, 2002 4:38 pm

Two steps.

First, I've extended AbstractSeriesDataset.
----------------------
public class myXYData extends AbstractSeriesDataset implements IntervalXYDataset
----------------------

In this class, I've included a method to get Paint for the current (int series, int item). You can return whatever Paint you want from here.

------------------------
private static Paint[] seriesPaint = new Paint[] {Color.red, Color.lightGray, Color.green};
....
public Paint getItemPaint(int series, int item)
{
double d = change[item].doubleValue();
if (d < 0)
return this.seriesPaint[0];
else if (d == 0)
return this.seriesPaint[1];
else
return this.seriesPaint[2];
}
-------------------------------

Step 2:
I extended the VerticalXYBarRendered and overriden drawItem method.
...................
if (data instanceof myXYData)
seriesPaint = ((myXYData)data).getItemPaint(series, item);
else
seriesPaint = plot.getSeriesPaint(series);
....................

Thats it !!!!

Mudit

Locked