Rotate Labels JFreeChart
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
Thanks for posting this. I was looking for a way to do exactly this.
Matt
2007/09/24 at 06:48
GREAT !!! THANKS !!!
Felippe
2008/05/15 at 06:01
Very useful. Thank you ;)
Andrea
2008/06/06 at 01:03
Thanks alot! This was very helpful.
Robin
2008/07/20 at 02:58
Thanks! Just found this a used it!
Bob
2008/08/22 at 12:47
you bailed me out with this post!!! cheers
Shamanth
2008/08/28 at 16:12
Great! Just what I was looking for. Long Live Google and You :)
Saach
2009/01/14 at 17:15
Great tip! Thanks a lot!!!
Eduardo Costa
2009/08/11 at 12:52
You saved my life brother! Thanks a lot!
Thom
2009/10/16 at 13:55
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
Very helpful. Thanks,
Googling “jfreechart axis label rotation” returned this as the top hit.
J
2010/09/22 at 10:39
Is there any way to rotate labels for subCategory axis in jfreechart ?
Sam anderson
2011/08/26 at 15:04
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
Thanks for help :-)
lonelinessknowsmebyname
2011/12/04 at 18:16
I was looking for this. Thanks!
Sivaramakrishanan
2011/12/28 at 06:22