BarChart : shift domain axis legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
xtof05
Posts: 3
Joined: Thu Apr 30, 2015 1:42 pm
antibot: No, of course not.

BarChart : shift domain axis legend

Post by xtof05 » Thu Apr 30, 2015 1:56 pm

Hi,

I currently have the following graph:

Image

What I would like to do is to shift the legends on the domain axis, like that:

Image

I searched around the web, but I couldn't find how to do this. Is it even possible?

Thank you!

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: BarChart : shift domain axis legend

Post by paradoxoff » Fri May 01, 2015 2:07 pm

Get a reference to your chart, and put these lines in your code:

Code: Select all

CategoryAxis a = chart.getCategoryPlot().getDomainAxis();
CategoryLabelPositions positions = a.getCategoryLabelPositions();
CategoryLabelPosition newBottom = new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.BOTTOM_LEFT);
CategoryLabelPositions newPositions = CategoryLabelPositions.replaceBottomPosition(positions, newBottom);
a.setCategoryLabelPositions(newPositions);

xtof05
Posts: 3
Joined: Thu Apr 30, 2015 1:42 pm
antibot: No, of course not.

Re: BarChart : shift domain axis legend

Post by xtof05 » Mon May 04, 2015 10:30 am

Thank you very much for your answer. This gives me the following:

Image

Is there a way to shift them even more to the left, so that they are centered below the vertical lines of the bars?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: BarChart : shift domain axis legend

Post by paradoxoff » Mon May 04, 2015 11:45 am

Replace TextBlockAnchor.BOTTOM_LEFT with one of the following and see which one looks best:
TextBlockAnchor.BOTTOM_CENTER, TextBlockAnchor.CENTER,TextBlockAnchor.TOP_CENTER

xtof05
Posts: 3
Joined: Thu Apr 30, 2015 1:42 pm
antibot: No, of course not.

Re: BarChart : shift domain axis legend

Post by xtof05 » Mon May 04, 2015 2:46 pm

TextBlockAnchor.BOTTOM_CENTER looks perfect, thanks! :D

Locked