Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Collections/sources/HeuristicLab.Logging/LinechartView.cs @ 440

Last change on this file since 440 was 362, checked in by gkronber, 16 years ago

improved linechartView to use a different color for each line and automatically zoom in to the relevant area. (ticket #192)

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