Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Logging/3.2/LinechartView.cs @ 7005

Last change on this file since 7005 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Charting;
32using HeuristicLab.Charting.Data;
33
34namespace HeuristicLab.Logging {
35  /// <summary>
36  /// The visual representation of a <see cref="Linechart"/>.
37  /// </summary>
38  public partial class LinechartView : ViewBase {
39    private double maxY = double.MinValue, minY = double.MaxValue;
40    private static int[] colors = new int[] {
41      182,182,255,
42      218,255,182,
43      255,182,218,
44      182,255,255,
45      218,182,255,
46      255,182,255,
47      255,182,182,
48      255,218,182,
49      255,255,182,
50      182,255,182,
51      182,255,218,
52      182,218,255
53    };
54
55    /// <summary>
56    /// Gets or sets the Linechart object to represent visually.
57    /// </summary>
58    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
59    /// No own data storage present.</remarks>
60    public Linechart Linechart {
61      get { return (Linechart)base.Item; }
62      set { base.Item = value; }
63    }
64
65    /// <summary>
66    /// Initializes a new instance of <see cref="LinechartView"/>.
67    /// </summary>
68    public LinechartView() {
69      InitializeComponent();
70      Caption = "Linechart View";
71    }
72    /// <summary>
73    /// Initializes a new instance of <see cref="LinechartView"/> with the given <paramref name="linechart"/>.
74    /// </summary>
75    /// <param name="linechart">The linechart to represent visually.</param>
76    public LinechartView(Linechart linechart)
77      : this() {
78      Linechart = linechart;
79    }
80
81    /// <summary>
82    /// Removes the event handlers from the underlying <see cref="Linechart"/>.
83    /// </summary>
84    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
85    protected override void RemoveItemEvents() {
86      if(Linechart != null) {
87        Linechart.Values.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Values_ItemAdded);
88        Linechart.Values.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Values_ItemRemoved);
89      }
90      base.RemoveItemEvents();
91    }
92    /// <summary>
93    /// Adds event handlers to the underlying <see cref="Linechart"/>.
94    /// </summary>
95    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
96    protected override void AddItemEvents() {
97      base.AddItemEvents();
98      if(Linechart != null) {
99        Linechart.Values.ItemAdded += new EventHandler<ItemIndexEventArgs>(Values_ItemAdded);
100        Linechart.Values.ItemRemoved += new EventHandler<ItemIndexEventArgs>(Values_ItemRemoved);
101      }
102    }
103
104    /// <summary>
105    /// Updates all controls with the latest data of the model.
106    /// </summary>
107    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="UpdateControls"/>.</remarks>
108    protected override void UpdateControls() {
109      base.UpdateControls();
110      Datachart datachart = new Datachart(-50, -5000, 1000, 55000);
111      datachart.Title = "Line Chart";
112      dataChartControl.ScaleOnResize = false;
113      dataChartControl.Chart = datachart;
114      datachart.Group.Clear();
115      datachart.Group.Add(new Axis(datachart, 0, 0, AxisType.Both));
116      if(Linechart != null) {
117        datachart.UpdateEnabled = false;
118        for(int i = 0; i < Linechart.NumberOfLines; i++) {
119          int colorIndex = (i % 12)*3;
120          Color curCol = Color.FromArgb(colors[colorIndex],colors[colorIndex + 1], colors[colorIndex + 2]);
121          Pen p = new Pen(curCol);
122          SolidBrush b = new SolidBrush(curCol);
123          datachart.AddDataRow(DataRowType.Lines, p, b);
124        }
125
126        for(int i = 0; i < Linechart.Values.Count; i++) {
127          ItemList list = (ItemList)Linechart.Values[i];
128          for(int j = 0; j < list.Count; j++) {
129            double value = 0.0;
130            if (list[j] is IntData) value = (double)((IntData)list[j]).Data;
131            else value = ((DoubleData)list[j]).Data;
132            if(!double.IsInfinity(value) && !double.IsNaN(value)) {
133              if(value < minY) minY = value;
134              if(value > maxY) maxY = value;
135              datachart.AddDataPoint(j, i, value);
136            }
137          }
138        }
139        datachart.ZoomIn(-Linechart.Values.Count * 0.05, minY - (minY * 0.1), Linechart.Values.Count * 1.05, maxY * 1.05);
140        datachart.UpdateEnabled = true;
141        datachart.EnforceUpdate();
142      }
143    }
144
145    #region Values Events
146    private delegate void ItemIndexDelegate(object sender, ItemIndexEventArgs e);
147    private void Values_ItemRemoved(object sender, ItemIndexEventArgs e) {
148      if(InvokeRequired) {
149        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
150      } else {
151        Datachart datachart = dataChartControl.Chart;
152      }
153    }
154    private void Values_ItemAdded(object sender, ItemIndexEventArgs e) {
155      if(InvokeRequired) {
156        Invoke(new ItemIndexDelegate(Values_ItemAdded), sender, e);
157      } else {
158        Datachart datachart = dataChartControl.Chart;
159        ItemList list = (ItemList)e.Item;
160        datachart.UpdateEnabled = false;
161        for (int i = 0; i < list.Count; i++) {
162          double value = 0.0;
163          if (list[i] is IntData) value = (double)((IntData)list[i]).Data;
164          else value = ((DoubleData)list[i]).Data;
165          datachart.AddDataPoint(i, e.Index, value);
166          if (value < minY) minY = value;
167          if (value > maxY) maxY = value;
168        }
169        datachart.ZoomIn(-Linechart.Values.Count * 0.05, minY - (minY * 0.1), Linechart.Values.Count * 1.05, maxY * 1.05);
170        datachart.UpdateEnabled = true;
171        datachart.EnforceUpdate();
172      }
173    }
174    #endregion
175  }
176}
Note: See TracBrowser for help on using the repository browser.