Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/EnhancedStringConvertibleMatrixView.cs @ 8880

Last change on this file since 8880 was 8880, checked in by sforsten, 10 years ago

#1292: implemented changes suggested by mkommend in comment:49:ticket:1292

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common;
28using HeuristicLab.Data;
29using HeuristicLab.Data.Views;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  public partial class EnhancedStringConvertibleMatrixView : StringConvertibleMatrixView {
33    private bool[] columnVisibility, rowVisibility;
34
35    // sets the visibility of its columns for the next time it is updated
36    public IEnumerable<bool> ColumnVisibility {
37      get { return columnVisibility; }
38      set { columnVisibility = value.ToArray(); }
39    }
40    // sets the visibility of its rows for the next time it is updated
41    public IEnumerable<bool> RowVisibility {
42      get { return rowVisibility; }
43      set { rowVisibility = value.ToArray(); }
44    }
45
46    public double Maximum { get; set; }
47    public double Minimum { get; set; }
48
49    public new DoubleMatrix Content {
50      get { return (DoubleMatrix)base.Content; }
51      set { base.Content = value; }
52    }
53
54    public EnhancedStringConvertibleMatrixView() {
55      InitializeComponent();
56    }
57
58    public void ResetVisibility() {
59      columnVisibility = null;
60      rowVisibility = null;
61    }
62
63    protected override void UpdateColumnHeaders() {
64      base.UpdateColumnHeaders();
65      if (columnVisibility != null && Content != null && columnVisibility.Count() == dataGridView.ColumnCount) {
66        int i = 0;
67        foreach (var visibility in columnVisibility) {
68          dataGridView.Columns[i].Visible = visibility;
69          i++;
70        }
71      }
72    }
73    protected override void UpdateRowHeaders() {
74      if (rowVisibility == null) return;
75      if (Content == null) return;
76      if (rowVisibility.Count() != dataGridView.RowCount) return;
77
78      for (int index = 0; index < dataGridView.RowCount; index++) {
79        dataGridView.Rows[index].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndices[index]);
80        dataGridView.Rows[index].Visible = rowVisibility.ElementAt(virtualRowIndices[index]);
81      }
82    }
83
84    protected virtual void ShowHideRows_Click(object sender, EventArgs e) {
85      var dialog = new StringConvertibleMatrixRowVisibilityDialog(this.dataGridView.Rows.Cast<DataGridViewRow>());
86      dialog.ShowDialog();
87      RowVisibility = dialog.Visibility;
88    }
89
90    protected override void ShowHideColumns_Click(object sender, EventArgs e) {
91      var dialog = new StringConvertibleMatrixColumnVisibilityDialog(this.dataGridView.Columns.Cast<DataGridViewColumn>());
92      dialog.ShowDialog();
93      ColumnVisibility = dialog.Visibility;
94    }
95
96    protected void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
97      if (Content == null) return;
98      if (e.RowIndex < 0) return;
99      if (e.ColumnIndex < 0) return;
100      if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
101      if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
102
103      int rowIndex = virtualRowIndices[e.RowIndex];
104      Color backColor = GetDataPointColor(Content[rowIndex, e.ColumnIndex], Minimum, Maximum);
105      using (Brush backColorBrush = new SolidBrush(backColor)) {
106        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
107      }
108      e.PaintContent(e.CellBounds);
109      e.Handled = true;
110    }
111
112    protected virtual Color GetDataPointColor(double value, double min, double max) {
113      if (double.IsNaN(value)) {
114        return Color.DarkGray;
115      }
116      IList<Color> colors = ColorGradient.Colors;
117      int index = (int)((colors.Count - 1) * (value - min) / (max - min));
118      if (index >= colors.Count) index = colors.Count - 1;
119      if (index < 0) index = 0;
120      return colors[index];
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.