Skip to content

Commit

Permalink
ttools: rewrite tick generation
Browse files Browse the repository at this point in the history
Axis major tick labelling is now done in a more modular and extensible way,
and also in a way which is sensitive to the captioner and orientation.
This means that tick labels are generated in such a way that they will
not overlap, rather than generating them and weeding out overlapping ones
more or less at random.  In this way the visible ticks form a more
logical set.

This is done for linear, log and time axis labelling.
  • Loading branch information
mbtaylor committed Oct 21, 2013
1 parent b4cd800 commit 57ac36c
Show file tree
Hide file tree
Showing 11 changed files with 1,458 additions and 718 deletions.
27 changes: 4 additions & 23 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/Axis.java
Expand Up @@ -134,7 +134,6 @@ private Rectangle calculateLabels( Tick[] ticks, String title,

/* Place ticks. */
int tickUnit = orient.isDown() ? -2 : +2;
Rectangle lastBox = null;
for ( int it = 0; it < ticks.length; it++ ) {
Tick tick = ticks[ it ];
String label = tick.getLabel();
Expand All @@ -149,38 +148,20 @@ private Rectangle calculateLabels( Tick[] ticks, String title,
label == null ? null
: orient.captionTransform( cbounds, cpad );

/* Work out the extent of the label and add them to the
* bounding box. If this label is so close to the last one
* that the text overlaps, just don't draw it.
* This isn't perfect, since you may end up with an irregular
* grid of labelled ticks, but it's fairly comprehensible.
* To do it better you'd have to check for overlaps when
* calculating ticks in the first place and re-do it
* if any occurred. */
final boolean overlap;
/* Update bounding box for tick labels. */
if ( label != null ) {
Rectangle box = combineTrans( tTrans, oTrans )
.createTransformedShape( cbounds ).getBounds();
overlap = lastBox != null && box.intersects( lastBox );
if ( ! overlap ) {
tickBounds.add( box );
lastBox = box;
}
}
else {
overlap = false;
tickBounds.add( box );
}

/* If we are drawing, draw now. */
if ( hasGraphics ) {
g2.setTransform( combineTrans( trans0, tTrans ) );
if ( label != null ) {
g2.drawLine( 0, -tickUnit, 0, tickUnit );
if ( ! overlap ) {
g2.setTransform( combineTrans( trans0,
tTrans, oTrans ) );
captioner.drawCaption( label, g2 );
}
g2.setTransform( combineTrans( trans0, tTrans, oTrans ) );
captioner.drawCaption( label, g2 );
}
else {
g2.drawLine( 0, 0, 0, tickUnit );
Expand Down

0 comments on commit 57ac36c

Please sign in to comment.