Get X and Y Labels from a JFreeChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
pooo
Posts: 9
Joined: Tue Jul 13, 2010 2:04 pm
antibot: No, of course not.

Get X and Y Labels from a JFreeChart

Post by pooo » Tue Jul 13, 2010 2:29 pm

Hello,

I was wondering if it would be possible to get the XLabel and YLabel from a JFreeChart object. I searched the Javadoc and saw that I can get the Title of the chart, or even the Legend items, but I can't figure how to get the axis labels. I know that these labels only exist in some types of charts, such as bar charts, and I assume this is the reason why there is no getXLabel() method in the JFreeChart class. Nevertheless, it must be stored somewhere.

I am using JFreeChart v1.0.13 and JDK v1.6.0_21.

Thank you for your time.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: Get X and Y Labels from a JFreeChart

Post by skunk » Tue Jul 13, 2010 3:42 pm

Depends on which labels you are looking for. First, get a reference to the axis by calling plot.getDomainAxis(...) or plot.getRangeAxis(...)
The base class org.jfree.chart.axis.Axis defines this method

Code: Select all

public String getLabel()
If you are looking for the actual tick labels, call this method

Code: Select all

public abstract java.util.List refreshTicks(java.awt.Graphics2D g2,
                                            AxisState state,
                                            java.awt.geom.Rectangle2D dataArea,
                                            org.jfree.ui.RectangleEdge edge)
Each element of the returned List is an instance of org.jfree.chart.axis.Tick

pooo
Posts: 9
Joined: Tue Jul 13, 2010 2:04 pm
antibot: No, of course not.

Re: Get X and Y Labels from a JFreeChart

Post by pooo » Thu Jul 15, 2010 8:10 am

Thank you ! Problem solved ! :D

For future reference, this is the exact code I used :

Code: Select all

JFreeChart chart = ChartFactory.createBarChart3D(...);

...

String xLabel = chart.getCategoryPlot().getDomainAxis().getLabel();
String yLabel = chart.getCategoryPlot().getRangeAxis().getLabel();

Locked