how to customize the color of a bar chart?
how to customize the color of a bar chart?
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?
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
The only way to do this at present is to subclass the BarRenderer class and override the getItemPaint() method.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- Posts: 2
- Joined: Fri Nov 12, 2004 5:00 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;
}
}
}
}
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;
}
}
}
}
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Make sure JCommon is in your classpath.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Re:how to customize the color of a bar chart?
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);
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);
Code sample..
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
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