File size: 9,291 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//============================================================================ 
//DataSourcePointList Class 
//Copyright � 2006 John Champion, Jerry Vos 
// 
//This library is free software; you can redistribute it and/or 
//modify it under the terms of the GNU General Public 
//License as published by the Free Software Foundation; either 
//version 2.1 of the License, or (at your option) any later version. 
// 
//This library is distributed in the hope that it will be useful, 
//but WITHOUT ANY WARRANTY; without even the implied warranty of 
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
//General Public License for more details. 
// 
//You should have received a copy of the GNU General Public 
//License along with this library; if not, write to the Free Software 
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//============================================================================= 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using System.Data;

namespace ZedGraph
{
	/// <summary> 
	///  
	/// </summary> 
	/// <seealso cref="IPointList" /> 
	/// <seealso cref="IPointListEdit" /> 
	///  
	/// <author>John Champion</author> 
	/// <version> $Revision: 3.7 $ $Date: 2007-11-05 04:33:26 $ </version> 
	[Serializable]
	public class DataSourcePointList : IPointList
	{
		private BindingSource _bindingSource;

		//private object _dataSource = null; 
		private string _xDataMember = null;
		private string _yDataMember = null;
		private string _zDataMember = null;
		private string _tagDataMember = null;

		#region Properties

		/// <summary> 
		/// Indexer to access the specified <see cref="PointPair"/> object by 
		/// its ordinal position in the list. 
		/// </summary> 
		/// <param name="index">The ordinal position (zero-based) of the 
		/// <see cref="PointPair"/> object to be accessed.</param> 
		/// <value>A <see cref="PointPair"/> object reference.</value> 
		public PointPair this[int index]
		{
			get
			{
				if ( index < 0 || index >= _bindingSource.Count )
					throw new System.ArgumentOutOfRangeException( "Error: Index out of range" );

				object row = _bindingSource[index];

				double x = GetDouble( row, _xDataMember, index );
				double y = GetDouble( row, _yDataMember, index );
				double z = GetDouble( row, _zDataMember, index );
				object tag = GetObject( row, _tagDataMember );

				PointPair pt = new PointPair( x, y, z );
				pt.Tag = tag;
				return pt;
			}
		}

		/// <summary> 
		/// gets the number of points available in the list 
		/// </summary> 
		public int Count
		{
			get
			{
				if ( _bindingSource != null )
					return _bindingSource.Count;
				else
					return 0;
			}
		}

		/// <summary> 
		/// The <see cref="BindingSource" /> object from which to get the bound data 
		/// </summary> 
		/// <remarks> 
		/// Typically, you set the <see cref="System.Windows.Forms.BindingSource.DataSource" /> 
		/// property to a reference to your database, table or list object. The 
		/// <see cref="System.Windows.Forms.BindingSource.DataMember" /> property would be set 
		/// to the name of the datatable within the 
		/// <see cref="System.Windows.Forms.BindingSource.DataSource" />, 
		/// if applicable.</remarks> 
		public BindingSource BindingSource
		{
			get { return _bindingSource; }
		}

		/// <summary> 
		/// The table or list object from which to extract the data values. 
		/// </summary> 
		/// <remarks> 
		/// This property is just an alias for 
		/// <see cref="System.Windows.Forms.BindingSource.DataSource" />. 
		/// </remarks> 
		public object DataSource
		{
			get { return _bindingSource.DataSource; }
			set { _bindingSource.DataSource = value; }
		}

		/// <summary> 
		/// The <see cref="string" /> name of the property or column from which to obtain the 
		/// X data values for the chart. 
		/// </summary> 
		/// <remarks>Set this to null leave the X data values set to <see cref="PointPairBase.Missing" /> 
		/// </remarks> 
		public string XDataMember
		{
			get { return _xDataMember; }
			set { _xDataMember = value; }
		}

		/// <summary> 
		/// The <see cref="string" /> name of the property or column from which to obtain the 
		/// Y data values for the chart. 
		/// </summary> 
		/// <remarks>Set this to null leave the Y data values set to <see cref="PointPairBase.Missing" /> 
		/// </remarks> 
		public string YDataMember
		{
			get { return _yDataMember; }
			set { _yDataMember = value; }
		}

		/// <summary> 
		/// The <see cref="string" /> name of the property or column from which to obtain the 
		/// Z data values for the chart. 
		/// </summary> 
		/// <remarks>Set this to null leave the Z data values set to <see cref="PointPairBase.Missing" /> 
		/// </remarks> 
		public string ZDataMember
		{
			get { return _zDataMember; }
			set { _zDataMember = value; }
		}

