Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Problems.DataAnalysis.Views/3.3/LineChartView.cs @ 10009

Last change on this file since 10009 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.1 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.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
46      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
47      this.chart.ChartAreas[0].CursorX.Interval = 1;
48
49      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
50      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
51      this.chart.ChartAreas[0].CursorY.Interval = 0;
52    }
53
54    private void RedrawChart() {
55      this.chart.Series.Clear();
56      if (Content != null) {
57        this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
58        this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable.Value;
59        this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
60        this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable.Value));
61        this.UpdateStripLines();
62
63        this.chart.Series.Add(ESTIMATEDVALUES_SERIES_NAME);
64        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].LegendText = Content.ItemName;
65        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
66        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.DataBindY(Content.EstimatedValues.ToArray());
67        this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Tag = Content;
68        UpdateCursorInterval();
69      }
70    }
71
72    private void UpdateCursorInterval() {
73      var estimatedValues = this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
74      var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
75      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
76      double targetValuesRange = targetValues.Max() - targetValues.Min();
77      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
78      double digits = (int)Math.Log10(interestingValuesRange) - 3;
79      double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
80      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
81    }
82
83    #region events
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
86      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
87      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
88    }
89    protected override void DeregisterContentEvents() {
90      base.DeregisterContentEvents();
91      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
92      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
93    }
94
95    private void Content_ProblemDataChanged(object sender, EventArgs e) {
96      RedrawChart();
97    }
98
99    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
100      UpdateEstimatedValuesLineChart();
101    }
102
103    protected override void OnContentChanged() {
104      base.OnContentChanged();
105      RedrawChart();
106    }
107
108    private void UpdateEstimatedValuesLineChart() {
109      if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
110      else {
111        if (this.chart.Series.Count > 0) {
112          Series s = this.chart.Series.SingleOrDefault(x => x.Tag == Content);
113          if (s != null) {
114            s.Points.DataBindY(Content.EstimatedValues.ToArray());
115            s.LegendText = Content.ItemName;
116            this.UpdateStripLines();
117            UpdateCursorInterval();
118          }
119        }
120      }
121    }
122    #endregion
123
124    private void UpdateStripLines() {
125      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
126      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
127        Content.ProblemData.TrainingSamplesStart.Value,
128        Content.ProblemData.TrainingSamplesEnd.Value);
129      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
130        Content.ProblemData.TestSamplesStart.Value,
131        Content.ProblemData.TestSamplesEnd.Value);
132    }
133
134    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
135      StripLine stripLine = new StripLine();
136      stripLine.BackColor = c;
137      stripLine.Text = title;
138      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
139      stripLine.StripWidth = end - start;
140      stripLine.IntervalOffset = start;
141      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.