Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/ScatterPlotView.cs @ 3480

Last change on this file since 3480 was 3480, checked in by gkronber, 14 years ago

minor bug fixes #937 (Data types and operators for symbolic expression tree encoding)

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
21using System;
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Drawing;
25using System.Data;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29using System.Windows.Forms.DataVisualization.Charting;
30using HeuristicLab.Common;
31using System.Collections.Specialized;
32using HeuristicLab.MainForm;
33using HeuristicLab.Problems.DataAnalysis;
34using HeuristicLab.MainForm.WindowsForms;
35
36namespace HeuristicLab.Problems.DataAnalysis.Views {
37  [View("Scatter Plot View")]
38  [Content(typeof(DataAnalysisSolution), true)]
39  public partial class ScatterPlotView : AsynchronousContentView {
40    private const string DEFAULT_CAPTION = "Scatter Plot";
41    private const string ALL_SERIES = "All Samples";
42    private const string TRAINING_SERIES = "Training Samples";
43    private const string TEST_SERIES = "Test Samples";
44
45    public new DataAnalysisSolution Content {
46      get { return (DataAnalysisSolution)base.Content; }
47      set { base.Content = value; }
48    }
49
50    public ScatterPlotView()
51      : base() {
52      InitializeComponent();
53      this.Caption = DEFAULT_CAPTION;
54
55      this.chart.Series.Add(ALL_SERIES);
56      this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
57      this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
58
59      this.chart.Series.Add(TRAINING_SERIES);
60      this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
61      this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
62
63      this.chart.Series.Add(TEST_SERIES);
64      this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
65      this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
66
67      this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
68      this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
69
70      //configure axis                 
71      this.chart.ChartAreas[0].AxisX.Title = "Estimated Values";
72      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
73      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
74      this.chart.ChartAreas[0].CursorX.Interval = 0;
75      this.chart.ChartAreas[0].CursorY.Interval = 0;
76
77      this.chart.ChartAreas[0].AxisY.Title = "Target Values";
78      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
79      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
80      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
81    }
82
83    public ScatterPlotView(DataAnalysisSolution dataAnalysisSolution)
84      : this() {
85      Content = dataAnalysisSolution;
86      UpdateChart();
87    }
88
89    protected override void RegisterContentEvents() {
90      base.RegisterContentEvents();
91      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
92      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
93    }
94    protected override void DeregisterContentEvents() {
95      base.DeregisterContentEvents();
96      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
97      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
98    }
99
100
101    void Content_ProblemDataChanged(object sender, EventArgs e) {
102      UpdateChart();
103    }
104
105    void Content_EstimatedValuesChanged(object sender, EventArgs e) {
106      UpdateSeries();
107    }
108
109    protected override void OnContentChanged() {
110      base.OnContentChanged();
111      UpdateChart();
112    }
113
114    private void UpdateChart() {
115      if (InvokeRequired) Invoke((Action)UpdateChart);
116      else {
117        if (Content != null) {
118          this.Caption = Content.ItemName + " " + DEFAULT_CAPTION;
119          this.UpdateSeries();
120          if (!this.chart.Series.Any(s => s.Points.Count > 0))
121            this.ToggleSeriesData(this.chart.Series[TRAINING_SERIES]);
122        } else {
123          this.Caption = DEFAULT_CAPTION;
124          this.ClearChart();
125        }
126      }
127    }
128
129    private void UpdateSeries() {
130      if (InvokeRequired) Invoke((Action)UpdateSeries);
131      else {
132        string targetVariableName = Content.ProblemData.TargetVariable.Value;
133        Dataset dataset = Content.ProblemData.Dataset;
134        int trainingStart = Content.ProblemData.TrainingSamplesStart.Value;
135        int trainingEnd = Content.ProblemData.TrainingSamplesEnd.Value;
136        int testStart = Content.ProblemData.TestSamplesStart.Value;
137        int testEnd = Content.ProblemData.TestSamplesEnd.Value;
138        if (this.chart.Series[ALL_SERIES].Points.Count > 0)
139          this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
140            dataset[targetVariableName], "");
141        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
142          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
143            dataset.GetVariableValues(targetVariableName, trainingStart, trainingEnd), "");
144        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
145          this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
146            dataset.GetVariableValues(targetVariableName, testStart, testEnd), "");
147
148        double max = Math.Max(Content.EstimatedValues.Max(), dataset.GetMax(targetVariableName));
149        double min = Math.Min(Content.EstimatedValues.Min(), dataset.GetMin(targetVariableName));
150
151        max = Math.Ceiling(max) * 1.2;
152        min = Math.Floor(min) * 0.8;
153
154        this.chart.ChartAreas[0].AxisX.Maximum = max;
155        this.chart.ChartAreas[0].AxisX.Minimum = min;
156        this.chart.ChartAreas[0].AxisY.Maximum = max;
157        this.chart.ChartAreas[0].AxisY.Minimum = min;
158      }
159    }
160
161    private void ClearChart() {
162      this.chart.Series[ALL_SERIES].Points.Clear();
163      this.chart.Series[TRAINING_SERIES].Points.Clear();
164      this.chart.Series[TEST_SERIES].Points.Clear();
165    }
166
167    private void ToggleSeriesData(Series series) {
168      if (series.Points.Count > 0) {  //checks if series is shown
169        if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
170          series.Points.Clear();
171        }
172      } else if (Content != null) {
173        string targetVariableName = Content.ProblemData.TargetVariable.Value;
174        Dataset dataset = Content.ProblemData.Dataset;
175        int trainingStart = Content.ProblemData.TrainingSamplesStart.Value;
176        int trainingEnd = Content.ProblemData.TrainingSamplesEnd.Value;
177        int testStart = Content.ProblemData.TestSamplesStart.Value;
178        int testEnd = Content.ProblemData.TestSamplesEnd.Value;
179
180        IEnumerable<double> predictedValues = null;
181        IEnumerable<double> targetValues = null;
182        switch (series.Name) {
183          case ALL_SERIES:
184            predictedValues = Content.EstimatedValues;
185            targetValues = dataset[targetVariableName];
186            break;
187          case TRAINING_SERIES:
188            predictedValues = Content.EstimatedTrainingValues;
189            targetValues = dataset.GetVariableValues(targetVariableName, trainingStart, trainingEnd);
190            break;
191          case TEST_SERIES:
192            predictedValues = Content.EstimatedTestValues;
193            targetValues = dataset.GetVariableValues(targetVariableName, testStart, testEnd);
194            break;
195        }
196        series.Points.DataBindXY(predictedValues, "", targetValues, "");
197        this.chart.Legends[series.Legend].ForeColor = Color.Black;
198      }
199    }
200
201    private void chart_MouseDown(object sender, MouseEventArgs e) {
202      HitTestResult result = chart.HitTest(e.X, e.Y);
203      if (result.ChartElementType == ChartElementType.LegendItem) {
204        this.ToggleSeriesData(result.Series);
205      }
206    }
207
208    private void chart_MouseMove(object sender, MouseEventArgs e) {
209      HitTestResult result = chart.HitTest(e.X, e.Y);
210      if (result.ChartElementType == ChartElementType.LegendItem)
211        this.Cursor = Cursors.Hand;
212      else
213        this.Cursor = Cursors.Default;
214    }
215
216    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
217      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
218      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
219    }
220
221    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
222      e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
223      e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
224      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
225    }
226  }
227}
Note: See TracBrowser for help on using the repository browser.