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 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
00231
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
00241
00242
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
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
00286
00287 if ( !this.neverShiftAxes_ )
00288 {
00289
00290 if ( position.X < padding )
00291 {
00292 int changeAmount = -position.X + padding;
00293
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
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
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
00327 if ( changeAmount > 0.0f )
00328 {
00329 bottomIndent = changeAmount;
00330 }
00331 position.Y -= changeAmount;
00332 }
00333
00334
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 }