Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Logging/PointXYChartView.cs @ 866

Last change on this file since 866 was 677, checked in by mkommend, 16 years ago

PointXYChart refactored to use generic ItemList instead of Itemlist<ItemList<DoubleArrayData>> (ticket #310)

File size: 5.4 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 PointXYChartView : 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 PointXYChart PointXYChart {
52      get { return (PointXYChart)base.Item; }
53      set { base.Item = value; }
54    }
55
56    public PointXYChartView() {
57      InitializeComponent();
58      Caption = "PointXYChart View";
59    }
60    public PointXYChartView(PointXYChart pointXYChart)
61      : this() {
62      PointXYChart = pointXYChart;
63    }
64
65    protected override void RemoveItemEvents() {
66      if (PointXYChart != null) {
67        PointXYChart.Values.ItemAdded -= new EventHandler<ItemIndexEventArgs>(Values_ItemAdded);
68        PointXYChart.Values.ItemRemoved -= new EventHandler<ItemIndexEventArgs>(Values_ItemRemoved);
69      }
70      base.RemoveItemEvents();
71    }
72    protected override void AddItemEvents() {
73      base.AddItemEvents();
74      if (PointXYChart != null) {
75        PointXYChart.Values.ItemAdded += new EventHandler<ItemIndexEventArgs>(Values_ItemAdded);
76        PointXYChart.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 = "Point X/Y 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      double maxX = double.MinValue, minX = double.MaxValue;
90      if (PointXYChart != null) {
91        datachart.UpdateEnabled = false;
92        for (int i = 0; i < PointXYChart.Values.Count; i++) {
93          int colorIndex = (i % 12) * 3;
94          Color curCol = Color.FromArgb(colors[colorIndex], colors[colorIndex + 1], colors[colorIndex + 2]);
95          Pen p = new Pen(curCol);
96          SolidBrush b = new SolidBrush(curCol);
97          datachart.AddDataRow(PointXYChart.ConnectDots.Data ? DataRowType.Lines : DataRowType.Points, p, b);
98        }
99
100        for (int i = 0; i < PointXYChart.Values.Count; i++) {
101          ItemList list = (ItemList) PointXYChart.Values[i];
102          for (int j = 0; j < list.Count; j++) {
103           ItemList value = ((ItemList)list[j]);
104           double x = ((DoubleData)value[0]).Data;
105           double y = ((DoubleData)value[1]).Data;
106            if (!double.IsInfinity(x) && !double.IsNaN(x) && !double.IsInfinity(y) && !double.IsNaN(y)) {
107              if (x < minX) minX = x;
108              if (x > maxX) maxX = x;
109              if (y < minY) minY = y;
110              if (y > maxY) maxY = y;
111
112              datachart.AddDataPoint(i, x, y);                                                         
113            }
114          }
115        }
116        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));
117        datachart.UpdateEnabled = true;
118        datachart.EnforceUpdate();
119      }
120    }
121
122    #region Values Events
123    private delegate void ItemIndexDelegate(object sender, ItemIndexEventArgs e);
124    private void Values_ItemRemoved(object sender, ItemIndexEventArgs e) {
125      if (InvokeRequired) {
126        Invoke(new ItemIndexDelegate(Values_ItemRemoved), sender, e);
127      } else {
128        Datachart datachart = dataChartControl.Chart;
129      }
130    }
131    private void Values_ItemAdded(object sender, ItemIndexEventArgs e) {
132      if (InvokeRequired) {
133        Invoke(new ItemIndexDelegate(Values_ItemAdded), sender, e);
134      } else {
135        //Datachart datachart = dataChartControl.Chart;
136        //ItemList list = (ItemList)e.Item;
137        //datachart.UpdateEnabled = false;
138        //for (int i = 0; i < list.Count; i++)
139        //  datachart.AddDataPoint(i, e.Index, ((DoubleArrayData)list[i]).Data[0]);
140        //datachart.UpdateEnabled = true;
141        //datachart.EnforceUpdate();
142      }
143    }
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.