Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4746 was 4651, checked in by mkommend, 14 years ago

Integrated EnhancedChart in all GP specific views (ticket #1228).

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
[4651]45      this.chart.CustomizeAllChartAreas();
[3408]46      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
47      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
[3707]48      this.chart.ChartAreas[0].CursorX.Interval = 1;
[3408]49
50      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
51      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
[3442]52      this.chart.ChartAreas[0].CursorY.Interval = 0;
[3408]53    }
54
[3462]55    private void RedrawChart() {
[3442]56      this.chart.Series.Clear();
[4011]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();
[3462]63
[4011]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      }
[3408]71    }
72
[3707]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
[3442]84    #region events
85    protected override void RegisterContentEvents() {
86      base.RegisterContentEvents();
[3462]87      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
[3442]88      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
[3408]89    }
[3442]90    protected override void DeregisterContentEvents() {
91      base.DeregisterContentEvents();
[3462]92      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
[3442]93      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
[3408]94    }
95
[3916]96    private void Content_ProblemDataChanged(object sender, EventArgs e) {
[3462]97      RedrawChart();
[3408]98    }
99
[3916]100    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
[3462]101      UpdateEstimatedValuesLineChart();
[3442]102    }
103
104    protected override void OnContentChanged() {
105      base.OnContentChanged();
[3462]106      RedrawChart();
[3442]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();
[3707]118            UpdateCursorInterval();
[3442]119          }
[3408]120        }
121      }
122    }
[3442]123    #endregion
[3408]124
125    private void UpdateStripLines() {
126      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
[3442]127      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
128        Content.ProblemData.TrainingSamplesStart.Value,
129        Content.ProblemData.TrainingSamplesEnd.Value);
130      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
131        Content.ProblemData.TestSamplesStart.Value,
132        Content.ProblemData.TestSamplesEnd.Value);
[3408]133    }
134
135    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
136      StripLine stripLine = new StripLine();
137      stripLine.BackColor = c;
138      stripLine.Text = title;
139      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
140      stripLine.StripWidth = end - start;
141      stripLine.IntervalOffset = start;
142      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.