ChartDataSet.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using MuchInfo.Chart.Data.Interfaces;
  2. using MuchInfo.Chart.Data.Models;
  3. using MuchInfo.Chart.Infrastructure.Data;
  4. using MuchInfo.Chart.Infrastructure.EventArgs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Windows.Media;
  9. namespace MuchInfo.Chart.WPF.Primitives
  10. {
  11. /// <summary>
  12. /// 实现数据集合接口
  13. /// </summary>
  14. public class ChartDataSet : ILineDataSet
  15. {
  16. #region Constructors
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="ChartDataSet" /> class.
  19. /// </summary>
  20. /// <param name="points">The points.</param>
  21. public ChartDataSet(List<ILineDataPoint> points)
  22. {
  23. DataPoints = points;
  24. }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="ChartDataSet"/> class.
  27. /// </summary>
  28. internal ChartDataSet()
  29. {
  30. DataPoints = new List<ILineDataPoint>();
  31. }
  32. #endregion Constructors
  33. #region Events
  34. /// <summary>
  35. /// 数据改变事件
  36. /// </summary>
  37. public event LineDataSetDataChangedEventHandler DataChanged;
  38. #endregion Events
  39. #region Properties
  40. #region Public Properties
  41. /// <summary>
  42. /// 数据点集合
  43. /// </summary>
  44. public List<ILineDataPoint> DataPoints
  45. {
  46. get;
  47. set;
  48. }
  49. #endregion Public Properties
  50. #endregion Properties
  51. #region Methods
  52. #region Public Methods
  53. /// <summary>
  54. /// Adds the bar data point.
  55. /// </summary>
  56. /// <param name="date">The date.</param>
  57. /// <param name="open">The open.</param>
  58. /// <param name="high">The high.</param>
  59. /// <param name="low">The low.</param>
  60. /// <param name="close">The close.</param>
  61. /// <param name="volume">The volume.</param>
  62. /// <param name="turnOver">The turn over.</param>
  63. /// <param name="holdVolume">持仓量</param>
  64. /// <param name="increase">The increase.</param>
  65. public void AddBarDataPoint(DateTime date, float open, float high, float low, float close, float volume, float turnOver, float holdVolume, float increase)
  66. {
  67. this.DataPoints.Add(new BarDataPoint(date, open, high, low, close, volume, turnOver, holdVolume, increase));
  68. }
  69. /// <summary>
  70. /// Inserts the bar data point.
  71. /// </summary>
  72. /// <param name="index">The index.</param>
  73. /// <param name="date">The date.</param>
  74. /// <param name="open">The open.</param>
  75. /// <param name="high">The high.</param>
  76. /// <param name="low">The low.</param>
  77. /// <param name="close">The close.</param>
  78. /// <param name="volume">The volume.</param>
  79. /// <param name="turnOver">The turn over.</param>
  80. /// <param name="holdVolume">The hold volume.</param>
  81. /// <param name="increase">The increase.</param>
  82. public void InsertBarDataPoint(int index, DateTime date, float open, float high, float low, float close, float volume, float turnOver, float holdVolume, float increase)
  83. {
  84. this.DataPoints.Insert(index, new BarDataPoint(date, open, high, low, close, volume, turnOver, holdVolume, increase));
  85. }
  86. /// <summary>
  87. /// Adds the color data point.
  88. /// </summary>
  89. /// <param name="date">The date.</param>
  90. /// <param name="value">The value.</param>
  91. /// <param name="color">The color.</param>
  92. /// <param name="increase">The increase.</param>
  93. public void AddColorDataPoint(DateTime date, float value, Color color, float increase)
  94. {
  95. this.DataPoints.Add(new ColoredDataPoint(date, value, value, 0, value, color, increase));
  96. }
  97. /// <summary>
  98. /// Adds the colored bar data point.
  99. /// </summary>
  100. /// <param name="date">The date.</param>
  101. /// <param name="open">The open.</param>
  102. /// <param name="high">The high.</param>
  103. /// <param name="low">The low.</param>
  104. /// <param name="close">The close.</param>
  105. /// <param name="volume">The volume.</param>
  106. /// <param name="color">The color.</param>
  107. /// <param name="turnOver">The turn over.</param>
  108. /// <param name="holdVolume">The hold volume.</param>
  109. /// <param name="increase">The increase.</param>
  110. public void AddColoredBarDataPoint(DateTime date, float open, float high, float low, float close, float volume, Color color, float turnOver, float holdVolume, float increase)
  111. {
  112. this.DataPoints.Add(new ColoredBarDataPoint(date, open, high, low, close, volume, color, turnOver, holdVolume, increase));
  113. }
  114. /// <summary>
  115. /// Adds the colored data point.
  116. /// </summary>
  117. /// <param name="date">The date.</param>
  118. /// <param name="open">The open.</param>
  119. /// <param name="high">The high.</param>
  120. /// <param name="low">The low.</param>
  121. /// <param name="close">The close.</param>
  122. /// <param name="color">The color.</param>
  123. /// <param name="increase">The increase.</param>
  124. public void AddColoredDataPoint(DateTime date, float open, float high, float low, float close, Color color, float increase)
  125. {
  126. this.DataPoints.Add(new ColoredDataPoint(date, open, high, low, close, color, increase));
  127. }
  128. /// <summary>
  129. /// Adds the OHLCD data point.
  130. /// </summary>
  131. /// <param name="date">The date.</param>
  132. /// <param name="value">The value.</param>
  133. public void AddOHLCDDataPoint(DateTime date, float value)
  134. {
  135. this.DataPoints.Add(new OHLCDataPoint(date, value, value, 0, value));
  136. }
  137. /// <summary>
  138. /// Adds the OHLCD data point.
  139. /// </summary>
  140. /// <param name="date">The date.</param>
  141. /// <param name="open">The open.</param>
  142. /// <param name="high">The high.</param>
  143. /// <param name="low">The low.</param>
  144. /// <param name="close">The close.</param>
  145. public void AddOHLCDDataPoint(DateTime date, float open, float high, float low, float close)
  146. {
  147. this.DataPoints.Add(new OHLCDataPoint(date, open, high, low, close));
  148. }
  149. /// <summary>
  150. /// 获取集合中的bar集合
  151. /// </summary>
  152. public IList<IOHLCDataPoint> BarData()
  153. {
  154. if (DataPoints == null || !DataPoints.Any()) return null;
  155. try
  156. {
  157. return DataPoints.Where(z => z is IOHLCDataPoint) as IList<IOHLCDataPoint>;
  158. }
  159. catch
  160. {
  161. return null;
  162. }
  163. }
  164. /// <summary>
  165. /// 检查是否包含OHLCDataPoint
  166. /// </summary>
  167. /// <param name="arg">参数</param>
  168. public bool IsIncludeOHLCDataPoint(DataChangedArgs arg)
  169. {
  170. var current = DataPoints.FirstOrDefault(z => z is IOHLCDataPoint && DateTime.Compare(z.Date, arg.OldDate) == 0);
  171. if (current == null) return false;
  172. current.Date = arg.NewDate;
  173. OnDataChanged(arg);
  174. return true;
  175. }
  176. /// <summary>
  177. /// Called when [delete].
  178. /// </summary>
  179. public void OnDelete()
  180. {
  181. this.DataPoints = null;
  182. }
  183. #endregion Public Methods
  184. #region Internal Methods
  185. /// <summary>
  186. /// Called when [data set data changed].
  187. /// </summary>
  188. /// <param name="dataSet">The data set.</param>
  189. internal void OnDataSetDataChanged(ILineDataSet dataSet)
  190. {
  191. if (dataSet == null) return;
  192. this.DataPoints = dataSet.DataPoints;
  193. OnDataChanged(null);
  194. }
  195. #endregion Internal Methods
  196. #region Private Methods
  197. /// <summary>
  198. /// 触发datachanged事件
  199. /// </summary>
  200. /// <param name="obj">The obj.</param>
  201. private void OnDataChanged(object obj)
  202. {
  203. if (DataChanged != null)
  204. {
  205. DataChanged(obj);
  206. }
  207. }
  208. #endregion Private Methods
  209. #endregion Methods
  210. }
  211. }