using OxyPlot; using OxyPlot.Axes; using OxyPlot.Wpf; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using Tango.DispenserAnalyzer.UI.Analysis; using Tango.DispenserAnalyzer.UI.Models; using Tango.SharedUI; namespace Tango.DispenserAnalyzer.UI.ViewModels { public class ComparePoint { public double X { get; set; } public double Y { get; set; } } public class CompareResultsDlgVM : ViewModel { public Plot PlotControl { get; set; } private Dictionary IndexToColor; #region properties List> ChartPoints { get; set; } /// /// Gets or sets the results. Input values. /// public ObservableCollection Results { get; set; } public string Title { get; set; } private int _step; public int XStep { get { return _step; } set { _step = value; RaisePropertyChangedAuto(); } } private double _fromXAxis; public double FromXAxis { get { return _fromXAxis; } set { _fromXAxis = value; RaisePropertyChangedAuto(); } } private double _toXAxis; public double ToXAxis { get { return _toXAxis; } set { _toXAxis = value; RaisePropertyChangedAuto(); } } private double _fromYAxis; public double FromYAxis { get { return _fromYAxis; } set { _fromYAxis = value; RaisePropertyChangedAuto(); } } private double _toYAxis; public double ToYAxis { get { return _toYAxis; } set { _toYAxis = value; RaisePropertyChangedAuto(); } } /// /// Gets or sets the last point on mouse click. /// private DataPoint LastPoint { get; set; } /// /// Gets or sets the arrow annotation to show difference from 2 clicks. /// ArrowAnnotation ArrowAnnot { get; set; } public string _differenceText; public string DifferenceText { get { return _differenceText; } set { _differenceText = value; RaisePropertyChangedAuto(); } } #endregion public CompareResultsDlgVM(ObservableCollection results) { Results = results; Title = $"Compare Results Moving Average Values"; _toYAxis = _fromYAxis = _toXAxis = _fromXAxis = 0; _step = 1; LastPoint = DataPoint.Undefined; ArrowAnnot = new ArrowAnnotation { FontSize = 12, Color = Colors.OrangeRed, TextColor = Colors.DarkBlue, TextVerticalAlignment = System.Windows.VerticalAlignment.Center, TextHorizontalAlignment = System.Windows.HorizontalAlignment.Center, HeadLength = 14, HeadWidth = 6, Veeness = 4, }; ChartPoints = new List>(); foreach (var result in Results) { ChartPoints.Add(result.Result.LineChart.Points); } InitColors(); } public void Init(Plot plotControl) { PlotControl = plotControl; for (int index = 0; index < ChartPoints.Count; index++) { var points = ChartPoints[index]; LineSeries linePoints = new LineSeries(); if(index x.Y) + 2), _toYAxis); FromYAxis = Math.Min(points.Min(x => x.Y), _fromYAxis); ToXAxis = Math.Max((points.Max(x => x.X) + 2), _toXAxis); FromXAxis = Math.Min(points.Min(x => x.X), _fromXAxis); linePoints.TrackerFormatString = "{0} " + Environment.NewLine + "{1}: {2:0.0} " + Environment.NewLine + "{3}: {4:0.0} ";// "{}{0} {1}: {2:0.0} {3}: {4:0.0}"; PlotControl.Series.Add(linePoints); } OxyPlot.ElementCollection axisList = PlotControl.ActualModel.Axes; OxyPlot.Axes.Axis xAxis = axisList.FirstOrDefault(ax => ax.Position == AxisPosition.Bottom); OxyPlot.Axes.Axis yAxis = axisList.FirstOrDefault(ax => ax.Position == AxisPosition.Left); //PlotControl.ActualModel.TrackerChanged += (sender, eventArgs) => // { // string CurrentTrackerValue = ""; // CurrentTrackerValue = eventArgs.HitResult != null ? eventArgs.HitResult.Text : CurrentTrackerValue; // if (!String.IsNullOrEmpty(CurrentTrackerValue)) // { // var x = Regex.Matches(CurrentTrackerValue, "[0-9]+[.[0-9]+]?"); // if (x.Count == 2) // { // var CurrentTrackerValueX = x[0].Value; // var CurrentTrackerValueY = x[1].Value; // if (LastPoint.IsDefined()) // { // LastPoint = DataPoint.Undefined; // } // else // { // } // } // }; // }; PlotControl.PreviewMouseLeftButtonUp += OnPrevMouseLeftButtonUp; } private void OnPrevMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ScreenPoint screenpoint = new ScreenPoint(e.GetPosition((IInputElement)e.Source).X, e.GetPosition((IInputElement)e.Source).Y); ; OxyPlot.ElementCollection axisList = PlotControl.ActualModel.Axes; OxyPlot.Axes.Axis xAxis = axisList.FirstOrDefault(ax => ax.Position == AxisPosition.Bottom); OxyPlot.Axes.Axis yAxis = axisList.FirstOrDefault(ax => ax.Position == AxisPosition.Left); if (LastPoint.IsDefined()) { ArrowAnnot.EndPoint = OxyPlot.Axes.Axis.InverseTransform(screenpoint, xAxis, yAxis); var difX = Math.Round(Math.Abs(ArrowAnnot.StartPoint.X - ArrowAnnot.EndPoint.X), 2); var difY = Math.Round(Math.Abs(ArrowAnnot.StartPoint.Y - ArrowAnnot.EndPoint.Y), 2); DifferenceText = String.Format($"Difference of coordinates of selected points X:{difX} Y:{difY}"); ; //ArrowAnnot.Text = String.Format($"Difference of coordinates of selected points X:{difX} Y:{difY}"); var ta = new TextAnnotation { TextPosition = new DataPoint(ArrowAnnot.EndPoint.X, ArrowAnnot.EndPoint.Y), Text = String.Format($"X:{Math.Round(ArrowAnnot.EndPoint.X, 2)} Y:{Math.Round(ArrowAnnot.EndPoint.Y, 2)}"), Background = Color.FromArgb(255, 200, 253, 251), Stroke = Colors.Transparent }; PlotControl.Annotations.Add(ta); PlotControl.Annotations.Add(ArrowAnnot); PlotControl.InvalidatePlot(false); LastPoint = DataPoint.Undefined; } else//start point { PlotControl.Annotations.Clear(); ArrowAnnot.StartPoint = OxyPlot.Axes.Axis.InverseTransform(screenpoint, xAxis, yAxis); ArrowAnnot.EndPoint = ArrowAnnot.StartPoint; var ta = new TextAnnotation { TextPosition = new DataPoint(ArrowAnnot.StartPoint.X, ArrowAnnot.StartPoint.Y), Text = String.Format($"X:{Math.Round(ArrowAnnot.StartPoint.X, 2)} Y:{Math.Round(ArrowAnnot.StartPoint.Y, 2)}"), Background = Color.FromArgb(255, 200, 253, 251), Stroke = Colors.Transparent }; PlotControl.Annotations.Add(ta); DifferenceText = ""; ArrowAnnot.Text = ""; LastPoint = ArrowAnnot.StartPoint; PlotControl.InvalidatePlot(false); } e.Handled = false; } private void InitColors() { IndexToColor = new Dictionary(); IndexToColor.Add(0, System.Windows.Media.Color.FromRgb(0, 255, 255)); IndexToColor.Add(1, System.Windows.Media.Color.FromRgb(0, 255, 0)); IndexToColor.Add(2, System.Windows.Media.Color.FromRgb(0, 0, 255)); IndexToColor.Add(3, System.Windows.Media.Color.FromRgb(255, 0, 0)); IndexToColor.Add(4, System.Windows.Media.Color.FromRgb(255, 255, 0)); IndexToColor.Add(5, System.Windows.Media.Color.FromRgb(255, 0, 255)); } } }