Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implement Y-Axis Position (left/right) (#554)

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