Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Created July 25, 2016 15:35
Show Gist options
  • Save ulmangt/a224df9e8736d8cb9126cce4f875205b to your computer and use it in GitHub Desktop.
Save ulmangt/a224df9e8736d8cb9126cce4f875205b to your computer and use it in GitHub Desktop.
package com.metsci.glimpse.examples.axis;
import com.metsci.glimpse.axis.Axis2D;
import com.metsci.glimpse.axis.listener.mouse.AxisMouseListener2D;
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.layout.GlimpseAxisLayout2D;
import com.metsci.glimpse.layout.GlimpseLayout;
import com.metsci.glimpse.layout.GlimpseLayoutProvider;
import com.metsci.glimpse.painter.shape.PointSetPainter;
import com.metsci.glimpse.painter.shape.PolygonPainter;
import com.metsci.glimpse.plot.MultiAxisPlot2D;
/**
* Example demonstrating how to set up MultiAxisPlot to display data on two
* different sets of axes.
*
* @author ulman
*/
public class SimpleMultiAxisPlotExample implements GlimpseLayoutProvider
{
public static void main( String[] args ) throws Exception
{
Example.showWithSwing( new SimpleMultiAxisPlotExample( ) );
}
@Override
public GlimpseLayout getLayout( ) throws Exception
{
final MultiAxisPlot2D plot = new MultiAxisPlot2D( );
// Add some axes to the plot
plot.createAxisBottom( "b1" );
plot.createAxisRight( "r1" );
plot.createAxisLeft( "l1" );
// Create a new Axis2D from the two Axis1D we wish to use
// Then create a GlimpseAxisLayout2D using the Axis2D
GlimpseAxisLayout2D layout1 = new GlimpseAxisLayout2D( new Axis2D( plot.getAxis( "b1" ), plot.getAxis( "r1" ) ) );
// Add our layout, which draws according to the bounds of axes "b1" and "r1"
// to the center of the plot
plot.getLayoutCenter( ).addLayout( layout1 );
// Do the same thing for another layout, which draws according to different axes
GlimpseAxisLayout2D layout2 = new GlimpseAxisLayout2D( new Axis2D( plot.getAxis( "b1" ), plot.getAxis( "l1" ) ) );
plot.getLayoutCenter( ).addLayout( layout2 );
// By default, placing layout1 and layout2 in the same container layout (plot.getLayoutCenter( ))
// will cause them to be laid out side-by-side. We use custom mig-layout constraints here
// to lay them out on top of one another (http://www.miglayout.com/cheatsheet.html)
layout1.setLayoutData( "pos container.x container.y container.x2 container.y2" );
layout2.setLayoutData( "pos container.x container.y container.x2 container.y2" );
// Add listeners to each layout to adjust the axis constraints based on user clicks/drags
// This will cause the axes to move together when the user drags on the plot, but move
// separately when the user drags on the individual axes.
layout1.addGlimpseMouseAllListener( new AxisMouseListener2D( ) );
layout2.addGlimpseMouseAllListener( new AxisMouseListener2D( ) );
// Tell the layouts to not consume mouse events so that events reach both layouts
layout1.setEventConsumer( false );
layout2.setEventConsumer( false );
float[] x = new float[] { 0, 0, 1 };
float[] y = new float[] { 0, 1, 0 };
// Create a painter to draw something onto one of the layouts
PolygonPainter p1 = new PolygonPainter( );
p1.addPolygon( 0, 0, x, y, 0 );
layout1.addPainter( p1 );
// Create another painter to draw something onto one of the other layouts
PointSetPainter p2 = new PointSetPainter( false );
p2.setData( x, y );
p2.setPointColor( 0, 0, 0, 1 );
layout2.addPainter( p2 );
return plot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment