[13074] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System;
|
---|
[13072] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 28 | using HeuristicLab.BioBoost.Representation;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
| 31 | using HeuristicLab.Core.Views;
|
---|
| 32 | using HeuristicLab.MainForm;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.BioBoost.Views {
|
---|
| 36 |
|
---|
| 37 | [Content(typeof (ItemCollection<BioBoostCompoundSolution>), false)]
|
---|
| 38 | public partial class BioBoostParetoSetView : ItemView {
|
---|
| 39 |
|
---|
| 40 | private Point mouseDownPos;
|
---|
| 41 |
|
---|
| 42 | public new ItemCollection<BioBoostCompoundSolution> Content {
|
---|
| 43 | get { return (ItemCollection<BioBoostCompoundSolution>)base.Content; }
|
---|
| 44 | set { base.Content = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public BioBoostParetoSetView() {
|
---|
| 48 | InitializeComponent();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | protected override void RegisterContentEvents() {
|
---|
| 52 | base.RegisterContentEvents();
|
---|
| 53 | Content.ItemsAdded += UpdateChart;
|
---|
| 54 | Content.ItemsRemoved += UpdateChart;
|
---|
| 55 | Content.CollectionReset += UpdateChart;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | protected override void DeregisterContentEvents() {
|
---|
| 59 | base.DeregisterContentEvents();
|
---|
| 60 | Content.CollectionReset -= UpdateChart;
|
---|
| 61 | Content.ItemsRemoved -= UpdateChart;
|
---|
| 62 | Content.CollectionReset -= UpdateChart;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | protected override void OnContentChanged() {
|
---|
| 66 | base.OnContentChanged();
|
---|
| 67 | UpdateChart(this, EventArgs.Empty);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | protected virtual void UpdateChart(object sender, EventArgs args) {
|
---|
| 71 | if (InvokeRequired) {
|
---|
| 72 | Invoke(new EventHandler(UpdateChart), sender, args);
|
---|
| 73 | } else {
|
---|
| 74 | try {
|
---|
| 75 | if (paretoSetChart.IsDisposed) return;
|
---|
| 76 | var points = paretoSetChart.Series[0].Points;
|
---|
| 77 | if (Content == null) {
|
---|
| 78 | points.Clear();
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 | var newPoints = Content.Where(solution => solution.Qualities.Length >= 2).Select(s => new { x = s.Qualities[0], y = s.Qualities[1], solution = s}).ToList();
|
---|
| 82 | if (newPoints.Count > 0) {
|
---|
| 83 | var chartArea = paretoSetChart.ChartAreas[0];
|
---|
| 84 | AdaptAxis(chartArea.AxisX, newPoints.Select(p => p.x), 1);
|
---|
| 85 | AdaptAxis(chartArea.AxisY, newPoints.Select(p => p.y), 1);
|
---|
| 86 | paretoSetChart.BeginInit();
|
---|
| 87 | points.Clear();
|
---|
| 88 | foreach (var point in newPoints) {
|
---|
| 89 | points.Add(new DataPoint(point.x, point.y) {
|
---|
| 90 | Tag = point.solution,
|
---|
| 91 | });
|
---|
| 92 | }
|
---|
| 93 | paretoSetChart.EndInit();
|
---|
| 94 | }
|
---|
| 95 | } catch (Exception x) {
|
---|
| 96 | ErrorHandling.ShowErrorDialog(x);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private void AdaptAxis(Axis axis, IEnumerable<double> values, int precision = 0, double overshot = 0.5, double margin = 0.25) {
|
---|
| 102 | var newMax = values.Max();
|
---|
| 103 | var newMin = values.Min();
|
---|
| 104 | var range = newMax - newMin;
|
---|
| 105 | newMax = newMax + range*margin;
|
---|
| 106 | newMin = newMin - range*margin;
|
---|
| 107 | if (range == 0) return;
|
---|
| 108 | var oldRange = axis.Maximum - axis.Minimum;
|
---|
| 109 | if (range*3 < oldRange) {
|
---|
| 110 | axis.Maximum = Math.Round(newMax+range*overshot, precision);
|
---|
| 111 | axis.Minimum = Math.Round(newMin-range*overshot , precision);
|
---|
| 112 | } else {
|
---|
| 113 | if (newMax > axis.Maximum) axis.Maximum = Math.Round(newMax+range*overshot, precision);
|
---|
| 114 | if (newMin < axis.Minimum) axis.Minimum = Math.Round(newMin-range*overshot, precision);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private void paretoSetChart_MouseDown(object sender, MouseEventArgs e) { mouseDownPos = e.Location; }
|
---|
| 119 | private void paretoSetChart_DoubleClick(object sender, EventArgs e) {
|
---|
| 120 | var pos = mouseDownPos;
|
---|
| 121 | var result = paretoSetChart.HitTest(pos.X, pos.Y, ChartElementType.DataPoint);
|
---|
| 122 | var point = result.Object as DataPoint;
|
---|
| 123 | if (point != null && point.Tag is IContent)
|
---|
| 124 | MainFormManager.MainForm.ShowContent((IContent)point.Tag);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | }
|
---|
| 129 | }
|
---|