aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.DispenserAnalyzer.UI/ViewModels/CompareResultsDlgVM.cs
blob: 258c322363862979b41f93e94488482e68c9c665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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<int, Color> IndexToColor;

        #region properties

        List<ObservableCollection<DataPoint>> ChartPoints { get; set; }

        /// <summary>
        /// Gets or sets the results. Input values.
        /// </summary>
        public ObservableCollection<CompareResultModel> 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(); }
        }

        /// <summary>
        /// Gets or sets the last point on mouse click.
        /// </summary>
        private DataPoint LastPoint { get; set; }

        /// <summary>
        /// Gets or sets the arrow annotation to show difference from 2 clicks.
        /// </summary>
        ArrowAnnotation ArrowAnnot { get; set; }
        
        public string _differenceText;
        public string DifferenceText
        {
            get { return _differenceText; }
            set { _differenceText = value;
                RaisePropertyChangedAuto(); }
        }
        #endregion

        public CompareResultsDlgVM(ObservableCollection<CompareResultModel> 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<ObservableCollection<DataPoint>>();
            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 <Results.Count)
                {
                    linePoints.Title = Results[index].ResultName;
                }
                
                linePoints.ItemsSource = points;
                linePoints.Color  = IndexToColor[index];
                ToYAxis = Math.Max((points.Max(x => 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}&#x0a;{1}: {2:0.0}&#x0a;{3}: {4:0.0}";
                PlotControl.Series.Add(linePoints);
            }
            

            OxyPlot.ElementCollection<OxyPlot.Axes.Axis> 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<OxyPlot.Axes.Axis> 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<int, Color>();
            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));
        }
    }
}