PlotView.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.UI.Charting.ViewModel;
  15. using Abt.Controls.SciChart;
  16. using DataAnalysisControl.Core;
  17. namespace Aitex.UI.Charting.View
  18. {
  19. /// <summary>
  20. /// Interaction logic for PlotView.xaml
  21. /// </summary>
  22. public partial class PlotView : UserControl
  23. {
  24. public PlotView()
  25. {
  26. InitializeComponent();
  27. _viewModel = new PlotViewModel(this);
  28. DataContext = _viewModel;
  29. IsVisibleChanged += new DependencyPropertyChangedEventHandler(PlotView_IsVisibleChanged);
  30. //Loaded += (s, e) => DataContext = _viewModel;
  31. sciChart.XAxis.VisibleRangeChanged += new EventHandler<Abt.Controls.SciChart.VisibleRangeChangedEventArgs>(Axis_VisibleRangeChanged);
  32. sciChart.YAxis.VisibleRangeChanged += new EventHandler<Abt.Controls.SciChart.VisibleRangeChangedEventArgs>(Axis_VisibleRangeChanged);
  33. }
  34. void PlotView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  35. {
  36. if ((bool)e.NewValue)
  37. {
  38. //设置Charting的主题模式
  39. //ThemeManager.SetTheme(sciChart, "ExpressionLight");
  40. //ThemeManager.SetTheme(sciChart, "Chrome");
  41. //ThemeManager.SetTheme(sciChart, "BlackSteel");
  42. //重置Charting的X轴显示区域
  43. _viewModel.ResetXAxisRange2DataSourceRange.Execute(sciChart);
  44. }
  45. }
  46. List<Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>> _rangeHistory = new List<Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>>();
  47. bool _skipUndo = false;
  48. void Axis_VisibleRangeChanged(object sender, Abt.Controls.SciChart.VisibleRangeChangedEventArgs e)
  49. {
  50. if (!_skipUndo)
  51. {
  52. try
  53. {
  54. //System.Diagnostics.Debug.Write(".");
  55. if (_rangeHistory.Count > 0 && DateTime.Now - _rangeHistory[_rangeHistory.Count - 1].Item5 < new TimeSpan(0, 0, 0, 0, 500))
  56. {
  57. var old = _rangeHistory[_rangeHistory.Count -1];
  58. _rangeHistory[_rangeHistory.Count - 1] =
  59. new Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>(old.Item1, old.Item2,
  60. old.Item3, old.Item4, DateTime.Now);
  61. System.Diagnostics.Debug.Write("*");
  62. }
  63. else
  64. {
  65. if (_rangeHistory.Count > 100) _rangeHistory.RemoveAt(0);
  66. _rangeHistory.Add(new Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>(
  67. sciChart.XAxis.VisibleRange.Min, sciChart.XAxis.VisibleRange.Max,
  68. sciChart.YAxis.VisibleRange.Min, sciChart.YAxis.VisibleRange.Max, DateTime.Now));
  69. System.Diagnostics.Debug.Write("#");
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. CONTEXT.WriteLog(ex);
  75. }
  76. }
  77. }
  78. PlotViewModel _viewModel;
  79. /// <summary>
  80. /// 撤销至上一次的视图
  81. /// </summary>
  82. private void Undo_Last_View_Range()
  83. {
  84. sciChart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  85. sciChart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
  86. if (_rangeHistory.Count > 0)
  87. {
  88. try
  89. {
  90. _skipUndo = true;
  91. var s1 = (Abt.Controls.SciChart.IRange)sciChart.XAxis.VisibleRange.Clone();
  92. s1.Min = _rangeHistory[_rangeHistory.Count - 1].Item1;
  93. s1.Max = _rangeHistory[_rangeHistory.Count - 1].Item2;
  94. sciChart.XAxis.VisibleRange = s1;
  95. var s2 = (Abt.Controls.SciChart.IRange)sciChart.YAxis.VisibleRange.Clone();
  96. s2.Min = _rangeHistory[_rangeHistory.Count - 1].Item3;
  97. s2.Max = _rangeHistory[_rangeHistory.Count - 1].Item4;
  98. sciChart.YAxis.VisibleRange = s2;
  99. sciChart.YAxis.InvalidateElement();
  100. sciChart.XAxis.InvalidateElement();
  101. _rangeHistory.RemoveAt(_rangeHistory.Count - 1);
  102. }
  103. catch (Exception ex)
  104. {
  105. CONTEXT.WriteLog(ex);
  106. }
  107. finally
  108. {
  109. _skipUndo = false;
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 鼠标双击事件的定义
  115. /// </summary>
  116. /// <param name="sender"></param>
  117. /// <param name="e"></param>
  118. private void sciChart_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  119. {
  120. if (e.ChangedButton == MouseButton.Right)
  121. {
  122. //右键双击,恢复1:1的视图
  123. this.sciChart.ZoomExtents();
  124. }
  125. else if (e.ChangedButton == MouseButton.Left)
  126. {
  127. //左键双击,恢复至上一次的视图比例
  128. Undo_Last_View_Range();
  129. }
  130. }
  131. /// <summary>
  132. /// 显示全部数据
  133. /// </summary>
  134. /// <param name="sender"></param>
  135. /// <param name="e"></param>
  136. private void showAllButton_Click(object sender, RoutedEventArgs e)
  137. {
  138. this.sciChart.ZoomExtents();
  139. }
  140. }
  141. }