Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3198 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.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;
33using HeuristicLab.Common;
34using HeuristicLab.Core.Views;
35using HeuristicLab.MainForm;
36
37namespace HeuristicLab.Logging {
38  /// <summary>
39  /// Visual representation of a <see cref="PointXYChart"/>.
40  /// </summary>
41  [Content(typeof(PointXYChart), true)]
42  public partial class PointXYChartView : ItemViewBase {
43    private static int[] colors = new int[] {
44      182,182,255,
45      218,255,182,
46      255,182,218,
47      182,255,255,
48      218,182,255,
49      255,182,255,
50      255,182,182,
51      255,218,182,
52      255,255,182,
53      182,255,182,
54      182,255,218,
55      182,218,255
56    };
57
58    /// <summary>
59    /// Gets or sets the chart to represent visually.
60    /// </summary>
61    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
62    /// No own data storage present.</remarks>
63    public PointXYChart PointXYChart {
64      get { return (PointXYChart)base.Item; }
65      set { base.Item = value; }
66    }
67
68    /// <summary>
69    /// Initializes a new instance of <see cref="PointXYChartView"/>.
70    /// </summary>
71    public PointXYChartView() {
72      InitializeComponent();
73      Caption = "PointXYChart View";
74    }
75    /// <summary>
76    /// Initializes a new instance of <see cref="PointXYChartView"/> with the given
77    /// <paramref name="pointXYChart"/>.
78    /// </summary>
79    /// <param name="pointXYChart">The chart to represent visually.</param>
80    public PointXYChartView(PointXYChart pointXYChart)
81      : this() {
82      PointXYChart = pointXYChart;
83    }
84
85    /// <summary>
86    /// Removes the eventhandlers from the underlying <see cref="PointXYChart"/>.
87    /// </summary>
88    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
89    protected override void RemoveItemEvents() {
90      if (PointXYChart != null) {
91        PointXYChart.Values.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
92        PointXYChart.Values.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(Values_ItemRemoved);
93      }
94      base.RemoveItemEvents();
95    }
96    /// <summary>
97    /// Adds eventhandlers to the underlying <see cref="IOperatorGraph"/>.
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 (PointXYChart != null) {
103        PointXYChart.Values.ItemAdded += new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
104        PointXYChart.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="ViewBase"/>.</remarks>
112    protected override void UpdateControls() {
113      base.UpdateControls();
114      Datachart datachart = new Datachart(-50, -5000, 1000, 55000);
115      datachart.Title = "Point X/Y 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      double maxY = double.MinValue, minY = double.MaxValue;
121      double maxX = double.MinValue, minX = double.MaxValue;
122      if (PointXYChart != null) {
123        datachart.UpdateEnabled = false;
124        for (int i = 0; i < PointXYChart.Values.Count; i++) {
125          int colorIndex = (i % 12) * 3;
126          Color curCol = Color.FromArgb(colors[colorIndex], colors[colorIndex + 1], colors[colorIndex + 2]);
127          Pen p = new Pen(curCol);
128          SolidBrush b = new SolidBrush(curCol);
129          datachart.AddDataRow(PointXYChart.ConnectDots.Data ? DataRowType.Lines : DataRowType.Points, p, b);
130        }
131
132        for (int i = 0; i < PointXYChart.Values.Count; i++) {
133          ItemList list = (ItemList) PointXYChart.Values[i];
134          for (int j = 0; j < list.Count; j++) {
135           ItemList value = ((ItemList)list[j]);
136           double x = ((DoubleData)value[0]).Data;
137           double y = ((DoubleData)value[1]).Data;
138            if (!double.IsInfinity(x) && !double.IsNaN(x) && !double.IsInfinity(y) && !double.IsNaN(y)) {
139              if (x < minX) minX = x;
140              if (x > maxX) maxX = x;
141              if (y < minY) minY = y;
142              if (y > maxY) maxY = y;
143
144              datachart.AddDataPoint(i, x, y);                                                         
145            }
146          }
147        }
148        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));
149        datachart.UpdateEnabled = true;
150        datachart.EnforceUpdate();
151      }
152    }
153
154    #region Values Events
155    private delegate void ItemIndexDelegate(object sender, EventArgs<IItem, int> e);
156    private void Values_ItemRemoved(object sender, EventArgs<IItem, int> e) {
157      if (InvokeRequired) {
158        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
159      } else {
160        Datachart datachart = dataChartControl.Chart;
161      }
162    }
163    private void Values_ItemAdded(object sender, EventArgs<IItem, int> e) {
164      if (InvokeRequired) {
165        Invoke(new ItemIndexDelegate(Values_ItemAdded), sender, e);
166      } else {
167        //Datachart datachart = dataChartControl.Chart;
168        //ItemList list = (ItemList)e.Item;
169        //datachart.UpdateEnabled = false;
170        //for (int i = 0; i < list.Count; i++)
171        //  datachart.AddDataPoint(i, e.Index, ((DoubleArrayData)list[i]).Data[0]);
172        //datachart.UpdateEnabled = true;
173        //datachart.EnforceUpdate();
174      }
175    }
176    #endregion
177  }
178}
Note: See TracBrowser for help on using the repository browser.