Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 9.6 KB
RevLine 
[3408]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3408]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;
[4068]25using System.Windows.Forms.DataVisualization.Charting;
[5829]26using HeuristicLab.Core.Views;
[3408]27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
[3442]30namespace HeuristicLab.Problems.DataAnalysis.Views {
[5975]31  [View("Line Chart")]
[5663]32  [Content(typeof(IRegressionSolution))]
[5829]33  public partial class RegressionSolutionLineChartView : ItemView, IRegressionSolutionEvaluationView {
[6238]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)";
[3442]37
[5663]38    public new IRegressionSolution Content {
39      get { return (IRegressionSolution)base.Content; }
[3916]40      set { base.Content = value; }
[3442]41    }
42
[5663]43    public RegressionSolutionLineChartView()
[3408]44      : base() {
45      InitializeComponent();
46      //configure axis
[4651]47      this.chart.CustomizeAllChartAreas();
[3408]48      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
49      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
[6238]50      this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
[3707]51      this.chart.ChartAreas[0].CursorX.Interval = 1;
[3408]52
53      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
54      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
[3442]55      this.chart.ChartAreas[0].CursorY.Interval = 0;
[3408]56    }
57
[3462]58    private void RedrawChart() {
[3442]59      this.chart.Series.Clear();
[4011]60      if (Content != null) {
[6238]61        this.chart.ChartAreas[0].AxisX.Minimum = 0;
62        this.chart.ChartAreas[0].AxisX.Maximum = Content.ProblemData.Dataset.Rows - 1;
63
[4011]64        this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
[5663]65        this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable;
[4011]66        this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
[6238]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));
[3462]69
[6238]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());
[6302]75        this.chart.DataManipulator.InsertEmptyPoints(Content.ProblemData.Dataset.Rows, IntervalType.Number, ESTIMATEDVALUES_TRAINING_SERIES_NAME);
[6238]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;
[4011]84        UpdateCursorInterval();
[6238]85        this.UpdateStripLines();
[4011]86      }
[3408]87    }
88
[3707]89    private void UpdateCursorInterval() {
[6238]90      var estimatedValues = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
[3707]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
[3442]100    #region events
101    protected override void RegisterContentEvents() {
102      base.RegisterContentEvents();
[5663]103      Content.ModelChanged += new EventHandler(Content_ModelChanged);
[3442]104      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
[3408]105    }
[3442]106    protected override void DeregisterContentEvents() {
107      base.DeregisterContentEvents();
[5663]108      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
[3442]109      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
[3408]110    }
111
[3916]112    private void Content_ProblemDataChanged(object sender, EventArgs e) {
[3462]113      RedrawChart();
[3408]114    }
115
[5663]116    private void Content_ModelChanged(object sender, EventArgs e) {
[3462]117      UpdateEstimatedValuesLineChart();
[3442]118    }
119
120    protected override void OnContentChanged() {
121      base.OnContentChanged();
[3462]122      RedrawChart();
[3442]123    }
124
125    private void UpdateEstimatedValuesLineChart() {
126      if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
127      else {
128        if (this.chart.Series.Count > 0) {
[6238]129          Series s = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME];
[3442]130          if (s != null) {
[6238]131            s.Points.DataBindXY(Content.ProblemData.TrainingIndizes.ToArray(), Content.EstimatedTrainingValues.ToArray());
132            s.LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME;
[3442]133          }
[6238]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();
[3408]141        }
142      }
143    }
[5006]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    }
[3442]154    #endregion
[3408]155
156    private void UpdateStripLines() {
157      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
[6238]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      }
[3408]189    }
190
[6238]191    private void CreateAndAddStripLine(string title, int start, int end, Color color, Color secondColor, ChartHatchStyle hatchStyle = ChartHatchStyle.None) {
[3408]192      StripLine stripLine = new StripLine();
[6238]193      stripLine.BackColor = color;
194      stripLine.BackSecondaryColor = secondColor;
195      stripLine.BackHatchStyle = hatchStyle;
[3408]196      stripLine.Text = title;
197      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
[6252]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
[3408]202      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.