Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Last active December 26, 2015 01:49
Show Gist options
  • Save ulmangt/7073787 to your computer and use it in GitHub Desktop.
Save ulmangt/7073787 to your computer and use it in GitHub Desktop.
Demonstrates one approach to adding a simple text annotation to a timeline plot.
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.layout.GlimpseAxisLayout2D;
import com.metsci.glimpse.painter.info.AnnotationPainter;
import com.metsci.glimpse.painter.info.AnnotationPainter.AnnotationFont;
import com.metsci.glimpse.plot.timeline.StackedTimePlot2D;
import com.metsci.glimpse.plot.timeline.layout.TimePlotInfo;
import com.metsci.glimpse.support.color.GlimpseColor;
import com.metsci.glimpse.support.settings.OceanLookAndFeel;
import com.metsci.glimpse.util.units.time.Time;
import com.metsci.glimpse.util.units.time.TimeStamp;
import com.metsci.glimpse.util.units.time.format.TimeStampFormat;
import com.metsci.glimpse.util.units.time.format.TimeStampFormatStandard;
public class LabelPointExample extends CollapsibleTimelinePlotExample
{
protected static final TimeStampFormat TIME_FORMAT = new TimeStampFormatStandard( "%H:%m:%0S Z", "UTC" );
public static void main( String[] args ) throws Exception
{
Example example = Example.showWithSwing( new LabelPointExample( ) );
// 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.getTimePlot( "speed-plot-1-id" );
// get the GlimpseLayout for the plot (to which painters can be added
// in order to add arbitrary graphics to the plot)
GlimpseAxisLayout2D plotLayout = plotInfo.getLayout( );
// use TooltipPainter to display label for data point
AnnotationPainter labelPainter = new AnnotationPainter( );
// add the new painter to the layout
plotLayout.addPainter( labelPainter );
// for example purposes, hard code the time and y coordinate of the label
float y = ( float ) -5.0;
TimeStamp xTimestamp = plot.getEpoch( ).getTimeStamp( ).subtract( Time.fromHours( 1 ) );
float x = ( float ) plot.getEpoch( ).fromTimeStamp( xTimestamp );
// generate text for the labels
String yText = String.format( "Value: %.3f", y );
String xText = String.format( "Time: %s", xTimestamp.toString( TIME_FORMAT ) );
// add labels to the painter by specifying the axis coordinates they should be linked to (x and y)
// as well as the x and y pixel amount they should be offset from that axis coordinate, the color, font,
// and whether they should be centered
labelPainter.addAnnotation( yText, x, y, 10, -10, false, false, AnnotationFont.Helvetical_12, GlimpseColor.getWhite( ) );
labelPainter.addAnnotation( xText, x, y, 10, 4, false, false, AnnotationFont.Helvetical_12, GlimpseColor.getWhite( ) );
return plot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment