Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Analysis.Views/3.3/HeatMapView.cs @ 5726

Last change on this file since 5726 was 4739, checked in by swagner, 14 years ago

Worked on population diversity analysis (#1188)

File size: 6.5 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.Drawing;
24using System.Windows.Forms;
25using System.Windows.Forms.DataVisualization.Charting;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Analysis.Views {
30  [View("HeatMap View")]
31  [Content(typeof(HeatMap), true)]
32  public partial class HeatMapView : ItemView {
33    protected static Color[] colors = new Color[256];
34    protected static Color[] grayscaleColors = new Color[256];
35
36    #region Initialize Colors
37    static HeatMapView() {
38      int stepWidth = (256 * 4) / colors.Length;
39      int currentValue;
40      int currentClass;
41      for (int i = 0; i < colors.Length; i++) {
42        currentValue = (i * stepWidth) % 256;
43        currentClass = (i * stepWidth) / 256;
44        switch (currentClass) {
45          case 0: { colors[i] = Color.FromArgb(0, currentValue, 255); break; }        // blue -> cyan
46          case 1: { colors[i] = Color.FromArgb(0, 255, 255 - currentValue); break; }  // cyan -> green
47          case 2: { colors[i] = Color.FromArgb(currentValue, 255, 0); break; }        // green -> yellow
48          case 3: { colors[i] = Color.FromArgb(255, 255 - currentValue, 0); break; }  // yellow -> red
49        }
50      }
51      for (int i = 0; i < 256; i++)
52        grayscaleColors[i] = Color.FromArgb(255 - i, 255 - i, 255 - i);  // white -> black
53    }
54    #endregion
55
56    public new HeatMap Content {
57      get { return (HeatMap)base.Content; }
58      set { base.Content = value; }
59    }
60
61    public HeatMapView() {
62      InitializeComponent();
63      chart.CustomizeAllChartAreas();
64    }
65
66    protected override void DeregisterContentEvents() {
67      Content.TitleChanged -= new EventHandler(Content_TitleChanged);
68      Content.MinimumChanged -= new EventHandler(Content_MinimumChanged);
69      Content.MaximumChanged -= new EventHandler(Content_MaximumChanged);
70      base.DeregisterContentEvents();
71    }
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.TitleChanged += new EventHandler(Content_TitleChanged);
75      Content.MinimumChanged += new EventHandler(Content_MinimumChanged);
76      Content.MaximumChanged += new EventHandler(Content_MaximumChanged);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        chart.Series.Clear();
83        chart.Titles[0].Text = "Heat Map";
84        minimumLabel.Text = "0.0";
85        maximumLabel.Text = "1.0";
86      } else {
87        chart.Titles[0].Text = Content.Title;
88        minimumLabel.Text = Content.Minimum.ToString();
89        maximumLabel.Text = Content.Maximum.ToString();
90        UpdatePoints();
91      }
92    }
93
94    protected override void SetEnabledStateOfControls() {
95      base.SetEnabledStateOfControls();
96      chart.Enabled = Content != null;
97      grayscaleCheckBox.Enabled = Content != null;
98    }
99
100    protected virtual void UpdatePoints() {
101      chart.Series.Clear();
102      Series series = new Series();
103      series.ChartType = SeriesChartType.Point;
104      series.XValueType = ChartValueType.Int32;
105      series.YValueType = ChartValueType.Int32;
106      series.YAxisType = AxisType.Primary;
107      for (int i = 1; i < Content.Rows + 1; i++)
108        for (int j = 1; j < Content.Columns + 1; j++)
109          series.Points.Add(CreateDataPoint(j, i, Content[i - 1, j - 1]));
110      chart.ChartAreas[0].AxisX.Minimum = 0;
111      chart.ChartAreas[0].AxisX.Maximum = Content.Columns + 1;
112      chart.ChartAreas[0].AxisY.Minimum = 0;
113      chart.ChartAreas[0].AxisY.Maximum = Content.Rows + 1;
114      chart.Series.Add(series);
115    }
116
117    protected virtual DataPoint CreateDataPoint(int index1, int index2, double value) {
118      DataPoint p = new DataPoint(index1, index2);
119      p.Color = GetDataPointColor(value, Content.Minimum, Content.Maximum, grayscaleCheckBox.Checked);
120      p.MarkerStyle = MarkerStyle.Square;
121      //p.MarkerSize = 10;
122      //string nl = Environment.NewLine;
123      //p.ToolTip = string.Format("Row: {0}{3}Column: {1}{3}Value: {2}", index2, index1, value, nl);
124      return p;
125    }
126
127    protected virtual Color GetDataPointColor(double value, double min, double max, bool grayscale) {
128      int count = grayscale ? grayscaleColors.Length : colors.Length;
129      int index = (int)((count - 1) * (value - min) / (max - min));
130      if (index >= count) index = count - 1;
131      if (index < 0) index = 0;
132      return grayscale ? grayscaleColors[index] : colors[index];
133    }
134
135    #region Content Events
136    protected virtual void Content_TitleChanged(object sender, EventArgs e) {
137      if (InvokeRequired)
138        Invoke(new EventHandler(Content_TitleChanged), sender, e);
139      else {
140        chart.Titles[0].Text = Content.Title;
141      }
142    }
143    protected virtual void Content_MinimumChanged(object sender, EventArgs e) {
144      if (InvokeRequired)
145        Invoke(new EventHandler(Content_MinimumChanged), sender, e);
146      else {
147        minimumLabel.Text = Content.Minimum.ToString();
148        UpdatePoints();
149      }
150    }
151    protected virtual void Content_MaximumChanged(object sender, EventArgs e) {
152      if (InvokeRequired)
153        Invoke(new EventHandler(Content_MaximumChanged), sender, e);
154      else {
155        maximumLabel.Text = Content.Maximum.ToString();
156        UpdatePoints();
157      }
158    }
159    #endregion
160
161    #region Control Events
162    protected virtual void grayscaledImagesCheckBox_CheckedChanged(object sender, EventArgs e) {
163      grayscalesPictureBox.Visible = grayscaleCheckBox.Checked;
164      colorsPictureBox.Visible = !grayscaleCheckBox.Checked;
165      UpdatePoints();
166    }
167    #endregion
168  }
169}
Note: See TracBrowser for help on using the repository browser.