Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/3.3/PointXYChartView.cs @ 2474

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

Implemented generic EventArgs (#796)

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;
34
35namespace HeuristicLab.Logging {
36  /// <summary>
37  /// Visual representation of a <see cref="PointXYChart"/>.
38  /// </summary>
39  public partial class PointXYChartView : ViewBase {
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 chart 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 PointXYChart PointXYChart {
61      get { return (PointXYChart)base.Item; }
62      set { base.Item = value; }
63    }
64
65    /// <summary>
66    /// Initializes a new instance of <see cref="PointXYChartView"/>.
67    /// </summary>
68    public PointXYChartView() {
69      InitializeComponent();
70      Caption = "PointXYChart View";
71    }
72    /// <summary>
73    /// Initializes a new instance of <see cref="PointXYChartView"/> with the given
74    /// <paramref name="pointXYChart"/>.
75    /// </summary>
76    /// <param name="pointXYChart">The chart to represent visually.</param>
77    public PointXYChartView(PointXYChart pointXYChart)
78      : this() {
79      PointXYChart = pointXYChart;
80    }
81
82    /// <summary>
83    /// Removes the eventhandlers from the underlying <see cref="PointXYChart"/>.
84    /// </summary>
85    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
86    protected override void RemoveItemEvents() {
87      if (PointXYChart != null) {
88        PointXYChart.Values.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
89        PointXYChart.Values.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(Values_ItemRemoved);
90      }
91      base.RemoveItemEvents();
92    }
93    /// <summary>
94    /// Adds eventhandlers to the underlying <see cref="IOperatorGraph"/>.
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 (PointXYChart != null) {
100        PointXYChart.Values.ItemAdded += new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
101        PointXYChart.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="ViewBase"/>.</remarks>
109    protected override void UpdateControls() {
110      base.UpdateControls();
111      Datachart datachart = new Datachart(-50, -5000, 1000, 55000);
112      datachart.Title = "Point X/Y 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      double maxY = double.MinValue, minY = double.MaxValue;
118      double maxX = double.MinValue, minX = double.MaxValue;
119      if (PointXYChart != null) {
120        datachart.UpdateEnabled = false;
121        for (int i = 0; i < PointXYChart.Values.Count; i++) {
122          int colorIndex = (i % 12) * 3;
123          Color curCol = Color.FromArgb(colors[colorIndex], colors[colorIndex + 1], colors[colorIndex + 2]);
124          Pen p = new Pen(curCol);
125          SolidBrush b = new SolidBrush(curCol);
126          datachart.AddDataRow(PointXYChart.ConnectDots.Data ? DataRowType.Lines : DataRowType.Points, p, b);
127        }
128
129        for (int i = 0; i < PointXYChart.Values.Count; i++) {
130          ItemList list = (ItemList) PointXYChart.Values[i];
131          for (int j = 0; j < list.Count; j++) {
132           ItemList value = ((ItemList)list[j]);
133           double x = ((DoubleData)value[0]).Data;
134           double y = ((DoubleData)value[1]).Data;
135            if (!double.IsInfinity(x) && !double.IsNaN(x) && !double.IsInfinity(y) && !double.IsNaN(y)) {
136              if (x < minX) minX = x;
137              if (x > maxX) maxX = x;
138              if (y < minY) minY = y;
139              if (y > maxY) maxY = y;
140
141              datachart.AddDataPoint(i, x, y);                                                         
142            }
143          }
144        }
145        datachart.ZoomIn(minX - (Math.Abs(maxX - minX) * 0.1), minY - (Math.Abs(maxY -minY) * 0.1), maxX + (Math.Abs(maxX - minX) * 0.1), maxY + (Math.Abs(maxY - minY) * 0.1));
146        datachart.UpdateEnabled = true;
147        datachart.EnforceUpdate();
148      }
149    }
150
151    #region Values Events
152    private delegate void ItemIndexDelegate(object sender, EventArgs<IItem, int> e);
153    private void Values_ItemRemoved(object sender, EventArgs<IItem, int> e) {
154      if (InvokeRequired) {
155        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
156      } else {
157        Datachart datachart = dataChartControl.Chart;
158      }
159    }
160    private void Values_ItemAdded(object sender, EventArgs<IItem, int> e) {
161      if (InvokeRequired) {
162        Invoke(new ItemIndexDelegate(Values_ItemAdded), sender, e);
163      } else {
164        //Datachart datachart = dataChartControl.Chart;
165        //ItemList list = (ItemList)e.Item;
166        //datachart.UpdateEnabled = false;
167        //for (int i = 0; i < list.Count; i++)
168        //  datachart.AddDataPoint(i, e.Index, ((DoubleArrayData)list[i]).Data[0]);
169        //datachart.UpdateEnabled = true;
170        //datachart.EnforceUpdate();
171      }
172    }
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.