Different label color for each bar

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
nn14
Posts: 4
Joined: Wed Aug 06, 2014 4:02 pm
antibot: No, of course not.

Different label color for each bar

Post by nn14 » Wed Sep 10, 2014 3:34 pm

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.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Different label color for each bar

Post by John Matthews » Thu Sep 11, 2014 3:10 am

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.

nn14
Posts: 4
Joined: Wed Aug 06, 2014 4:02 pm
antibot: No, of course not.

Re: Different label color for each bar

Post by nn14 » Mon Sep 15, 2014 2:23 pm

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.
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?


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);

		}

david.gilbert
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

Post by david.gilbert » Wed Sep 17, 2014 8:31 pm

Have a look in the drawItemLabel() method of the AbstractCategoryItemRenderer class, you will see that the label paint comes from:

Code: Select all

Paint paint = getItemLabelPaint(row, column);
Override getItemLabelPaint() to return whatever color you need.
David Gilbert
JFreeChart Project Leader

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

Locked