LabelPointPlot.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 LabelPointPlot.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.Data;
00055 
00056 namespace NPlot
00057 {
00058 
00062         public class LabelPointPlot : PointPlot, ISequencePlot
00063         {
00064 
00069                 private class TextDataAdapter
00070                 {
00071 
00072                         private object data_;
00073                         private object dataSource_;
00074                         private string dataMember_;
00075 
00076                         public TextDataAdapter( object dataSource, string dataMember, object data )
00077                         {
00078                                 this.data_ = data;
00079                                 this.dataSource_ = dataSource;
00080                                 this.dataMember_ = dataMember;
00081                         }
00082 
00083                         public string this[int i]
00084                         {
00085                                 get
00086                                 {
00087 
00088                                         // this is inefficient [could set up delegates in constructor].
00089 
00090                                         if (data_ is string[])
00091                                         {
00092                                                 return ((string[])data_)[i];
00093                                         }
00094                                         
00095                                         if (data_ is string)
00096                                         {
00097                                                 if (dataSource_ == null)
00098                                                 {
00099                                                         throw new NPlotException( "Error: DataSource null" );
00100                                                 }
00101 
00102                                                 System.Data.DataRowCollection rows;
00103 
00104                                                 if ( dataSource_ is System.Data.DataSet )
00105                                                 {
00106                                                         if (dataMember_ != null)
00107                                                         {
00108                                                                 // TODO error check
00109                                                                 rows = ((DataTable)((DataSet)dataSource_).Tables[dataMember_]).Rows;
00110                                                         }
00111                                                         else
00112                                                         {
00113                                                                 // TODO error check
00114                                                                 rows = ((DataTable)((DataSet)dataSource_).Tables[0]).Rows;
00115                                                         }
00116                                                 }
00117 
00118                                                 else if (dataSource_ is System.Data.DataTable )
00119                                                 {
00120                                                         rows = ((DataTable)dataSource_).Rows;
00121                                                 }
00122 
00123                                                 else
00124                                                 {
00125                                                         throw new NPlotException ( "not implemented yet" );
00126                                                 }
00127 
00128                                                 return (string)((System.Data.DataRow)(rows[i]))[(string)data_];
00129                                         }
00130 
00131                                         if (data_ is System.Collections.ArrayList)
00132                                         {
00133                                                 object dataPoint = ((System.Collections.ArrayList)data_)[i];
00134                                                 if (dataPoint is string)
00135                                                         return (string)dataPoint;
00136                                                 throw new NPlotException( "TextDataAdapter: data not in recognised format" );
00137                                         }
00138                                         
00139                                         if (data_ == null)
00140                                         {
00141                                                 return "text";
00142                                         }
00143 
00144                                         throw new NPlotException( "Text data not of recognised type" );
00145                                 }
00146                         }
00147 
00148 
00149                         public int Count
00150                         {
00151                                 get
00152                                 {
00153                                         // this is inefficient [could set up delegates in constructor].
00154 
00155                                         if (data_ == null)
00156                                         {
00157                                                 return 0;
00158                                         }
00159                                         if (data_ is string[])
00160                                         {
00161                                                 return ((string[])data_).Length;
00162                                         }
00163                                         if (data_ is System.Collections.ArrayList)
00164                                         {
00165                                                 return ((System.Collections.ArrayList)data_).Count;
00166                                         }
00167                                         throw new NPlotException( "Text data not in correct format" );
00168                                 }
00169                         }
00170 
00171                 }
00172 
00173 
00177                 public enum LabelPositions
00178                 {
00182                         Above,
00186                         Below,
00190                         Left,
00194                         Right
00195                 }
00196 
00197 
00201                 public LabelPointPlot()
00202                 {
00203                 }
00204 
00205 
00210                 public LabelPointPlot( Marker marker ) 
00211                         : base( marker )
00212                 {
00213                 }
00214 
00215                 
00219                 public LabelPositions LabelTextPosition
00220                 {
00221                         get
00222                         {
00223                                 return labelTextPosition_;
00224                         }
00225                         set
00226                         {
00227                                 labelTextPosition_ = value;
00228                         }
00229                 }
00230                 private LabelPositions labelTextPosition_ = LabelPositions.Above;
00231 
00232 
00236                 public object TextData
00237                 {
00238                         get
00239                         {
00240                                 return textData_;
00241                         }
00242                         set
00243                         {
00244                                 textData_ = value;
00245                         }
00246                 }
00247                 object textData_;
00248 
00249 
00253                 public Font Font
00254                 {
00255                         get
00256                         {
00257                                 return font_;
00258                         }
00259                         set
00260                         {
00261                                 font_ = value;
00262                         }
00263                 }
00264                 private Font font_ = new Font( "Arial", 8.0f );
00265 
00266 
00273                 public override void Draw( Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis )
00274                 {
00275                         SequenceAdapter data = 
00276                                 new SequenceAdapter( this.DataSource, this.DataMember, this.OrdinateData, this.AbscissaData );
00277 
00278                         TextDataAdapter textData =
00279                                 new TextDataAdapter( this.DataSource, this.DataMember, this.TextData );
00280 
00281                         // first plot the marker
00282                     // we can do this cast, since the constructor accepts only this type!
00283                         for (int i=0; i<data.Count; ++i)
00284                         {
00285                                 try
00286                                 {
00287                                         PointD pt = data[i];
00288                                         if ( !Double.IsNaN(pt.X) && !Double.IsNaN(pt.Y) )
00289                                         {
00290                                                 PointF xPos = xAxis.WorldToPhysical( pt.X, false);
00291                                                 PointF yPos = yAxis.WorldToPhysical( pt.Y, false);
00292                                                 Marker.Draw( g, (int)xPos.X, (int)yPos.Y );
00293                                                 if ( textData[i] != "" )
00294                                                 {
00295                                                         SizeF size = g.MeasureString( textData[i], this.Font );
00296                                                         switch (labelTextPosition_)
00297                                                         {
00298                                                                 case LabelPositions.Above:
00299                                                                         g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width/2,yPos.Y-size.Height-Marker.Size*2/3));
00300                                                                         break;
00301                                                                 case LabelPositions.Below:
00302                                                                         g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width/2,yPos.Y+Marker.Size*2/3));
00303                                                                         break;
00304                                                                 case LabelPositions.Left:
00305                                                                         g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X-size.Width-Marker.Size*2/3,yPos.Y-size.Height/2));
00306                                                                         break;
00307                                                                 case LabelPositions.Right:
00308                                                                         g.DrawString( textData[i], font_, Brushes.Black, new PointF(xPos.X+Marker.Size*2/3,yPos.Y-size.Height/2));
00309                                                                         break;
00310                                                         }
00311                                                 }
00312                                         }
00313                                 }
00314                                 catch
00315                                 {
00316                                         throw new NPlotException("Error in TextPlot.Draw");
00317                                 }
00318                         }
00319 
00320                 }
00321 
00322 
00323         }
00324 }

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