Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Views/3.3/AlleleFrequencyCollectionView.cs @ 5815

Last change on this file since 5815 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 7.4 KB
RevLine 
[4623]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4623]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
[4639]22using System;
[4631]23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
[4623]26using System.Windows.Forms;
27using System.Windows.Forms.DataVisualization.Charting;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Analysis.Views {
[4639]32  [View("AlleleFrequencyCollection View")]
33  [Content(typeof(AlleleFrequencyCollection), true)]
34  public partial class AlleleFrequencyCollectionView : ItemView {
[4631]35    private List<Series> invisibleSeries;
36
[4639]37    public new AlleleFrequencyCollection Content {
38      get { return (AlleleFrequencyCollection)base.Content; }
[4623]39      set { base.Content = value; }
40    }
41
[4639]42    public AlleleFrequencyCollectionView() {
[4623]43      InitializeComponent();
[4631]44      invisibleSeries = new List<Series>();
[4637]45      chart.CustomizeAllChartAreas();
[4623]46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
[4631]50      if (Content == null) {
51        chart.Series.Clear();
52        invisibleSeries.Clear();
53      } else {
54        if (chart.Series.Count == 0) CreateSeries();
55        UpdateSeries();
56      }
[4623]57    }
58
59    protected override void SetEnabledStateOfControls() {
60      base.SetEnabledStateOfControls();
61      chart.Enabled = Content != null;
62    }
63
[4631]64    protected virtual void CreateSeries() {
65      Series bestKnown = new Series("Alleles of Best Known Solution");
[4639]66      bestKnown.ChartType = SeriesChartType.Column;
[4631]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");
[4639]73      others.ChartType = SeriesChartType.Column;
[4631]74      others.XValueType = ChartValueType.String;
75      others.YValueType = ChartValueType.Double;
76      others.YAxisType = AxisType.Primary;
77      chart.Series.Add(others);
[4639]78      invisibleSeries.Add(others);
[4631]79
[4623]80      Series qualities = new Series("Average Solution Qualities");
[4639]81      qualities.ChartType = SeriesChartType.FastPoint;
[4623]82      qualities.XValueType = ChartValueType.String;
83      qualities.YValueType = ChartValueType.Double;
84      qualities.YAxisType = AxisType.Secondary;
85      chart.Series.Add(qualities);
[4631]86
87      Series impacts = new Series("Average Impact");
[4639]88      impacts.ChartType = SeriesChartType.FastPoint;
[4631]89      impacts.XValueType = ChartValueType.String;
90      impacts.YValueType = ChartValueType.Double;
91      impacts.YAxisType = AxisType.Secondary;
92      chart.Series.Add(impacts);
[4641]93      invisibleSeries.Add(impacts);
[4623]94    }
[4631]95
96    protected virtual void UpdateSeries() {
[4639]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();
[4631]106
[4641]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
[4639]114      if (!invisibleSeries.Contains(bestKnown)) {
115        foreach (AlleleFrequency af in Content.Where(x => x.ContainedInBestKnownSolution).OrderBy(x => x.AverageImpact)) {
[4641]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));
[4639]119          index++;
120        }
121      }
122      if (!invisibleSeries.Contains(others)) {
123        foreach (AlleleFrequency af in Content.Where(x => !x.ContainedInBestKnownSolution).OrderBy(x => x.AverageImpact)) {
[4641]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));
[4639]127          index++;
128        }
129      }
130    }
[4631]131
[4641]132    protected virtual DataPoint CreateDataPoint(int index, double value, AlleleFrequency af) {
[4639]133      string nl = Environment.NewLine;
[4641]134      DataPoint p = new DataPoint(index, value);
[4639]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);
[4641]142      p.IsEmpty = value == 0;
[4639]143      return p;
[4631]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
[4623]182  }
183}
Note: See TracBrowser for help on using the repository browser.