change bar color depending of a limit

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

change bar color depending of a limit

Post by Olivier » Tue Oct 01, 2002 10:38 am

I would like to set a limit where bar graph change color.

For instance under 50, bar graph are blue and after 50 they are red.

Does anybody has already done somthing like this or has a cloud how
should I do ?

Thanks.

O.

Rich Unger

Re: change bar color depending of a limit

Post by Rich Unger » Tue Oct 01, 2002 7:44 pm

I presume you're talking about a bar chart with a single data series. You can do this with the setCategoriesPaint() method (see http://www.object-refinery.com/jfreecha ... .awt.Paint[]))

In 0.9.3, this will only take effect in one of the bar chart renderers (2d-horizontal I think). The code for the 2d and 3d horizontal and vertical bar renderers is checked into CVS, so presumably it will be in 0.9.4.

At any rate, you should be able to loop over your data set, and create a Color[] based on the values in that set. Then just call renderer.setCategoriesPaint(Color[]).

Cheers,
Rich

David Gilbert

Re: change bar color depending of a limit

Post by David Gilbert » Tue Oct 01, 2002 11:12 pm

Alternatively, a custom renderer would be able to achieve this easily. Just subclass VerticalBarRenderer and override the drawItem(...) method (just cut and paste the method, then modify the color setting based on some value threshold that you could set as an attribute for your custom renderer).

Regards,

DG

Tony

Re: change bar color depending of a limit

Post by Tony » Fri Oct 04, 2002 10:20 am

I did it follow the suggestion. It works fine. However, it doesn't work if the Verical Bar Chart has 2 Series.

How can I solve it??

Thx,

Tony

David Gilbert

Re: change bar color depending of a limit

Post by David Gilbert » Fri Oct 04, 2002 4:59 pm

That's because the 'category paint' setting is for a special case where there is only one series (and you don't want one color for all the bars). I think you will find a better solution is to subclass VerticalBarRenderer, then you have full control over all the series...

Regards,

DG

Locked