Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Logging/3.2/PointXYChartView.cs @ 3962

Last change on this file since 3962 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

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