Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added a getter to the model for the default Y-axis descriptor. (#433)

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