Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Analysis.Views/3.3/AlleleFrequencyCollectionView.cs @ 6036

Last change on this file since 6036 was 4641, checked in by swagner, 14 years ago

Worked on allele frequency analysis (#1234)

File size: 7.4 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.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      invisibleSeries.Add(impacts);
94    }
95
96    protected virtual void UpdateSeries() {
97      int index = 1;
98      Series bestKnown = chart.Series["Alleles of Best Known Solution"];
99      Series others = chart.Series["Other Alleles"];
100      Series qualities = chart.Series["Average Solution Qualities"];
101      Series impacts = chart.Series["Average Impact"];
102      bestKnown.Points.Clear();
103      others.Points.Clear();
104      qualities.Points.Clear();
105      impacts.Points.Clear();
106
107      if (!invisibleSeries.Contains(qualities) && !invisibleSeries.Contains(impacts))
108        chart.ChartAreas["Default"].AxisY2.Title = "Average Solution Quality / Average Impact";
109      else if (!invisibleSeries.Contains(qualities))
110        chart.ChartAreas["Default"].AxisY2.Title = "Average Solution Quality";
111      else if (!invisibleSeries.Contains(impacts))
112        chart.ChartAreas["Default"].AxisY2.Title = "Average Impact";
113
114      if (!invisibleSeries.Contains(bestKnown)) {
115        foreach (AlleleFrequency af in Content.Where(x => x.ContainedInBestKnownSolution).OrderBy(x => x.AverageImpact)) {
116          bestKnown.Points.Add(CreateDataPoint(index, af.Frequency, af));
117          if (!invisibleSeries.Contains(qualities)) qualities.Points.Add(CreateDataPoint(index, af.AverageSolutionQuality, af));
118          if (!invisibleSeries.Contains(impacts)) impacts.Points.Add(CreateDataPoint(index, af.AverageImpact, af));
119          index++;
120        }
121      }
122      if (!invisibleSeries.Contains(others)) {
123        foreach (AlleleFrequency af in Content.Where(x => !x.ContainedInBestKnownSolution).OrderBy(x => x.AverageImpact)) {
124          others.Points.Add(CreateDataPoint(index, af.Frequency, af));
125          if (!invisibleSeries.Contains(qualities)) qualities.Points.Add(CreateDataPoint(index, af.AverageSolutionQuality, af));
126          if (!invisibleSeries.Contains(impacts)) impacts.Points.Add(CreateDataPoint(index, af.AverageImpact, af));
127          index++;
128        }
129      }
130    }
131
132    protected virtual DataPoint CreateDataPoint(int index, double value, AlleleFrequency af) {
133      string nl = Environment.NewLine;
134      DataPoint p = new DataPoint(index, value);
135      p.ToolTip = string.Format("Id: {0}" + nl +
136                                "Relative Frequency: {1}" + nl +
137                                "Average Solution Quality: {2}" + nl +
138                                "Average Impact: {3}" + nl +
139                                "Contained in Best Known Solution: {4}" + nl +
140                                "Contained in Best Solution: {5}",
141                                af.Id, af.Frequency, af.AverageSolutionQuality, af.AverageImpact, af.ContainedInBestKnownSolution, af.ContainedInBestSolution);
142      p.IsEmpty = value == 0;
143      return p;
144    }
145
146    #region Chart Events
147    protected virtual void chart_MouseDown(object sender, MouseEventArgs e) {
148      HitTestResult result = chart.HitTest(e.X, e.Y);
149      if (result.ChartElementType == ChartElementType.LegendItem) {
150        ToggleSeriesVisible(result.Series);
151      }
152    }
153
154    protected virtual void ToggleSeriesVisible(Series series) {
155      if (!invisibleSeries.Contains(series))
156        invisibleSeries.Add(series);
157      else
158        invisibleSeries.Remove(series);
159      UpdateSeries();
160    }
161
162    protected virtual void chart_MouseMove(object sender, MouseEventArgs e) {
163      HitTestResult result = chart.HitTest(e.X, e.Y);
164      if (result.ChartElementType == ChartElementType.LegendItem)
165        this.Cursor = Cursors.Hand;
166      else
167        this.Cursor = Cursors.Default;
168    }
169
170    protected virtual void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
171      foreach (LegendItem legendItem in e.LegendItems) {
172        var series = chart.Series[legendItem.SeriesName];
173        if (series != null) {
174          bool seriesIsInvisible = invisibleSeries.Contains(series);
175          foreach (LegendCell cell in legendItem.Cells) {
176            cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
177          }
178        }
179      }
180    }
181    #endregion
182  }
183}
Note: See TracBrowser for help on using the repository browser.