Hi, Is it possible to set the color of a label to a different color based upon the value of the bar?
E.g if the bar is 90% then show the label in red, else label should be in green.
Different label color for each bar
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: Different label color for each bar
One approach is to override the getItemPaint() method of your BarRenderer, as outlined here. You can use the row and col parameters to query your dataset and determine the desired color to return.
Re: Different label color for each bar
thanks! One doubt, I have customized the bar color using the below code, didnt get on how to return the color of the label. Can you please clarify?John Matthews wrote:One approach is to override the getItemPaint() method of your BarRenderer, as outlined here. You can use the row and col parameters to query your dataset and determine the desired color to return.
Code: Select all
@Override
public Paint getItemPaint(int row, int column) {
CategoryDataset dataset = getPlot().getDataset();
double value = dataset.getValue(row, column).doubleValue();
if (value < 0.75 && column == 1) {
return Color.red;
} else if (value >= 0.75 && column == 1) {
return new Color(76, 184, 72);
} else
return new Color(195, 191, 191);
}
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Different label color for each bar
Have a look in the drawItemLabel() method of the AbstractCategoryItemRenderer class, you will see that the label paint comes from:
Override getItemLabelPaint() to return whatever color you need.
Code: Select all
Paint paint = getItemLabelPaint(row, column);
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program