Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowDataView.cs @ 9870

Last change on this file since 9870 was 9870, checked in by bburlacu, 11 years ago

#1837: Merged trunk changes and fixed sliding window visualization.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
21
22using System;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
30  [View("Sliding Window View")]
31  [Content(typeof(SlidingWindowData), IsDefaultView = true)]
32  public partial class SlidingWindowDataView : ItemView {
33    private const string TARGETVARIABLE_SERIES_NAME = "Target Variable";
34    private const string ESTIMATEDVAlUES_SERIES_NAME = "Estimated Values";
35
36    public new SlidingWindowData Content {
37      get { return (SlidingWindowData)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public SlidingWindowDataView() {
42      InitializeComponent();
43      chart.CustomizeAllChartAreas();
44      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
45      chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
46      chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
47      chart.ChartAreas[0].CursorX.Interval = 1;
48
49      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
50      chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
51      chart.ChartAreas[0].CursorY.Interval = 0;
52
53      StripLine stripLine = new StripLine();
54      stripLine.BackColor = Color.Gold;
55      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
56      stripLine.Text = "Sliding Window";
57      chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
58    }
59
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.SlidingWindowPosition.ValueChanged += new EventHandler(SlidingWindowPosition_ValueChanged);
63      Content.EstimatedValuesChanged += new EventHandler(EstimatedValuesChanged);
64    }
65    protected override void DeregisterContentEvents() {
66      Content.SlidingWindowPosition.ValueChanged -= new EventHandler(SlidingWindowPosition_ValueChanged);
67      Content.EstimatedValuesChanged -= new EventHandler(EstimatedValuesChanged);
68      base.DeregisterContentEvents();
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      RedrawChart();
74    }
75
76    private void SlidingWindowPosition_ValueChanged(object sender, EventArgs e) {
77      if (InvokeRequired) Invoke((Action<object, EventArgs>)SlidingWindowPosition_ValueChanged, sender, e);
78      else UpdateStripLine();
79    }
80    private void EstimatedValuesChanged(object sender, EventArgs e) {
81      if (InvokeRequired) Invoke((Action<object, EventArgs>)EstimatedValuesChanged, sender, e);
82      else RedrawChart();
83    }
84
85    private void RedrawChart() {
86      chart.Series.Clear();
87      if (Content == null) return;
88
89      chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
90      chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
91      var xValues = Enumerable.Range(Content.TrainingPartitionStart + 1, Content.TrainingPartitionEnd - Content.TrainingPartitionStart).ToArray();
92      chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(xValues, Content.TargetValues.ToArray());
93
94      if (Content.EstimatedValues.Any()) {
95        chart.Series.Add(ESTIMATEDVAlUES_SERIES_NAME);
96        chart.Series[ESTIMATEDVAlUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
97        var yValues = Content.EstimatedValues.ToArray();
98        chart.Series[ESTIMATEDVAlUES_SERIES_NAME].Points.DataBindXY(xValues, yValues);
99      }
100
101      UpdateStripLine();
102      UpdateCursorInterval();
103    }
104
105    private void UpdateStripLine() {
106      chart.ChartAreas[0].AxisX.StripLines[0].StripWidth = Content.SlidingWindowPosition.End - Content.SlidingWindowPosition.Start;
107      chart.ChartAreas[0].AxisX.StripLines[0].IntervalOffset = Content.SlidingWindowPosition.Start - 0.5;
108    }
109
110    private void UpdateCursorInterval() {
111      var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
112      double targetValuesRange = targetValues.Max() - targetValues.Min();
113      double interestingValuesRange = Math.Max(targetValuesRange, 1.0);
114      double digits = (int)Math.Log10(interestingValuesRange) - 3;
115      double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
116      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.