Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4040 was 3933, checked in by mkommend, 14 years ago

removed cloning of dataset and made it readonly (ticket #938)

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