summaryrefslogtreecommitdiff
path: root/grapher/Models/Charts/ChartXY.cs
blob: bd80ea27c65eeae4feb3a4a81687c056f4c0fb6f (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using grapher.Models.Mouse;
using System;
using System.Collections;
using System.Windows.Forms.DataVisualization.Charting;

namespace grapher
{
    public class ChartXY
    {
        #region Constructors

        public ChartXY(Chart chartX, Chart chartY, string title)
        {
            ChartX = chartX;
            ChartY = chartY;
            Title = title;

            ChartY.Top = ChartX.Top;
            ChartY.Height = ChartX.Height;
            ChartY.Width = ChartX.Width;
            ChartY.Left = ChartX.Left + ChartX.Width + Constants.ChartSeparationHorizontal;

            SetupChart(ChartX);
            SetupChart(ChartY);

            Combined = false;
            SetCombined();
            Visible = true;
        }

        #endregion Constructors

        private const double VerticalMargin = 0.1;

        #region Properties

        public Chart ChartX { get; }

        public Chart ChartY { get; }

        public int Height { 
            get
            {
                return ChartX.Height;
            }
        }

        public int Width { 
            get
            {
                if (Combined)
                {
                    return ChartX.Width;
                }
                else
                {
                    return ChartY.Left + ChartY.Width - ChartX.Left;
                }
            }
        }

        public int Top { 
            get
            {
                return ChartX.Top;
            }
        }

        public int Left { 
            get
            {
                return ChartX.Left;
            }
        }

        public bool Combined { get; set; }

        public bool Visible { get; set; }

        public string Title { get; }

        private PointData CombinedPointData { get; set; }

        private PointData XPointData { get; set; }

        private PointData YPointData { get; set; }

        #endregion Properties

        #region Methods

        public static void SetupChart(Chart chart)
        {
            chart.ChartAreas[0].AxisX.RoundAxisValues();

            chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;

            chart.ChartAreas[0].AxisY.ScaleView.MinSize = 0.01;
            chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 0.001;

            chart.ChartAreas[0].AxisX.LabelStyle.Format = "0.##";
            chart.ChartAreas[0].AxisY.LabelStyle.Format = "0.##";

            chart.ChartAreas[0].CursorY.Interval = 0.001;

            chart.ChartAreas[0].CursorX.AutoScroll = true;
            chart.ChartAreas[0].CursorY.AutoScroll = true;

            chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;

            chart.ChartAreas[0].CursorX.IsUserEnabled = true;
            chart.ChartAreas[0].CursorY.IsUserEnabled = true;

            chart.Series[1].Points.Clear();
            chart.Series[1].Points.AddXY(0, 0);

            chart.Titles[0].Font = new System.Drawing.Font(chart.Titles[0].Font.Name, 9.0f, System.Drawing.FontStyle.Italic);
        }

        public static void DrawPoint(Chart chart, PointData pointOne, PointData pointTwo = null)
        {
            if (chart.Visible)
            {
                pointOne.Get(out var x, out var y);
                chart.Series[1].Points.DataBindXY(x, y);
                if (pointTwo != null)
                {
                    pointTwo.Get(out x, out y);
                    chart.Series[3].Points.DataBindXY(x, y);
                }
            }
        }

        public static void SetLogarithmic(Chart chart)
        {
            /*
            chart.ChartAreas[0].AxisX.Minimum = 0.001;
            chart.ChartAreas[0].AxisX.Maximum = 3500;
            chart.ChartAreas[0].AxisY.Minimum = 0.001;
            chart.ChartAreas[0].AxisY.Maximum = 10;
            chart.ChartAreas[0].AxisX.IsLogarithmic = true;
            chart.ChartAreas[0].AxisY.IsLogarithmic = true;
            */
        }

        public static void SetStandard(Chart chart)
        {
            /*
            chart.ChartAreas[0].AxisX.IsLogarithmic = false;
            chart.ChartAreas[0].AxisY.IsLogarithmic = false;
            */
        }

        public void ClearSecondDots()
        {
            ChartX.Series[3].Points.Clear();
        }

        public void Update()
        {
            ChartX.Update();
            if (ChartY.Visible)
            {
                ChartY.Update();
            }
        }

        public void SetPointBinds(PointData combined, PointData x, PointData y)
        {
            CombinedPointData = combined;
            XPointData = x;
            YPointData = y;
        }

        public void DrawLastMovementValue(bool twoDotsPerGraph = false)
        {
            if(Combined)
            {
                if (twoDotsPerGraph)
                {
                    DrawPoint(ChartX, XPointData, YPointData);
                }
                else
                {
                    DrawPoint(ChartX, CombinedPointData);
                }
            }
            else
            {
                DrawPoint(ChartX, XPointData);
                DrawPoint(ChartY, YPointData);
            }
        }

        public void ClearLastValue()
        {
            ChartX.Series[1].Points.Clear();
            ChartY.Series[1].Points.Clear();
        }

        public void Bind(IDictionary data)
        {
            ChartX.Series[0].Points.DataBindXY(data.Keys, data.Values);
            ChartX.Series[2].IsVisibleInLegend = false;
            ChartX.Series[2].Points.Clear();
        }

        public void BindXY(IDictionary dataX, IDictionary dataY)
        {
            ChartX.Series[0].Points.DataBindXY(dataX.Keys, dataX.Values);
            ChartY.Series[0].Points.DataBindXY(dataY.Keys, dataY.Values);
            ChartX.Series[2].IsVisibleInLegend = false;
            ChartX.Series[2].Points.Clear();
        }

        public void BindXYCombined(IDictionary dataX, IDictionary dataY)
        {
            ChartX.Series[0].Points.DataBindXY(dataX.Keys, dataX.Values);
            ChartX.Series[2].Points.DataBindXY(dataY.Keys, dataY.Values);
            ChartX.Series[2].IsVisibleInLegend = true;
        }

        private void VerifyRange(double min, double max)
        {
            if (min > max)
            {
                throw new ArgumentException($"invalid chart range: ({min}, {max})");
            }
        }

        public void SetMinMax(double min, double max)
        {
            VerifyRange(min, max);
            ChartX.ChartAreas[0].AxisY.Minimum = min * (1 - VerticalMargin);
            ChartX.ChartAreas[0].AxisY.Maximum = max * (1 + VerticalMargin);
        }

        public void SetMinMaxXY(double minX, double maxX, double minY, double maxY)
        {
            VerifyRange(minX, maxX);
            ChartX.ChartAreas[0].AxisY.Minimum = minX * (1 - VerticalMargin);
            ChartX.ChartAreas[0].AxisY.Maximum = maxX * (1 + VerticalMargin);

            VerifyRange(minY, maxY);
            ChartY.ChartAreas[0].AxisY.Minimum = minY * (1 - VerticalMargin);
            ChartY.ChartAreas[0].AxisY.Maximum = maxY * (1 + VerticalMargin);
        }

        public void SetCombined()
        {
            if (!Combined)
            {
                ChartY.Hide();
                Combined = true;
                ChartX.Titles[0].Text = Title;
            }
        }

        public void SetSeparate()
        {
            if (Combined)
            {
                if (Visible)
                {
                    ChartY.Show();
                }

                ChartX.Titles[0].Text = SetComponentTitle(Constants.XComponent);
                ChartY.Titles[0].Text = SetComponentTitle(Constants.YComponent);

                Combined = false;
            }
        }


        public void Hide()
        {

            if (Visible)
            {
                ChartX.Hide();
                ChartY.Hide();
                Visible = false;
            }
        }

        public void Show()
        {
            if (!Visible)
            {
                ChartX.Show();

                if (!Combined)
                {
                    ChartY.Show();
                }

                Visible = true;
            }
        }

        public void SetTop(int top)
        {
            ChartX.Top = top;
            ChartY.Top = top;
        }

        public void SetHeight(int height)
        {
            ChartX.Height = height;
            ChartY.Height = height;
        }

        private string SetComponentTitle(string component)
        {
            return $"{Title} : {component}";
        }
        #endregion Methods
    }
}