AdapterUtils.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 AdapterUtils.cs
00005 Copyright (C) 2003-2005
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 
00053 using System;
00054 using System.Collections;
00055 using System.Data;
00056 
00057 namespace NPlot
00058 {
00059 
00066         public class AdapterUtils
00067         {
00068 
00069                 #region AxisSuggesters
00070 
00074                 public interface IAxisSuggester
00075                 {
00080                         Axis Get();
00081                 }
00082 
00083 
00089                 public class AxisSuggester_MultiColumns : IAxisSuggester
00090                 {                       
00091 
00092                         DataRowCollection rows_;
00093                         string abscissaName_;
00094 
00095 
00101                         public AxisSuggester_MultiColumns(DataRowCollection rows, string abscissaName)
00102                         {
00103                                 rows_ = rows;
00104                                 abscissaName_ = abscissaName;
00105                         }
00106 
00107 
00112                         public Axis Get()
00113                         {
00114                                 double t_min = double.MaxValue;
00115                                 double t_max = double.MinValue;
00116 
00117                                 System.Collections.IEnumerator en = rows_[0].Table.Columns.GetEnumerator();
00118                                 
00119                                 while (en.MoveNext())
00120                                 {
00121                                         string colName = ((DataColumn)en.Current).Caption;
00122                                         
00123                                         if (colName == abscissaName_)
00124                                         {
00125                                                 continue;
00126                                         }
00127 
00128                                         double min;
00129                                         double max;
00130                                         if (Utils.RowArrayMinMax(rows_, out min, out max, colName))
00131                                         {
00132                                                 if (min < t_min)
00133                                                 {
00134                                                         t_min = min;
00135                                                 }
00136                                                 if (max > t_max)
00137                                                 {
00138                                                         t_max = max;
00139                                                 }
00140                                         }
00141                                 }
00142 
00143                                 return new LinearAxis(t_min, t_max);
00144                         }
00145                 
00146                 }
00147 
00148         
00152         public class AxisSuggester_IList : IAxisSuggester
00153         {
00154             private IList data_;
00155 
00160             public AxisSuggester_IList(IList data)
00161             {
00162                 data_ = data;
00163             }
00164 
00169             public Axis Get()
00170             {
00171                 double min;
00172                 double max;
00173 
00174                 if (Utils.ArrayMinMax(data_, out min, out max))
00175                 {
00176                     if (data_[0] is DateTime)
00177                     {
00178                         return new DateTimeAxis(min, max);
00179                     }
00180 
00181                     else
00182                     {
00183                         return new LinearAxis(min, max);
00184                     }
00185 
00186                     // perhaps return LogAxis here if range large enough 
00187                     // + other constraints?
00188                 }
00189 
00190                 return new LinearAxis(0.0, 1.0);
00191             }
00192         }
00193 
00194 
00198         public class AxisSuggester_Null : IAxisSuggester
00199         {
00204             public Axis Get()
00205             {
00206                 return new LinearAxis(0.0, 1.0);
00207             }
00208         }
00209 
00210 
00215         public class AxisSuggester_StartStep : IAxisSuggester
00216         {
00217             StartStep abscissaData_;
00218             IList ordinateData_;
00219 
00225             public AxisSuggester_StartStep(StartStep axisOfInterest, IList otherAxisData)
00226             {
00227                 ordinateData_ = otherAxisData;
00228                 abscissaData_ = axisOfInterest;
00229             }
00230 
00235             public Axis Get()
00236             {
00237                 return new LinearAxis(
00238                     abscissaData_.Start,
00239                     abscissaData_.Start + (double)(ordinateData_.Count - 1) * abscissaData_.Step);
00240             }
00241         }
00242 
00243 
00247         public class AxisSuggester_Auto : IAxisSuggester
00248         {
00249             
00250                         IList ordinateData_;
00251 
00256             public AxisSuggester_Auto(IList ordinateData)
00257             {
00258                 ordinateData_ = ordinateData;
00259             }
00260 
00265             public Axis Get()
00266             {
00267                 return new LinearAxis(0, ordinateData_.Count - 1);
00268             }
00269         }
00270 
00271 
00275         public class AxisSuggester_RowAuto : IAxisSuggester
00276         {
00277             DataRowCollection ordinateData_;
00278 
00283             public AxisSuggester_RowAuto(DataRowCollection ordinateData)
00284             {
00285                 ordinateData_ = ordinateData;
00286             }
00287 
00292             public Axis Get()
00293             {
00294                 return new LinearAxis(0, ordinateData_.Count - 1);
00295             }
00296         }
00297 
00301         public class AxisSuggester_Rows : IAxisSuggester
00302         {
00303             DataRowCollection rows_;
00304             string columnName_;
00305 
00311             public AxisSuggester_Rows(DataRowCollection rows, string columnName)
00312             {
00313                 rows_ = rows;
00314                 columnName_ = columnName;
00315             }
00316 
00321             public Axis Get()
00322             {
00323                 double min;
00324                 double max;
00325 
00326                 if (Utils.RowArrayMinMax(rows_, out min, out max, columnName_))
00327                 {
00328                     if ((rows_[0])[columnName_] is DateTime)
00329                     {
00330                         return new DateTimeAxis(min, max);
00331                     }
00332 
00333                     else
00334                     {
00335                         return new LinearAxis(min, max);
00336                     }
00337                 }
00338 
00339                 return new LinearAxis(0.0, 1.0);
00340             }
00341         }
00342 
00343 
00347         public class AxisSuggester_DataView : IAxisSuggester
00348         {
00349             DataView data_;
00350             string columnName_;
00351 
00357             public AxisSuggester_DataView(DataView data, string columnName)
00358             {
00359                 data_ = data;
00360                 columnName_ = columnName;
00361             }
00362 
00367             public Axis Get()
00368             {
00369                 double min;
00370                 double max;
00371 
00372                 if (Utils.DataViewArrayMinMax(data_, out min, out max, columnName_))
00373                 {
00374                     if ((data_[0])[columnName_] is DateTime)
00375                     {
00376                         return new DateTimeAxis(min, max);
00377                     }
00378 
00379                     else
00380                     {
00381                         return new LinearAxis(min, max);
00382                     }
00383                 }
00384 
00385                 return new LinearAxis(0.0, 1.0);
00386             }
00387         }
00388 
00389         #endregion
00390         #region Counters
00391 
00395         public interface ICounter
00396         {
00401             int Count { get; }
00402         }
00403 
00407         public class Counter_IList : ICounter
00408         {
00409             private IList data_;
00410 
00415             public Counter_IList(IList data)
00416             {
00417                 data_ = data;
00418             }
00419 
00424             public int Count
00425             {
00426                 get
00427                 {
00428                     return data_.Count;
00429                 }
00430             }
00431         }
00432 
00436         public class Counter_Null : ICounter
00437         {
00442             public int Count
00443             {
00444                 get
00445                 {
00446                     return 0;
00447                 }
00448             }
00449         }
00450 
00454         public class Counter_Rows : ICounter
00455         {
00456             DataRowCollection rows_;
00457 
00462             public Counter_Rows(DataRowCollection rows)
00463             {
00464                 rows_ = rows;
00465             }
00466 
00471             public int Count
00472             {
00473                 get
00474                 {
00475                     return rows_.Count;
00476                 }
00477             }
00478         }
00479 
00483         public class Counter_DataView : ICounter
00484         {
00485             DataView dataView_;
00486 
00491             public Counter_DataView(DataView dataView)
00492             {
00493                 dataView_ = dataView;
00494             }
00495 
00500             public int Count
00501             {
00502                 get
00503                 {
00504                     return dataView_.Count;
00505                 }
00506             }
00507 
00508         }
00509 
00510         #endregion
00511         #region DataGetters
00512 
00519                 public interface IDataGetter
00520         {
00526             double Get(int i);
00527         }
00528 
00532         public class DataGetter_IList : IDataGetter
00533         {
00534             private IList data_;
00535             
00540             public DataGetter_IList(IList data)
00541             {
00542                 data_ = data;
00543             }
00544 
00550             public double Get(int i)
00551             {
00552                 return Utils.ToDouble(data_[i]);
00553             }
00554         }
00555 
00559         public class DataGetter_Null : IDataGetter
00560         {
00566             public double Get(int i)
00567             {
00568                 throw new NPlotException( "No Data!" );
00569             }
00570         }
00571 
00575         public class DataGetter_StartStep : IDataGetter
00576         {
00577             StartStep data_;
00578 
00583             public DataGetter_StartStep(StartStep data)
00584             {
00585                 data_ = data;
00586             }
00587 
00593             public double Get(int i)
00594             {
00595                 return data_.Start + (double)(i) * data_.Step;
00596             }
00597         }
00598 
00602         public class DataGetter_Count : IDataGetter
00603         {
00609             public double Get(int i)
00610             {
00611                 return (double)i;
00612             }
00613         }
00614 
00615 
00619         public class DataGetter_Rows : IDataGetter
00620         {
00621             private DataRowCollection rows_;
00622             private string columnName_;
00623 
00629             public DataGetter_Rows(DataRowCollection rows, string columnName)
00630             {
00631                 rows_ = rows;
00632                 columnName_ = columnName;
00633             }
00634 
00640             public double Get(int i)
00641             {
00642                 return Utils.ToDouble((rows_[i])[columnName_]);
00643             }
00644         }
00645 
00649         public class DataGetter_DataView : IDataGetter
00650         {
00651             private DataView data_;
00652             private string columnName_;
00653 
00659             public DataGetter_DataView(DataView data, string columnName)
00660             {
00661                 data_ = data;
00662                 columnName_ = columnName;
00663             }
00664 
00670             public double Get(int i)
00671             {
00672                 return Utils.ToDouble((data_[i])[columnName_]);
00673             }
00674 
00675         }
00676 
00677                 
00682                 public class DataGetter_MultiRows 
00683                 {
00684                         
00685                         DataRowCollection rows_;
00686                         string abscissaName_;
00687                         int abscissaColumnNumber_;
00688 
00694                         public DataGetter_MultiRows(DataRowCollection rows, string omitThisColumn )
00695                         {
00696                                 rows_ = rows;
00697                                 abscissaName_ = omitThisColumn;
00698 
00699                                 abscissaColumnNumber_ = rows_[0].Table.Columns.IndexOf( omitThisColumn );
00700                                 if (abscissaColumnNumber_ < 0)
00701                                         throw new NPlotException( "invalid column name" );
00702                         }
00703 
00707                         public int Count
00708                         {
00709                                 get
00710                                 {
00711                                         return rows_[0].Table.Columns.Count-1;
00712                                 }
00713                         }
00714 
00721                         public double PointAt( int index, int seriesIndex )
00722                         {
00723                                 if (seriesIndex < abscissaColumnNumber_)
00724                                         return Utils.ToDouble( rows_[index][seriesIndex] );
00725                                 else
00726                                         return Utils.ToDouble( rows_[index][seriesIndex+1] );
00727                         }
00728 
00729                 }
00730 
00731 
00732         #endregion
00733 
00734     }
00735 }

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