Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/LineChartView.cs @ 4475

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

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

File size: 6.1 KB
RevLine 
[3408]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;
[4068]25using System.Windows.Forms.DataVisualization.Charting;
[3408]26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
[3442]29namespace HeuristicLab.Problems.DataAnalysis.Views {
[3408]30  [View("Line Chart View")]
31  [Content(typeof(DataAnalysisSolution))]
[3442]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; }
[3916]38      set { base.Content = value; }
[3442]39    }
40
[3408]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;
[3707]47      this.chart.ChartAreas[0].CursorX.Interval = 1;
[3408]48
49      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
50      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
[3442]51      this.chart.ChartAreas[0].CursorY.Interval = 0;
[3408]52    }
53
[3462]54    private void RedrawChart() {
[3442]55      this.chart.Series.Clear();
[4011]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();
[3462]62
[4011]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      }
[3408]70    }
71
[3707]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
[3442]83    #region events
84    protected override void RegisterContentEvents() {
85      base.RegisterContentEvents();
[3462]86      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
[3442]87      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
[3408]88    }
[3442]89    protected override void DeregisterContentEvents() {
90      base.DeregisterContentEvents();
[3462]91      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
[3442]92      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
[3408]93    }
94
[3916]95    private void Content_ProblemDataChanged(object sender, EventArgs e) {
[3462]96      RedrawChart();
[3408]97    }
98
[3916]99    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
[3462]100      UpdateEstimatedValuesLineChart();
[3442]101    }
102
103    protected override void OnContentChanged() {
104      base.OnContentChanged();
[3462]105      RedrawChart();
[3442]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();
[3707]117            UpdateCursorInterval();
[3442]118          }
[3408]119        }
120      }
121    }
[3442]122    #endregion
[3408]123
124    private void UpdateStripLines() {
125      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
[3442]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);
[3408]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.