Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.Views/FDC/ScatterPlotView.cs @ 14674

Last change on this file since 14674 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 4.8 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
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Analysis.FitnessLandscape {
32
33  [View("ScatterPlot View")]
34  [Content(typeof(ScatterPlot), true)]
35  public partial class ScatterPlotView : NamedItemView {
36
37
38    public new ScatterPlot Content {
39      get { return (ScatterPlot)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ScatterPlotView() {
44      InitializeComponent();
45      chart.CustomizeAllChartAreas();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content != null)
51        UpdateBitmap();
52    }
53
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.PixelsChanged += new EventHandler<PointsEventArgs>(Content_PixelChanged);
57      Content.AxisNameChanged += new EventHandler(Content_AxisNameChanged);
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.PixelsChanged -= new EventHandler<PointsEventArgs>(Content_PixelChanged);
62      Content.AxisNameChanged += new EventHandler(Content_AxisNameChanged);
63      base.DeregisterContentEvents();
64    }
65
66    void Content_AxisNameChanged(object sender, EventArgs e) {
67      if (InvokeRequired) {
68        Invoke(new EventHandler(Content_AxisNameChanged), sender, e);
69      } else {
70        chart.ChartAreas[0].Axes[0].Title = Content.XAxisName;
71        chart.ChartAreas[0].Axes[1].Title = Content.YAxisName;
72      }
73    }
74
75    void Content_PixelChanged(object sender, PointsEventArgs e) {
76      if (InvokeRequired)
77        Invoke(new EventHandler<PointsEventArgs>(Content_PixelChanged), sender, e);
78      else
79        AddPixels(e.NewPoints);
80    }
81
82    void Content_AreaChanged(object sender, EventArgs e) {
83      if (InvokeRequired)
84        Invoke(new EventHandler(Content_AreaChanged), sender, e);
85      else
86        UpdateBitmap();
87    }
88
89    private void AddPixels(IEnumerable<PointF> points) {
90      foreach (var p in points)
91        chart.Series[0].Points.AddXY(p.X, p.Y);
92    }
93
94    private void ConfigureChart() {
95      chart.ChartAreas[0].Axes[0].Title = Content.XAxisName;
96      chart.ChartAreas[0].Axes[1].Title = Content.YAxisName;
97      chart.Titles.Clear();
98      Title t = chart.Titles.Add(Content.Name);
99      t.Font = new System.Drawing.Font(t.Font.FontFamily, 10, FontStyle.Bold);
100    }
101
102    private void UpdateBitmap() {
103      chart.Series.Clear();
104      var series1 = new Series("Series1") {
105        ChartArea = "ChartArea1",
106        ChartType = SeriesChartType.FastPoint,
107        EmptyPointStyle = {BackGradientStyle = GradientStyle.LeftRight},
108        MarkerSize = 3,
109        MarkerStyle = MarkerStyle.Circle,
110      };
111      chart.Series.Add(series1);
112      ConfigureChart();
113      var points = Content.Points.ToArray();
114      correlationLabel.Text = alglib.basestat.pearsoncorrelation(points.Select(x => (double)x.X).ToArray(), points.Select(x => (double)x.Y).ToArray(), points.Length).ToString();
115      foreach (var p in points) {
116        chart.Series[0].Points.AddXY(p.X, p.Y);
117      }
118    }
119
120    private void ScatterPlotView_DragEnter(object sender, DragEventArgs e) {
121      if (e.Data.GetDataPresent("HeuristicLab"))
122        e.Effect = DragDropEffects.Copy;
123      else e.Effect = DragDropEffects.None;
124    }
125
126    private void ScatterPlotView_DragDrop(object sender, DragEventArgs e) {
127      ConfigureChart();
128      ScatterPlot sp = (ScatterPlot)e.Data.GetData("HeuristicLab");
129      AddPixels(sp.Points);
130    }
131
132    private void calcPearsonButton_Click(object sender, EventArgs e) {
133      correlationLabel.Text = alglib.basestat.pearsoncorrelation(chart.Series[0].Points.Select(x => x.XValue).ToArray(), chart.Series[0].Points.Select(x => x.YValues[0]).ToArray(), chart.Series[0].Points.Count).ToString();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.