Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6058 was 5975, checked in by mkommend, 14 years ago

#1313: Updated view names to use spaces instead of !camelCasing.

File size: 6.7 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 = "TargetVariable";
35    private const string ESTIMATEDVALUES_SERIES_NAME = "EstimatedValues";
36
37    public new IRegressionSolution Content {
38      get { return (IRegressionSolution)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public RegressionSolutionLineChartView()
43      : base() {
44      InitializeComponent();
45      //configure axis
46      this.chart.CustomizeAllChartAreas();
47      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
48      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
49      this.chart.ChartAreas[0].CursorX.Interval = 1;
50
51      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
52      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
53      this.chart.ChartAreas[0].CursorY.Interval = 0;
54    }
55
56    private void RedrawChart() {
57      this.chart.Series.Clear();
58      if (Content != null) {
59        this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
60        this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable;
61        this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
62        this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable));
63        this.UpdateStripLines();
64
65        this.chart.Series.Add(ESTIMATEDVALUES_SERIES_NAME);
66        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].LegendText = Content.ItemName;
67        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
68        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.DataBindY(Content.EstimatedValues.ToArray());
69        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Tag = Content;
70        UpdateCursorInterval();
71      }
72    }
73
74    private void UpdateCursorInterval() {
75      var estimatedValues = this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
76      var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
77      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
78      double targetValuesRange = targetValues.Max() - targetValues.Min();
79      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
80      double digits = (int)Math.Log10(interestingValuesRange) - 3;
81      double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
82      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
83    }
84
85    #region events
86    protected override void RegisterContentEvents() {
87      base.RegisterContentEvents();
88      Content.ModelChanged += new EventHandler(Content_ModelChanged);
89      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
90    }
91    protected override void DeregisterContentEvents() {
92      base.DeregisterContentEvents();
93      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
94      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
95    }
96
97    private void Content_ProblemDataChanged(object sender, EventArgs e) {
98      RedrawChart();
99    }
100
101    private void Content_ModelChanged(object sender, EventArgs e) {
102      UpdateEstimatedValuesLineChart();
103    }
104
105    protected override void OnContentChanged() {
106      base.OnContentChanged();
107      RedrawChart();
108    }
109
110    private void UpdateEstimatedValuesLineChart() {
111      if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
112      else {
113        if (this.chart.Series.Count > 0) {
114          Series s = this.chart.Series.SingleOrDefault(x => x.Tag == Content);
115          if (s != null) {
116            s.Points.DataBindY(Content.EstimatedValues.ToArray());
117            s.LegendText = Content.ItemName;
118            this.UpdateStripLines();
119            UpdateCursorInterval();
120          }
121        }
122      }
123    }
124
125    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
126      HitTestResult result = chart.HitTest(e.X, e.Y);
127      if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
128                                       result.ChartElementType == ChartElementType.Gridlines) ||
129                                       result.ChartElementType == ChartElementType.StripLines) {
130        foreach (var axis in result.ChartArea.Axes)
131          axis.ScaleView.ZoomReset(int.MaxValue);
132      }
133    }
134    #endregion
135
136    private void UpdateStripLines() {
137      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
138      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
139        Content.ProblemData.TrainingPartition.Start,
140        Content.ProblemData.TrainingPartition.End);
141      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
142        Content.ProblemData.TestPartition.Start,
143        Content.ProblemData.TestPartition.End);
144    }
145
146    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
147      StripLine stripLine = new StripLine();
148      stripLine.BackColor = c;
149      stripLine.Text = title;
150      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
151      stripLine.StripWidth = end - start;
152      stripLine.IntervalOffset = start;
153      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.