Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/3.3/LinechartView.cs @ 2546

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

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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