Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Views/3.3/HeatMapView.cs @ 4949

Last change on this file since 4949 was 4808, checked in by mkommend, 14 years ago

Extracted ColorGradient in a separate class (ticket #1261).

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