Skip to content

Commit

Permalink
ttools: modify tick calculation interfaces
Browse files Browse the repository at this point in the history
The various tick calculation methods now pass around the Captioner and
Orientation objects that determine how the tick labels will actually
be painted on the plotting surface.  This doesn't do any useful work yet,
but it means that implementations have the information necessary to
work out whether tick labels will overlap, and adjust the assigned
ticks accordingly if required.
  • Loading branch information
mbtaylor committed Oct 15, 2013
1 parent 3259bd6 commit db3e766
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 67 deletions.
8 changes: 5 additions & 3 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/ShadeAxis.java
Expand Up @@ -27,6 +27,7 @@ public class ShadeAxis {
private final double dhi_;
private final String label_;
private final Captioner captioner_;
private static final Orientation ORIENTATION = Orientation.ANTI_Y;

/**
* Constructor.
Expand Down Expand Up @@ -105,7 +106,8 @@ public boolean isFlip() {
*/
private ShaderIcon createShaderAxisIcon( Rectangle rampBounds ) {
Tick[] ticks =
Tick.getTicks( dlo_, dhi_, rampBounds.height, log_, false, 2 );
Tick.getTicks( dlo_, dhi_, log_, false, captioner_, ORIENTATION,
rampBounds.height, 2 );
return new ShaderIcon( shader_, log_, flip_, dlo_, dhi_, label_,
captioner_, rampBounds, ticks );
}
Expand Down Expand Up @@ -176,7 +178,7 @@ public void paintIcon( Component c, Graphics g, int x, int y ) {
g2.drawRect( box_.x, box_.y, box_.width, box_.height );
g2.translate( box_.x + box_.width, box_.y + box_.height );
g2.rotate( - Math.PI / 2 );
axis_.drawLabels( ticks_, label_, captioner_, Orientation.ANTI_Y,
axis_.drawLabels( ticks_, label_, captioner_, ORIENTATION,
false, g2 );
g2.setColor( color0 );
g2.setTransform( trans0 );
Expand All @@ -190,7 +192,7 @@ public void paintIcon( Component c, Graphics g, int x, int y ) {
public Insets getInsets() {
Rectangle bounds =
axis_.getLabelBounds( ticks_, label_, captioner_,
Orientation.ANTI_Y, false );
ORIENTATION, false );
bounds = AffineTransform.getRotateInstance( - Math.PI / 2 )
.createTransformedShape( bounds )
.getBounds();
Expand Down
44 changes: 14 additions & 30 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/Tick.java
Expand Up @@ -87,42 +87,26 @@ public String toString() {

/**
* Generates a tick array suitable for labelling a plot axis.
* This convenience method just uses <code>crowding</code> to
* produce a not-very-sophisticated guess for <code>approxMajorCount</code>
* and invokes the other <code>getTicks</code> method.
*
* @param dlo minimum axis data value
* @param dhi maximum axis data value
* @param npix number of pixels along the axis
* @param log true for logarithmic scaling, false for linear
* @param dlo minimum axis data value
* @param dhi maximum axis data value
* @param log true for logarithmic scaling, false for linear
* @param withMinor if true minor axes are included,
* if false only major (labelled) ones are
* @param crowding 1 for normal tick density on the axis,
* lower for fewer labels, higher for more
* @return tick array
*/
public static Tick[] getTicks( double dlo, double dhi, int npix,
boolean log, boolean withMinor,
double crowding ) {
int nt0 = 1 + (int) Math.round( crowding * npix / 100 );
return getTicks( dlo, dhi, log, nt0, withMinor );
}

/**
* Generates a tick array suitable for labelling a plot axis.
*
* @param dlo minimum axis data value
* @param dhi maximum axis data value
* @param log true for logarithmic scaling, false for linear
* @param approxMajorCount approximate number of major ticks required
* @param withMinor if true minor ticks are included,
* if false only major (labelled) ones are
* @param captioner caption painter
* @param orient label orientation
* @param npix number of pixels along the axis
* @param crowding 1 for normal tick density on the axis,
* lower for fewer labels, higher for more
* @return tick array
*/
public static Tick[] getTicks( double dlo, double dhi, boolean log,
int approxMajorCount, boolean withMinor ) {
return log ? labelLogAxis( dlo, dhi, approxMajorCount, withMinor )
: labelLinearAxis( dlo, dhi, approxMajorCount, withMinor );
boolean withMinor,
Captioner captioner, Orientation orient,
int npix, double crowding ) {
int nt0 = 1 + (int) Math.round( crowding * npix / 100 );
return log ? labelLogAxis( dlo, dhi, nt0, withMinor )
: labelLinearAxis( dlo, dhi, nt0, withMinor );
}

/**
Expand Down
Expand Up @@ -62,6 +62,8 @@ public class CubeSurface implements Surface {
private final double[] dScales_;
private final double[] dOffs_;

private static final Orientation ORIENTATION = Orientation.X;

/**
* Constructor.
*
Expand Down Expand Up @@ -807,8 +809,8 @@ private void drawFrameAxis( Graphics g, double[] dpos0, double[] dpos1 ) {
Axis ax = Axis.createAxis( 0, sx, dlos_[ iaxis ], dhis_[ iaxis ],
logFlags_[ iaxis ],
( ! forward ) ^ flipFlags_[ iaxis ] );
ax.drawLabels( ticks_[ iaxis ], labels_[ iaxis ], captioner_,
Orientation.X, false, g2 );
ax.drawLabels( ticks_[ iaxis ], labels_[ iaxis ],
captioner_, ORIENTATION, false, g2 );
g2.setTransform( atf0 );
}

Expand Down Expand Up @@ -876,9 +878,9 @@ public static CubeSurface createSurface( Rectangle plotBounds,
for ( int i = 0; i < 3; i++ ) {
dlos[ i ] = limits[ i ][ 0 ];
dhis[ i ] = limits[ i ][ 1 ];
ticks[ i ] = Tick.getTicks( dlos[ i ], dhis[ i ], npix,
logFlags[ i ], minor,
crowdFactors[ i ] );
ticks[ i ] = Tick.getTicks( dlos[ i ], dhis[ i ], logFlags[ i ],
minor, captioner, ORIENTATION,
npix, crowdFactors[ i ] );
}
double[] rotmat = aspect.getRotation();
double zoom = aspect.getZoom();
Expand Down
Expand Up @@ -36,8 +36,8 @@ public class PlaneAxisAnnotation implements AxisAnnotation {
private final int yoff_;

public static final boolean INVERT_Y = true;
private static final Orientation X_ORIENT = Orientation.X;
private static final Orientation Y_ORIENT = Orientation.Y;
public static final Orientation X_ORIENT = Orientation.X;
public static final Orientation Y_ORIENT = Orientation.Y;

/**
* Constructor.
Expand Down
20 changes: 12 additions & 8 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/geom/PlaneSurface.java
Expand Up @@ -341,10 +341,12 @@ public static PlaneSurface createSurface( Rectangle plotBounds,
double dxhi = aspect.getXMax();
double dylo = aspect.getYMin();
double dyhi = aspect.getYMax();
Tick[] xticks =
Tick.getTicks( dxlo, dxhi, plotBounds.width, xlog, minor, xcrowd );
Tick[] yticks =
Tick.getTicks( dylo, dyhi, plotBounds.height, ylog, minor, ycrowd );
Tick[] xticks = Tick.getTicks( dxlo, dxhi, xlog, minor,
captioner, PlaneAxisAnnotation.X_ORIENT,
plotBounds.width, xcrowd );
Tick[] yticks = Tick.getTicks( dylo, dyhi, ylog, minor,
captioner, PlaneAxisAnnotation.Y_ORIENT,
plotBounds.height, ycrowd );

/* Fixed ratio of X/Y data scales. Interpret this by ensuring that
* all of both requested data ranges is included, and one of them is
Expand Down Expand Up @@ -382,10 +384,12 @@ public static PlaneSurface createSurface( Rectangle plotBounds,
dx = log ? Math.log( dxhi / dxlo ) : dxhi - dxlo;
dy = log ? Math.log( dyhi / dylo ) : dyhi - dylo;
assert Math.abs( xyfactor * ( gy / dy ) / ( gx / dx ) - 1 ) < 1e-6;
xticks =
Tick.getTicks( dxlo, dxhi, plotBounds.width, xlog, minor, 1 );
yticks =
Tick.getTicks( dylo, dyhi, plotBounds.height, ylog, minor, 1 );
xticks = Tick.getTicks( dxlo, dxhi, xlog, minor,
captioner, PlaneAxisAnnotation.X_ORIENT,
plotBounds.width, 1 );
yticks = Tick.getTicks( dylo, dyhi, ylog, minor,
captioner, PlaneAxisAnnotation.Y_ORIENT,
plotBounds.height, 1 );
}
return new PlaneSurface( gxlo, gxhi, gylo, gyhi, dxlo, dxhi, dylo, dyhi,
xlog, ylog, xflip, yflip, xticks, yticks,
Expand Down
45 changes: 30 additions & 15 deletions ttools/src/main/uk/ac/starlink/ttools/plot2/geom/TimeFormat.java
Expand Up @@ -15,7 +15,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import uk.ac.starlink.ttools.func.Times;
import uk.ac.starlink.ttools.plot2.Captioner;
import uk.ac.starlink.ttools.plot2.Equality;
import uk.ac.starlink.ttools.plot2.Orientation;
import uk.ac.starlink.ttools.plot2.PlotUtil;
import uk.ac.starlink.ttools.plot2.Tick;

Expand Down Expand Up @@ -99,12 +101,18 @@ private TimeFormat( String name ) {
* @param unixSecMin time lower bound in unix seconds
* @param unixSecMax time upper bound in unix seconds
* @param withMinor true if include minor ticks are required
* @param approxMajorCount approximate number of major ticks required
* @param captioner caption painter
* @param orient tick label orientation
* @param npix number of pixels along the axis
* @param crowding 1 for normal tick density on the axis,
* lower for fewer labels, higher for more
* @return array of ticks
*/
public abstract Tick[] getAxisTicks( double unixSecMin, double unixSecMax,
boolean withMinor,
int approxMajorCount );
Captioner captioner,
Orientation orient,
int npix, double crowding );

@Override
public String toString() {
Expand Down Expand Up @@ -225,13 +233,16 @@ else if ( ndp <= 0 ) {
}

public Tick[] getAxisTicks( double unixSecMin, double unixSecMax,
boolean withMinor, int approxMajorCount ) {
boolean withMinor, Captioner captioner,
Orientation orient, int npix,
double crowding ) {

/* Get the Tick class to do the hard work of calcuating the
* labels over a given range for this time scale. */
Tick[] ticks = Tick.getTicks( fromUnixSeconds( unixSecMin ),
fromUnixSeconds( unixSecMax ),
false, approxMajorCount, withMinor );
fromUnixSeconds( unixSecMax ), false,
withMinor, captioner, orient,
npix, crowding );

/* Then convert the data values to be suitable for this format. */
for ( int i = 0; i < ticks.length; i++ ) {
Expand Down Expand Up @@ -298,20 +309,24 @@ public String formatTime( double unixSec, double secPrec ) {
}

public Tick[] getAxisTicks( double unixSecMin, double unixSecMax,
boolean withMinor, int approxMajorCount ) {
boolean withMinor, Captioner captioner,
Orientation orient, int npix,
double crowding ) {

/* Work out the approximate interval in seconds between major
* ticks. */
double secPrec = 2 * ( unixSecMax - unixSecMin ) / approxMajorCount;
double secPrec = 2 * ( unixSecMax - unixSecMin )
/ ( npix / 175. )
/ crowding;

/* If we're dealing with years, treat the values like decimals. */
if ( secPrec > DateLevel.YEAR_SECS ) {
Tick[] ticks =
Tick.getTicks( unixSecondsToDecimalYear( unixSecMin ),
unixSecondsToDecimalYear( unixSecMax ),
false, approxMajorCount,
withMinor
&& secPrec > DateLevel.YEAR_SECS * 10 );
Tick
.getTicks( unixSecondsToDecimalYear( unixSecMin ),
unixSecondsToDecimalYear( unixSecMax ), false,
withMinor && secPrec > DateLevel.YEAR_SECS * 10,
captioner, orient, npix, crowding );
for ( int i = 0; i < ticks.length; i++ ) {
Tick t0 = ticks[ i ];
ticks[ i ] =
Expand All @@ -326,9 +341,9 @@ public Tick[] getAxisTicks( double unixSecMin, double unixSecMax,
else if ( secPrec < 1 ) {
long floor = secLevel_.floorUnixSeconds( unixSecMin );
Tick[] ticks = Tick.getTicks( unixSecMin - floor,
unixSecMax - floor,
false, approxMajorCount,
withMinor );
unixSecMax - floor, false,
withMinor, captioner, orient,
npix, crowding );
for ( int i = 0; i < ticks.length; i++ ) {
Tick t0 = ticks[ i ];
double unixSec = t0.getValue() + floor;
Expand Down
Expand Up @@ -331,13 +331,14 @@ public static TimeSurface createSurface( Rectangle plotBounds,
double dthi = aspect.getTMax();
double dylo = aspect.getYMin();
double dyhi = aspect.getYMax();
int approxMajorTCount =
1 + (int) Math.round( tcrowd * plotBounds.width / 175 );
Tick[] tticks =
tformat.getAxisTicks( aspect.getTMin(), aspect.getTMax(), minor,
approxMajorTCount );
captioner, PlaneAxisAnnotation.X_ORIENT,
plotBounds.width, tcrowd );
Tick[] yticks =
Tick.getTicks( dylo, dyhi, plotBounds.height, ylog, minor, ycrowd );
Tick.getTicks( dylo, dyhi, ylog, minor,
captioner, PlaneAxisAnnotation.Y_ORIENT,
plotBounds.height, ycrowd );
return new TimeSurface( gxlo, gxhi, gylo, gyhi, dtlo, dthi, dylo, dyhi,
ylog, yflip, tticks, yticks, tlabel, ylabel,
captioner, grid, tformat );
Expand Down

0 comments on commit db3e766

Please sign in to comment.