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
DateAxis and DateLabel rotate
-
- Posts: 6
- Joined: Thu Jul 17, 2008 3:57 pm
- Location: Salt Lake City, Utah
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.
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.
That worked for me.
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;
}
Code: Select all
JFreeChart chart = ...;
XYPlot plot = chart.getXYPlot();
plot.setDomainAxis(new TmxDateAxis());
DateAxis axis = (DateAxis)plot.getDomainAxis();
axis.setLabelAngle(-45);
axis.setVerticalTickLabels(true);
Travis
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?
Perhaps David can incorporate this in the next version and provide a seperate setter for the tick label angles?
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.
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.