Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Created July 25, 2016 17:33
Show Gist options
  • Save ulmangt/21dd09076618e7acb7eda4b75c086f2f to your computer and use it in GitHub Desktop.
Save ulmangt/21dd09076618e7acb7eda4b75c086f2f to your computer and use it in GitHub Desktop.
package com.metsci.glimpse.examples.axis;
import java.util.ArrayList;
import java.util.List;
import com.metsci.glimpse.axis.Axis1D;
import com.metsci.glimpse.axis.painter.label.GridAxisLabelHandler;
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.gl.texture.ColorTexture1D;
import com.metsci.glimpse.layout.GlimpseLayoutProvider;
import com.metsci.glimpse.painter.info.CursorTextZPainter;
import com.metsci.glimpse.painter.texture.HeatMapPainter;
import com.metsci.glimpse.plot.ColorAxisPlot2D;
import com.metsci.glimpse.support.color.GlimpseColor;
import com.metsci.glimpse.support.colormap.ColorGradients;
import com.metsci.glimpse.support.projection.FlatProjection;
import com.metsci.glimpse.support.projection.Projection;
import com.metsci.glimpse.support.texture.FloatTextureProjected2D;
public class CustomAxisTicksExample implements GlimpseLayoutProvider
{
public static void main( String[] args ) throws Exception
{
Example.showWithSwing( new CustomAxisTicksExample( ) );
}
@Override
public ColorAxisPlot2D getLayout( )
{
// create a premade heat map window
ColorAxisPlot2D plot = newPlot( );
// set axis labels and chart title
plot.setTitle( "Heat Map Example" );
plot.setAxisLabelX( "x axis" );
plot.setAxisLabelY( "y axis" );
// set border and offset sizes in pixels
plot.setBorderSize( 30 );
plot.setAxisSizeX( 40 );
plot.setAxisSizeY( 60 );
// set the x, y, and z initial axis bounds
plot.setMinX( 0.0f );
plot.setMaxX( 1000.0f );
plot.setMinY( 0.0f );
plot.setMaxY( 1000.0f );
plot.setMinZ( 0.0f );
plot.setMaxZ( 1000.0f );
plot.setAbsoluteMinY( 0 );
// lock the aspect ratio of the x and y axis to 1 to 1
plot.lockAspectRatioXY( 1.0f );
// set the size of the selection box to 100.0 units
plot.setSelectionSize( 100.0f );
// show minor tick marks on all the plot axes
plot.setShowMinorTicks( true );
plot.setMinorTickCount( 9 );
// create a heat map painter
HeatMapPainter heatmapPainter = newHeatMapPainter( newColorTexture( ), plot.getAxisZ( ) );
// add the painter to the plot
plot.addPainter( heatmapPainter );
// load the color map into the plot (so the color scale is displayed on the z axis)
plot.setColorScale( heatmapPainter.getColorScale( ) );
// create a painter which displays the cursor position and data value under the cursor
// add it to the foreground layer so that it draws on top of the plot data
// this is equivalent to: plot.addPainter( cursorPainter, Plot2D.FOREGROUND_LAYER )
CursorTextZPainter cursorPainter = new CursorTextZPainter( );
plot.addPainterForeground( cursorPainter );
// tell the cursor painter what texture to report data values from
cursorPainter.setTexture( heatmapPainter.getData( ) );
//XXX lock the z axis
plot.getAxisZ( ).setMin( 0.5 );
plot.getAxisZ( ).setMax( 5.5 );
plot.getAxisZ( ).lock( );
return plot;
}
//XXX customize the z axis tick mark positions and labels
protected ColorAxisPlot2D newPlot( )
{
return new ColorAxisPlot2D( )
{
protected GridAxisLabelHandler createLabelHandlerZ( )
{
return new GridAxisLabelHandler( )
{
@Override
public double[] getTickPositions( Axis1D axis )
{
return new double[] { 1, 2, 3, 4, 5 };
}
@Override
public double[] getMinorTickPositions( double[] tickPositions )
{
// no minor ticks
return new double[0];
}
@Override
public String[] getTickLabels( Axis1D axis, double[] tickPositions )
{
return new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
}
};
}
};
}
//XXX customize the color scale to show five discrete colors
public static ColorTexture1D newColorTexture( )
{
// setup the color map for the painter
ColorTexture1D colorTexture = new ColorTexture1D( 5 );
List<float[]> colorList = new ArrayList<>( );
colorList.add( GlimpseColor.getGreen( ) );
colorList.add( GlimpseColor.getRed( ) );
colorList.add( GlimpseColor.getBlue( ) );
colorList.add( GlimpseColor.getYellow( ) );
colorList.add( GlimpseColor.getCyan( ) );
colorTexture.setColorGradient( ColorGradients.customMap( colorList ) );
return colorTexture;
}
public static double[][] generateData( int sizeX, int sizeY )
{
double[][] data = new double[sizeX][sizeY];
for ( int x = 0; x < sizeX; x++ )
{
for ( int y = 0; y < sizeY; y++ )
{
data[x][y] = (int) ( Math.random( ) * 6 );
}
}
return data;
}
public static HeatMapPainter newHeatMapPainter( ColorTexture1D colorScale, Axis1D axis )
{
// generate some data to display
double[][] data = generateData( 1000, 1000 );
// generate a projection indicating how the data should be mapped to plot coordinates
Projection projection = new FlatProjection( 0, 1000, 0, 1000 );
// create an OpenGL texture wrapper object
FloatTextureProjected2D texture = new FloatTextureProjected2D( 1000, 1000 );
// load the data and projection into the texture
texture.setProjection( projection );
texture.setData( data );
// create a painter to display the heatmap data
HeatMapPainter heatmapPainter = new HeatMapPainter( axis );
// add the heatmap data and color scale to the painter
heatmapPainter.setData( texture );
heatmapPainter.setColorScale( colorScale );
return heatmapPainter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment