SequenceAdapter.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 SequenceAdapter.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.Collections;
00054 using System.Data;
00055 
00056 namespace NPlot
00057 {
00058 
00065         public class SequenceAdapter
00066         {
00067                 private AdapterUtils.IAxisSuggester XAxisSuggester_;
00068                 private AdapterUtils.IAxisSuggester YAxisSuggester_;
00069         private AdapterUtils.ICounter counter_;
00070         private AdapterUtils.IDataGetter xDataGetter_;
00071         private AdapterUtils.IDataGetter yDataGetter_;
00072 
00080                 public SequenceAdapter( object dataSource, string dataMember, object ordinateData, object abscissaData )
00081                 {
00082 
00083                         if (dataSource == null && dataMember == null)
00084                         {
00085                                 if (ordinateData is IList) 
00086                                 {
00087 
00088                     this.YAxisSuggester_ = new AdapterUtils.AxisSuggester_IList((IList)ordinateData);
00089                     this.yDataGetter_ = new AdapterUtils.DataGetter_IList((IList)ordinateData);
00090                     this.counter_ = new AdapterUtils.Counter_IList((IList)ordinateData);
00091 
00092                     if (abscissaData is IList)
00093                                         {
00094                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_IList((IList)abscissaData);
00095                         this.xDataGetter_ = new AdapterUtils.DataGetter_IList((IList)abscissaData);
00096                         return;
00097                                         }
00098 
00099                                         else if (abscissaData is StartStep)
00100                                         {
00101                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_StartStep((StartStep)abscissaData, (IList)ordinateData);
00102                         this.xDataGetter_ = new AdapterUtils.DataGetter_StartStep((StartStep)abscissaData);
00103                         return;
00104                                         }
00105 
00106                                         else if (abscissaData == null)
00107                                         {
00108                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_Auto((IList)ordinateData);
00109                         this.xDataGetter_ = new AdapterUtils.DataGetter_Count();
00110                         return;
00111                                         }
00112                                 }
00113                                 
00114 
00115                                 else if (ordinateData == null)
00116                                 {
00117                                         if (abscissaData == null)
00118                                         {
00119                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_Null();
00120                         this.YAxisSuggester_ = new AdapterUtils.AxisSuggester_Null();
00121                         this.counter_ = new AdapterUtils.Counter_Null();
00122                         this.xDataGetter_ = new AdapterUtils.DataGetter_Null();
00123                         this.yDataGetter_ = new AdapterUtils.DataGetter_Null();
00124                         return;
00125                                         }
00126                                         else if (abscissaData is IList)
00127                                         {
00128                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_IList((IList)abscissaData);
00129                         this.YAxisSuggester_ = new AdapterUtils.AxisSuggester_Auto((IList)abscissaData);
00130                         this.counter_ = new AdapterUtils.Counter_IList((IList)abscissaData);
00131                         this.xDataGetter_ = new AdapterUtils.DataGetter_IList((IList)abscissaData);
00132                         this.yDataGetter_ = new AdapterUtils.DataGetter_Count();
00133                         return;
00134                                         }
00135                                         
00136                                         else
00137                                         {
00138                                                 // unknown.
00139                                         }
00140 
00141                                 }
00142                                 else
00143                                 {
00144                                         // unknown
00145                                 }
00146 
00147                         }
00148 
00149                         else if (dataSource is IList && dataMember == null)
00150                         {
00151                                 if (dataSource is DataView)
00152                                 {
00153                                         DataView data = (DataView)dataSource;
00154 
00155                     this.counter_ = new AdapterUtils.Counter_DataView(data);
00156                     this.xDataGetter_ = new AdapterUtils.DataGetter_DataView(data, (string)abscissaData);
00157                     this.yDataGetter_ = new AdapterUtils.DataGetter_DataView(data, (string)ordinateData);
00158                     this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_DataView(data, (string)abscissaData);
00159                     this.YAxisSuggester_ = new AdapterUtils.AxisSuggester_DataView(data, (string)ordinateData);
00160                     return;
00161                                 }
00162 
00163                                 else
00164                                 {
00165                     this.YAxisSuggester_ = new AdapterUtils.AxisSuggester_IList((IList)dataSource);
00166                     this.counter_ = new AdapterUtils.Counter_IList((IList)dataSource);
00167                     this.yDataGetter_ = new AdapterUtils.DataGetter_IList((IList)dataSource);
00168 
00169                     if ((ordinateData == null) && (abscissaData == null))
00170                                         {
00171                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_Auto((IList)dataSource);
00172                         this.xDataGetter_ = new AdapterUtils.DataGetter_Count();
00173                         return;
00174                                         }
00175 
00176                                         else if ((ordinateData == null) && (abscissaData is StartStep))
00177                                         {
00178                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_StartStep((StartStep)abscissaData, (IList)ordinateData);
00179                         this.xDataGetter_ = new AdapterUtils.DataGetter_StartStep((StartStep)abscissaData);
00180                         return;
00181                                         }
00182 
00183                                         else if ((ordinateData == null) && (abscissaData is IList))
00184                                         {
00185                         this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_IList((IList)abscissaData);
00186                         this.xDataGetter_ = new AdapterUtils.DataGetter_IList((IList)abscissaData);
00187                         return;
00188                                         }
00189 
00190                                         else
00191                                         {
00192                                                 // unknown.
00193                                         }
00194                                 }
00195                         }
00196 
00197                         else if ( ((dataSource is DataTable) && (dataMember == null)) || (dataSource is DataSet) )
00198                         {
00199                                 DataRowCollection rows = null;
00200 
00201                                 if (dataSource is DataSet)
00202                                 {
00203                                         if (dataMember != null)
00204                                         {
00205                                                 rows = ((DataTable)((DataSet)dataSource).Tables[dataMember]).Rows;
00206                                         }
00207                                         else
00208                                         {
00209                                                 rows = ((DataTable)((DataSet)dataSource).Tables[0]).Rows;
00210                                         }
00211                                 }
00212                                 else
00213                                 {
00214                                         rows = ((DataTable)dataSource).Rows;
00215                                 }
00216 
00217                 this.yDataGetter_ = new AdapterUtils.DataGetter_Rows(rows, (string)ordinateData);
00218                 this.YAxisSuggester_ = new AdapterUtils.AxisSuggester_Rows(rows, (string)ordinateData);
00219                 this.counter_ = new AdapterUtils.Counter_Rows(rows);
00220 
00221                 if ((abscissaData is string) && (ordinateData is string))
00222                                 {
00223                     this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_Rows(rows, (string)abscissaData);
00224                     this.xDataGetter_ = new AdapterUtils.DataGetter_Rows(rows, (string)abscissaData);
00225                     return;
00226                                 }
00227                                 else if ((abscissaData == null) && (ordinateData is string))
00228                                 {
00229                     this.XAxisSuggester_ = new AdapterUtils.AxisSuggester_RowAuto(rows);
00230                     this.xDataGetter_ = new AdapterUtils.DataGetter_Count();
00231                     return;
00232                                 }
00233                                 else
00234                                 {
00235                                         // unknown.
00236                                 }
00237                         }
00238 
00239                         else
00240                         {
00241                                 // unknown.
00242                         }
00243 
00244                         throw new NPlotException( "Do not know how to interpret data provided to chart." );
00245 
00246                 }
00247 
00248 
00252                 public int Count
00253                 {
00254                         get
00255                         {
00256                                 return counter_.Count;
00257                         }
00258                 }
00259 
00260 
00264                 public PointD this[int i] 
00265                 {
00266                         get
00267                         {
00268                                 return new PointD( this.xDataGetter_.Get(i), this.yDataGetter_.Get(i) );
00269                         }
00270                 }
00271 
00272 
00277                 public Axis SuggestXAxis()
00278                 {
00279                         return this.XAxisSuggester_.Get();
00280                 }
00281 
00282 
00287                 public Axis SuggestYAxis()
00288                 {
00289                         Axis a = this.YAxisSuggester_.Get();
00290                         // TODO make 0.08 a parameter.
00291                         a.IncreaseRange( 0.08 );
00292                         return a;
00293                 }
00294 
00295 
00302                 public void WriteData( System.Text.StringBuilder sb, RectangleD region, bool onlyInRegion )
00303                 {
00304                         for (int i=0; i<this.Count;     ++i)
00305                         {
00306                                 if ( !(onlyInRegion && 
00307                                            (this[i].X >= region.X && this[i].X <= region.X+region.Width) &&
00308                                            (this[i].Y >= region.Y && this[i].Y <= region.Y+region.Height)) )
00309                                         continue;
00310 
00311                                 sb.Append( this[i].ToString() );
00312                                 sb.Append( "\r\n" );
00313                         }
00314                 }
00315 
00316 
00317         }
00318 }

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