DateAxis and DateLabel rotate

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dvicente72
Posts: 6
Joined: Wed Jun 27, 2007 8:03 am

DateAxis and DateLabel rotate

Post by dvicente72 » Fri Jul 18, 2008 3:02 pm

Hi all,

i used a TimeChart with DateAxis.

But i want to rotate all date labels as can be done with CategoryAxis :

categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.78539816339744828D));


is it possible to do that in standard with DateAxis or i must implement this ?

Thanks

travis_cooper
Posts: 6
Joined: Thu Jul 17, 2008 3:57 pm
Location: Salt Lake City, Utah

Post by travis_cooper » Fri Jul 18, 2008 3:26 pm

I recently had to do this. There is a way to set the angle, but the DateAxis class never looks at that. What I did was to extend the DateAxis class and overrode the refreshTicksHorizontal method, I only have dates on the horizontal axis. Then I call the super method and loop through the list changing the ticks to use the angle that is set in the axis. So the overridden method looks like this.

Code: Select all

/**
	 * @see org.jfree.chart.axis.DateAxis#refreshTicksHorizontal(java.awt.Graphics2D, java.awt.geom.Rectangle2D, org.jfree.ui.RectangleEdge)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {
		List ticks = super.refreshTicksHorizontal(g2, dataArea, edge);
		List ret = new ArrayList();
		for (Tick tick : (List<Tick>)ticks) {
			if (tick instanceof DateTick) {
				DateTick dateTick = (DateTick)tick;
				ret.add(new DateTick(dateTick.getDate(), dateTick.getText(), dateTick.getTextAnchor(), dateTick.getRotationAnchor(), getLabelAngle()));
			} else {
				ret.add(tick);
			}
		}
		return ret;
	}
Then when you create the chart you need to change the axis to be of your new class, and set the angle to what you want, like this.

Code: Select all

JFreeChart chart = ...;
					XYPlot plot = chart.getXYPlot();
					plot.setDomainAxis(new TmxDateAxis());
					DateAxis axis = (DateAxis)plot.getDomainAxis();
					axis.setLabelAngle(-45);
					axis.setVerticalTickLabels(true);
That worked for me.
Travis

dvicente72
Posts: 6
Joined: Wed Jun 27, 2007 8:03 am

Post by dvicente72 » Fri Jul 18, 2008 3:43 pm

thanks a lot for your help

jleech
Posts: 62
Joined: Fri Oct 26, 2007 9:18 pm

Post by jleech » Thu Jul 24, 2008 8:28 pm

That worked for me as well... Good stuff. The only thing I noticed so far was that calling setLabelAngle(-45) also changes the axis label, not just the tick labels. So I just hardcoded my angle in the overrided method.

Perhaps David can incorporate this in the next version and provide a seperate setter for the tick label angles?

jleech
Posts: 62
Joined: Fri Oct 26, 2007 9:18 pm

Post by jleech » Thu Jul 24, 2008 8:46 pm

Also should point out that the units of all the rotation methods in JFreeChart are radians, so you would want -0.25 * Math.PI instead of -45.

Also note that JFreeChart calculates the size of the graph area based on a vertical orientation - so there will be more empty space below the labels the lower the rotation angle. This will also cause long values to get cropped off the left edge, so be careful with font sizes and date formats.

Locked