Grid.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 Grid.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 
00064         public class Grid : IDrawable
00065         {
00066 
00070                 public enum GridType
00071                 {
00075                         None = 0,
00079                         Coarse = 1,
00083                         Fine = 2
00084                 }
00085 
00086 
00090                 public Grid()
00091                 {
00092                         minorGridPen_ = new Pen( Color.LightGray );
00093                         float[] pattern = {1.0f, 2.0f};
00094                         minorGridPen_.DashPattern = pattern;
00095                         
00096                         majorGridPen_ = new Pen( Color.LightGray );
00097 
00098                         horizontalGridType_ = GridType.Coarse;
00099                         
00100                         verticalGridType_ = GridType.Coarse;
00101                 }
00102 
00103 
00107                 public GridType HorizontalGridType
00108                 {
00109                         get
00110                         {
00111                                 return horizontalGridType_;
00112                         }
00113                         set
00114                         {
00115                                 horizontalGridType_ = value;
00116                         }
00117                 }
00118                 GridType horizontalGridType_;
00119 
00120 
00124                 public GridType VerticalGridType
00125                 {
00126                         get
00127                         {
00128                                 return verticalGridType_;
00129                         }
00130                         set
00131                         {
00132                                 verticalGridType_ = value;
00133                         }
00134                 }
00135                 GridType verticalGridType_;
00136 
00137 
00141                 public System.Drawing.Pen MajorGridPen
00142                 {
00143                         get
00144                         {
00145                                 return majorGridPen_;
00146                         }
00147                         set
00148                         {
00149                                 majorGridPen_ = value;
00150                         }
00151                 }
00152                 private Pen majorGridPen_;
00153 
00154 
00158                 public System.Drawing.Pen MinorGridPen
00159                 {
00160                         get
00161                         {
00162                                 return minorGridPen_;
00163                         }
00164                         set
00165                         {
00166                                 minorGridPen_ = value;
00167                         }
00168                 }
00169                 private Pen minorGridPen_;
00170 
00171 
00181                 private void DrawGridLines( 
00182                         Graphics g, PhysicalAxis axis, PhysicalAxis orthogonalAxis,
00183                         System.Collections.ArrayList a, bool horizontal, Pen p )
00184                 {
00185                         for (int i=0; i<a.Count; ++i)
00186                         {
00187                                 PointF p1 = axis.WorldToPhysical((double)a[i], true);
00188                                 PointF p2 = p1;
00189                                 PointF p3 = orthogonalAxis.PhysicalMax;
00190                                 PointF p4 = orthogonalAxis.PhysicalMin;
00191                                 if (horizontal)
00192                                 {
00193                                         p1.Y = p4.Y;
00194                                         p2.Y = p3.Y;
00195                                 }
00196                                 else
00197                                 {
00198                                         p1.X = p4.X;
00199                                         p2.X = p3.X;
00200                                 }
00201                                 // note: casting all drawing was necessary for sane display. why?
00202                                 g.DrawLine( p, (int)p1.X, (int)p1.Y, (int)p2.X, (int)p2.Y );
00203                         }
00204                 }
00205 
00212                 public void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
00213                 {
00214 
00215                         ArrayList xLargePositions = null;
00216                         ArrayList yLargePositions = null;
00217                         ArrayList xSmallPositions = null;
00218                         ArrayList ySmallPositions = null;
00219 
00220                         if (this.horizontalGridType_ != GridType.None)
00221                         {
00222                                 xAxis.Axis.WorldTickPositions_FirstPass( xAxis.PhysicalMin, xAxis.PhysicalMax, out xLargePositions, out xSmallPositions );
00223                                 DrawGridLines( g, xAxis, yAxis, xLargePositions, true, this.MajorGridPen );     
00224                         }
00225 
00226                         if (this.verticalGridType_ != GridType.None)
00227                         {
00228                                 yAxis.Axis.WorldTickPositions_FirstPass( yAxis.PhysicalMin, yAxis.PhysicalMax, out yLargePositions, out ySmallPositions );
00229                                 DrawGridLines( g, yAxis, xAxis, yLargePositions, false, this.MajorGridPen );
00230                         }
00231 
00232 
00233                         if (this.horizontalGridType_ == GridType.Fine)
00234                         {
00235                                 xAxis.Axis.WorldTickPositions_SecondPass( xAxis.PhysicalMin, xAxis.PhysicalMax, xLargePositions, ref xSmallPositions );
00236                                 DrawGridLines( g, xAxis, yAxis, xSmallPositions, true, this.MinorGridPen );
00237                         }
00238 
00239                         if (this.verticalGridType_ == GridType.Fine)
00240                         {
00241                                 yAxis.Axis.WorldTickPositions_SecondPass( yAxis.PhysicalMin, yAxis.PhysicalMax, yLargePositions, ref ySmallPositions );
00242                                 DrawGridLines( g, yAxis, xAxis, ySmallPositions, false, this.MinorGridPen );
00243                         }
00244 
00245                 }
00246 
00247         }
00248 }

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