Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12271 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 7.3 KB
RevLine 
[4623]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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);
[4623]93    }
[4631]94
95    protected virtual void UpdateSeries() {
[4639]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();
[4631]105
[4641]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
[4639]113      if (!invisibleSeries.Contains(bestKnown)) {
[6342]114        foreach (AlleleFrequency af in Content.Where(x => x.ContainedInBestKnownSolution).OrderBy(x => x.Id)) {
[4641]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));
[4639]118          index++;
119        }
120      }
121      if (!invisibleSeries.Contains(others)) {
[6342]122        foreach (AlleleFrequency af in Content.Where(x => !x.ContainedInBestKnownSolution).OrderBy(x => x.Id)) {
[4641]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));
[4639]126          index++;
127        }
128      }
129    }
[4631]130
[4641]131    protected virtual DataPoint CreateDataPoint(int index, double value, AlleleFrequency af) {
[4639]132      string nl = Environment.NewLine;
[4641]133      DataPoint p = new DataPoint(index, value);
[4639]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);
[4641]141      p.IsEmpty = value == 0;
[4639]142      return p;
[4631]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
[4623]181  }
182}
Note: See TracBrowser for help on using the repository browser.