Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3408 was 3408, checked in by gkronber, 14 years ago

Worked on views for DataAnalysis problems #938 (Data types and operators for regression problems)

File size: 7.3 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;
34
35namespace HeuristicLab.Problems.DataAnalysis {
36  [View("Line Chart View")]
37  [Content(typeof(DataAnalysisSolution))]
38  public partial class LineChartView : ContentView {
39   
40    public LineChartView()
41      : base() {
42      InitializeComponent();
43      this.Caption = "Line Chart View";
44      //configure axis
45      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
46      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
47      this.chart.ChartAreas[0].CursorX.Interval = 0;
48
49      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
50      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
51      this.chart.ChartAreas[0].CursorY.Interval = 0;     
52    }
53
54    public LineChartView(DataAnalysisSolution dataAnalysisSolution)
55      : this() {
56     
57    }
58
59    private void model_Changed(object sender, EventArgs e) {
60      if (InvokeRequired) {
61        Action<object, EventArgs> action = new Action<object, EventArgs>(model_Changed);
62        this.Invoke(action, sender, e);
63      } else {
64        IVisualModel model = (IVisualModel)sender;
65        Series s = this.chart.Series.Single(x => x.Tag == model);
66        s.Points.DataBindY(model.PredictedValues.ToArray());
67        s.LegendText = model.ModelName;
68        this.UpdateStripLines();
69      }
70    }
71
72    protected override void ModelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
73      base.ModelsCollectionChanged(sender, e);
74      if (InvokeRequired) {
75        Action<object, NotifyCollectionChangedEventArgs> action = new Action<object, NotifyCollectionChangedEventArgs>(ModelsCollectionChanged);
76        this.Invoke(action, sender, e);
77      } else {
78        if (e.Action == NotifyCollectionChangedAction.Remove) {
79          foreach (IVisualModel model in e.OldItems) {
80            if (this.chart.Series.Any(x => x.Tag == model))
81              this.RemoveModel(model);
82          }
83          this.UpdateStripLines();
84        } else if (e.Action == NotifyCollectionChangedAction.Reset)
85          this.RemoveAllModels();
86      }
87    }
88
89    private void AddModel(IVisualModel model) {
90      if (this.targetVariableName != model.TargetVariableName) {
91        this.RemoveAllModels();
92        this.chart.Series.Clear();
93        this.chart.Series.Add(TARGETVARIABLE);
94        this.chart.Series[TARGETVARIABLE].LegendText = model.TargetVariableName;
95        this.chart.Series[TARGETVARIABLE].ChartType = SeriesChartType.FastLine;
96        this.chart.Series[TARGETVARIABLE].Points.DataBindY(model.Dataset.GetVariableValues(model.TargetVariableName));
97        this.targetVariableName = model.TargetVariableName;
98        this.Caption = this.targetVariableName + " Model Line Chart";
99      }
100      string seriesName = model.GetHashCode().ToString();
101      this.chart.Series.Add(seriesName);
102      this.chart.Series[seriesName].Tag = model;
103      this.chart.Series[seriesName].LegendText = model.ModelName;
104      this.chart.Series[seriesName].ChartType = SeriesChartType.FastLine;
105      this.chart.Series[seriesName].Points.DataBindY(model.PredictedValues.ToArray());
106      model.Changed += new EventHandler(model_Changed);
107      this.UpdateStripLines();
108    }
109
110    private void RemoveModel(IVisualModel model) {
111      Series s = this.chart.Series.Single(x => x.Tag == model);
112      this.chart.Series.Remove(s);
113      model.Changed -= new EventHandler(model_Changed);
114    }
115
116    private void RemoveAllModels() {
117      var models = (from s in chart.Series
118                    let m = s.Tag as IVisualModel
119                    where m != null
120                    select m).ToArray(); // select and copy currently displayed models
121      foreach (var m in models) {
122        this.RemoveModel(m);
123      }
124      this.UpdateStripLines();
125    }
126
127    private void chart_MouseDown(object sender, MouseEventArgs e) {
128      HitTestResult result = this.chart.HitTest(e.X, e.Y);
129      if (result.ChartElementType == ChartElementType.LegendItem) {
130        if (result.Series.Name != TARGETVARIABLE) {
131          this.RemoveModel(result.Series.Tag as IVisualModel);
132          this.UpdateStripLines();
133        }
134      }
135    }
136
137    private void chart_MouseMove(object sender, MouseEventArgs e) {
138      HitTestResult result = this.chart.HitTest(e.X, e.Y);
139      if (result.ChartElementType == ChartElementType.LegendItem) {
140        if (result.Series.Name != TARGETVARIABLE)
141          this.Cursor = Cursors.Hand;
142      } else
143        this.Cursor = Cursors.Default;
144    }
145
146    private void UpdateStripLines() {
147      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
148      IEnumerable<IVisualModel> visualModels = from Series s in this.chart.Series
149                                               where s.Tag is IVisualModel
150                                               select (IVisualModel)s.Tag;
151      if (visualModels.Count() > 0) {
152        IVisualModel model = visualModels.ElementAt(0);
153        if (visualModels.All(x => x.TrainingSamplesStart == model.TrainingSamplesStart && x.TrainingSamplesEnd == model.TrainingSamplesEnd))
154          this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green), model.TrainingSamplesStart, model.TrainingSamplesEnd);
155        if (visualModels.All(x => x.ValidationSamplesStart == model.ValidationSamplesStart && x.ValidationSamplesEnd == model.ValidationSamplesEnd))
156          this.CreateAndAddStripLine("Validation", Color.FromArgb(20, Color.Yellow), model.ValidationSamplesStart, model.ValidationSamplesEnd);
157        if (visualModels.All(x => x.TestSamplesStart == model.TestSamplesStart && x.TestSamplesEnd == model.TestSamplesEnd))
158          this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red), model.TestSamplesStart, model.TestSamplesEnd);
159      }
160    }
161
162    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
163      StripLine stripLine = new StripLine();
164      stripLine.BackColor = c;
165      stripLine.Text = title;
166      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
167      stripLine.StripWidth = end - start;
168      stripLine.IntervalOffset = start;
169      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.