/************************************************************************** /* This class is part of the Java Plotlib Toolkit. /* /* Copyright (c) 1999-2004 by Bernhard Bablok (bablokb@gmx.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU Library General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.plotlib.data; import java.awt.geom.*; import de.bablokb.plotlib.core.*; /** Calculate an optimal bounding rectangle for a given collection of Datasets. The properties GlueX, GlueY and GlueType are used to enlarge the rectangle. @see de.bablokb.plotlib.data.Dataset @see de.bablokb.plotlib.data.DatasetTracker @version $Revision: 1.4 $ @author $Author: bablokb $ */ public class DefaultRange { /** GlueType constant. */ public final static int ABSOLUTE = 0, RELATIVE = 1; /** GlueType (defaults to RELATIVE). */ private int iGlueType = RELATIVE; /** Amount of glue in x-direction (defaults to 5%). */ private float iGlueX = 0.05f; /** Amount of glue in y-direction (defaults to 5%). */ private float iGlueY = 0.05f; /** Collection of Datasets. */ private DatasetTracker iDatasets = new DatasetTracker(); ///////////////////////////////////////////////////////////////////////////// /** Default constructor. */ public DefaultRange() { } ///////////////////////////////////////////////////////////////////////////// /** Set GlueType. */ public void setGlueType(int type) { iGlueType = type; } ///////////////////////////////////////////////////////////////////////////// /** Set GlueType. */ public int getGlueType() { return iGlueType; } ///////////////////////////////////////////////////////////////////////////// /** Set glue in x-direction. */ public void setGlueX(float glue) { iGlueX = glue; } ///////////////////////////////////////////////////////////////////////////// /** Get glue in x-direction. */ public float getGlueX() { return iGlueX; } ///////////////////////////////////////////////////////////////////////////// /** Set glue in y-direction. */ public void setGlueY(float glue) { iGlueY = glue; } ///////////////////////////////////////////////////////////////////////////// /** Get glue in y-direction. */ public float getGlueY() { return iGlueY; } ///////////////////////////////////////////////////////////////////////////// /** Set DatasetTracker. */ public void setDatasets(DatasetTracker dt) { iDatasets = dt; } ///////////////////////////////////////////////////////////////////////////// /** Get DatasetTracker. */ public DatasetTracker getDatasets() { return iDatasets; } ///////////////////////////////////////////////////////////////////////////// /** Calculate optimal range rectangle. The range maps minimum and maximum of all datasets to the size of the graphics-area. To prevent axis and labels hitting the border, add some glue. */ public Rectangle2D getRange() { Rectangle2D r = Toolkit.getDefaultToolkit().getRectangle2D(); double xmin = iDatasets.xmin(), xmax = iDatasets.xmax(), ymin = iDatasets.ymin(), ymax = iDatasets.ymax(); double xdelta, ydelta; if (iGlueType == RELATIVE) { xdelta = iGlueX*(xmax-xmin); ydelta = iGlueY*(ymax-ymin); } else { xdelta = iGlueX; ydelta = iGlueY; } xmin -= xdelta; xmax += xdelta; ymin -= ydelta; ymax += ydelta; r.setRect(xmin,ymin,xmax-xmin,ymax-ymin); return r; } }