how to customize the color of a bar chart?

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

how to customize the color of a bar chart?

Post by davidwkw » Tue Oct 26, 2004 6:15 pm

I have created a bar chart with serveral categories. I want to indicate one of the categories with different color. How can I change the color of each bar and not using the default colors?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Oct 26, 2004 11:06 pm

The only way to do this at present is to subclass the BarRenderer class and override the getItemPaint() method.
David Gilbert
JFreeChart Project Leader

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

Guest

Post by Guest » Fri Oct 29, 2004 6:03 pm

No, you must overide the PaintList class, it worked for much, safe that plot line, I got the same red color 2 times , while in the other graphs I got different colors...

pstevlingson
Posts: 2
Joined: Fri Nov 12, 2004 5:00 pm

Post by pstevlingson » Mon Nov 15, 2004 6:35 pm

Thanks for the tip Dave.... here is my implementation

import org.jfree.chart.renderer.BarRenderer;

import java.awt.*;

public class BarCategoryRenderer extends BarRenderer {

Paint paintArray[][];

public Paint getItemPaint(int row, int column) {
return paintArray[row][column];
}

public void setItemPaint(Paint paint, int row, int column) throws Exception {
if( paintArray.length ==0) {
throw new Exception("You must call setPaintListSize first.");
}
paintArray[row][column]=paint;
}

public void setPaintListSize(int rowCount, int columnCount,Paint defaultPaint) {
paintArray = new Paint[rowCount][columnCount];
for(int row=0; row<rowCount; row++) {
for(int col=0; col<columnCount; col++) {
paintArray[row][col]=defaultPaint;
}
}
}
}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Nov 15, 2004 9:46 pm

Thanks for posting your solution, it should help others looking for something similar. Eventually, I'd like the renderers to provide default support for overriding the colors on a per item basis, but I need to think of a data structure that is more adaptable than the fixed size array you have used. Too many other things to work on right now though...
David Gilbert
JFreeChart Project Leader

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

cris-rj
Posts: 16
Joined: Mon Oct 11, 2004 2:44 pm
Location: rio de janeiro, brazil

Post by cris-rj » Thu Nov 18, 2004 8:55 pm

hi everbody.
I used the code posted by pstevlingson and it didn't work correctly because the parameters of the method getItemPaint are changed.
This only work when I put the column as the 1º parameter and the row as 2º.
[]´s
Cristiano

kumrdh
Posts: 3
Joined: Thu Jan 13, 2005 3:51 pm

Post by kumrdh » Thu Jan 13, 2005 8:32 pm

I tried to compile the attached code and am getting

C:\>javac BarcategoryRenderer.java
BarcategoryRenderer.java:4: cannot access org.jfree.util.PublicCloneable
file org\jfree\util\PublicCloneable.class not found
public class BarCategoryRenderer extends BarRenderer {
^
1 error

any idea why?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Jan 13, 2005 10:51 pm

Make sure JCommon is in your classpath.
David Gilbert
JFreeChart Project Leader

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

sanyukta

hi

Post by sanyukta » Mon Jan 17, 2005 5:13 am

I am using the same code but it gives me following error:

java.lang.NoSuchMethodError: main
Exception in thread "main"

PLease help
regards
san

sreenivasarao

Re:how to customize the color of a bar chart?

Post by sreenivasarao » Tue Jan 18, 2005 11:34 am

I hope these methods(Jfreechart API calls) will helpful to customize colors of the bar chart and pie chart

JFreeChart jfreechart = ChartFactory.createBarChart("", "Months", "%", dataset, PlotOrientation.VERTICAL, true,false, false);
jfreechart.setBackgroundPaint(Color.WHITE);
jfreechart.getLegend().setAnchor(3);
CategoryPlot categoryplot = jfreechart.getCategoryPlot();
BarRenderer bar = new BarRenderer();
bar.setItemMargin(0); //reduce the width between the bars.
bar.setSeriesPaint(0,new Color(153,153,255)); //first bar
bar.setSeriesPaint(1,new Color(153,51,102)); // second bar
categoryplot.setRenderer(bar);
categoryplot.setBackgroundPaint(new Color(192, 192, 192));

for pie charts:
JFreeChart jfreechart = ChartFactory.createPieChart("",piedataset, true, false, false);
jfreechart.setBackgroundPaint(Color.WHITE);
PiePlot pieplot = (PiePlot)jfreechart.getPlot();
pieplot.setLabelFont(new Font("Verdana", 0, 12));
pieplot.setNoDataMessage("No data available");
pieplot.setCircular(true);
pieplot.setLabelGap(0.02D);
pieplot.setDirection(Rotation.CLOCKWISE);
pieplot.setStartAngle(0);
pieplot.setBackgroundPaint(new Color(255,255,255));
pieplot.setSectionPaint(0,Color.RED);
pieplot.setSectionPaint(1,Color.YELLOW);
pieplot.setSectionPaint(2,Color.GREEN);

for area charts(scatter diagrams):

JFreeChart jfreechart = ChartFactory.createXYAreaChart("", "Quality Performance", "Delivery Performance", createDatasetForScatter(), PlotOrientation.VERTICAL, true, false, false);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
xyplot.setBackgroundPaint(Color.WHITE);
XYItemRenderer item = xyplot.getRenderer();
item.setSeriesPaint(0,Color.RED);
item.setSeriesPaint(1,Color.YELLOW);
item.setSeriesPaint(2,Color.GREEN);
xyplot.setRenderer(item);

stan

Code sample..

Post by stan » Tue Jan 18, 2005 5:19 pm

In reference to this code sample:

bar.setSeriesPaint(0,new Color(153,153,255)); //first bar
bar.setSeriesPaint(1,new Color(153,51,102)); // second bar

The API docs say this:

setSeriesPaint(int series, java.awt.Paint paint)

So was that code sample incorrect?


Stan

stan

oops

Post by stan » Tue Jan 18, 2005 5:20 pm

I didn't realise Color implemented Paint... ;) What a schoolboy error.

Stan

Guest

Post by Guest » Wed Jan 26, 2005 3:10 pm

and how to change the transparent on the bar chart..to look like a 3d pie..

Locked