Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Analysis.Views/3.3/AlleleFrequencyCollectionView.cs @ 17564

Last change on this file since 17564 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Views {
32  [View("AlleleFrequencyCollection View")]
33  [Content(typeof(AlleleFrequencyCollection), true)]
34  public partial class AlleleFrequencyCollectionView : ItemView {
35    private List<Series> invisibleSeries;
36
37    public new AlleleFrequencyCollection Content {
38      get { return (AlleleFrequencyCollection)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public AlleleFrequencyCollectionView() {
43      InitializeComponent();
44      invisibleSeries = new List<Series>();
45      chart.CustomizeAllChartAreas();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content == null) {
51        chart.Series.Clear();
52        invisibleSeries.Clear();
53      } else {
54        if (chart.Series.Count == 0) CreateSeries();
55        UpdateSeries();
56      }
57    }
58
59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
61      chart.Enabled = Content != null;
62    }
63
64    protected virtual void CreateSeries() {
65      Series bestKnown = new Series("Alleles of Best Known Solution");
66      bestKnown.ChartType = SeriesChartType.Column;
67      bestKnown.XValueType = ChartValueType.String;
68      bestKnown.YValueType = ChartValueType.Double;
69      bestKnown.YAxisType = AxisType.Primary;
70      chart.Series.Add(bestKnown);
71
72      Series others = new Series("Other Alleles");
73      others.ChartType = SeriesChartType.Column;
74      others.XValueType = ChartValueType.String;
75      others.YValueType = ChartValueType.Double;
76      others.YAxisType = AxisType.Primary;
77      chart.Series.Add(others);
78      invisibleSeries.Add(others);
79
80      Series qualities = new Series("Average Solution Qualities");
81      qualities.ChartType = SeriesChartType.FastPoint;
82      qualities.XValueType = ChartValueType.String;
83      qualities.YValueType = ChartValueType.Double;
84      qualities.YAxisType = AxisType.Secondary;
85      chart.Series.Add(qualities);
86
87      Series impacts = new Series("Average Impact");
88      impacts.ChartType = SeriesChartType.FastPoint;
89      impacts.XValueType = ChartValueType.String;
90      impacts.YValueType = ChartValueType.Double;
91      impacts.YAxisType = AxisType.Secondary;
92      chart.Series.Add(impacts);
93    }
94
95    protected virtual void UpdateSeries() {
96      int index = 1;
97      Series bestKnown = chart.Series["Alleles of Best Known Solution"];
98      Series others = chart.Series["Other Alleles"];
99      Series qualities = chart.Series["Average Solution Qualities"];
100      Series impacts = chart.Series["Average Impact"];
101      bestKnown.Points.Clear();
102      others.Points.Clear();
103      qualities.Points.Clear();
104      impacts.Points.Clear();
105
106      if (!invisibleSeries.Contains(qualities) && !invisibleSeries.Contains(impacts))
107        chart.ChartAreas["Default"].AxisY2.Title = "Average Solution Quality / Average Impact";
108      else if (!invisibleSeries.Contains(qualities))
109        chart.ChartAreas["Default"].AxisY2.Title = "Average Solution Quality";
110      else if (!invisibleSeries.Contains(impacts))
111        chart.ChartAreas["Default"].AxisY2.Title = "Average Impact";
112
113      if (!invisibleSeries.Contains(bestKnown)) {
114        foreach (AlleleFrequency af in Content.Where(x => x.ContainedInBestKnownSolution).OrderBy(x => x.Id)) {
115          bestKnown.Points.Add(CreateDataPoint(index, af.Frequency, af));
116          if (!invisibleSeries.Contains(qualities)) qualities.Points.Add(CreateDataPoint(index, af.AverageSolutionQuality, af));
117          if (!invisibleSeries.Contains(impacts)) impacts.Points.Add(CreateDataPoint(index, af.AverageImpact, af));
118          index++;
119        }
120      }
121      if (!invisibleSeries.Contains(others)) {
122        foreach (AlleleFrequency af in Content.Where(x => !x.ContainedInBestKnownSolution).OrderBy(x => x.Id)) {
123          others.Points.Add(CreateDataPoint(index, af.Frequency, af));
124          if (!invisibleSeries.Contains(qualities)) qualities.Points.Add(CreateDataPoint(index, af.AverageSolutionQuality, af));
125          if (!invisibleSeries.Contains(impacts)) impacts.Points.Add(CreateDataPoint(index, af.AverageImpact, af));
126          index++;
127        }
128      }
129    }
130
131    protected virtual DataPoint CreateDataPoint(int index, double value, AlleleFrequency af) {
132      string nl = Environment.NewLine;
133      DataPoint p = new DataPoint(index, value);
134      p.ToolTip = string.Format("Id: {0}" + nl +
135                                "Relative Frequency: {1}" + nl +
136                                "Average Solution Quality: {2}" + nl +
137                                "Average Impact: {3}" + nl +
138                                "Contained in Best Known Solution: {4}" + nl +
139                                "Contained in Best Solution: {5}",
140                                af.Id, af.Frequency, af.AverageSolutionQuality, af.AverageImpact, af.ContainedInBestKnownSolution, af.ContainedInBestSolution);
141      p.IsEmpty = value == 0;
142      return p;
143    }
144
145    #region Chart Events
146    protected virtual void chart_MouseDown(object sender, MouseEventArgs e) {
147      HitTestResult result = chart.HitTest(e.X, e.Y);
148      if (result.ChartElementType == ChartElementType.LegendItem) {
149        ToggleSeriesVisible(result.Series);
150      }
151    }
152
153    protected virtual void ToggleSeriesVisible(Series series) {
154      if (!invisibleSeries.Contains(series))
155        invisibleSeries.Add(series);
156      else
157        invisibleSeries.Remove(series);
158      UpdateSeries();
159    }
160
161    protected virtual void chart_MouseMove(object sender, MouseEventArgs e) {
162      HitTestResult result = chart.HitTest(e.X, e.Y);
163      if (result.ChartElementType == ChartElementType.LegendItem)
164        this.Cursor = Cursors.Hand;
165      else
166        this.Cursor = Cursors.Default;
167    }
168
169    protected virtual void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
170      foreach (LegendItem legendItem in e.LegendItems) {
171        var series = chart.Series[legendItem.SeriesName];
172        if (series != null) {
173          bool seriesIsInvisible = invisibleSeries.Contains(series);
174          foreach (LegendCell cell in legendItem.Cells) {
175            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
176          }
177        }
178      }
179    }
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.