Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/3.2/LinechartView.cs @ 3425

Last change on this file since 3425 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Charting;
33using HeuristicLab.Charting.Data;
34
35namespace HeuristicLab.Logging {
36  /// <summary>
37  /// The visual representation of a <see cref="Linechart"/>.
38  /// </summary>
39  public partial class LinechartView : ViewBase {
40    private double maxY = double.MinValue, minY = double.MaxValue;
41    private static int[] colors = new int[] {
42      182,182,255,
43      218,255,182,
44      255,182,218,
45      182,255,255,
46      218,182,255,
47      255,182,255,
48      255,182,182,
49      255,218,182,
50      255,255,182,
51      182,255,182,
52      182,255,218,
53      182,218,255
54    };
55
56    /// <summary>
57    /// Gets or sets the Linechart object to represent visually.
58    /// </summary>
59    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
60    /// No own data storage present.</remarks>
61    public Linechart Linechart {
62      get { return (Linechart)base.Item; }
63      set { base.Item = value; }
64    }
65
66    /// <summary>
67    /// Initializes a new instance of <see cref="LinechartView"/>.
68    /// </summary>
69    public LinechartView() {
70      InitializeComponent();
71      Caption = "Linechart View";
72    }
73    /// <summary>
74    /// Initializes a new instance of <see cref="LinechartView"/> with the given <paramref name="linechart"/>.
75    /// </summary>
76    /// <param name="linechart">The linechart to represent visually.</param>
77    public LinechartView(Linechart linechart)
78      : this() {
79      Linechart = linechart;
80    }
81
82    /// <summary>
83    /// Removes the event handlers from the underlying <see cref="Linechart"/>.
84    /// </summary>
85    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
86    protected override void RemoveItemEvents() {
87      if(Linechart != null) {
88        Linechart.Values.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
89        Linechart.Values.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(Values_ItemRemoved);
90      }
91      base.RemoveItemEvents();
92    }
93    /// <summary>
94    /// Adds event handlers to the underlying <see cref="Linechart"/>.
95    /// </summary>
96    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
97    protected override void AddItemEvents() {
98      base.AddItemEvents();
99      if(Linechart != null) {
100        Linechart.Values.ItemAdded += new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
101        Linechart.Values.ItemRemoved += new EventHandler<EventArgs<IItem, int>>(Values_ItemRemoved);
102      }
103    }
104
105    /// <summary>
106    /// Updates all controls with the latest data of the model.
107    /// </summary>
108    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="UpdateControls"/>.</remarks>
109    protected override void UpdateControls() {
110      base.UpdateControls();
111      Datachart datachart = new Datachart(-50, -5000, 1000, 55000);
112      datachart.Title = "Line Chart";
113      dataChartControl.ScaleOnResize = false;
114      dataChartControl.Chart = datachart;
115      datachart.Group.Clear();
116      datachart.Group.Add(new Axis(datachart, 0, 0, AxisType.Both));
117      if(Linechart != null) {
118        datachart.UpdateEnabled = false;
119        for(int i = 0; i < Linechart.NumberOfLines; i++) {
120          int colorIndex = (i % 12)*3;
121          Color curCol = Color.FromArgb(colors[colorIndex],colors[colorIndex + 1], colors[colorIndex + 2]);
122          Pen p = new Pen(curCol);
123          SolidBrush b = new SolidBrush(curCol);
124          datachart.AddDataRow(DataRowType.Lines, p, b);
125        }
126
127        for(int i = 0; i < Linechart.Values.Count; i++) {
128          ItemList list = (ItemList)Linechart.Values[i];
129          for(int j = 0; j < list.Count; j++) {
130            double value = 0.0;
131            if (list[j] is IntData) value = (double)((IntData)list[j]).Data;
132            else value = ((DoubleData)list[j]).Data;
133            if(!double.IsInfinity(value) && !double.IsNaN(value)) {
134              if(value < minY) minY = value;
135              if(value > maxY) maxY = value;
136              datachart.AddDataPoint(j, i, value);
137            }
138          }
139        }
140        datachart.ZoomIn(-Linechart.Values.Count * 0.05, minY - (minY * 0.1), Linechart.Values.Count * 1.05, maxY * 1.05);
141        datachart.UpdateEnabled = true;
142        datachart.EnforceUpdate();
143      }
144    }
145
146    #region Values Events
147    private delegate void ItemIndexDelegate(object sender, EventArgs<IItem, int> e);
148    private void Values_ItemRemoved(object sender, EventArgs<IItem, int> e) {
149      if(InvokeRequired) {
150        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
151      } else {
152        Datachart datachart = dataChartControl.Chart;
153      }
154    }
155    private void Values_ItemAdded(object sender, EventArgs<IItem, int> e) {
156      if(InvokeRequired) {
157        Invoke(new ItemIndexDelegate(Values_ItemAdded), sender, e);
158      } else {
159        Datachart datachart = dataChartControl.Chart;
160        ItemList list = (ItemList)e.Value;
161        datachart.UpdateEnabled = false;
162        for (int i = 0; i < list.Count; i++) {
163          double value = 0.0;
164          if (list[i] is IntData) value = (double)((IntData)list[i]).Data;
165          else value = ((DoubleData)list[i]).Data;
166          datachart.AddDataPoint(i, e.Value2, value);
167          if (value < minY) minY = value;
168          if (value > maxY) maxY = value;
169        }
170        datachart.ZoomIn(-Linechart.Values.Count * 0.05, minY - (minY * 0.1), Linechart.Values.Count * 1.05, maxY * 1.05);
171        datachart.UpdateEnabled = true;
172        datachart.EnforceUpdate();
173      }
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.