Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1342 was 1342, checked in by shofstad, 15 years ago

Legend implementation updated with position setting (#407)

File size: 10.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
80      row1.Color = Color.Red;
81      row1.Thickness = 3;
82      row1.Style = DrawingStyle.Solid;
83
84      row2.Color = Color.Green;
85      row2.Thickness = 3;
86      row2.Style = DrawingStyle.Solid;
87
88      model.AddDataRow(row1);
89      model.AddDataRow(row2);
90
91      Random rand = new Random(42);
92     
93      for (int i = 0; i < 10; i++) {
94        row1.AddValue(rand.NextDouble()*10);
95        row2.AddValue(rand.NextDouble()*10);
96      }
97
98      f.ShowDialog();
99    }
100
101    [Test]
102    public void TestAggregator() {
103      LineChartTestForm f = new LineChartTestForm(model);
104
105      IDataRow row1 = new DataRow();
106      row1.Label = "row";
107      row1.Color = Color.Red;
108      row1.Thickness = 3;
109      row1.Style = DrawingStyle.Solid;
110
111      model.AddDataRow(row1);
112
113
114      IAggregator aggregator = new MinAggregator();
115      aggregator.Label = "MinAggregator";
116      aggregator.Color = Color.Pink;
117      aggregator.Thickness = 5;
118      aggregator.Style = DrawingStyle.Solid;
119      aggregator.AddValue(2);
120      aggregator.LineType = DataRowType.SingleValue;
121
122      IDataRow lineTest = new DataRow("testline");
123      lineTest.Color = Color.DarkSalmon;
124      lineTest.Thickness = 2;
125      lineTest.Style = DrawingStyle.Dashed;
126      lineTest.LineType = DataRowType.SingleValue;
127      model.AddDataRow(lineTest);
128      lineTest.AddValue(9);
129      lineTest.AddValue(2);
130      lineTest.AddValue(3);
131      lineTest.AddValue(4);
132
133      aggregator.AddWatch(row1);
134
135      model.AddDataRow(aggregator);
136
137
138      row1.AddValue(10);
139      row1.AddValue(5);
140      row1.AddValue(7);
141      row1.AddValue(3);
142      row1.AddValue(10);
143      row1.AddValue(2);
144
145      f.ShowDialog();
146    }
147
148
149    public class Worker {
150      // This method will be called when the thread is started.
151      private ChartDataRowsModel model;
152      public Worker(ChartDataRowsModel model) {
153        this.model = model;
154      }
155     
156      public void DoWork() {
157
158        IDataRow row1 = new DataRow();
159        row1.Color = Color.Red;
160        row1.Thickness = 2;
161        row1.Label = "Simon";
162        row1.Style = DrawingStyle.Solid;
163        model.AddDataRow(row1);
164
165        IDataRow row2 = new DataRow();
166        row2.Color = Color.Red;
167        row2.Thickness = 3;
168        row2.Label = "Simon";
169        row2.Style = DrawingStyle.Solid;
170        model.AddDataRow(row2);
171
172
173        IAggregator aggregator = new MinAggregator();
174        aggregator.Label = "MinAggregator";
175        aggregator.Color = Color.Pink;
176        aggregator.Thickness = 3;
177        aggregator.Style = DrawingStyle.Solid;
178        aggregator.LineType = DataRowType.SingleValue;
179        aggregator.AddWatch(row1);
180        model.AddDataRow(aggregator);
181
182        IAggregator maxAggregator = new MaxAggregator();
183        maxAggregator.Label = "MaxAggregator";
184        maxAggregator.Color = Color.DeepSkyBlue;
185        maxAggregator.Thickness = 3;
186        maxAggregator.Style = DrawingStyle.Solid;
187        maxAggregator.LineType = DataRowType.SingleValue;
188        maxAggregator.AddWatch(row1);
189        model.AddDataRow(maxAggregator);
190       
191       
192        IAggregator avgAggregator = new AvgAggregator();
193        avgAggregator.Label = "AvgAggregator";
194        avgAggregator.Color = Color.Violet;
195        avgAggregator.Thickness = 3;
196        avgAggregator.Style = DrawingStyle.Solid;
197        avgAggregator.LineType = DataRowType.SingleValue;
198        avgAggregator.AddWatch(row1);
199        model.AddDataRow(avgAggregator);
200
201        IAggregator 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        IAggregator 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        IAggregator 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        IAggregator 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 RequestStop() {
258        _shouldStop = true;
259      }
260      // Volatile is used as hint to the compiler that this data
261      // member will be accessed by multiple threads.
262      private volatile bool _shouldStop;
263    }
264
265
266    [Test]
267    public void TestAggregator2() {
268      LineChartTestForm f = new LineChartTestForm(model);
269   
270      // Create the thread object. This does not start the thread.
271      Worker workerObject = new Worker(model);
272      Thread workerThread = new Thread(workerObject.DoWork);
273
274      // Start the worker thread.
275      workerThread.Start();
276
277      f.ShowDialog();
278      workerObject.RequestStop();
279    }
280
281   
282
283    [Test]
284    public void TestAutoZoomInConstructor() {
285      IDataRow row1 = new DataRow();
286
287      row1.Color = Color.Red;
288      row1.Thickness = 3;
289      row1.Style = DrawingStyle.Solid;
290
291      model.AddDataRow(row1);
292
293      row1.AddValue(10);
294      row1.AddValue(5);
295      row1.AddValue(7);
296      row1.AddValue(3);
297      row1.AddValue(10);
298      row1.AddValue(2);
299
300      LineChartTestForm f = new LineChartTestForm(model);
301      f.ShowDialog();
302    }
303
304
305    [Test]
306    public void TestSingleValueDataRows() {
307      LineChartTestForm f = new LineChartTestForm(model);
308
309      IDataRow row1 = new DataRow();
310      IDataRow row2 = new DataRow();
311      IDataRow row3 = new DataRow();
312
313      IDataRow row4 = new DataRow();
314      IDataRow row5 = new DataRow();
315      IDataRow row6 = new DataRow();
316
317      row1.Color = Color.Red;
318      row2.Color = Color.Green;
319      row3.Color = Color.Blue;
320
321      row4.Color = Color.DeepPink;
322      row5.Color = Color.Firebrick;
323      row6.Color = Color.DarkSlateGray;
324
325      row1.Thickness = 3;
326      row2.Thickness = 4;
327      row3.Thickness = 5;
328
329      row4.Thickness = 3;
330      row5.Thickness = 4;
331      row6.Thickness = 5;
332
333      row1.Label = "SingleValue";
334      row2.Label = "Gertschi";
335      row3.Label = "Maxi";
336
337      row4.Label = "Simon";
338      row5.Label = "klausmuellerwesternhagenunddierasperies";
339      row6.Label = "anyways";
340
341      row1.Style = DrawingStyle.Solid;
342      row2.Style = DrawingStyle.Solid;
343      row3.Style = DrawingStyle.Dashed;
344
345      row4.Style = DrawingStyle.Solid;
346      row5.Style = DrawingStyle.Solid;
347      row6.Style = DrawingStyle.Dashed;
348
349      row1.LineType = DataRowType.SingleValue;
350      row2.LineType = DataRowType.SingleValue;
351      row1.AddValue(12);
352
353      row2.AddValue(5);
354     
355
356      row3.AddValue(2);
357      row3.AddValue(5);
358      row3.AddValue(9);
359      row3.AddValue(1);
360      row3.AddValue(3);
361
362
363      row4.AddValue(10);
364      row5.AddValue(11);
365      row6.AddValue(11);
366
367      model.AddDataRow(row1);
368      model.AddDataRow(row2);
369      model.AddDataRow(row3);
370      model.AddDataRow(row4);
371      model.AddDataRow(row5);
372      model.AddDataRow(row6);
373
374      f.ShowDialog();
375    }
376
377
378
379    [Test]
380    public void TestMainForm() {
381      MainForm f = new MainForm();
382      f.ShowDialog();
383    }
384  }
385}
Note: See TracBrowser for help on using the repository browser.