00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
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
00202 }
00203 }
00204 }
00205
00206
00211 public IGradient Gradient
00212 {
00213 get
00214 {
00215 if (gradient_ == null)
00216 {
00217
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
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 }