Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/LinechartView.cs @ 1141

Last change on this file since 1141 was 926, checked in by abeham, 16 years ago

Added automatic update of the zoom region (#423)

File size: 5.4 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  public partial class LinechartView : ViewBase {
36    private double maxY = double.MinValue, minY = double.MaxValue;
37    private static int[] colors = new int[] {
38      182,182,255,
39      218,255,182,
40      255,182,218,
41      182,255,255,
42      218,182,255,
43      255,182,255,
44      255,182,182,
45      255,218,182,
46      255,255,182,
47      182,255,182,
48      182,255,218,
49      182,218,255
50    };
51
52    public Linechart Linechart {
53      get { return (Linechart)base.Item; }
54      set { base.Item = value; }
55    }
56
57    public LinechartView() {
58      InitializeComponent();
59      Caption = "Linechart View";
60    }
61    public LinechartView(Linechart linechart)
62      : this() {
63      Linechart = linechart;
64    }
65
66    protected override void RemoveItemEvents() {
67      if(Linechart != null) {
68        Linechart.Values.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Values_ItemAdded);
69        Linechart.Values.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Values_ItemRemoved);
70      }
71      base.RemoveItemEvents();
72    }
73    protected override void AddItemEvents() {
74      base.AddItemEvents();
75      if(Linechart != null) {
76        Linechart.Values.ItemAdded += new EventHandler<ItemIndexEventArgs>(Values_ItemAdded);
77        Linechart.Values.ItemRemoved += new EventHandler<ItemIndexEventArgs>(Values_ItemRemoved);
78      }
79    }
80
81    protected override void UpdateControls() {
82      base.UpdateControls();
83      Datachart datachart = new Datachart(-50, -5000, 1000, 55000);
84      datachart.Title = "Line Chart";
85      dataChartControl.ScaleOnResize = false;
86      dataChartControl.Chart = datachart;
87      datachart.Group.Clear();
88      datachart.Group.Add(new Axis(datachart, 0, 0, AxisType.Both));
89      if(Linechart != null) {
90        datachart.UpdateEnabled = false;
91        for(int i = 0; i < Linechart.NumberOfLines; i++) {
92          int colorIndex = (i % 12)*3;
93          Color curCol = Color.FromArgb(colors[colorIndex],colors[colorIndex + 1], colors[colorIndex + 2]);
94          Pen p = new Pen(curCol);
95          SolidBrush b = new SolidBrush(curCol);
96          datachart.AddDataRow(DataRowType.Lines, p, b);
97        }
98
99        for(int i = 0; i < Linechart.Values.Count; i++) {
100          ItemList list = (ItemList)Linechart.Values[i];
101          for(int j = 0; j < list.Count; j++) {
102            double value = 0.0;
103            if (list[j] is IntData) value = (double)((IntData)list[j]).Data;
104            else value = ((DoubleData)list[j]).Data;
105            if(!double.IsInfinity(value) && !double.IsNaN(value)) {
106              if(value < minY) minY = value;
107              if(value > maxY) maxY = value;
108              datachart.AddDataPoint(j, i, value);
109            }
110          }
111        }
112        datachart.ZoomIn(-Linechart.Values.Count * 0.05, minY - (minY * 0.1), Linechart.Values.Count * 1.05, maxY * 1.05);
113        datachart.UpdateEnabled = true;
114        datachart.EnforceUpdate();
115      }
116    }
117
118    #region Values Events
119    private delegate void ItemIndexDelegate(object sender, ItemIndexEventArgs e);
120    private void Values_ItemRemoved(object sender, ItemIndexEventArgs e) {
121      if(InvokeRequired) {
122        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
123      } else {
124        Datachart datachart = dataChartControl.Chart;
125      }
126    }
127    private void Values_ItemAdded(object sender, ItemIndexEventArgs e) {
128      if(InvokeRequired) {
129        Invoke(new ItemIndexDelegate(Values_ItemAdded), sender, e);
130      } else {
131        Datachart datachart = dataChartControl.Chart;
132        ItemList list = (ItemList)e.Item;
133        datachart.UpdateEnabled = false;
134        for (int i = 0; i < list.Count; i++) {
135          double value = 0.0;
136          if (list[i] is IntData) value = (double)((IntData)list[i]).Data;
137          else value = ((DoubleData)list[i]).Data;
138          datachart.AddDataPoint(i, e.Index, value);
139          if (value < minY) minY = value;
140          if (value > maxY) maxY = value;
141        }
142        datachart.ZoomIn(-Linechart.Values.Count * 0.05, minY - (minY * 0.1), Linechart.Values.Count * 1.05, maxY * 1.05);
143        datachart.UpdateEnabled = true;
144        datachart.EnforceUpdate();
145      }
146    }
147    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.