001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030/** 031 * 032 */ 033package org.openimaj.vis.general; 034 035import org.openimaj.image.MBFImage; 036import org.openimaj.math.geometry.shape.Rectangle; 037 038/** 039 * A horizontal axis on which items can be plotted and grouped into bands. 040 * 041 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 042 * @param <O> The type of item to plot on the axis 043 * @created 3 Jun 2013 044 */ 045public class DiversityAxis<O> extends XYPlotVisualisation<O> 046{ 047 /** */ 048 private static final long serialVersionUID = 1L; 049 050 /** 051 * Default constructor 052 * @param plotter The plotter to use for items 053 */ 054 public DiversityAxis( final ItemPlotter<O,Float[],MBFImage> plotter ) 055 { 056 super( plotter ); 057 this.init(); 058 } 059 060 /** 061 * Default constructor that takes the width and height of the visualisation 062 * 063 * @param w The width of the axis visualisation 064 * @param h The height of the axis visualisation 065 * @param plotter The plotter to use for items 066 */ 067 public DiversityAxis( final int w, final int h, 068 final ItemPlotter<O,Float[],MBFImage> plotter ) 069 { 070 super( w, h, plotter ); 071 this.init(); 072 } 073 074 /** 075 * Initialise 076 */ 077 private void init() 078 { 079 this.axesRenderer2D.setDrawYTicks( false ); 080 this.axesRenderer2D.setDrawYTickLabels( false ); 081 this.axesRenderer2D.setyLabelSpacing( 1 ); 082 this.axesRenderer2D.setMinYValue( 0 ); 083 this.axesRenderer2D.setAxisPaddingBottom( 50 ); 084 this.axesRenderer2D.setyAxisName( "" ); 085 } 086 087 /** 088 * Set the name of the diversity axis. 089 * @param name The name of the diversity axis 090 */ 091 public void setDiversityAxisName( final String name ) 092 { 093 this.axesRenderer2D.setxAxisName( name ); 094 } 095 096 /** 097 * Add an object to the axis at the given position in the given band. 098 * Note that the band is 1-based. 099 * 100 * @param band The band in which the object is to be put (1-based) 101 * @param position The position of the object on the axis 102 * @param object The object 103 */ 104 public void addObject( final int band, final double position, final O object ) 105 { 106 super.addPoint( position, band, object ); 107 } 108 109 /** 110 * {@inheritDoc} 111 * @see org.openimaj.vis.general.XYPlotVisualisation#beforeAxesRender(org.openimaj.image.MBFImage, org.openimaj.vis.general.AxesRenderer2D) 112 */ 113 @Override 114 public void beforeAxesRender( final MBFImage visImage, 115 final AxesRenderer2D<Float[],MBFImage> renderer ) 116 { 117 int maxBand = 1; 118 for( final XYPlotVisualisation.LocatedObject<O> s : this.data ) 119 maxBand = Math.max( maxBand, (int)s.y ); 120 121 renderer.setMaxYValue( maxBand ); 122 renderer.setImage( visImage ); 123 renderer.precalc( ); 124 125 final Float[][] cols = new Float[][]{ {0.4f,0.4f,0.4f}, {0.3f,0.3f,0.3f} }; 126 for( int b = 1; b <= maxBand; b++ ) 127 { 128 final int topOfBand = (int)renderer.calculatePosition( 0, b ).getY(); 129 final int bottomOfBand = (int)renderer.calculatePosition( 0, b-1 ).getY(); 130 visImage.createRenderer().drawShapeFilled( 131 new Rectangle( 0, topOfBand, visImage.getWidth(), bottomOfBand-topOfBand ), 132 cols[b%2] ); 133 134 if( b == 1 ) 135 this.bandSizeKnown( bottomOfBand - topOfBand ); 136 } 137 } 138 139 /** 140 * Called once the band size has been calculated 141 * @param bandSize The band size 142 */ 143 public void bandSizeKnown( final int bandSize ) 144 { 145 // Can be overridden if you need to know this 146 // - for example, to let the item plotter know the size of the bands 147 } 148}