PageRenderTime 49ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/MVT Charts for Silverlight/MVT Charts for Silverlight/Datamodel/DataModel.cs

http://MVTCharts.codeplex.com
C# | 254 lines | 150 code | 40 blank | 64 comment | 13 complexity | 5d9482535a2cdc98b92e36eec13d75c9 MD5 | raw file
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Documents;
  5. using System.Windows.Ink;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. using System.Windows.Shapes;
  10. using System.Collections.Generic;
  11. // 2008 by Mario Meir-Huber
  12. // Mail: mario_mh@vb-magazin.de
  13. // i-mameir@microsoft.com
  14. namespace ChartLibrary.Data
  15. {
  16. /// <summary>
  17. /// This is the main Data Object where the chart stores its data
  18. /// </summary>
  19. /// <history>
  20. /// 03/15/08: Design and Implementation for 0.1
  21. /// </history>
  22. public class DataModel : IDataChanged, IEnumerator<DataSource>, IEnumerable<DataSource>
  23. {
  24. private List<DataSource> _lst = null; //this stores the data
  25. private double _min = 0.0; //the minimal value over all data sources
  26. private double _max = 0.0; //the maximal value over all data sources
  27. private object _tag = null; //tag to store custom data
  28. private int _enumpositon = -1; //enumerator position
  29. public event ChartLibrary.Base.DataChangedEvent DataChanged; //delegate to changed data
  30. /// <summary>
  31. /// Initializes the Data Model
  32. /// </summary>
  33. public DataModel()
  34. {
  35. _lst = new List<DataSource>();
  36. }
  37. /// <summary>
  38. /// This is handled from the Child to Notify the Parent that data was changed
  39. /// </summary>
  40. /// <param name="sender"></param>
  41. public void NotifyDataChanged(object sender)
  42. {
  43. UpdateMinMax();
  44. if (null != DataChanged) DataChanged();
  45. }
  46. /// <summary>
  47. /// This updates the lowest and highest value
  48. /// </summary>
  49. /// <history>
  50. /// 03/18/08: Changed some logic
  51. /// </history>
  52. private void UpdateMinMax()
  53. {
  54. bool minset = false, maxset = false;
  55. if (_lst.Count < 1) return;
  56. for (int i = 0; i < _lst.Count; i++)
  57. {
  58. if (_lst[i].Length > 0)
  59. {
  60. if (!minset)
  61. {
  62. _min = _lst[i].Min;
  63. minset = true;
  64. }
  65. if (!maxset)
  66. {
  67. _max = _lst[i].Max;
  68. maxset = true;
  69. }
  70. if (_lst[i].Max > _max) _max = _lst[i].Max;
  71. if (_lst[i].Min < _min) _min = _lst[i].Min;
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// Adds a new Datasource to the Collection
  77. /// </summary>
  78. /// <returns>New Datasource Object</returns>
  79. public DataSource Add()
  80. {
  81. return Add("Unnamed Datasource");
  82. }
  83. /// <summary>
  84. /// Adds a new Datasource to the Collection
  85. /// </summary>
  86. /// <param name="name">Name of the object</param>
  87. /// <returns></returns>
  88. public DataSource Add(String name)
  89. {
  90. DataSource ds = new DataSource(this, name);
  91. return Add(ds);
  92. }
  93. /// <summary>
  94. /// Adds a new Datasource to the Collection
  95. /// </summary>
  96. /// <param name="data">Datasource Item</param>
  97. /// <returns></returns>
  98. public DataSource Add(DataSource data)
  99. {
  100. _lst.Add(data);
  101. return data;
  102. }
  103. /// <summary>
  104. /// Clears the list
  105. /// </summary>
  106. public void Clear()
  107. {
  108. _lst.Clear();
  109. }
  110. /// <summary>
  111. /// Removes an Item
  112. /// </summary>
  113. /// <param name="item"></param>
  114. public void Remove(DataSource item)
  115. {
  116. _lst.Remove(item);
  117. }
  118. /// <summary>
  119. /// Removes an item at the index
  120. /// </summary>
  121. /// <param name="index">Position</param>
  122. public void RemoveAt(int index)
  123. {
  124. _lst.RemoveAt(index);
  125. }
  126. /// <summary>
  127. /// Gets a Tag for the Class or sets it
  128. /// </summary>
  129. public object Tag
  130. {
  131. get { return _tag; }
  132. set { _tag = value; }
  133. }
  134. /// <summary>
  135. /// Gets the minimal value
  136. /// </summary>
  137. public double Min
  138. {
  139. get { return _min; }
  140. }
  141. /// <summary>
  142. /// Gets the maximum value
  143. /// </summary>
  144. public double Max
  145. {
  146. get { return _max; }
  147. }
  148. /// <summary>
  149. /// This returns the count of the Data Sources
  150. /// </summary>
  151. public int Count
  152. {
  153. get { return _lst.Count; }
  154. }
  155. /// <summary>
  156. /// Gets the Datasource at the specified position or sets it
  157. /// </summary>
  158. /// <param name="i">Index</param>
  159. /// <returns></returns>
  160. public DataSource this[int i]
  161. {
  162. get
  163. {
  164. if (i >= _lst.Count) throw new IndexOutOfRangeException("The index is out of the bounds of the array");
  165. return _lst[i];
  166. }
  167. set
  168. {
  169. if (i >= _lst.Count) throw new IndexOutOfRangeException("The index is out of the bounds of the array");
  170. _lst[i] = value;
  171. }
  172. }
  173. #region IEnumerator<DataSource> Members
  174. public DataSource Current
  175. {
  176. get { return _lst[_enumpositon]; }
  177. }
  178. #endregion
  179. #region IDisposable Members
  180. public void Dispose()
  181. {
  182. _enumpositon = -1;
  183. }
  184. #endregion
  185. #region IEnumerator Members
  186. object System.Collections.IEnumerator.Current
  187. {
  188. get { return _lst[_enumpositon]; }
  189. }
  190. public bool MoveNext()
  191. {
  192. _enumpositon++;
  193. if (_enumpositon < _lst.Count) return true;
  194. else return false;
  195. }
  196. public void Reset()
  197. {
  198. _enumpositon = -1;
  199. }
  200. #endregion
  201. #region IEnumerable<DataSource> Members
  202. public IEnumerator<DataSource> GetEnumerator()
  203. {
  204. return this;
  205. }
  206. #endregion
  207. #region IEnumerable Members
  208. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  209. {
  210. return this;
  211. }
  212. #endregion
  213. }
  214. }