123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using Aitex.UI.Charting.ViewModel;
- using Abt.Controls.SciChart;
- using DataAnalysisControl.Core;
- namespace Aitex.UI.Charting.View
- {
- /// <summary>
- /// Interaction logic for PlotView.xaml
- /// </summary>
- public partial class PlotView : UserControl
- {
- public PlotView()
- {
- InitializeComponent();
- _viewModel = new PlotViewModel(this);
- DataContext = _viewModel;
- IsVisibleChanged += new DependencyPropertyChangedEventHandler(PlotView_IsVisibleChanged);
- //Loaded += (s, e) => DataContext = _viewModel;
-
- sciChart.XAxis.VisibleRangeChanged += new EventHandler<Abt.Controls.SciChart.VisibleRangeChangedEventArgs>(Axis_VisibleRangeChanged);
- sciChart.YAxis.VisibleRangeChanged += new EventHandler<Abt.Controls.SciChart.VisibleRangeChangedEventArgs>(Axis_VisibleRangeChanged);
- }
- void PlotView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if ((bool)e.NewValue)
- {
- //设置Charting的主题模式
- //ThemeManager.SetTheme(sciChart, "ExpressionLight");
- //ThemeManager.SetTheme(sciChart, "Chrome");
- //ThemeManager.SetTheme(sciChart, "BlackSteel");
- //重置Charting的X轴显示区域
- _viewModel.ResetXAxisRange2DataSourceRange.Execute(sciChart);
- }
- }
- List<Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>> _rangeHistory = new List<Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>>();
- bool _skipUndo = false;
- void Axis_VisibleRangeChanged(object sender, Abt.Controls.SciChart.VisibleRangeChangedEventArgs e)
- {
- if (!_skipUndo)
- {
- try
- {
- //System.Diagnostics.Debug.Write(".");
- if (_rangeHistory.Count > 0 && DateTime.Now - _rangeHistory[_rangeHistory.Count - 1].Item5 < new TimeSpan(0, 0, 0, 0, 500))
- {
- var old = _rangeHistory[_rangeHistory.Count -1];
- _rangeHistory[_rangeHistory.Count - 1] =
- new Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>(old.Item1, old.Item2,
- old.Item3, old.Item4, DateTime.Now);
- System.Diagnostics.Debug.Write("*");
- }
- else
- {
- if (_rangeHistory.Count > 100) _rangeHistory.RemoveAt(0);
- _rangeHistory.Add(new Tuple<IComparable, IComparable, IComparable, IComparable, DateTime>(
- sciChart.XAxis.VisibleRange.Min, sciChart.XAxis.VisibleRange.Max,
- sciChart.YAxis.VisibleRange.Min, sciChart.YAxis.VisibleRange.Max, DateTime.Now));
- System.Diagnostics.Debug.Write("#");
- }
- }
- catch (Exception ex)
- {
- CONTEXT.WriteLog(ex);
- }
- }
- }
- PlotViewModel _viewModel;
- /// <summary>
- /// 撤销至上一次的视图
- /// </summary>
- private void Undo_Last_View_Range()
- {
- sciChart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
- sciChart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never;
- if (_rangeHistory.Count > 0)
- {
- try
- {
- _skipUndo = true;
- var s1 = (Abt.Controls.SciChart.IRange)sciChart.XAxis.VisibleRange.Clone();
- s1.Min = _rangeHistory[_rangeHistory.Count - 1].Item1;
- s1.Max = _rangeHistory[_rangeHistory.Count - 1].Item2;
- sciChart.XAxis.VisibleRange = s1;
- var s2 = (Abt.Controls.SciChart.IRange)sciChart.YAxis.VisibleRange.Clone();
- s2.Min = _rangeHistory[_rangeHistory.Count - 1].Item3;
- s2.Max = _rangeHistory[_rangeHistory.Count - 1].Item4;
- sciChart.YAxis.VisibleRange = s2;
- sciChart.YAxis.InvalidateElement();
- sciChart.XAxis.InvalidateElement();
- _rangeHistory.RemoveAt(_rangeHistory.Count - 1);
- }
- catch (Exception ex)
- {
- CONTEXT.WriteLog(ex);
- }
- finally
- {
- _skipUndo = false;
- }
- }
- }
- /// <summary>
- /// 鼠标双击事件的定义
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void sciChart_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- if (e.ChangedButton == MouseButton.Right)
- {
- //右键双击,恢复1:1的视图
- this.sciChart.ZoomExtents();
- }
- else if (e.ChangedButton == MouseButton.Left)
- {
- //左键双击,恢复至上一次的视图比例
- Undo_Last_View_Range();
- }
- }
- /// <summary>
- /// 显示全部数据
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void showAllButton_Click(object sender, RoutedEventArgs e)
- {
- this.sciChart.ZoomExtents();
- }
- }
- }
|