Changing the colors of a bar chart in JFreeChart – Does it really have to be this much work?
The default colors of the first two bars in a bar chart are red and blue. We created a chart for a customer, but he wanted red and green bars. Changing that color takes about five minutes, I thought. But when I tried it, it took me way more time.
First of all I think that JFreeChart should rename itself to JNotSoFreeChart, when I look for documentation I only see this : "> Click here to BUY the JFreeChart Developer Guide <". Of course it’s great that we can use JFreeChart for free, but at least provide a little bit of documentation and write a book that people can buy in a store. AMIS wil definitely buy that book.

I hooked up the debugger to our chart drawing class and found out that the colors come from an object that implements the DrawingSupplier interface. I can’t find a way to create such an object, so I had to create one myself.
As we can see in the javadoc of DrawingSupplier we have to implement 5 methods. For a simple bar chart the outline, outline stroke, shape and stroke will always be the same, so we can use static fields for these objects.
My implementation of DrawingSupplier:
<code><br /><font color="#7f0055"><strong>public class </strong></font><font color="#000000">AmisDrawingSupplier </font><font color="#7f0055"><strong>implements </strong></font><font color="#000000">DrawingSupplier </font><font color="#000000">{</font><br /><br /><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>private static </strong></font><font color="#000000">Stroke stroke = </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">BasicStroke</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>private static </strong></font><font color="#000000">Shape shape = </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">Rectangle2D.Double</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>private </strong></font><font color="#7f0055"><strong>int </strong></font><font color="#000000">cursor = </font><font color="#990000">0</font><font color="#000000">;</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>private </strong></font><font color="#000000">List<Color> colorList;</font><br /><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>public </strong></font><font color="#000000">LocatusDrawingSupplier</font><font color="#000000">(</font><font color="#000000">List<Color> colorList</font><font color="#000000">) {</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>this</strong></font><font color="#000000">.colorList = colorList;</font><br /><font color="#ffffff"> </font><font color="#000000">}</font><br /><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>public </strong></font><font color="#000000">Paint getNextPaint</font><font color="#000000">() {</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>if </strong></font><font color="#000000">(</font><font color="#000000">colorList == </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">|| colorList.size</font><font color="#000000">() </font><font color="#000000">== </font><font color="#990000">0</font><font color="#000000">) {</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>return </strong></font><font color="#000000">Color.RED; </font><font color="#3f7f5f">//return red on empty or no list</font><br /><font color="#ffffff"> </font><font color="#000000">}</font><br /><br /><font color="#ffffff"> </font><font color="#000000">Color returnColor=colorList.get</font><font color="#000000">(</font><font color="#000000">cursor</font><font color="#000000">)</font><font color="#000000">;</font><br /><br /><font color="#ffffff"> </font><font color="#000000">cursor++;</font><br /><br /><font color="#ffffff"> </font><font color="#3f7f5f">//wrap cursor when all items in the list are traversed</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>if </strong></font><font color="#000000">(</font><font color="#000000">cursor >= colorList.size</font><font color="#000000">()) {</font><br /><font color="#ffffff"> </font><font color="#000000">cursor = </font><font color="#990000">0</font><font color="#000000">;</font><br /><font color="#ffffff"> </font><font color="#000000">}</font><br /><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>return </strong></font><font color="#000000">returnColor;</font><br /><font color="#ffffff"> </font><font color="#000000">}</font><br /><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>public </strong></font><font color="#000000">Paint getNextOutlinePaint</font><font color="#000000">() { </font><font color="#7f0055"><strong>return </strong></font><font color="#000000">Color.BLACK; </font><font color="#000000">}</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>public </strong></font><font color="#000000">Stroke getNextStroke</font><font color="#000000">() { </font><font color="#7f0055"><strong>return </strong></font><font color="#000000">stroke; </font><font color="#000000">}</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>public </strong></font><font color="#000000">Stroke getNextOutlineStroke</font><font color="#000000">() { </font><font color="#7f0055"><strong>return </strong></font><font color="#000000">stroke; </font><font color="#000000">}</font><br /><font color="#ffffff"> </font><font color="#7f0055"><strong>public </strong></font><font color="#000000">Shape getNextShape</font><font color="#000000">() { </font><font color="#7f0055"><strong>return </strong></font><font color="#000000">shape; </font><font color="#000000">}</font><br /><font color="#000000">}</font></code>
I decided to give my DrawingSupplier a list with colors, when you prefer an array or random colors that’s also fine.
The final step is hooking up the DrawingSupplier to your chart:
<code><br /><font color="#000000">JFreeChart chart = ... create chart here ...</font><br /><font color="#000000">List<Color> colorList = .. create list with colors here ...</font><br /><font color="#000000">CategoryPlot cp = chart.getCategoryPlot</font><font color="#000000">()</font><font color="#000000">;</font><br /><font color="#000000">DrawingSupplier ds = </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">AmisDrawing Supplier</font><font color=" #000000">(</font><font color="#000000">colorList</font><font color="#000000">)</font><font color="#000000">;</font><br /><font color="#000000">cp.setDrawingSupplier</font><font color="#000000">(</font><font color="#000000">ds</font><font color="#000000">)</font><font color="#000000">;</font></code>
And here the final result:

Conclusion
Well, that wasn’t to difficult wasn’t it?, it’s only too much work for such a simple thing.
Maybe I overlooked something, but I’m afraid this is the easiest way to change the color of a bar.
Related posts:
- Using Lucene with Spring – Introduction to Spring Modules
- Made to order: Eirik's Hi-Lo Chart in SQL – extending the SQL Chart Palette (Pie, Stacked Bar Chart, Gauge, Gantt,…)
- Generating SVG Graphics in JSPs using JSTL & XSL(T) – from MySQL to Bar Chart and Pie Chart
- Using jQuery, the jQuery Form plugin and Stripes to create pretty Javascript and Java
- SUSE support for next release of Oracle
But isn’t that the mantra of the Java World? Make things hard for things that should be much simpler.
How to read a file?