Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionLineChartView.cs @ 6675

Last change on this file since 6675 was 6647, checked in by mkommend, 14 years ago

#1479: Updated grammar editor branch.

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