Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/08/10 03:00:46 (12 years ago)
Author:
swagner
Message:

Worked on population diversity analysis (#1188)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis.Views/3.3/HeatMapView.cs

    r4703 r4739  
    3131  [Content(typeof(HeatMap), true)]
    3232  public partial class HeatMapView : ItemView {
     33    protected static Color[] colors = new Color[256];
     34    protected static Color[] grayscaleColors = new Color[256];
    3335
    34     private static Color[] Colors;
    35     private static int ColorsCount = 500;
    36     private static Color[] GrayscaleColors = new Color[256];
    37 
    38     #region InitializeColors
     36    #region Initialize Colors
    3937    static HeatMapView() {
    40       int stepWidth = (255 * 6) / ColorsCount;
    41       Color[] colors = new Color[ColorsCount];
     38      int stepWidth = (256 * 4) / colors.Length;
    4239      int currentValue;
    4340      int currentClass;
    44       for (int i = 0; i < ColorsCount; i++) {
    45         currentValue = (i * stepWidth) % 255;
    46         currentClass = (i * stepWidth) / 255;
     41      for (int i = 0; i < colors.Length; i++) {
     42        currentValue = (i * stepWidth) % 256;
     43        currentClass = (i * stepWidth) / 256;
    4744        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; }
     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
    5449        }
    5550      }
    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];
    6051      for (int i = 0; i < 256; i++)
    61         GrayscaleColors[i] = Color.FromArgb(i, i, i);
     52        grayscaleColors[i] = Color.FromArgb(255 - i, 255 - i, 255 - i);  // white -> black
    6253    }
    6354    #endregion
     
    7364    }
    7465
     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
    7579    protected override void OnContentChanged() {
    7680      base.OnContentChanged();
    7781      if (Content == null) {
    7882        chart.Series.Clear();
     83        chart.Titles[0].Text = "Heat Map";
     84        minimumLabel.Text = "0.0";
     85        maximumLabel.Text = "1.0";
    7986      } else {
    80         UpdateChart();
     87        chart.Titles[0].Text = Content.Title;
     88        minimumLabel.Text = Content.Minimum.ToString();
     89        maximumLabel.Text = Content.Maximum.ToString();
     90        UpdatePoints();
    8191      }
    8292    }
    8393
    84     private void UpdateChart() {
     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() {
    85101      chart.Series.Clear();
    86102      Series series = new Series();
     
    89105      series.YValueType = ChartValueType.Int32;
    90106      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]));
     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;
    94112      chart.ChartAreas[0].AxisY.Minimum = 0;
    95       chart.ChartAreas[0].AxisY.Maximum = Content.Rows;
     113      chart.ChartAreas[0].AxisY.Maximum = Content.Rows + 1;
    96114      chart.Series.Add(series);
    97       chart.Legends.Clear();
    98115    }
    99116
    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 
     117    protected virtual DataPoint CreateDataPoint(int index1, int index2, double value) {
    110118      DataPoint p = new DataPoint(index1, index2);
    111       p.Color = color;
     119      p.Color = GetDataPointColor(value, Content.Minimum, Content.Maximum, grayscaleCheckBox.Checked);
    112120      p.MarkerStyle = MarkerStyle.Square;
    113       p.ToolTip = string.Format("Solution {0} vs. solution {1}: {2}",
    114                                 index1, index2, value);
     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);
    115124      return p;
    116125    }
    117126
    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();
     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];
    124133    }
    125134
     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    }
    126159    #endregion
    127160
     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
    128168  }
    129 
    130169}
Note: See TracChangeset for help on using the changeset viewer.