Bar Chart in Java Swing

Update !

This bar chart compoment has now been moved into the Iceberg Charts project . Please use Iceberg Charts for a more complete charting experience.

I’ve created a little Java component which creates bar graphs. It only displays bars along the Y axis, however the component is very versatile and configurable. You can change its size, fonts, ticks on the Y-axis, and bar width.

Here are some examples of using it :





The code is below. It’s basically three classes: BarChart, Bar and Axis.
A few things to note :

  • It is highly customizable. All the package level fields in BarChart can modified.
  • The Y-Axis can have up to 3 categories of ticks. In the Axis constructor, you just need to set the second and third to zero if you don’t want to used them (See my examples).
  • It is geometrically relative, however you can have problems with it. The key things to know are width and height, and leftOffset, rightOffset, topOffset and bottomOffset. The offsets need to be big enough to accomodate a title with a very large font (again see my examples).

Please let me know if this component has been useful to you. I spent a lot of time creating it, so a little feedback would be very important to me 

Posted in Swing, Swing Components | Tagged , , | 7 Comments

7 Responses to Bar Chart in Java Swing

  1. saurabh says:

    This is a wrong code check it again…………..
    for (Bar bar : bars) gives type mismatch

  2. Anurag says:

    Awesome, it worked like perfect. 2-3 minor misses, but I guess that because of configuration. You needed to add public for Bar and Axis constructor and define the type of arraylist in but that is something anybody can easily do.

    Really appreciate it.

  3. Natasja says:

    I changed the 3 things stated here earlier and my code compiles and runs without errors. There is no graph showing tho, what am I doing wrong? I’m not using an IDE. Any suggestions?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">