using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using Aitex.UI.Charting.Command; using Abt.Controls.SciChart; using Aitex.UI.Charting.View; using Abt.Controls.SciChart.Visuals; using DataAnalysisControl.Core; namespace Aitex.UI.Charting.ViewModel { public class PlotViewModel : ChartingBaseViewModel { public PlotViewModel(PlotView plot) { _plot = plot; _chart = plot.sciChart; CommonViewModel = CommonViewModel.Instance; ResetXAxisRange2DataSourceRange = new ChartingCommand((o) => true, (o) => ResetXAxisRange2DataSourceTime(o)); ToggleOverViewPanel = new ChartingCommand((o) => true, (o) => { //在overview模式下,x轴的autorange需关闭,否则在overview模式下不能进行拖拽操作 OverViewPanelHeight = OverViewPanelHeight > 0 ? 0 : 50; _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; InvokePropertyChanged("OverViewPanelHeight"); InvokePropertyChanged("IsOverViewPanelVisable"); }); OverViewPanelHeight = 0; ToggleAutoRangeCommand = new ChartingCommand((o) => true, (o) => { if (_chart.XAxis.AutoRange == Abt.Controls.SciChart.Visuals.Axes.AutoRange.Always) _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; else _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Always; _chart.YAxis.AutoRange = _chart.XAxis.AutoRange; if (_chart.XAxis.AutoRange == Abt.Controls.SciChart.Visuals.Axes.AutoRange.Always) { //同时自动Y轴缩放 //在overview模式下,x轴的autorange需关闭,否则在overview模式下不能进行拖拽操作 OverViewPanelHeight = 0; InvokePropertyChanged("OverViewPanelHeight"); InvokePropertyChanged("IsOverViewPanelVisable"); //关闭滚轮滚动X、Y轴的功能 _plot.mouseWheelZoomModifier.IsEnabled = false; } InvokePropertyChanged("ToggleAutoRangeCommand"); }); /*ToggleXYPanCommand = new ChartingCommand((o) => true, (o) => { var isEnabled = (bool)o; _plot.zoomPanModifier.IsEnabled = isEnabled; if (_plot.zoomPanModifier.IsEnabled) { //X\Y轴的自动缩放取消 _chart.XAxis.AutoRange = false; _chart.YAxis.AutoRange = false; //自动游标显示功能取消 _plot.rolloverModifier.IsEnabled = false; } });*/ ToggleXWheelZoomCommand = new ChartingCommand((o) => true, (o) => { if (!_plot.mouseWheelZoomModifier.IsEnabled) { //关闭auto zoom功能,因为在Auto zoom模式下,不能滚轮对x、y轴进行缩放操作 _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; _plot.mouseWheelZoomModifier.IsEnabled = true; _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XDirection; } else { if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XDirection) { _plot.mouseWheelZoomModifier.IsEnabled = false; } else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XYDirection) { _plot.mouseWheelZoomModifier.XyDirection = XyDirection.YDirection; } else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.YDirection) { _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XYDirection; } } }); ToggleYWheelZoomCommand = new ChartingCommand((o) => true, (o) => { if (!_plot.mouseWheelZoomModifier.IsEnabled) { //关闭auto zoom功能,因为在Auto zoom模式下,不能滚轮对x、y轴进行缩放操作 _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; _plot.mouseWheelZoomModifier.IsEnabled = true; _plot.mouseWheelZoomModifier.XyDirection = XyDirection.YDirection; } else { if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XDirection) { _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XYDirection; } else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.XYDirection) { _plot.mouseWheelZoomModifier.XyDirection = XyDirection.XDirection; } else if (_plot.mouseWheelZoomModifier.XyDirection == XyDirection.YDirection) { _plot.mouseWheelZoomModifier.IsEnabled = false; } } }); ToggleVerticalLineCommand = new ChartingCommand((o) => true, (o) => { ShowVerticalTimeLines((bool)o); }); } public static SciChartSurface SciChart { get { return _chart; } } public static PlotView PlotView { get { return _plot; } } private static SciChartSurface _chart; private static PlotView _plot; public CommonViewModel CommonViewModel { get; private set; } public ICommand ResetXAxisRange2DataSourceRange { get; set; } public ICommand ToggleOverViewPanel { get; set; } public ICommand ToggleAutoRangeCommand { get; set; } //public ICommand ToggleXYPanCommand { get; set; } public ICommand ToggleXWheelZoomCommand { get; set; } public ICommand ToggleYWheelZoomCommand { get; set; } public ICommand ToggleVerticalLineCommand { get; set; } public double OverViewPanelHeight { get; set; } public bool IsOverViewPanelVisable { get { return OverViewPanelHeight > 0; } } /// /// 显示时间线 /// /// public static void ShowVerticalTimeLines(bool show) { try { if (show) { if (_plot.vertical_Line1.IsHidden || _plot.vertical_Line2.IsHidden) { var minTime = (DateTime)_chart.XAxis.VisibleRange.Min; var maxTime = (DateTime)_chart.XAxis.VisibleRange.Max; var totalDiff = (maxTime - minTime).TotalSeconds; _plot.vertical_Line1.X1 = minTime + new TimeSpan(0, 0, (int)(totalDiff / 4.0)); _plot.vertical_Line2.X1 = maxTime - new TimeSpan(0, 0, (int)(totalDiff / 4.0)); } _plot.vertical_Line1.IsHidden = false; _plot.vertical_Line2.IsHidden = false; _plot.sciChart.InvalidateElement(); } else { _plot.vertical_Line1.IsHidden = true; _plot.vertical_Line2.IsHidden = true; } } catch (Exception ex) { CONTEXT.WriteLog(ex); } } /// /// 当切换到Charting视图时,自动将时间轴进行调整 /// /// private void ResetXAxisRange2DataSourceTime(object param) { DateTime minT = DateTime.MaxValue; DateTime maxT = DateTime.MinValue; for (int i = 0; i < CommonViewModel.DataSourceList.Count; i++) { bool isSourceVisiable = false; foreach (MyLineSeries series in CommonViewModel.RenderableSeries) { if (series.IsVisible && series.DataSource == CommonViewModel.DataSourceList[i]) { isSourceVisiable = true; break; } } if (!isSourceVisiable) continue; var from = CommonViewModel.DataSourceList[i].BeginTime; var to = CommonViewModel.DataSourceList[i].EndTime; var timeMove = CommonViewModel.DataSourceList[i].TimeMove; if (from + timeMove < minT) minT = from + timeMove; if (to + timeMove > maxT) maxT = to + timeMove; } if (minT == DateTime.MaxValue || minT == DateTime.MinValue || maxT == DateTime.MinValue || maxT == DateTime.MaxValue || minT > maxT) { _chart.XAxis.VisibleRange = new DateRange(DateTime.Now - new TimeSpan(1, 0, 0), DateTime.Now + new TimeSpan(1, 0, 0)); _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; } else { _chart.XAxis.VisibleRange = new DateRange(minT - new TimeSpan(1, 0, 0), maxT + new TimeSpan(1, 0, 0)); _chart.XAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Never; _chart.YAxis.VisibleRange = new DoubleRange(0, 1000); _chart.YAxis.AutoRange = Abt.Controls.SciChart.Visuals.Axes.AutoRange.Once; } } } }