PlotViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. using Aitex.UI.Charting.Command;
  7. using Abt.Controls.SciChart;
  8. using Aitex.UI.Charting.View;
  9. using Abt.Controls.SciChart.Visuals;
  10. using DataAnalysisControl.Core;
  11. namespace Aitex.UI.Charting.ViewModel
  12. {
  13. public class PlotViewModel : ChartingBaseViewModel
  14. {
  15. public PlotViewModel(PlotView plot)
  16. {
  17. _plot = plot;
  18. _chart = plot.sciChart;
  19. CommonViewModel = CommonViewModel.Instance;
  20. ResetXAxisRange2DataSourceRange = new ChartingCommand((o) => true, (o) => ResetXAxisRange2DataSourceTime(o));
  21. ToggleOverViewPanel = new ChartingCommand((o) => true, (o) =>
  22. {
  23. //在overview模式下,x轴的autorange需关闭,否则在overview模式下不能进行拖拽操作
  24. OverViewPanelHeight = OverViewPanelHeight > 0 ? 0 : 50;
  25. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  26. _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  27. InvokePropertyChanged("OverViewPanelHeight");
  28. InvokePropertyChanged("IsOverViewPanelVisable");
  29. });
  30. OverViewPanelHeight = 0;
  31. ToggleAutoRangeCommand = new ChartingCommand((o) => true, (o) =>
  32. {
  33. if (_chart.XAxis.AutoRange == Abt.Controls.SciChart.Visuals.Axes.AutoRange.Always)
  34. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  35. else
  36. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Always;
  37. _chart.YAxis.AutoRange = _chart.XAxis.AutoRange;
  38. if (_chart.XAxis.AutoRange == Abt.Controls.SciChart.Visuals.Axes.AutoRange.Always)
  39. {
  40. //同时自动Y轴缩放
  41. //在overview模式下,x轴的autorange需关闭,否则在overview模式下不能进行拖拽操作
  42. OverViewPanelHeight = 0;
  43. InvokePropertyChanged("OverViewPanelHeight");
  44. InvokePropertyChanged("IsOverViewPanelVisable");
  45. //关闭滚轮滚动X、Y轴的功能
  46. _plot.mouseWheelZoomModifier.IsEnabled = false;
  47. }
  48. InvokePropertyChanged("ToggleAutoRangeCommand");
  49. });
  50. /*ToggleXYPanCommand = new ChartingCommand((o) => true, (o) =>
  51. {
  52. var isEnabled = (bool)o;
  53. _plot.zoomPanModifier.IsEnabled = isEnabled;
  54. if (_plot.zoomPanModifier.IsEnabled)
  55. {
  56. //X\Y轴的自动缩放取消
  57. _chart.XAxis.AutoRange = false;
  58. _chart.YAxis.AutoRange = false;
  59. //自动游标显示功能取消
  60. _plot.rolloverModifier.IsEnabled = false;
  61. }
  62. });*/
  63. ToggleXWheelZoomCommand = new ChartingCommand((o) => true, (o) =>
  64. {
  65. if (!_plot.mouseWheelZoomModifier.IsEnabled)
  66. {
  67. //关闭auto zoom功能,因为在Auto zoom模式下,不能滚轮对x、y轴进行缩放操作
  68. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  69. _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  70. _plot.mouseWheelZoomModifier.IsEnabled = true;
  71. _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XDirection;
  72. }
  73. else
  74. {
  75. if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XDirection)
  76. {
  77. _plot.mouseWheelZoomModifier.IsEnabled = false;
  78. }
  79. else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XYDirection)
  80. {
  81. _plot.mouseWheelZoomModifier.XyDirection = XyDirection.YDirection;
  82. }
  83. else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.YDirection)
  84. {
  85. _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XYDirection;
  86. }
  87. }
  88. });
  89. ToggleYWheelZoomCommand = new ChartingCommand((o) => true, (o) =>
  90. {
  91. if (!_plot.mouseWheelZoomModifier.IsEnabled)
  92. {
  93. //关闭auto zoom功能,因为在Auto zoom模式下,不能滚轮对x、y轴进行缩放操作
  94. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  95. _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  96. _plot.mouseWheelZoomModifier.IsEnabled = true;
  97. _plot.mouseWheelZoomModifier.XyDirection = XyDirection.YDirection;
  98. }
  99. else
  100. {
  101. if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XDirection)
  102. {
  103. _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XYDirection;
  104. }
  105. else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XYDirection)
  106. {
  107. _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XDirection;
  108. }
  109. else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.YDirection)
  110. {
  111. _plot.mouseWheelZoomModifier.IsEnabled = false;
  112. }
  113. }
  114. });
  115. ToggleVerticalLineCommand = new ChartingCommand((o) => true, (o) =>
  116. {
  117. ShowVerticalTimeLines((bool)o);
  118. });
  119. }
  120. public static SciChartSurface SciChart { get { return _chart; } }
  121. public static PlotView PlotView { get { return _plot; } }
  122. private static SciChartSurface _chart;
  123. private static PlotView _plot;
  124. public CommonViewModel CommonViewModel { get; private set; }
  125. public ICommand ResetXAxisRange2DataSourceRange { get; set; }
  126. public ICommand ToggleOverViewPanel { get; set; }
  127. public ICommand ToggleAutoRangeCommand { get; set; }
  128. //public ICommand ToggleXYPanCommand { get; set; }
  129. public ICommand ToggleXWheelZoomCommand { get; set; }
  130. public ICommand ToggleYWheelZoomCommand { get; set; }
  131. public ICommand ToggleVerticalLineCommand { get; set; }
  132. public double OverViewPanelHeight { get; set; }
  133. public bool IsOverViewPanelVisable { get { return OverViewPanelHeight > 0; } }
  134. /// <summary>
  135. /// 显示时间线
  136. /// </summary>
  137. /// <param name="show"></param>
  138. public static void ShowVerticalTimeLines(bool show)
  139. {
  140. try
  141. {
  142. if (show)
  143. {
  144. if (_plot.vertical_Line1.IsHidden || _plot.vertical_Line2.IsHidden)
  145. {
  146. var minTime = (DateTime)_chart.XAxis.VisibleRange.Min;
  147. var maxTime = (DateTime)_chart.XAxis.VisibleRange.Max;
  148. var totalDiff = (maxTime - minTime).TotalSeconds;
  149. _plot.vertical_Line1.X1 = minTime + new TimeSpan(0, 0, (int)(totalDiff / 4.0));
  150. _plot.vertical_Line2.X1 = maxTime - new TimeSpan(0, 0, (int)(totalDiff / 4.0));
  151. }
  152. _plot.vertical_Line1.IsHidden = false;
  153. _plot.vertical_Line2.IsHidden = false;
  154. _plot.sciChart.InvalidateElement();
  155. }
  156. else
  157. {
  158. _plot.vertical_Line1.IsHidden = true;
  159. _plot.vertical_Line2.IsHidden = true;
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. CONTEXT.WriteLog(ex);
  165. }
  166. }
  167. /// <summary>
  168. /// 当切换到Charting视图时,自动将时间轴进行调整
  169. /// </summary>
  170. /// <param name="param"></param>
  171. private void ResetXAxisRange2DataSourceTime(object param)
  172. {
  173. DateTime minT = DateTime.MaxValue;
  174. DateTime maxT = DateTime.MinValue;
  175. for (int i = 0; i < CommonViewModel.DataSourceList.Count; i++)
  176. {
  177. bool isSourceVisiable = false;
  178. foreach (MyLineSeries series in CommonViewModel.RenderableSeries)
  179. {
  180. if (series.IsVisible &&
  181. series.DataSource == CommonViewModel.DataSourceList[i])
  182. {
  183. isSourceVisiable = true;
  184. break;
  185. }
  186. }
  187. if (!isSourceVisiable)
  188. continue;
  189. var from = CommonViewModel.DataSourceList[i].BeginTime;
  190. var to = CommonViewModel.DataSourceList[i].EndTime;
  191. var timeMove = CommonViewModel.DataSourceList[i].TimeMove;
  192. if (from + timeMove < minT) minT = from + timeMove;
  193. if (to + timeMove > maxT) maxT = to + timeMove;
  194. }
  195. if (minT == DateTime.MaxValue || minT == DateTime.MinValue || maxT == DateTime.MinValue || maxT == DateTime.MaxValue || minT > maxT)
  196. {
  197. _chart.XAxis.VisibleRange = new DateRange(DateTime.Now - new TimeSpan(1, 0, 0), DateTime.Now + new TimeSpan(1, 0, 0));
  198. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  199. }
  200. else
  201. {
  202. _chart.XAxis.VisibleRange = new DateRange(minT - new TimeSpan(1, 0, 0), maxT + new TimeSpan(1, 0, 0));
  203. _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  204. _chart.YAxis.VisibleRange = new DoubleRange(0, 1000);
  205. _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Once;
  206. }
  207. }
  208. }
  209. }