		/// <summary> 
		/// The <see cref="string" /> name of the property or column from which to obtain the 
		/// tag values for the chart. 
		/// </summary> 
		/// <remarks>Set this to null leave the tag values set to null. If this references string 
		/// data, then the tags may be used as tooltips using the 
		/// <see cref="ZedGraphControl.IsShowPointValues" /> option. 
		/// </remarks> 
		public string TagDataMember
		{
			get { return _tagDataMember; }
			set { _tagDataMember = value; }
		}

		#endregion

		#region Constructors

		/// <summary> 
		/// Default Constructor 
		/// </summary> 
		public DataSourcePointList()
		{
			_bindingSource = new BindingSource();
			_xDataMember = string.Empty;
			_yDataMember = string.Empty;
			_zDataMember = string.Empty;
			_tagDataMember = string.Empty;
		}

		/// <summary> 
		/// Constructor to initialize the DataSourcePointList from an 
		/// existing <see cref="DataSourcePointList" /> 
		/// </summary> 
		public DataSourcePointList( DataSourcePointList rhs )
			: this()
		{
			_bindingSource.DataSource = rhs._bindingSource.DataSource;
			if ( rhs._xDataMember != null )
				_xDataMember = (string)rhs._xDataMember.Clone();
			if ( rhs._yDataMember != null )
				_yDataMember = (string)rhs._yDataMember.Clone();
			if ( rhs._zDataMember != null )
				_zDataMember = (string)rhs._zDataMember.Clone();
			if ( rhs._tagDataMember != null )
				_tagDataMember = (string)rhs._tagDataMember.Clone();
		}

		/// <summary> 
		/// Implement the <see cref="ICloneable" /> interface in a typesafe manner by just 
		/// calling the typed version of <see cref="Clone" /> 
		/// </summary> 
		/// <returns>A deep copy of this object</returns> 
		object ICloneable.Clone()
		{
			return this.Clone();
		}

		/// <summary> 
		/// Typesafe, deep-copy clone method. 
		/// </summary> 
		/// <returns>A new, independent copy of this class</returns> 
		public DataSourcePointList Clone()
		{
			return new DataSourcePointList( this );
		}


		#endregion

		#region Methods

		/// <summary> 
		/// Extract a double value from the specified table row or data object with the 
		/// specified column name. 
		/// </summary> 
		/// <param name="row">The data object from which to extract the value</param> 
		/// <param name="dataMember">The property name or column name of the value 
		/// to be extracted</param> 
		/// <param name="index">The zero-based index of the point to be extracted. 
		/// </param> 
		private double GetDouble( object row, string dataMember, int index )
		{
			if ( dataMember == null || dataMember == string.Empty )
				return index + 1;

			//Type myType = row.GetType();
			DataRowView drv = row as DataRowView;
			PropertyInfo pInfo = null;
			if ( drv == null )
				pInfo = row.GetType().GetProperty( dataMember );

			object val = null;

			if ( pInfo != null )
				val = pInfo.GetValue( row, null );
			else if ( drv != null )
				val = drv[dataMember];
			else if ( pInfo == null )
				throw new System.Exception( "Can't find DataMember '" + dataMember + "' in DataSource" );

			// if ( val == null ) 
			// throw new System.Exception( "Can't find DataMember '" + dataMember + "' in DataSource" ); 

			double x;
			if ( val == null || val == DBNull.Value )
				x = PointPair.Missing;
			else if ( val.GetType() == typeof( DateTime ) )
				x = ( (DateTime)val ).ToOADate();
			else if ( val.GetType() == typeof( string ) )
				x = index + 1;
			else
				x = Convert.ToDouble( val );

			return x;
		}

		/// <summary> 
		/// Extract an object from the specified table row or data object with the 
		/// specified column name. 
		/// </summary> 
		/// <param name="row">The data object from which to extract the object</param> 
		/// <param name="dataMember">The property name or column name of the object 
		/// to be extracted</param> 
		private object GetObject( object row, string dataMember )
		{
			if ( dataMember == null || dataMember == string.Empty )
				return null;

			PropertyInfo pInfo = row.GetType().GetProperty( dataMember );
			DataRowView drv = row as DataRowView;

			object val = null;

			if ( pInfo != null )
				val = pInfo.GetValue( row, null );
			else if ( drv != null )
				val = drv[dataMember];

			if ( val == null )
				throw new System.Exception( "Can't find DataMember '" + dataMember + "' in DataSource" );

			return val;
		}


		#endregion
	}
}