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:


public class AmisDrawingSupplier implements DrawingSupplier {


    private static Stroke stroke = new BasicStroke();
    private static Shape shape = new Rectangle2D.Double();
    private int cursor = 0;
    private List<Color> colorList;

    public LocatusDrawingSupplier(List<Color> colorList) {
        this.colorList = colorList;
    }

    public Paint getNextPaint() {
        if (colorList == null || colorList.size() == 0) {
            return Color.RED; //return red on empty or no list
        }

        Color returnColor=colorList.get(cursor);

        cursor++;

        //wrap cursor when all items in the list are traversed
        if (cursor >= colorList.size()) {
            cursor = 0;
        }

        return returnColor;
    }

    public Paint getNextOutlinePaint() { return Color.BLACK; }
    public Stroke getNextStroke() { return stroke; }
    public Stroke getNextOutlineStroke() { return stroke; }
    public Shape getNextShape() { return shape; }
}

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:


JFreeChart chart = ... create chart here ...
List<Color> colorList = .. create list with colors here ...
CategoryPlot cp = chart.getCategoryPlot();
DrawingSupplier ds = new AmisDrawing
Supplier(colorList);
cp.setDrawingSupplier(ds);

And here the final result:

Changing the colors of a bar chart in JFreeChart - Does it really have to be this much work? jfreechart color

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.

9 Comments

  1. @Neha February 9, 2012
  2. Phil March 2, 2010
  3. Dave Gilbert May 18, 2007
  4. p3t0r May 17, 2007
  5. Jeroen van Wilgenburg May 16, 2007
  6. p3t0r May 16, 2007
  7. Jeroen van Wilgenburg May 16, 2007
  8. p3t0r May 16, 2007
  9. Mark Hatham May 16, 2007