1 Star 0 Fork 0

long080900317/nplot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
AdapterUtils.cs 24.17 KB
一键复制 编辑 原始数据 按行查看 历史
Matt Howlett 提交于 9年前 . initial commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
/*
* NPlot - A charting library for .NET
*
* AdapterUtils.cs
* Copyright (C) 2003-2006 Matt Howlett and others.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections;
using System.Data;
namespace NPlot
{
/// <summary>
/// Encapsulates functionality relating to exposing data in various
/// different data structures in a consistent way.
/// </summary>
/// <remarks>
/// It would be more efficient to have iterator style access
/// to the data, rather than index based, and Count.
/// </remarks>
public class AdapterUtils
{
#region AxisSuggesters
/// <summary>
/// Provides default axis if only data corresponding to orthogonal axis is provided.
/// </summary>
public class AxisSuggester_Auto : IAxisSuggester
{
private readonly IList ordinateData_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="ordinateData">Data corresponding to orthogonal axis.</param>
public AxisSuggester_Auto(IList ordinateData)
{
ordinateData_ = ordinateData;
}
/// <summary>
/// Calculates a suggested axis given the data specified in the constructor.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
if (ordinateData_ != null && ordinateData_.Count > 0)
{
return new LinearAxis(0, ordinateData_.Count - 1);
}
else
{
return new LinearAxis(0.0, 1.0);
}
}
}
/// <summary>
/// Provides axis suggestion for data in a particular column of a DataView.
/// </summary>
public class AxisSuggester_DataView : IAxisSuggester
{
private readonly string columnName_;
private readonly DataView data_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">DataView that contains data to suggest axis for</param>
/// <param name="columnName">the column of interest in the DataView</param>
public AxisSuggester_DataView(DataView data, string columnName)
{
data_ = data;
columnName_ = columnName;
}
/// <summary>
/// Calculates a suggested axis given the data specified in the constructor.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
double min;
double max;
if (Utils.DataViewArrayMinMax(data_, out min, out max, columnName_))
{
if ((data_[0])[columnName_] is DateTime)
{
return new DateTimeAxis(min, max);
}
else
{
return new LinearAxis(min, max);
}
}
return new LinearAxis(0.0, 1.0);
}
}
/// <summary>
/// This class gets an axis suitable for plotting the data contained in an IList.
/// </summary>
public class AxisSuggester_IList : IAxisSuggester
{
private readonly IList data_;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="data">the data we want to find a suitable axis for.</param>
public AxisSuggester_IList(IList data)
{
data_ = data;
}
/// <summary>
/// Calculates a suggested axis for the IList data.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
double min;
double max;
if (Utils.ArrayMinMax(data_, out min, out max))
{
if (data_[0] is DateTime)
{
return new DateTimeAxis(min, max);
}
else
{
return new LinearAxis(min, max);
}
// perhaps return LogAxis here if range large enough
// + other constraints?
}
return new LinearAxis(0.0, 1.0);
}
}
/// <summary>
/// Implements functionality for suggesting an axis suitable for charting
/// data in multiple columns of a DataRowCollection.
/// </summary>
/// <remarks>This is currently not used.</remarks>
public class AxisSuggester_MultiColumns : IAxisSuggester
{
private readonly string abscissaName_;
private readonly DataRowCollection rows_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="rows">The DataRowCollection containing the data.</param>
/// <param name="abscissaName">the column with this name is not considered</param>
public AxisSuggester_MultiColumns(DataRowCollection rows, string abscissaName)
{
rows_ = rows;
abscissaName_ = abscissaName;
}
/// <summary>
/// Calculates a suggested axis for the DataRowCollection data.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
double t_min = double.MaxValue;
double t_max = double.MinValue;
IEnumerator en = rows_[0].Table.Columns.GetEnumerator();
while (en.MoveNext())
{
string colName = ((DataColumn) en.Current).Caption;
if (colName == abscissaName_)
{
continue;
}
double min;
double max;
if (Utils.RowArrayMinMax(rows_, out min, out max, colName))
{
if (min < t_min)
{
t_min = min;
}
if (max > t_max)
{
t_max = max;
}
}
}
return new LinearAxis(t_min, t_max);
}
}
/// <summary>
/// This class is responsible for supplying a default axis via the IAxisSuggester interface.
/// </summary>
public class AxisSuggester_Null : IAxisSuggester
{
/// <summary>
/// Returns a default axis.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
return new LinearAxis(0.0, 1.0);
}
}
/// <summary>
/// Provides default axis if only data corresponding to orthogonal axis is provided.
/// </summary>
public class AxisSuggester_RowAuto : IAxisSuggester
{
private readonly DataRowCollection ordinateData_;
/// <summary>
/// Construbtor
/// </summary>
/// <param name="ordinateData">Data corresponding to orthogonal axis.</param>
public AxisSuggester_RowAuto(DataRowCollection ordinateData)
{
ordinateData_ = ordinateData;
}
/// <summary>
/// Calculates a suggested axis given the data specified in the constructor.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
if (ordinateData_ != null && ordinateData_.Count > 0)
{
return new LinearAxis(0, ordinateData_.Count - 1);
}
else
{
return new LinearAxis(0.0, 1.0);
}
}
}
/// <summary>
/// Provides axis for data in a given column of a DataRowCollection.
/// </summary>
public class AxisSuggester_Rows : IAxisSuggester
{
private readonly string columnName_;
private readonly DataRowCollection rows_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="rows">DataRowCollection containing the data to suggest axis for.</param>
/// <param name="columnName">the column to get data.</param>
public AxisSuggester_Rows(DataRowCollection rows, string columnName)
{
rows_ = rows;
columnName_ = columnName;
}
/// <summary>
/// Calculates a suggested axis given the data specified in the constructor.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
double min;
double max;
if (Utils.RowArrayMinMax(rows_, out min, out max, columnName_))
{
if ((rows_[0])[columnName_] is DateTime)
{
return new DateTimeAxis(min, max);
}
else
{
return new LinearAxis(min, max);
}
}
return new LinearAxis(0.0, 1.0);
}
}
/// <summary>
/// This class gets an axis corresponding to a StartStep object. The data on
/// the orthogonal axis is of course also needed to calculate this.
/// </summary>
public class AxisSuggester_StartStep : IAxisSuggester
{
private readonly StartStep abscissaData_;
private readonly IList ordinateData_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="axisOfInterest">StartStep object corresponding to axis of interest</param>
/// <param name="otherAxisData">data of other axis (needed to get count value)</param>
public AxisSuggester_StartStep(StartStep axisOfInterest, IList otherAxisData)
{
ordinateData_ = otherAxisData;
abscissaData_ = axisOfInterest;
}
/// <summary>
/// Calculates a suggested axis given the data specified in the constructor.
/// </summary>
/// <returns>the suggested axis</returns>
public Axis Get()
{
if (ordinateData_ != null && ordinateData_.Count > 0)
{
return new LinearAxis(
abscissaData_.Start,
abscissaData_.Start + (ordinateData_.Count - 1)*abscissaData_.Step);
}
else
{
return new LinearAxis(0.0, 1.0);
}
}
}
/// <summary>
/// Interface for classes that can suggest an axis for data they contain.
/// </summary>
public interface IAxisSuggester
{
/// <summary>
/// Calculates a suggested axis for the data contained by the implementing class.
/// </summary>
/// <returns>the suggested axis</returns>
Axis Get();
}
#endregion
#region Counters
/// <summary>
/// Class that provides the number of items in a DataView via the ICounter interface.
/// </summary>
public class Counter_DataView : ICounter
{
private readonly DataView dataView_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="dataView">the DataBiew data to provide count of number of rows of.</param>
public Counter_DataView(DataView dataView)
{
dataView_ = dataView;
}
/// <summary>
/// Number of data items in container.
/// </summary>
/// <value>Number of data items in container.</value>
public int Count
{
get { return dataView_.Count; }
}
}
/// <summary>
/// Class that provides the number of items in an IList via the ICounter interface.
/// </summary>
public class Counter_IList : ICounter
{
private readonly IList data_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">the IList data to provide count of</param>
public Counter_IList(IList data)
{
data_ = data;
}
/// <summary>
/// Number of data items in container.
/// </summary>
/// <value>Number of data items in container.</value>
public int Count
{
get { return data_.Count; }
}
}
/// <summary>
/// Class that returns 0 via the ICounter interface.
/// </summary>
public class Counter_Null : ICounter
{
/// <summary>
/// Number of data items in container.
/// </summary>
/// <value>Number of data items in container.</value>
public int Count
{
get { return 0; }
}
}
/// <summary>
/// Class that provides the number of items in a DataRowCollection via the ICounter interface.
/// </summary>
public class Counter_Rows : ICounter
{
private readonly DataRowCollection rows_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="rows">the DataRowCollection data to provide count of number of rows of.</param>
public Counter_Rows(DataRowCollection rows)
{
rows_ = rows;
}
/// <summary>
/// Number of data items in container.
/// </summary>
/// <value>Number of data items in container.</value>
public int Count
{
get { return rows_.Count; }
}
}
/// <summary>
/// Interface that enables a dataholding class to report how many data items it holds.
/// </summary>
public interface ICounter
{
/// <summary>
/// Number of data items in container.
/// </summary>
/// <value>Number of data items in container.</value>
int Count { get; }
}
#endregion
#region DataGetters
/// <summary>
/// Provides the natural numbers (and 0) via the IDataGetter interface.
/// </summary>
public class DataGetter_Count : IDataGetter
{
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
return i;
}
}
/// <summary>
/// Provides data in a DataView via the IDataGetter interface.
/// </summary>
public class DataGetter_DataView : IDataGetter
{
private readonly string columnName_;
private readonly DataView data_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">DataView to get data from.</param>
/// <param name="columnName">Get data in this column</param>
public DataGetter_DataView(DataView data, string columnName)
{
data_ = data;
columnName_ = columnName;
}
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
return Utils.ToDouble((data_[i])[columnName_]);
}
}
/// <summary>
/// Provides data in an array of doubles via the IDataGetter interface.
/// </summary>
/// <remarks>
/// A speed-up version of DataDetter_IList; no boxing/unboxing overhead.
/// </remarks>
public class DataGetter_DoublesArray : IDataGetter
{
private readonly double[] data_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">array of doubles that contains the data</param>
public DataGetter_DoublesArray(double[] data)
{
data_ = data;
}
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
return data_[i];
}
}
/// <summary>
/// Provides data in an IList via the IDataGetter interface.
/// </summary>
public class DataGetter_IList : IDataGetter
{
private readonly IList data_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">IList that contains the data</param>
public DataGetter_IList(IList data)
{
data_ = data;
}
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
return Utils.ToDouble(data_[i]);
}
}
/// <summary>
/// Gets data
/// </summary>
/// <remarks>Note: Does not implement IDataGetter... Currently this class is not used.</remarks>
public class DataGetter_MultiRows
{
private readonly int abscissaColumnNumber_;
private readonly DataRowCollection rows_;
//private string abscissaName_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="rows">DataRowCollection to get data from.</param>
/// <param name="omitThisColumn">don't get data from this column</param>
public DataGetter_MultiRows(DataRowCollection rows, string omitThisColumn)
{
rows_ = rows;
//abscissaName_ = omitThisColumn;
abscissaColumnNumber_ = rows_[0].Table.Columns.IndexOf(omitThisColumn);
if (abscissaColumnNumber_ < 0)
throw new NPlotException("invalid column name");
}
/// <summary>
/// Number of data points
/// </summary>
public int Count
{
get { return rows_[0].Table.Columns.Count - 1; }
}
/// <summary>
/// Gets data at a given index, in the given series (column number).
/// </summary>
/// <param name="index">index in the series to get data for</param>
/// <param name="seriesIndex">series number (column number) to get data for.</param>
/// <returns>the required data point.</returns>
public double PointAt(int index, int seriesIndex)
{
if (seriesIndex < abscissaColumnNumber_)
return Utils.ToDouble(rows_[index][seriesIndex]);
else
return Utils.ToDouble(rows_[index][seriesIndex + 1]);
}
}
/// <summary>
/// Provides no data.
/// </summary>
public class DataGetter_Null : IDataGetter
{
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
throw new NPlotException("No Data!");
}
}
/// <summary>
/// Provides data in a DataRowCollection via the IDataGetter interface.
/// </summary>
public class DataGetter_Rows : IDataGetter
{
private readonly string columnName_;
private readonly DataRowCollection rows_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="rows">DataRowCollection to get data from</param>
/// <param name="columnName">Get data in this column</param>
public DataGetter_Rows(DataRowCollection rows, string columnName)
{
rows_ = rows;
columnName_ = columnName;
}
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
return Utils.ToDouble((rows_[i])[columnName_]);
}
}
/// <summary>
/// Provides data points from a StartStep object via the IDataGetter interface.
/// </summary>
public class DataGetter_StartStep : IDataGetter
{
private readonly StartStep data_;
/// <summary>
/// Constructor
/// </summary>
/// <param name="data">StartStep to derive data from.</param>
public DataGetter_StartStep(StartStep data)
{
data_ = data;
}
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
public double Get(int i)
{
return data_.Start + (i)*data_.Step;
}
}
/// <summary>
/// Interface for data holding classes that allows users to get the ith value.
/// </summary>
/// <remarks>
/// TODO: should change this to GetNext() and Reset() for more generality.
/// </remarks>
public interface IDataGetter
{
/// <summary>
/// Gets the ith data value.
/// </summary>
/// <param name="i">sequence number of data to get.</param>
/// <returns>ith data value.</returns>
double Get(int i);
}
#endregion
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/long080900317/nplot.git
git@gitee.com:long080900317/nplot.git
long080900317
nplot
nplot
master

搜索帮助

371d5123 14472233 46e8bd33 14472233