Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/LineChartView.cs @ 7624

Last change on this file since 7624 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

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