Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionScatterPlotView.cs @ 14112

Last change on this file since 14112 was 14112, checked in by mkommend, 8 years ago

#2620: Merged r13958 into stable.

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