Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Visualization.Test/3.2/LineChartTests.cs @ 7005

Last change on this file since 7005 was 1818, checked in by cbahner, 16 years ago

#519 added floating average

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