Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionScatterPlotView.cs @ 7555

Last change on this file since 7555 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
23using System.Linq;
24using System.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Scatter Plot")]
31  [Content(typeof(IRegressionSolution))]
32  public partial class RegressionSolutionScatterPlotView : DataAnalysisSolutionEvaluationView {
33    private const string ALL_SERIES = "All samples";
34    private const string TRAINING_SERIES = "Training samples";
35    private const string TEST_SERIES = "Test samples";
36
37    public new IRegressionSolution Content {
38      get { return (IRegressionSolution)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public RegressionSolutionScatterPlotView()
43      : base() {
44      InitializeComponent();
45
46      this.chart.Series.Add(ALL_SERIES);
47      this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
48      this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
49
50      this.chart.Series.Add(TRAINING_SERIES);
51      this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
52      this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
53      this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
54
55      this.chart.Series.Add(TEST_SERIES);
56      this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
57      this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
58
59      this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
60      this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
61
62      //configure axis
63      this.chart.CustomizeAllChartAreas();
64      this.chart.ChartAreas[0].AxisX.Title = "Estimated Values";
65      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
66      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
67      this.chart.ChartAreas[0].CursorX.Interval = 1;
68      this.chart.ChartAreas[0].CursorY.Interval = 1;
69
70      this.chart.ChartAreas[0].AxisY.Title = "Target Values";
71      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
72      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
73      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
74    }
75
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.ModelChanged += new EventHandler(Content_ModelChanged);
79      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
80    }
81    protected override void DeregisterContentEvents() {
82      base.DeregisterContentEvents();
83      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
84      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
85    }
86
87
88    private void Content_ProblemDataChanged(object sender, EventArgs e) {
89      UpdateChart();
90    }
91    private void Content_ModelChanged(object sender, EventArgs e) {
92      UpdateSeries();
93    }
94
95    protected override void OnContentChanged() {
96      base.OnContentChanged();
97      UpdateChart();
98    }
99
100    private void UpdateChart() {
101      if (InvokeRequired) Invoke((Action)UpdateChart);
102      else {
103        if (Content != null) {
104          this.UpdateSeries();
105          if (!this.chart.Series.Any(s => s.Points.Count > 0))
106            this.ClearChart();
107        }
108      }
109    }
110
111    private void UpdateCursorInterval() {
112      var estimatedValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
113      var targetValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
114      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
115      double targetValuesRange = targetValues.Max() - targetValues.Min();
116      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
117      double digits = (int)Math.Log10(interestingValuesRange) - 3;
118      double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
119      this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
120      this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
121    }
122
123
124    private void UpdateSeries() {
125      if (InvokeRequired) Invoke((Action)UpdateSeries);
126      else {
127        string targetVariableName = Content.ProblemData.TargetVariable;
128        Dataset dataset = Content.ProblemData.Dataset;
129        if (this.chart.Series[ALL_SERIES].Points.Count > 0)
130          this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
131            dataset.GetDoubleValues(targetVariableName).ToArray(), "");
132        if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
133          this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
134            dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray(), "");
135        if (this.chart.Series[TEST_SERIES].Points.Count > 0)
136          this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
137           dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray(), "");
138
139        double max = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Max();
140        double min = Content.EstimatedTrainingValues.Concat(Content.EstimatedTestValues.Concat(Content.EstimatedValues.Concat(dataset.GetDoubleValues(targetVariableName)))).Min();
141
142        max = max + 0.2 * Math.Abs(max);
143        min = min - 0.2 * Math.Abs(min);
144
145        double interestingValuesRange = max - min;
146        int digits = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRange));
147
148        max = Math.Round(max, digits);
149        min = Math.Round(min, digits);
150
151        this.chart.ChartAreas[0].AxisX.Maximum = max;
152        this.chart.ChartAreas[0].AxisX.Minimum = min;
153        this.chart.ChartAreas[0].AxisY.Maximum = max;
154        this.chart.ChartAreas[0].AxisY.Minimum = min;
155        UpdateCursorInterval();
156      }
157    }
158
159    private void ClearChart() {
160      this.chart.Series[ALL_SERIES].Points.Clear();
161      this.chart.Series[TRAINING_SERIES].Points.Clear();
162      this.chart.Series[TEST_SERIES].Points.Clear();
163    }
164
165    private void ToggleSeriesData(Series series) {
166      if (series.Points.Count > 0) {  //checks if series is shown
167        if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
168          series.Points.Clear();
169        }
170      } else if (Content != null) {
171        string targetVariableName = Content.ProblemData.TargetVariable;
172
173        double[] predictedValues = null;
174        double[] targetValues = null;
175        switch (series.Name) {
176          case ALL_SERIES:
177            predictedValues = Content.EstimatedValues.ToArray();
178            targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName).ToArray();
179            break;
180          case TRAINING_SERIES:
181            predictedValues = Content.EstimatedTrainingValues.ToArray();
182            targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray();
183            break;
184          case TEST_SERIES:
185            predictedValues = Content.EstimatedTestValues.ToArray();
186            targetValues = Content.ProblemData.Dataset.GetDoubleValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray();
187            break;
188        }
189        if (predictedValues.Length == targetValues.Length)
190          series.Points.DataBindXY(predictedValues, "", targetValues, "");
191        this.chart.Legends[series.Legend].ForeColor = Color.Black;
192        UpdateCursorInterval();
193      }
194    }
195
196    private void chart_MouseDown(object sender, MouseEventArgs e) {
197      HitTestResult result = chart.HitTest(e.X, e.Y);
198      if (result.ChartElementType == ChartElementType.LegendItem) {
199        this.ToggleSeriesData(result.Series);
200      }
201    }
202
203    private void chart_MouseMove(object sender, MouseEventArgs e) {
204      HitTestResult result = chart.HitTest(e.X, e.Y);
205      if (result.ChartElementType == ChartElementType.LegendItem)
206        this.Cursor = Cursors.Hand;
207      else
208        this.Cursor = Cursors.Default;
209    }
210
211    private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
212      this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
213      this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
214    }
215
216    private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
217      e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
218      e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
219      e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
220    }
221
222    private void chart_PostPaint(object sender, ChartPaintEventArgs e) {
223      var chartArea = e.ChartElement as ChartArea;
224      if (chartArea != null) {
225        ChartGraphics chartGraphics = e.ChartGraphics;
226        using (Pen p = new Pen(Color.DarkGray)) {
227          double xmin = chartArea.AxisX.ScaleView.ViewMinimum;
228          double xmax = chartArea.AxisX.ScaleView.ViewMaximum;
229          double ymin = chartArea.AxisY.ScaleView.ViewMinimum;
230          double ymax = chartArea.AxisY.ScaleView.ViewMaximum;
231
232          if (xmin > ymax || ymin > xmax) return;
233
234          PointF start = PointF.Empty;
235          start.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Max(xmin, ymin));
236          start.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Max(xmin, ymin));
237          PointF end = PointF.Empty;
238          end.X = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisX.AxisName, Math.Min(xmax, ymax));
239          end.Y = (float)chartGraphics.GetPositionFromAxis(chartArea.Name, chartArea.AxisY.AxisName, Math.Min(xmax, ymax));
240
241          chartGraphics.Graphics.DrawLine(p, chartGraphics.GetAbsolutePoint(start), chartGraphics.GetAbsolutePoint(end));
242        }
243      }
244    }
245  }
246}
Note: See TracBrowser for help on using the repository browser.