Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Last active December 26, 2015 01:49
Show Gist options
  • Save ulmangt/7074289 to your computer and use it in GitHub Desktop.
Save ulmangt/7074289 to your computer and use it in GitHub Desktop.
Demonstrates creating a custom GridAxisLabelHandler to customize how tick marks appear on a Y axis.
import com.metsci.glimpse.axis.Axis1D;
import com.metsci.glimpse.axis.painter.label.GridAxisLabelHandler;
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.plot.timeline.StackedTimePlot2D;
import com.metsci.glimpse.plot.timeline.layout.TimePlotInfo;
import com.metsci.glimpse.support.settings.OceanLookAndFeel;
public class CustomAxisTicksExample extends CollapsibleTimelinePlotExample
{
public static void main( String[] args ) throws Exception
{
Example example = Example.showWithSwing( new CustomAxisTicksExample( ) );
// use the ocean look and feel
example.getCanvas( ).setLookAndFeel( new OceanLookAndFeel( ) );
}
@Override
public StackedTimePlot2D getLayout( )
{
StackedTimePlot2D plot = super.getLayout( );
// get a reference to one of the plots
TimePlotInfo plotInfo = plot.createTimePlot( );
// create a custom AxisLabelHandler which always returns ticks at an interval of 10
// regardless of the axis bounds
GridAxisLabelHandler tickHandler = new GridAxisLabelHandler( )
{
@Override
public double[] getTickPositions( Axis1D axis )
{
// round the start and end bounds of the axis to the nearest 10
double start = Math.floor( axis.getMin( ) / 10.0 ) * 10.0;
double end = Math.ceil( axis.getMax( ) / 10.0 ) * 10.0;
// calculate the number of ticks between start and end
int tickCount = ( int ) ( ( end - start ) / 10.0 );
double[] ticks = new double[tickCount];
// fill the ticks array
for ( int i = 0; i < tickCount; i++ )
{
ticks[i] = start + i * 10.0;
}
return ticks;
}
};
// set the new AxisLabelHandler
// NOTE: this call is not yet available, it will be included in the next Glimpse release
plotInfo.getAxisPainter( ).setLabelHandlerY( tickHandler );
return plot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment