Free cookie consent management tool by TermsFeed Policy Generator

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

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

added ResultsView for DataAnalysisSolutions and implemented IStringConvertibleMatrix in DataAnalysisSolution (ticket #1020)

File size: 6.2 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.Collections.Generic;
23using System.Collections.Specialized;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32using HeuristicLab.Problems.DataAnalysis;
33using HeuristicLab.MainForm.WindowsForms;
34using System.Windows.Forms.DataVisualization.Charting;
35
36namespace HeuristicLab.Problems.DataAnalysis.Views {
37  [View("Line Chart View")]
38  [Content(typeof(DataAnalysisSolution))]
39  public partial class LineChartView : AsynchronousContentView {
40    private const string TARGETVARIABLE_SERIES_NAME = "TargetVariable";
41    private const string ESTIMATEDVALUES_SERIES_NAME = "EstimatedValues";
42
43    public new DataAnalysisSolution Content {
44      get { return (DataAnalysisSolution)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public LineChartView()
49      : base() {
50      InitializeComponent();
51      //configure axis
52      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
53      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
54      this.chart.ChartAreas[0].CursorX.Interval = 1;
55
56      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
57      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
58      this.chart.ChartAreas[0].CursorY.Interval = 0;
59    }
60
61    private void RedrawChart() {
62      this.chart.Series.Clear();
63      this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
64      this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable.Value;
65      this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
66      this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset[Content.ProblemData.TargetVariable.Value]);
67      this.UpdateStripLines();
68
69      this.chart.Series.Add(ESTIMATEDVALUES_SERIES_NAME);
70      this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].LegendText = Content.ItemName;
71      this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
72      this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.DataBindY(Content.EstimatedValues.ToArray());
73      this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Tag = Content;
74      UpdateCursorInterval();
75    }
76
77    private void UpdateCursorInterval() {
78      var estimatedValues = this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
79      var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
80      double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
81      double targetValuesRange = targetValues.Max() - targetValues.Min();
82      double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
83      double digits = (int)Math.Log10(interestingValuesRange) - 3;
84      double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
85      this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
86    }
87
88    #region events
89    protected override void RegisterContentEvents() {
90      base.RegisterContentEvents();
91      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
92      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
93    }
94    protected override void DeregisterContentEvents() {
95      base.DeregisterContentEvents();
96      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
97      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
98    }
99
100    private void Content_ProblemDataChanged(object sender, EventArgs e) {
101      RedrawChart();
102    }
103
104    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
105      UpdateEstimatedValuesLineChart();
106    }
107
108    protected override void OnContentChanged() {
109      base.OnContentChanged();
110      RedrawChart();
111    }
112
113    private void UpdateEstimatedValuesLineChart() {
114      if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
115      else {
116        if (this.chart.Series.Count > 0) {
117          Series s = this.chart.Series.SingleOrDefault(x => x.Tag == Content);
118          if (s != null) {
119            s.Points.DataBindY(Content.EstimatedValues.ToArray());
120            s.LegendText = Content.ItemName;
121            this.UpdateStripLines();
122            UpdateCursorInterval();
123          }
124        }
125      }
126    }
127    #endregion
128
129    private void UpdateStripLines() {
130      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
131      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
132        Content.ProblemData.TrainingSamplesStart.Value,
133        Content.ProblemData.TrainingSamplesEnd.Value);
134      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
135        Content.ProblemData.TestSamplesStart.Value,
136        Content.ProblemData.TestSamplesEnd.Value);
137    }
138
139    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
140      StripLine stripLine = new StripLine();
141      stripLine.BackColor = c;
142      stripLine.Text = title;
143      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
144      stripLine.StripWidth = end - start;
145      stripLine.IntervalOffset = start;
146      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.