Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization.Test/3.2/LineChartTests.cs @ 1904

Last change on this file since 1904 was 1885, checked in by mstoeger, 15 years ago

xaxis grid color can be set in the options dialog #555

File size: 17.6 KB
Line 
1using System;
2using System.Drawing;
3using System.Threading;
4using NUnit.Framework;
5
6namespace HeuristicLab.Visualization.Test {
7  [TestFixture]
8  public class LineChartTests {
9    private ChartDataRowsModel model;
10
11    [SetUp]
12    public void SetUp() {
13      model = new ChartDataRowsModel();
14    }
15
16    [Test]
17    public void TestLineChart() {
18      LineChartTestForm f = new LineChartTestForm(model);
19
20      IDataRow row1 = new DataRow();
21      IDataRow row2 = new DataRow();
22      IDataRow row3 = new DataRow();
23
24      row1.Color = Color.Red;
25      row2.Color = Color.Green;
26      row3.Color = Color.Blue;
27
28      row1.Thickness = 3;
29      row2.Thickness = 4;
30      row3.Thickness = 5;
31
32      row1.Label = "Simon";
33      row2.Label = "Gertschi";
34      row3.Label = "Maxi";
35
36      row1.Style = DrawingStyle.Solid;
37      row2.Style = DrawingStyle.Solid;
38      row3.Style = DrawingStyle.Dashed;
39
40
41      model.AddDataRow(row1);
42      model.AddDataRow(row2);
43      model.AddDataRow(row3);
44
45      row1.AddValue(10);
46      row1.AddValue(5);
47      row1.AddValue(7);
48      row1.AddValue(3);
49      row1.AddValue(10);
50      row1.AddValue(2);
51
52      row2.AddValue(5);
53      row2.AddValue(6);
54      row2.AddValue(5);
55
56      row3.AddValue(2);
57      row3.AddValue(2);
58      row3.AddValue(2);
59      row3.AddValue(2);
60      row3.AddValue(2);
61
62      Random rand = new Random();
63
64      for (int i = 0; i < 10000; i++) {
65        row1.AddValue(rand.NextDouble() * 10);
66        row2.AddValue(rand.NextDouble() * 10);
67        row3.AddValue(rand.NextDouble() * 10);
68      }
69
70      f.ShowDialog();
71    }
72
73    [Test]
74    public void TestAxes() {
75      LineChartTestForm f = new LineChartTestForm(model);
76
77      IDataRow row1 = new DataRow();
78      IDataRow row2 = new DataRow();
79      IDataRow row3 = new DataRow();
80
81      model.XAxis.Label = "X Axis";
82      model.XAxis.ShowLabel = true;
83      model.XAxis.ShowGrid = true;
84      model.XAxis.GridColor = Color.Blue;
85
86      YAxisDescriptor yaxis1 = model.DefaultYAxis;
87      YAxisDescriptor yaxis2 = new YAxisDescriptor();
88
89      yaxis1.Label = "Y-Axis 1";
90      yaxis1.ShowYAxisLabel = true;
91      yaxis1.Position = AxisPosition.Left;
92      yaxis1.ShowGrid = true;
93      yaxis1.GridColor = Color.Gray;
94
95      yaxis2.Label = "Y-Axis 2";
96      yaxis2.ShowYAxisLabel = true;
97      yaxis2.Position = AxisPosition.Right;
98      yaxis2.ShowGrid = false;
99
100      row1.Color = Color.Red;
101      row1.Thickness = 3;
102      row1.Style = DrawingStyle.Solid;
103      row1.Label = "Die Rote";
104
105      row2.Color = Color.Green;
106      row2.Thickness = 3;
107      row2.Style = DrawingStyle.Solid;
108      row2.Label = "Die Grüne";
109
110      row3.Color = Color.Blue;
111      row3.Thickness = 3;
112      row3.Style = DrawingStyle.Solid;
113      row3.Label = "Die Blaue";
114      row3.YAxis = yaxis2;
115
116      model.AddDataRow(row1);
117      model.AddDataRow(row2);
118      model.AddDataRow(row3);
119
120      Random rand = new Random(42);
121
122      for (int i = 0; i < 10; i++) {
123        row1.AddValue(rand.NextDouble() * 10);
124        row2.AddValue(rand.NextDouble() * 10);
125        row3.AddValue(rand.NextDouble() * 1);
126      }
127
128      f.AddValue += delegate {
129        row1.AddValue(rand.NextDouble() * 10);
130        row2.AddValue(rand.NextDouble() * 10);
131        row3.AddValue(rand.NextDouble() * 1);
132      };
133
134      f.ShowDialog();
135    }
136
137    [Test]
138    public void SimpleTestAggregator() {
139      LineChartTestForm f = new LineChartTestForm(model);
140
141      IDataRow row1 = new DataRow { Label = "row", Color = Color.Red, Thickness = 3, Style = DrawingStyle.Solid };
142
143      model.AddDataRow(row1);
144
145
146      MaxAggregator aggregator = new MaxAggregator {
147        Label = "MinAggregator",
148        Color = Color.Pink,
149        Thickness = 5,
150        Style = DrawingStyle.Solid,
151        LineType = DataRowType.SingleValue
152      };
153      aggregator.AddWatch(row1);
154
155      model.AddDataRow(aggregator);
156
157      row1.AddValue(10);
158      row1.AddValue(5);
159      row1.AddValue(7);
160      row1.AddValue(3);
161      row1.AddValue(10);
162      row1.AddValue(2);
163
164      f.ShowDialog();
165    }
166
167
168    public class Worker {
169      // This method will be called when the thread is started.
170      private readonly ChartDataRowsModel model;
171
172      public Worker(ChartDataRowsModel model) {
173        this.model = model;
174      }
175
176      public void DoWorkMultiLine() {
177        IDataRow row1 = new DataRow { Color = Color.Red, Thickness = 2, Label = "Sinus", Style = DrawingStyle.Solid, ShowMarkers = false };
178        model.AddDataRow(row1);
179
180        IDataRow row2 = new DataRow { Color = Color.Red, Thickness = 3, Label = "Growing", Style = DrawingStyle.Solid, ShowMarkers = false };
181        model.AddDataRow(row2);
182
183        AvgAggregator multiAvgAggregator = new AvgAggregator {
184          Label = "MultiAvgAggregator",
185          Color = Color.DarkOliveGreen,
186          Thickness = 3,
187          Style = DrawingStyle.Solid,
188          LineType = DataRowType.SingleValue,
189          ShowMarkers = false
190        };
191        multiAvgAggregator.AddWatch(row1);
192        multiAvgAggregator.AddWatch(row2);
193        model.AddDataRow(multiAvgAggregator);
194
195        MaxAggregator multiMaxAggregator = new MaxAggregator {
196          Label = "MultiMaxAggregator",
197          Color = Color.DarkKhaki,
198          Thickness = 3,
199          Style = DrawingStyle.Solid,
200          LineType = DataRowType.SingleValue,
201          ShowMarkers = false
202        };
203        multiMaxAggregator.AddWatch(row1);
204        multiMaxAggregator.AddWatch(row2);
205        model.AddDataRow(multiMaxAggregator);
206
207        MinAggregator multiMinAggregator = new MinAggregator {
208          Label = "MultiMinAggregator",
209          Color = Color.DarkRed,
210          Thickness = 3,
211          Style = DrawingStyle.Solid,
212          LineType = DataRowType.SingleValue,
213          ShowMarkers = false
214        };
215        multiMinAggregator.AddWatch(row1);
216        multiMinAggregator.AddWatch(row2);
217        model.AddDataRow(multiMinAggregator);
218
219        //        AvgLineAggregator multiLineAvgAggregator = new AvgLineAggregator {
220        //                                                                           Label = "MultiLineAvgAggregator",
221        //                                                                           Color = Color.Red,
222        //                                                                           Thickness = 4,
223        //                                                                           Style = DrawingStyle.Solid,
224        //                                                                           LineType = DataRowType.Normal,
225        //                                                                           ShowMarkers = false
226        //                                                                         };
227        //        multiLineAvgAggregator.AddWatch(row1);
228        //        multiLineAvgAggregator.AddWatch(row2);
229        //        multiLineAvgAggregator.AddValue(0);
230        //        model.AddDataRow(multiLineAvgAggregator);
231
232        double i = 0;
233
234
235        while (!_shouldStop && i <= 24) {
236          i += 0.2;
237          double newY = Math.Sin(i);
238          Console.WriteLine("working");
239          //row1.AddValue(rand.NextDouble() * 10);
240          row1.AddValue(newY * 10);
241          row2.AddValue(i * 2 - 15);
242          Thread.Sleep(100);
243        }
244        Console.WriteLine("worker thread: terminating gracefully.");
245      }
246
247      public void DoWorkSingleLine() {
248        IDataRow row1 = new DataRow {
249          Color = Color.Red,
250          Thickness = 2,
251          Label = "Sinus",
252          Style = DrawingStyle.Solid,
253          ShowMarkers = false
254        };
255        model.AddDataRow(row1);
256
257        IDataRow row2 = new DataRow {
258          Color = Color.Red,
259          Thickness = 3,
260          Label = "Growing",
261          Style = DrawingStyle.Solid,
262          ShowMarkers = false
263        };
264        model.AddDataRow(row2);
265
266        MinAggregator aggregator = new MinAggregator {
267          Label = "MinAggregator",
268          Color = Color.Pink,
269          Thickness = 3,
270          Style = DrawingStyle.Solid,
271          LineType = DataRowType.SingleValue
272        };
273        aggregator.AddWatch(row1);
274        model.AddDataRow(aggregator);
275
276        MaxAggregator maxAggregator = new MaxAggregator {
277          Label = "MaxAggregator",
278          Color = Color.DeepSkyBlue,
279          Thickness = 3,
280          Style = DrawingStyle.Solid,
281          LineType = DataRowType.SingleValue
282        };
283        maxAggregator.AddWatch(row1);
284        model.AddDataRow(maxAggregator);
285
286        AvgAggregator avgAggregator = new AvgAggregator {
287          Label = "AvgAggregator",
288          Color = Color.Violet,
289          Thickness = 3,
290          Style = DrawingStyle.Solid,
291          LineType = DataRowType.SingleValue
292        };
293        avgAggregator.AddWatch(row1);
294        model.AddDataRow(avgAggregator);
295
296        double i = 0;
297
298
299        while (!_shouldStop && i <= 240) {
300          i += 0.2;
301          double newY = Math.Sin(i);
302          Console.WriteLine("working");
303          //row1.AddValue(rand.NextDouble() * 10);
304          row1.AddValue(newY * 10);
305          row2.AddValue(i * 2 - 15);
306          //System.Threading.Thread.Sleep(100);
307        }
308        Console.WriteLine("worker thread: terminating gracefully.");
309      }
310
311      public void DoWorkAvgLine() {
312        IDataRow row1 = new DataRow {
313          Color = Color.Red,
314          Thickness = 2,
315          Label = "Sinus",
316          Style = DrawingStyle.Solid,
317          ShowMarkers = false
318        };
319        model.AddDataRow(row1);
320
321        IDataRow row2 = new DataRow {
322          Color = Color.Red,
323          Thickness = 3,
324          Label = "Growing",
325          Style = DrawingStyle.Solid,
326          ShowMarkers = false
327        };
328        model.AddDataRow(row2);
329
330        AvgLineAggregator avgLineAggregator = new AvgLineAggregator {
331          Label = "AvgLineAggregator",
332          Color = Color.Violet,
333          Thickness = 3,
334          Style = DrawingStyle.Solid,
335          LineType = DataRowType.Normal,
336          ShowMarkers = false
337        };
338        avgLineAggregator.AddWatch(row1);
339        avgLineAggregator.AddWatch(row2);
340        model.AddDataRow(avgLineAggregator);
341
342        double i = 0;
343
344        while (!_shouldStop && i <= 240) {
345          i += 0.2;
346          double newY = Math.Sin(i);
347          Console.WriteLine("working");
348          //row1.AddValue(rand.NextDouble() * 10);
349          row1.AddValue(newY * 10);
350          row2.AddValue(i * 2 - 15);
351          //Thread.Sleep(100);
352        }
353        Console.WriteLine("worker thread: terminating gracefully.");
354      }
355
356      public void DoWorkFloatingAvg() {
357        IDataRow row1 = new DataRow {
358          Color = Color.Red,
359          Thickness = 2,
360          Label = "SinusHacked",
361          Style = DrawingStyle.Solid,
362          ShowMarkers = false
363        };
364        model.AddDataRow(row1);
365
366        IDataRow row2 = new DataRow {
367          Color = Color.Red,
368          Thickness = 3,
369          Label = "GrowingHacked",
370          Style = DrawingStyle.Solid,
371          ShowMarkers = false
372        };
373        model.AddDataRow(row2);
374
375        FloatingAvgAggregator avgAggregator = new FloatingAvgAggregator {
376                                                                          Thickness = 2,
377                                                                          Label = "floatingAvg",
378                                                                          Color = Color.Peru,
379                                                                          ShowMarkers = false,
380                                                                          Style = DrawingStyle.Solid
381                                                                        };
382
383        avgAggregator.AddWatch(row1);
384        model.AddDataRow(avgAggregator);
385
386        FloatingAvgAggregator avgAggregator2 = new FloatingAvgAggregator {
387          Thickness = 2,
388          Label = "floatingAvg",
389          Color = Color.Aqua,
390          ShowMarkers = false,
391          Style = DrawingStyle.Solid
392        };
393
394        avgAggregator2.AddWatch(row2);
395        model.AddDataRow(avgAggregator2);
396
397       
398        double i = 0;
399        Random rnd = new Random();
400
401        while (!_shouldStop && i <= 100) {
402          i += 0.2;
403          double newY = Math.Sin(i);
404
405          double hack = rnd.NextDouble() * i / 10;
406          row1.AddValue(newY * 10 + hack);
407
408          hack = rnd.NextDouble() * i / 10;
409          row2.AddValue(i * 2 - 15 + hack);
410          //Thread.Sleep(100);
411        }
412        Console.WriteLine("worker thread: terminating gracefully.");
413      }
414
415      public void RequestStop() {
416        _shouldStop = true;
417      }
418
419      // Volatile is used as hint to the compiler that this data
420      // member will be accessed by multiple threads.
421      private volatile bool _shouldStop;
422    }
423
424
425    [Test]
426    public void TestAggregatorMultiLine() {
427      LineChartTestForm f = new LineChartTestForm(model);
428
429      // Create the thread object. This does not start the thread.
430      Worker workerObject = new Worker(model);
431      Thread workerThread = new Thread(workerObject.DoWorkMultiLine);
432
433      // Start the worker thread.
434      workerThread.Start();
435
436      f.ShowDialog();
437      workerObject.RequestStop();
438    }
439
440    [Test]
441    public void TestAggregatorSingleLine() {
442      LineChartTestForm f = new LineChartTestForm(model);
443      model.Title = "SingleLineAggregator Tests";
444
445      // Create the thread object. This does not start the thread.
446      Worker workerObject = new Worker(model);
447      Thread workerThread = new Thread(workerObject.DoWorkSingleLine);
448
449      // Start the worker thread.
450      workerThread.Start();
451
452      f.ShowDialog();
453      workerObject.RequestStop();
454    }
455
456    [Test]
457    public void TestAggregatorAvgLine() {
458      LineChartTestForm f = new LineChartTestForm(model);
459      model.Title = "AvgLineTest";
460
461      // Create the thread object. This does not start the thread.
462      Worker workerObject = new Worker(model);
463      Thread workerThread = new Thread(workerObject.DoWorkAvgLine);
464
465      // Start the worker thread.
466      workerThread.Start();
467
468      f.ShowDialog();
469      workerObject.RequestStop();
470    }
471
472    [Test]
473    public void TestFloatingAvg() {
474      LineChartTestForm f = new LineChartTestForm(model);
475      model.Title = "FloatingAvg Test";
476      model.ViewSettings.LegendPosition = Legend.LegendPosition.Top;
477
478      // Create the thread object. This does not start the thread.
479      Worker workerObject = new Worker(model);
480      Thread workerThread = new Thread(workerObject.DoWorkFloatingAvg);
481
482      // Start the worker thread.
483      workerThread.Start();
484
485      f.ShowDialog();
486      workerObject.RequestStop();
487    }
488
489
490    [Test]
491    public void TestAutoZoomInConstructor() {
492      IDataRow row1 = new DataRow { Color = Color.Red, Thickness = 3, Style = DrawingStyle.Solid };
493
494      model.AddDataRow(row1);
495
496      row1.AddValue(10);
497      row1.AddValue(5);
498      row1.AddValue(7);
499      row1.AddValue(3);
500      row1.AddValue(10);
501      row1.AddValue(2);
502
503      LineChartTestForm f = new LineChartTestForm(model);
504      f.ShowDialog();
505    }
506
507
508    [Test]
509    public void TestSingleValueDataRows() {
510      LineChartTestForm f = new LineChartTestForm(model);
511
512      IDataRow row1 = new DataRow();
513      IDataRow row2 = new DataRow();
514      IDataRow row3 = new DataRow();
515
516      IDataRow row4 = new DataRow();
517      IDataRow row5 = new DataRow();
518      IDataRow row6 = new DataRow();
519
520      row1.Color = Color.Red;
521      row2.Color = Color.Green;
522      row3.Color = Color.Blue;
523
524      row4.Color = Color.DeepPink;
525      row5.Color = Color.Firebrick;
526      row6.Color = Color.DarkSlateGray;
527
528      row1.Thickness = 3;
529      row2.Thickness = 4;
530      row3.Thickness = 5;
531
532      row4.Thickness = 3;
533      row5.Thickness = 4;
534      row6.Thickness = 5;
535
536      row1.Label = "SingleValue";
537      row2.Label = "Gertschi";
538      row3.Label = "Maxi";
539
540      row4.Label = "Simon";
541      row5.Label = "klausmuellerwesternhagenunddierasperies";
542      row6.Label = "anyways";
543
544      row1.Style = DrawingStyle.Solid;
545      row2.Style = DrawingStyle.Solid;
546      row3.Style = DrawingStyle.Dashed;
547
548      row4.Style = DrawingStyle.Solid;
549      row5.Style = DrawingStyle.Solid;
550      row6.Style = DrawingStyle.Dashed;
551
552      row1.LineType = DataRowType.SingleValue;
553      row2.LineType = DataRowType.SingleValue;
554      row1.AddValue(12);
555
556      row2.AddValue(5);
557
558
559      row3.AddValue(2);
560      row3.AddValue(5);
561      row3.AddValue(9);
562      row3.AddValue(1);
563      row3.AddValue(3);
564
565
566      row4.AddValue(10);
567      row5.AddValue(11);
568      row6.AddValue(11);
569
570      model.AddDataRow(row1);
571      model.AddDataRow(row2);
572      model.AddDataRow(row3);
573      model.AddDataRow(row4);
574      model.AddDataRow(row5);
575      model.AddDataRow(row6);
576
577      f.ShowDialog();
578    }
579
580    [Test]
581    public void TestMainForm() {
582      MainForm f = new MainForm();
583      f.ShowDialog();
584    }
585
586
587    [Test]
588    public void TestPointLines() {
589      IDataRow row1 = new DataRow { Color = Color.Red, Thickness = 3, Style = DrawingStyle.Dashed };
590
591      row1.LineType = DataRowType.Points;
592      model.AddDataRow(row1);
593
594      row1.AddValue(10);
595      row1.AddValue(5);
596      row1.AddValue(7);
597      row1.AddValue(3);
598      row1.AddValue(10);
599      row1.AddValue(2);
600
601      LineChartTestForm f = new LineChartTestForm(model);
602      f.ShowDialog();
603    }
604
605
606  }
607}
Note: See TracBrowser for help on using the repository browser.