Legend.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 Legend.cs
00005 Copyright (C) 2003
00006 Matt Howlett
00007 
00008 Redistribution and use of NPlot or parts there-of in source and
00009 binary forms, with or without modification, are permitted provided
00010 that the following conditions are met:
00011 
00012 1. Re-distributions in source form must retain at the head of each
00013    source file the above copyright notice, this list of conditions
00014    and the following disclaimer.
00015 
00016 2. Any product ("the product") that makes use NPlot or parts 
00017    there-of must either:
00018   
00019     (a) allow any user of the product to obtain a complete machine-
00020         readable copy of the corresponding source code for the 
00021         product and the version of NPlot used for a charge no more
00022         than your cost of physically performing source distribution,
00023         on a medium customarily used for software interchange, or:
00024 
00025     (b) reproduce the following text in the documentation, about 
00026         box or other materials intended to be read by human users
00027         of the product that is provided to every human user of the
00028         product: 
00029    
00030               "This product includes software developed as 
00031               part of the NPlot library project available 
00032               from: http://www.nplot.com/" 
00033 
00034         The words "This product" may optionally be replace with 
00035         the actual name of the product.
00036 
00037 ------------------------------------------------------------------------
00038 
00039 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00040 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00041 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00042 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00043 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00044 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00045 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00046 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00047 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00048 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00049 
00050 */
00051 
00052 using System;
00053 using System.Drawing;
00054 using System.Drawing.Drawing2D;
00055 using System.Collections;
00056 
00057 namespace NPlot
00058 {
00059 
00063         public class Legend : LegendBase
00064         {
00065 
00069                 public enum Placement
00070                 {
00074                         Inside = 0,
00078                         Outside = 1
00079                 }
00080 
00081                 private int xOffset_;
00082                 private int yOffset_;
00083                 private PlotSurface2D.XAxisPosition xAttach_;
00084                 private PlotSurface2D.YAxisPosition yAttach_;
00085                 private Placement horizontalEdgePlacement_;
00086                 private Placement verticalEdgePlacement_;
00087                 private bool neverShiftAxes_;
00088 
00093                 public bool NeverShiftAxes
00094                 {
00095                         get
00096                         {
00097                                 return neverShiftAxes_;
00098                         }
00099                         set
00100                         {
00101                                 neverShiftAxes_ = value;
00102                         }
00103                 }
00104 
00105 
00109                 public int XOffset
00110                 {
00111                         get
00112                         {
00113                                 return xOffset_;
00114                         }
00115                         set
00116                         {
00117                                 xOffset_ = value;
00118                         }
00119                 }
00120 
00121 
00125                 public int YOffset
00126                 {
00127                         get
00128                         {
00129                                 return yOffset_;
00130                         }
00131                         set
00132                         {
00133                                 yOffset_ = value;
00134                         }
00135                 }
00136 
00137 
00143                 public Legend.Placement VerticalEdgePlacement
00144                 {
00145                         get
00146                         {
00147                                 return verticalEdgePlacement_;
00148                         }
00149                         set
00150                         {
00151                                 verticalEdgePlacement_ = value;
00152                         }
00153                 }
00154 
00155                 
00161                 public Legend.Placement HorizontalEdgePlacement
00162                 {
00163                         get
00164                         {
00165                                 return horizontalEdgePlacement_;
00166                         }
00167                         set
00168                         {
00169                                 horizontalEdgePlacement_ = value;
00170                         }
00171                 }
00172 
00173 
00179                 public void AttachTo( PlotSurface2D.XAxisPosition xa, PlotSurface2D.YAxisPosition ya )
00180                 {
00181                         xAttach_ = xa;
00182                         yAttach_ = ya; 
00183                 }
00184 
00185 
00189                 public Legend()
00190                 {
00191                         xAttach_ = PlotSurface2D.XAxisPosition.Top;
00192                         yAttach_ = PlotSurface2D.YAxisPosition.Right;
00193                         xOffset_ = 10;
00194                         yOffset_ = 1;
00195                         verticalEdgePlacement_ = Placement.Outside;
00196                         horizontalEdgePlacement_ = Placement.Inside;
00197                         neverShiftAxes_ = false;
00198                 }
00199 
00200 
00213                 public void UpdateAxesPositions( 
00214                         PhysicalAxis pXAxis1,
00215                         PhysicalAxis pYAxis1,
00216                         PhysicalAxis pXAxis2,
00217                         PhysicalAxis pYAxis2,
00218                         ArrayList plots,
00219                         float scale, int padding, Rectangle bounds,
00220                         out Point position )
00221                 {
00222                 
00223                         int leftIndent = 0;
00224                         int rightIndent = 0;
00225                         int bottomIndent = 0;
00226                         int topIndent = 0;
00227 
00228                         position = new Point(0,0);
00229 
00230                         // now determine if legend should change any of these (legend should be fully 
00231                         // visible at all times), and draw legend.
00232 
00233                         Rectangle legendWidthHeight = this.GetBoundingBox( new Point(0,0), plots, scale );
00234 
00235                         if (legendWidthHeight.Width > bounds.Width)
00236                         {
00237                                 legendWidthHeight.Width = bounds.Width;
00238                         }
00239 
00240                         // (1) calculate legend position.
00241 
00242                         // y
00243 
00244                         position.Y = this.yOffset_;
00245                         
00246                         if ( this.xAttach_ == PlotSurface2D.XAxisPosition.Bottom )
00247                         {
00248                                 position.Y += pYAxis1.PhysicalMin.Y;
00249                                 if ( this.horizontalEdgePlacement_ == Legend.Placement.Inside )
00250                                 {
00251                                         position.Y -= legendWidthHeight.Height;
00252                                 }
00253                         }
00254                         else
00255                         {
00256                                 position.Y += pYAxis1.PhysicalMax.Y;
00257                                 if ( this.horizontalEdgePlacement_ == Legend.Placement.Outside )
00258                                 {
00259                                         position.Y -= legendWidthHeight.Height;
00260                                 }
00261                         }
00262         
00263                         // x
00264 
00265                         position.X = this.xOffset_;
00266                 
00267                         if ( this.yAttach_ == PlotSurface2D.YAxisPosition.Left )
00268                         {
00269                                 if ( this.verticalEdgePlacement_ == Legend.Placement.Outside ) 
00270                                 {
00271                                         position.X -= legendWidthHeight.Width;
00272                                 }
00273                                 position.X += pXAxis1.PhysicalMin.X;
00274                         }
00275                         else
00276                         {
00277                                 if ( this.verticalEdgePlacement_ == Legend.Placement.Inside )
00278                                 {
00279                                         position.X -= legendWidthHeight.Width;
00280                                 }
00281                                 position.X += pXAxis1.PhysicalMax.X;
00282                         }
00283 
00284 
00285                         // determine update amounts for axes
00286 
00287                         if ( !this.neverShiftAxes_ )
00288                         {
00289 
00290                                 if ( position.X < padding )
00291                                 {
00292                                         int changeAmount = -position.X + padding;
00293                                         // only allow axes to move away from bounds.
00294                                         if ( changeAmount > 0 )                                 
00295                                         {
00296                                                 leftIndent = changeAmount;
00297                                         }
00298                                         position.X += changeAmount;
00299                                 }
00300 
00301                                 if ( position.X + legendWidthHeight.Width > bounds.Right - padding )
00302                                 {
00303                                         int changeAmount = (position.X - bounds.Right + legendWidthHeight.Width + padding );
00304                                         // only allow axes to move away from bounds.
00305                                         if ( changeAmount > 0.0f ) 
00306                                         {
00307                                                 rightIndent = changeAmount;
00308                                         }
00309                                         position.X -= changeAmount;
00310                                 }
00311 
00312                                 if ( position.Y < padding )
00313                                 {
00314                                         int changeAmount = -position.Y + padding;
00315                                         // only allow axes to move away from bounds.
00316                                         if ( changeAmount > 0.0f )                                      
00317                                         {
00318                                                 topIndent = changeAmount;
00319                                         }
00320                                         position.Y += changeAmount;
00321                                 }
00322 
00323                                 if ( position.Y + legendWidthHeight.Height > bounds.Bottom - padding )
00324                                 {
00325                                         int changeAmount = (position.Y - bounds.Bottom + legendWidthHeight.Height + padding );
00326                                         // only allow axes to move away from bounds.
00327                                         if ( changeAmount > 0.0f ) 
00328                                         {
00329                                                 bottomIndent = changeAmount;
00330                                         }
00331                                         position.Y -= changeAmount;
00332                                 }
00333 
00334                                 // update axes.
00335 
00336                                 pXAxis1.PhysicalMin = new Point( pXAxis1.PhysicalMin.X + leftIndent, pXAxis1.PhysicalMin.Y - bottomIndent );
00337                                 pXAxis1.PhysicalMax = new Point( pXAxis1.PhysicalMax.X - rightIndent, pXAxis1.PhysicalMax.Y - bottomIndent );
00338                                 pYAxis1.PhysicalMin = new Point( pYAxis1.PhysicalMin.X + leftIndent, pYAxis1.PhysicalMin.Y - bottomIndent );
00339                                 pYAxis1.PhysicalMax = new Point( pYAxis1.PhysicalMax.X + leftIndent, pYAxis1.PhysicalMax.Y + topIndent );
00340 
00341                                 pXAxis2.PhysicalMin = new Point( pXAxis2.PhysicalMin.X + leftIndent, pXAxis2.PhysicalMin.Y + topIndent );
00342                                 pXAxis2.PhysicalMax = new Point( pXAxis2.PhysicalMax.X - rightIndent, pXAxis2.PhysicalMax.Y + topIndent );
00343                                 pYAxis2.PhysicalMin = new Point( pYAxis2.PhysicalMin.X - rightIndent, pYAxis2.PhysicalMin.Y - bottomIndent );
00344                                 pYAxis2.PhysicalMax = new Point( pYAxis2.PhysicalMax.X - rightIndent, pYAxis2.PhysicalMax.Y + topIndent );
00345                         
00346                         }
00347 
00348                 }
00349 
00350         }
00351 
00352 }

Generated on Sat Nov 5 01:04:06 2005 for NPlot by  doxygen 1.4.5