Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4703 was 4703, checked in by swagner, 13 years ago

Reviewed diversity analysis branch and merged it into the trunk (#1188)

File size: 4.6 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
34    private static Color[] Colors;
35    private static int ColorsCount = 500;
36    private static Color[] GrayscaleColors = new Color[256];
37
38    #region InitializeColors
39    static HeatMapView() {
40      int stepWidth = (255 * 6) / ColorsCount;
41      Color[] colors = new Color[ColorsCount];
42      int currentValue;
43      int currentClass;
44      for (int i = 0; i < ColorsCount; i++) {
45        currentValue = (i * stepWidth) % 255;
46        currentClass = (i * stepWidth) / 255;
47        switch (currentClass) {
48          case 0: { colors[i] = Color.FromArgb(255, currentValue, 0); break; }
49          case 1: { colors[i] = Color.FromArgb(255 - currentValue, 255, 0); break; }
50          case 2: { colors[i] = Color.FromArgb(0, 255, currentValue); break; }
51          case 3: { colors[i] = Color.FromArgb(0, 255 - currentValue, 255); break; }
52          case 4: { colors[i] = Color.FromArgb(currentValue, 0, 255); break; }
53          case 5: { colors[i] = Color.FromArgb(255, 0, 255 - currentValue); break; }
54        }
55      }
56      int n = (int)(ColorsCount * 0.7);
57      Colors = new Color[n];
58      for (int i = 0; i < n; i++)
59        Colors[i] = colors[i];
60      for (int i = 0; i < 256; i++)
61        GrayscaleColors[i] = Color.FromArgb(i, i, i);
62    }
63    #endregion
64
65    public new HeatMap Content {
66      get { return (HeatMap)base.Content; }
67      set { base.Content = value; }
68    }
69
70    public HeatMapView() {
71      InitializeComponent();
72      chart.CustomizeAllChartAreas();
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content == null) {
78        chart.Series.Clear();
79      } else {
80        UpdateChart();
81      }
82    }
83
84    private void UpdateChart() {
85      chart.Series.Clear();
86      Series series = new Series();
87      series.ChartType = SeriesChartType.Point;
88      series.XValueType = ChartValueType.Int32;
89      series.YValueType = ChartValueType.Int32;
90      series.YAxisType = AxisType.Primary;
91      for (int i = 0; i < Content.Rows; i++)
92        for (int j = 0; j < Content.Columns; j++)
93          series.Points.Add(CreateDataPoint(i, j, Content[i, j]));
94      chart.ChartAreas[0].AxisY.Minimum = 0;
95      chart.ChartAreas[0].AxisY.Maximum = Content.Rows;
96      chart.Series.Add(series);
97      chart.Legends.Clear();
98    }
99
100    private DataPoint CreateDataPoint(int index1, int index2, double value) {
101      bool grayScaleModus = grayscaledImagesCheckBox.Checked;
102      int n = grayScaleModus ? GrayscaleColors.Length : Colors.Length;
103      int colorIndex = (int)((n - 1) * value);
104      if (colorIndex >= n) colorIndex = n - 1;
105      if (colorIndex < 0) colorIndex = 0;
106      // invert so that red is 1, blue 0 / black is 1, white 0
107      colorIndex = n - colorIndex - 1;
108      Color color = grayScaleModus ? GrayscaleColors[colorIndex] : Colors[colorIndex];
109
110      DataPoint p = new DataPoint(index1, index2);
111      p.Color = color;
112      p.MarkerStyle = MarkerStyle.Square;
113      p.ToolTip = string.Format("Solution {0} vs. solution {1}: {2}",
114                                index1, index2, value);
115      return p;
116    }
117
118    #region Chart Events
119
120    private void grayscaledImagesCheckBox_CheckedChanged(object sender, EventArgs e) {
121      GrayscalesPictureBox.Visible = grayscaledImagesCheckBox.Checked;
122      ColorsPictureBox.Visible = !grayscaledImagesCheckBox.Checked;
123      UpdateChart();
124    }
125
126    #endregion
127
128  }
129
130}
Note: See TracBrowser for help on using the repository browser.