Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Problems.DataAnalysis.Views/3.3/ScatterPlotView.cs @ 4865

Last change on this file since 4865 was 3764, checked in by mkommend, 14 years ago

adapted view captions (ticket #893)

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