Different color for each bar

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

Different color for each bar

Post by Guest11 » Mon Nov 24, 2003 6:10 pm

Hi,
I want to get different color for each bar,
i am using the following code, please tell me where to make the change to get different colors for each bar.

Code: Select all

				JFreeChart chart = ChartFactory.createBarChart(
					"Bar Chart Demo",         // chart title
					"Category",               // domain axis label
					"Value",                  // range axis label
					dataset,                  // data
					PlotOrientation.VERTICAL,
					false,                     // include legend
					false,                     // tooltips?
					false                     // URLs?
				);

				chart.setBackgroundPaint(java.awt.Color.white);
				CategoryPlot plot = chart.getCategoryPlot();
				
				
				CategoryItemRenderer cir = plot.getRenderer();
				cir.setSeriesPaint(0,  new Color(50, 128, 24));
				cir.setSeriesPaint(1,  new Color(141, 243, 174));
				cir.setSeriesPaint(2,  new Color(50, 128, 24));
				cir.setSeriesPaint(3,  new Color(141, 243, 174));

Thanks,
Vikram

suprigya

Post by suprigya » Mon Nov 24, 2003 11:59 pm

To get different color for each bar; you need to have each bar in a separate series. Bars in same series are rendered in same color.

You can have a data something like this:
double[][] data = { { 3, 0, 0}, {0, 5, 0}, {0, 0, 1} };
This will reneder each bar in different color.

I hope this helps.

Jim D
Posts: 8
Joined: Wed Sep 10, 2003 6:31 am
Location: Silicon Valley CA
Contact:

different colors for each bar

Post by Jim D » Tue Nov 25, 2003 6:55 am

You can also override the renderer and set each color individually like this..

Code: Select all

//__________CustomHorizontalBarChartRenderer_____________
	//
	public class CustomHorizontalBarChartRenderer extends BarRenderer
	{
		
		public java.awt.Paint getItemPaint(int row,int column)
		{
			Color color;	
			
			if(column == 0) color = new Color(243,203,150);	
			else if(column == 1) color = new Color(237,177,98);
			else if(column == 2) color = new Color(231,151,45);
			else if(column == 3) color = new Color(162,150,85);
			else color = new Color(0,0,0);
			
			return color;
		}
Then, in the part of code where you are customizing the chart..

Code: Select all


// Set custom renderer which will custom draw the bar colors
plot.setRenderer( new CustomHorizontalBarChartRenderer());
The 'colum' value represents each bar in a given category. so you would need to set the color for as many bars as you expect to have. In the above example, I have four.

Hope that helps..

Jim

CaymanBeach
Posts: 2
Joined: Fri Jul 24, 2020 5:55 pm
antibot: No, of course not.

Re: Different color for each bar

Post by CaymanBeach » Thu Aug 26, 2021 2:18 am

After implementing this trick (separate series for positive and negative values), I discovered a much simpler way with more recent (1.15.x) versions of JFreeChart:

Code: Select all

		XYBarRenderer obvRenderer = new XYBarRenderer()
		{
			@Override
			public Paint getItemPaint(int series, int item)
			{
				if ( ((XYDataset)getPlot().getDataset()).getYValue(series, item) < 0. )
					return Color.RED;
				else
					return Color.GREEN;
			}
		};
Simply override getItemPaint and in the over-ridden method put the criteria to change color (I just wanted to differentiate between positive and negative numbers, and color the bar accordingly).

Locked