Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionLineChartView.cs @ 6360

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

#1450: fixed cloning bug and a problem in the regression line chart view.

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Line Chart")]
32  [Content(typeof(IRegressionSolution))]
33  public partial class RegressionSolutionLineChartView : ItemView, IRegressionSolutionEvaluationView {
34    private const string TARGETVARIABLE_SERIES_NAME = "Target Variable";
35    private const string ESTIMATEDVALUES_TRAINING_SERIES_NAME = "Estimated Values (training)";
36    private const string ESTIMATEDVALUES_TEST_SERIES_NAME = "Estimated Values (test)";
37
38    public new IRegressionSolution Content {
39      get { return (IRegressionSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public RegressionSolutionLineChartView()
44      : base() {
45      InitializeComponent();
46      //configure axis
47      this.chart.CustomizeAllChartAreas();
48      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
49      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
50      this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
51      this.chart.ChartAreas[0].CursorX.Interval = 1;
52
53      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
54      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
55      this.chart.ChartAreas[0].CursorY.Interval = 0;
56    }
57
58    private void RedrawChart() {
59      this.chart.Series.Clear();
60      if (Content != null) {
61        this.chart.ChartAreas[0].AxisX.Minimum = 0;
62        this.chart.ChartAreas[0].AxisX.Maximum = Content.ProblemData.Dataset.Rows - 1;
63
64        this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
65        this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable;
66        this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
67        this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray(),
68          Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable));
69
70        this.chart.Series.Add(ESTIMATEDVALUES_TRAINING_SERIES_NAME);
71        this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME;
72        this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].ChartType = SeriesChartType.FastLine;
73        this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TrainingIndizes.ToArray(),
74          Content.EstimatedTrainingValues.ToArray());
75        this.chart.DataManipulator.InsertEmptyPoints(Content.ProblemData.Dataset.Rows, IntervalType.Number, ESTIMATEDVALUES_TRAINING_SERIES_NAME);
76        this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Tag = Content;
77
78        this.chart.Series.Add(ESTIMATEDVALUES_TEST_SERIES_NAME);
79        this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].LegendText = ESTIMATEDVALUES_TEST_SERIES_NAME;
80        this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].ChartType = SeriesChartType.FastLine;
81        this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TestIndizes.ToArray(),
82          Content.EstimatedTestValues.ToArray());
83        this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Tag = Content;
84        UpdateCursorInterval();
85        this.UpdateStripLines();
86      }
87    }
88
89    private void UpdateCursorInterval() {
90      var estimatedValues = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
91      var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
92      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
93      double targetValuesRange = targetValues.Max() - targetValues.Min();
94      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
95      double digits = (int)Math.Log10(interestingValuesRange) - 3;
96      double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
97      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
98    }
99
100    #region events
101    protected override void RegisterContentEvents() {
102      base.RegisterContentEvents();
103      Content.ModelChanged += new EventHandler(Content_ModelChanged);
104      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
105    }
106    protected override void DeregisterContentEvents() {
107      base.DeregisterContentEvents();
108      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
109      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
110    }
111
112    private void Content_ProblemDataChanged(object sender, EventArgs e) {
113      RedrawChart();
114    }
115
116    private void Content_ModelChanged(object sender, EventArgs e) {
117      UpdateEstimatedValuesLineChart();
118    }
119
120    protected override void OnContentChanged() {
121      base.OnContentChanged();
122      RedrawChart();
123    }
124
125    private void UpdateEstimatedValuesLineChart() {
126      if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
127      else {
128        if (this.chart.Series.Count > 0) {
129          Series s = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME];
130          if (s != null) {
131            s.Points.DataBindXY(Content.ProblemData.TrainingIndizes.ToArray(), Content.EstimatedTrainingValues.ToArray());
132            s.LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME;
133          }
134          s = this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME];
135          if (s != null) {
136            s.Points.DataBindXY(Content.ProblemData.TestIndizes.ToArray(), Content.EstimatedTestValues.ToArray());
137            s.LegendText = ESTIMATEDVALUES_TEST_SERIES_NAME;
138          }
139          this.UpdateStripLines();
140          UpdateCursorInterval();
141        }
142      }
143    }
144
145    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
146      HitTestResult result = chart.HitTest(e.X, e.Y);
147      if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
148                                       result.ChartElementType == ChartElementType.Gridlines) ||
149                                       result.ChartElementType == ChartElementType.StripLines) {
150        foreach (var axis in result.ChartArea.Axes)
151          axis.ScaleView.ZoomReset(int.MaxValue);
152      }
153    }
154    #endregion
155
156    private void UpdateStripLines() {
157      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
158
159      int[] attr = new int[Content.ProblemData.Dataset.Rows + 1]; // add a virtual last row that is again empty to simplify loop further down
160      foreach (var row in Content.ProblemData.TrainingIndizes) {
161        attr[row] += 1;
162      }
163      foreach (var row in Content.ProblemData.TestIndizes) {
164        attr[row] += 2;
165      }
166      int start = 0;
167      int curAttr = attr[start];
168      for (int row = 0; row < attr.Length; row++) {
169        if (attr[row] != curAttr) {
170          switch (curAttr) {
171            case 0: break;
172            case 1:
173              this.CreateAndAddStripLine("Training", start, row, Color.FromArgb(40, Color.Green), Color.Transparent);
174              break;
175            case 2:
176              this.CreateAndAddStripLine("Test", start, row, Color.FromArgb(40, Color.Red), Color.Transparent);
177              break;
178            case 3:
179              this.CreateAndAddStripLine("Training and Test", start, row, Color.FromArgb(40, Color.Green), Color.FromArgb(40, Color.Red), ChartHatchStyle.WideUpwardDiagonal);
180              break;
181            default:
182              // should not happen
183              break;
184          }
185          curAttr = attr[row];
186          start = row;
187        }
188      }
189    }
190
191    private void CreateAndAddStripLine(string title, int start, int end, Color color, Color secondColor, ChartHatchStyle hatchStyle = ChartHatchStyle.None) {
192      StripLine stripLine = new StripLine();
193      stripLine.BackColor = color;
194      stripLine.BackSecondaryColor = secondColor;
195      stripLine.BackHatchStyle = hatchStyle;
196      stripLine.Text = title;
197      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
198      // strip range is [start .. end] inclusive, but we evaluate [start..end[ (end is exclusive)
199      // the strip should be by one longer (starting at start - 0.5 and ending at end + 0.5)
200      stripLine.StripWidth = end - start;
201      stripLine.IntervalOffset = start - 0.5; // start slightly to the left of the first point to clearly indicate the first point in the partition
202      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.