Andrew E. Bruno

A sourceful of secrets

Rotate Labels JFreeChart

with 15 comments

When creating a chart that has rather long labels for the x-axis it is sometimes desirable to rotate them a bit so they fit on the plot. The method to use is setCategoryLabelPositions(..) on the CategoryAxis class. Here’s a quick example:

And the code..

import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartColor;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.data.category.DefaultCategoryDataset;

public class RotateLabels {
    public static void main(String[] args) {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.addValue(51, "series", "Colonel Forbin");
        dataSet.addValue(92, "series", "The Lizards");
        dataSet.addValue(33, "series", "Wilson");
        dataSet.addValue(77, "series", "Rutherford the Brave");
        dataSet.addValue(37, "series", "The Unit Monster");
        dataSet.addValue(97, "series", "The Famous Mockingbird");
        dataSet.addValue(67, "series", "Poster Nutbag");

        JFreeChart chart = ChartFactory.createBarChart(
            "Gamehendge",
            null,
            null,
            dataSet,
            PlotOrientation.VERTICAL,
            false,
            false,
            false
        );

        CategoryPlot plot = (CategoryPlot)chart.getPlot();
        CategoryAxis xAxis = (CategoryAxis)plot.getDomainAxis();
        xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

        chart.setBackgroundPaint(ChartColor.WHITE);
        try {
            ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, 400, 300);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

Advertisement

Written by Andrew

2007/08/14 at 19:06

Posted in Java

15 Responses

Subscribe to comments with RSS.

  1. Thanks for posting this. I was looking for a way to do exactly this.

    Matt

    2007/09/24 at 06:48

  2. GREAT !!! THANKS !!!

    Felippe

    2008/05/15 at 06:01

  3. Very useful. Thank you ;)

    Andrea

    2008/06/06 at 01:03

  4. Thanks alot! This was very helpful.

    Robin

    2008/07/20 at 02:58

  5. Thanks! Just found this a used it!

    Bob

    2008/08/22 at 12:47

  6. you bailed me out with this post!!! cheers

    Shamanth

    2008/08/28 at 16:12

  7. Great! Just what I was looking for. Long Live Google and You :)

    Saach

    2009/01/14 at 17:15

  8. Great tip! Thanks a lot!!!

    Eduardo Costa

    2009/08/11 at 12:52

  9. You saved my life brother! Thanks a lot!

    Thom

    2009/10/16 at 13:55

  10. Thanks a lot for this! I’ve been looking a longgg time for this! JFreeChart is powerful, but it’s so damn complicated at times :)

    Dang

    2009/10/21 at 15:36

  11. Very helpful. Thanks,

    Googling “jfreechart axis label rotation” returned this as the top hit.

    J

    2010/09/22 at 10:39

  12. Is there any way to rotate labels for subCategory axis in jfreechart ?

    Sam anderson

    2011/08/26 at 15:04

  13. Hi Andrew,

    You’re post helped big time but that was in my bar chart. Now I’m making a time series chart and I just can’t find a way through to rotate the dates ! I can only rotate the domain axis label.

    I will deeply appreciate your assistance with this one :)

    Muhammad

    2011/09/14 at 13:19

  14. Thanks for help :-)

  15. I was looking for this. Thanks!

    Sivaramakrishanan

    2011/12/28 at 06:22


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.