Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2710 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
RevLine 
[671]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;
[2474]33using HeuristicLab.Common;
[2520]34using HeuristicLab.Core.Views;
35using HeuristicLab.MainForm;
[671]36
37namespace HeuristicLab.Logging {
[1167]38  /// <summary>
39  /// Visual representation of a <see cref="PointXYChart"/>.
40  /// </summary>
[2520]41  [Content(typeof(PointXYChart), true)]
[2546]42  public partial class PointXYChartView : ItemViewBase {
[671]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
[1167]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>
[671]63    public PointXYChart PointXYChart {
64      get { return (PointXYChart)base.Item; }
65      set { base.Item = value; }
66    }
67
[1167]68    /// <summary>
69    /// Initializes a new instance of <see cref="PointXYChartView"/>.
70    /// </summary>
[671]71    public PointXYChartView() {
72      InitializeComponent();
73      Caption = "PointXYChart View";
74    }
[1167]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>
[671]80    public PointXYChartView(PointXYChart pointXYChart)
81      : this() {
82      PointXYChart = pointXYChart;
83    }
84
[1167]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>
[671]89    protected override void RemoveItemEvents() {
90      if (PointXYChart != null) {
[2474]91        PointXYChart.Values.ItemAdded -= new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
92        PointXYChart.Values.ItemRemoved -= new EventHandler<EventArgs<IItem, int>>(Values_ItemRemoved);
[671]93      }
94      base.RemoveItemEvents();
95    }
[1167]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>
[671]100    protected override void AddItemEvents() {
101      base.AddItemEvents();
102      if (PointXYChart != null) {
[2474]103        PointXYChart.Values.ItemAdded += new EventHandler<EventArgs<IItem, int>>(Values_ItemAdded);
104        PointXYChart.Values.ItemRemoved += new EventHandler<EventArgs<IItem, int>>(Values_ItemRemoved);
[671]105      }
106    }
107
[1167]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>
[671]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++) {
[677]133          ItemList list = (ItemList) PointXYChart.Values[i];
[671]134          for (int j = 0; j < list.Count; j++) {
[677]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;
[671]143
[677]144              datachart.AddDataPoint(i, x, y);                                                         
[671]145            }
146          }
147        }
[673]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));
[671]149        datachart.UpdateEnabled = true;
150        datachart.EnforceUpdate();
151      }
152    }
153
154    #region Values Events
[2474]155    private delegate void ItemIndexDelegate(object sender, EventArgs<IItem, int> e);
156    private void Values_ItemRemoved(object sender, EventArgs<IItem, int> e) {
[671]157      if (InvokeRequired) {
158        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
159      } else {
160        Datachart datachart = dataChartControl.Chart;
161      }
162    }
[2474]163    private void Values_ItemAdded(object sender, EventArgs<IItem, int> e) {
[671]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.