Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/BioBoostParetoSetView.cs @ 13073

Last change on this file since 13073 was 13072, checked in by gkronber, 9 years ago

#2499: added code from HeuristicLab.BioBoost.Views (from private repository) nothing much has been changed

File size: 4.3 KB
Line 
1using System;
2using System.CodeDom.Compiler;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Drawing;
6using System.Data;
7using System.Linq;
8using System.Runtime.CompilerServices;
9using System.Runtime.InteropServices;
10using System.Security.Permissions;
11using System.Text;
12using System.Text.RegularExpressions;
13using System.Threading.Tasks;
14using System.Windows.Forms;
15using System.Windows.Forms.DataVisualization.Charting;
16using HeuristicLab.BioBoost.Representation;
17using HeuristicLab.Collections;
18using HeuristicLab.Common;
19using HeuristicLab.Core;
20using HeuristicLab.Core.Views;
21using HeuristicLab.MainForm;
22using HeuristicLab.MainForm.WindowsForms;
23using HeuristicLab.PluginInfrastructure;
24using NetTopologySuite.Noding;
25
26namespace HeuristicLab.BioBoost.Views {
27
28  [Content(typeof (ItemCollection<BioBoostCompoundSolution>), false)]
29  public partial class BioBoostParetoSetView : ItemView {
30
31    private Point mouseDownPos;
32
33    public new ItemCollection<BioBoostCompoundSolution> Content {
34      get { return (ItemCollection<BioBoostCompoundSolution>)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public BioBoostParetoSetView() {
39      InitializeComponent();
40    }
41
42    protected override void RegisterContentEvents() {
43      base.RegisterContentEvents();
44      Content.ItemsAdded += UpdateChart;
45      Content.ItemsRemoved += UpdateChart;
46      Content.CollectionReset += UpdateChart;
47    }
48
49    protected override void DeregisterContentEvents() {
50      base.DeregisterContentEvents();
51      Content.CollectionReset -= UpdateChart;
52      Content.ItemsRemoved -= UpdateChart;
53      Content.CollectionReset -= UpdateChart;
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      UpdateChart(this, EventArgs.Empty);
59    }
60
61    protected virtual void UpdateChart(object sender, EventArgs args) {
62      if (InvokeRequired) {
63        Invoke(new EventHandler(UpdateChart), sender, args);
64      } else {
65        try {
66          if (paretoSetChart.IsDisposed) return;
67          var points = paretoSetChart.Series[0].Points;
68          if (Content == null) {
69            points.Clear();
70            return;
71          }
72          var newPoints = Content.Where(solution => solution.Qualities.Length >= 2).Select(s => new { x = s.Qualities[0], y = s.Qualities[1], solution = s}).ToList();
73          if (newPoints.Count > 0) {
74            var chartArea = paretoSetChart.ChartAreas[0];
75            AdaptAxis(chartArea.AxisX, newPoints.Select(p => p.x), 1);
76            AdaptAxis(chartArea.AxisY, newPoints.Select(p => p.y), 1);
77            paretoSetChart.BeginInit();
78            points.Clear();
79            foreach (var point in newPoints) {
80              points.Add(new DataPoint(point.x, point.y) {
81                Tag = point.solution,
82              });
83            }
84            paretoSetChart.EndInit();
85          }
86        } catch (Exception x) {
87          ErrorHandling.ShowErrorDialog(x);
88        }
89      }
90    }
91
92    private void AdaptAxis(Axis axis, IEnumerable<double> values, int precision = 0, double overshot = 0.5, double margin = 0.25) {
93      var newMax = values.Max();
94      var newMin = values.Min();
95      var range = newMax - newMin;
96      newMax = newMax + range*margin;
97      newMin = newMin - range*margin;
98      if (range == 0) return;
99      var oldRange = axis.Maximum - axis.Minimum;
100      if (range*3 < oldRange) {
101        axis.Maximum = Math.Round(newMax+range*overshot, precision);
102        axis.Minimum = Math.Round(newMin-range*overshot , precision);
103      } else {
104        if (newMax > axis.Maximum) axis.Maximum = Math.Round(newMax+range*overshot, precision);
105        if (newMin < axis.Minimum) axis.Minimum = Math.Round(newMin-range*overshot, precision);
106      }
107    }
108
109    private void paretoSetChart_MouseDown(object sender, MouseEventArgs e) { mouseDownPos = e.Location; }
110    private void paretoSetChart_DoubleClick(object sender, EventArgs e) {
111      var pos = mouseDownPos;
112      var result = paretoSetChart.HitTest(pos.X, pos.Y, ChartElementType.DataPoint);
113      var point = result.Object as DataPoint;
114      if (point != null && point.Tag is IContent)
115        MainFormManager.MainForm.ShowContent((IContent)point.Tag);
116    }
117
118
119  }
120}
Note: See TracBrowser for help on using the repository browser.