LinePlot.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 LinePlot.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.Diagnostics;
00055 
00056 namespace NPlot
00057 {
00058 
00062         public class LinePlot : BaseSequencePlot, IPlot, ISequencePlot
00063         {
00064 
00068                 public LinePlot()
00069                 {
00070                 }
00071 
00072 
00077                 public LinePlot( object dataSource )
00078                 {
00079                         this.DataSource = dataSource;
00080                 }
00081 
00082 
00088                 public LinePlot( object ordinateData, object abscissaData )
00089                 {
00090                         this.OrdinateData = ordinateData;
00091                         this.AbscissaData = abscissaData;
00092                 }
00093 
00094 
00102                 public void DrawLineOrShadow( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis, bool drawShadow )
00103                 {
00104                         Pen shadowPen = null;
00105                         if (drawShadow)
00106                         {
00107                                 shadowPen = (Pen)this.Pen.Clone();
00108                                 shadowPen.Color = this.ShadowColor;
00109                         }
00110 
00111                         SequenceAdapter data = 
00112                                 new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
00113 
00114                         ITransform2D t = Transform2D.GetTransformer( xAxis, yAxis );
00115                         
00116                         int numberPoints = data.Count;
00117                         
00118                         if (data.Count == 0)
00119                         {
00120                                 return;
00121                         }
00122 
00123                         // clipping is now handled assigning a clip region in the
00124                         // graphic object before this call
00125                         if (numberPoints == 1)
00126                         {
00127                                 PointF physical = t.Transform( data[0] );
00128                                 
00129                                 if (drawShadow)
00130                                 {
00131                                         g.DrawLine( shadowPen, 
00132                                                 physical.X - 0.5f + this.ShadowOffset.X,
00133                                                 physical.Y + this.ShadowOffset.Y,
00134                                                 physical.X + 0.5f + this.ShadowOffset.X,
00135                                                 physical.Y + this.ShadowOffset.Y );
00136                                 }
00137                                 else
00138                                 {
00139                                         g.DrawLine( Pen, physical.X-0.5f, physical.Y, physical.X+0.5f, physical.Y);
00140                                 }
00141                         }
00142                         else
00143                         {
00144                                 for (int i = 1; i < numberPoints; ++i)
00145                                 {
00146                                         // check to see if any values null. If so, then continue.
00147                                         double dx1 = data[i-1].X;
00148                                         double dx2 = data[i].X;
00149                                         double dy1 = data[i-1].Y;
00150                                         double dy2 = data[i].Y;
00151                                         if ( Double.IsNaN(dx1) || Double.IsNaN(dy1) ||
00152                                                 Double.IsNaN(dx2) || Double.IsNaN(dy2) )
00153                                         {
00154                                                 continue;
00155                                         }
00156 
00157                                         // else draw line.
00158                                         PointF p1 = t.Transform( data[i-1] );
00159                                         PointF p2 = t.Transform( data[i] );
00160                     
00161                     // when very far zoomed in, points can fall ontop of each other,
00162                     // and g.DrawLine throws an overflow exception
00163                     if (p1.Equals(p2))
00164                         continue;
00165 
00166                                         if (drawShadow)
00167                                         {
00168                                                 g.DrawLine( shadowPen, 
00169                                                         p1.X + ShadowOffset.X,
00170                                                         p1.Y + ShadowOffset.Y,
00171                                                         p2.X + ShadowOffset.X,
00172                                                         p2.Y + ShadowOffset.Y );
00173                                         }
00174                                         else
00175                                         {
00176                                                 g.DrawLine( Pen, p1.X, p1.Y, p2.X, p2.Y );
00177                                         }
00178                                 }
00179                         }
00180 
00181                 }
00182 
00183 
00190                 public void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
00191                 {
00192                         if (this.shadow_)
00193                         {
00194                                 this.DrawLineOrShadow( g, xAxis, yAxis, true );
00195                         }
00196 
00197                         this.DrawLineOrShadow( g, xAxis, yAxis, false );
00198                 }
00199 
00200 
00205                 public Axis SuggestXAxis()
00206                 {
00207                         SequenceAdapter data_ = 
00208                                 new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
00209 
00210                         return data_.SuggestXAxis();
00211                 }
00212 
00213 
00218                 public Axis SuggestYAxis()
00219                 {
00220                         SequenceAdapter data_ = 
00221                                 new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
00222 
00223                         return data_.SuggestYAxis();
00224                 }
00225 
00226 
00230                 public bool Shadow
00231                 {
00232                         get
00233                         {
00234                                 return shadow_;
00235                         }
00236                         set
00237                         {
00238                                 shadow_ = value;
00239                         }
00240                 }
00241                 private bool shadow_ = false;
00242         
00243 
00247                 public Color ShadowColor
00248                 {
00249                         get
00250                         {
00251                                 return shadowColor_;
00252                         }
00253                         set
00254                         {
00255                                 shadowColor_ = value;
00256                         }
00257                 }
00258                 private Color shadowColor_ = Color.FromArgb(100,100,100);
00259 
00260 
00264                 public Point ShadowOffset
00265                 {
00266                         get
00267                         {
00268                                 return shadowOffset_;
00269                         }
00270                         set
00271                         {
00272                                 shadowOffset_ = value;
00273                         }
00274                 }
00275                 private Point shadowOffset_ = new Point( 1, 1 );
00276 
00277 
00283         public virtual void DrawInLegend(Graphics g, Rectangle startEnd)
00284         {
00285             g.DrawLine(pen_, startEnd.Left, (startEnd.Top + startEnd.Bottom) / 2,
00286                 startEnd.Right, (startEnd.Top + startEnd.Bottom) / 2);
00287         }
00288 
00289 
00293         public System.Drawing.Pen Pen
00294         {
00295             get
00296             {
00297                 return pen_;
00298             }
00299             set
00300             {
00301                 pen_ = value;
00302             }
00303         }
00304         private System.Drawing.Pen pen_ = new Pen(Color.Black);
00305 
00306 
00310         public System.Drawing.Color Color
00311         {
00312             set
00313             {
00314                 if (pen_ != null)
00315                 {
00316                     pen_.Color = value;
00317                 }
00318                 else
00319                 {
00320                     pen_ = new Pen(value);
00321                 }
00322             }
00323             get
00324             {
00325                 return pen_.Color;
00326             }
00327         }
00328 
00329     
00330     }
00331 }

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