Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6710 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 6.7 KB
RevLine 
[3408]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3408]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    }
[5006]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    }
[3442]133    #endregion
[3408]134
135    private void UpdateStripLines() {
136      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
[3442]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);
[3408]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.