ImagePlot.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 ImagePlot.cs
00005 Copyright (C) 2003-2004
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 
00056 namespace NPlot
00057 {
00058 
00062         public class ImagePlot : IPlot
00063         {
00064                 private double[,] data_;
00065                 private double xStart_ = 0.0;
00066                 private double xStep_ = 1.0;
00067                 private double yStart_ = 0.0;
00068                 private double yStep_ = 1.0;
00069                 private double dataMin_;
00070                 private double dataMax_;
00071 
00075                 private void calculateMinMax()
00076                 {
00077                         dataMin_ = data_[0,0];
00078                         dataMax_ = data_[0,0];
00079                         for (int i=0; i<data_.GetLength(0); ++i)
00080                         {
00081                                 for (int j=0; j<data_.GetLength(1); ++j)
00082                                 {
00083                                         if (data_[i,j]<dataMin_) 
00084                                         {
00085                                                 dataMin_ = data_[i,j];
00086                                         }
00087                                         if (data_[i,j]>dataMax_) 
00088                                         {
00089                                                 dataMax_ = data_[i,j];
00090                                         }
00091                                 }
00092                         }
00093                 }
00094 
00095 
00106                 public ImagePlot( double[,] data, double xStart, double xStep, double yStart, double yStep )
00107                 {
00108 
00109 #if CHECK_ERRORS
00110                         if (data == null || data.GetLength(0) == 0 || data.GetLength(1) == 0)
00111                         {
00112                                 throw new NPlotException( "ERROR: ImagePlot.ImagePlot: Data null, or zero length" );
00113                         }
00114 #endif
00115 
00116                         this.data_ = data;
00117                         this.xStart_ = xStart;
00118                         this.xStep_ = xStep;
00119                         this.yStart_ = yStart;
00120                         this.yStep_ = yStep;
00121                         this.calculateMinMax();
00122                 }
00123 
00124 
00129                 public ImagePlot( double[,] data )
00130                 {
00131                         this.data_ = data;
00132                         this.calculateMinMax();
00133                 }
00134 
00135 
00143                 public void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
00144                 {
00145                         if ( data_==null || data_.GetLength(0) == 0 || data_.GetLength(1) == 0 )
00146                         {
00147                                 return;
00148                         }
00149 
00150                         double worldWidth = xAxis.Axis.WorldMax - xAxis.Axis.WorldMin;
00151                         double numBlocksHorizontal = worldWidth / this.xStep_;
00152                         double worldHeight = yAxis.Axis.WorldMax - yAxis.Axis.WorldMin;
00153                         double numBlocksVertical = worldHeight / this.yStep_;
00154 
00155                         double physicalWidth = xAxis.PhysicalMax.X - xAxis.PhysicalMin.X;
00156                         double blockWidth = physicalWidth / numBlocksHorizontal;
00157                         bool wPositive = true;
00158                         if (blockWidth < 0.0)
00159                         {
00160                                 wPositive = false;
00161                         }
00162                         blockWidth = Math.Abs(blockWidth)+1;
00163 
00164                         double physicalHeight = yAxis.PhysicalMax.Y - yAxis.PhysicalMin.Y;
00165                         double blockHeight = physicalHeight / numBlocksVertical;
00166                         bool hPositive = true;
00167                         if (blockHeight < 0.0)
00168                         {
00169                                 hPositive = false;
00170                         }
00171                         blockHeight = Math.Abs(blockHeight)+1;
00172 
00173                         for (int i=0; i<data_.GetLength(0); ++i)
00174                         {
00175                                 for (int j=0; j<data_.GetLength(1); ++j)
00176                                 {
00177                                         double wX = (double)j*this.xStep_ + xStart_;
00178                                         double wY = (double)i*this.yStep_ + yStart_;
00179                                         if ( !hPositive )
00180                                         {
00181                                                 wY += yStep_;
00182                                         }
00183                                         if (!wPositive )
00184                                         {
00185                                                 wX += xStep_;
00186                                         }
00187 
00188                                         if (this.center_)
00189                                         {
00190                                                 wX -= this.xStep_/2.0;
00191                                                 wY -= this.yStep_/2.0;
00192                                         }
00193                                         Pen p = new Pen( this.Gradient.GetColor( (data_[i,j]-this.dataMin_)/(this.dataMax_-this.dataMin_) ) );
00194                                         int x = (int)xAxis.WorldToPhysical(wX,false).X;
00195                                         int y = (int)yAxis.WorldToPhysical(wY,false).Y;
00196                                         g.FillRectangle( p.Brush,
00197                                                 x,
00198                                                 y, 
00199                                                 (int)blockWidth,
00200                                                 (int)blockHeight);
00201                                         //g.DrawRectangle(Pens.White,x,y,(int)blockWidth,(int)blockHeight);
00202                                 }
00203                         }
00204                 }
00205 
00206 
00211                 public IGradient Gradient
00212                 {
00213                         get
00214                         {
00215                                 if (gradient_ == null)
00216                                 {
00217                                         // TODO: suboptimal.
00218                                         gradient_ = new LinearGradient( Color.FromArgb(255,255,255), Color.FromArgb(0,0,0) );
00219                                 }
00220                                 return this.gradient_;
00221                         }
00222                         set
00223                         {
00224                                 this.gradient_ = value;
00225                         }
00226                 }
00227                 private IGradient gradient_;
00228 
00229 
00235                 public void DrawInLegend( Graphics g, Rectangle startEnd )
00236                 {
00237                         // not implemented yet.
00238                 }
00239 
00240 
00244                 public string Label
00245                 {
00246                         get
00247                         {
00248                                 return label_;
00249                         }
00250                         set
00251                         {
00252                                 this.label_ = value;
00253                         }
00254                 }
00255                 private string label_ = "";
00256 
00257 
00262                 public Axis SuggestXAxis()
00263                 {
00264                         if (this.center_)
00265                         {
00266                                 return new LinearAxis( this.xStart_ - this.xStep_/2.0, this.xStart_ + this.xStep_ * data_.GetLength(1) - this.xStep_/2.0 );
00267                         }
00268                         
00269                         return new LinearAxis( this.xStart_, this.xStart_ + this.xStep_ * data_.GetLength(1) );
00270                 }
00271 
00272 
00277                 public Axis SuggestYAxis()
00278                 {
00279                         if (this.center_)
00280                         {
00281                                 return new LinearAxis( this.yStart_ - this.yStep_/2.0, this.yStart_ + this.yStep_ * data_.GetLength(0) - this.yStep_/2.0 );
00282                         }
00283                         
00284                         return new LinearAxis( this.yStart_, this.yStart_ + this.yStep_ * data_.GetLength(0) );
00285                 }
00286 
00287 
00292                 public bool Center
00293                 {
00294                         set
00295                         {
00296                                 center_ = value;
00297                         }
00298                         get
00299                         {
00300                                 return center_;
00301                         }
00302                 }
00303                 private bool center_ = true;
00304 
00305 
00309                 public bool ShowInLegend
00310                 {
00311                         get
00312                         {
00313                                 return showInLegend_;
00314                         }
00315                         set
00316                         {
00317                                 this.showInLegend_ = value;
00318                         }
00319                 }
00320                 private bool showInLegend_ = true;
00321 
00322 
00330                 public void WriteData( System.Text.StringBuilder sb, RectangleD region, bool onlyInRegion )
00331                 {
00332                 }
00333 
00334 
00335         }
00336 }

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