Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 4011, checked in by mkommend, 14 years ago
  • refactored ViewHost and various views to use fewer nested controls
  • added UnitTests for ContentViews to ensure proper using of the ContentAttribute
  • fixed some views which could not handle null as Content

(ticket #972)

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