Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/14/17 10:29:51 (7 years ago)
Author:
pfleck
Message:

#2709 merged to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.DataPreprocessing.Views

  • stable/HeuristicLab.DataPreprocessing.Views/3.4/PreprocessingChartView.cs

    r14963 r15242  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    2526using System.Windows.Forms;
    2627using HeuristicLab.Analysis;
     28using HeuristicLab.Analysis.Views;
    2729using HeuristicLab.Collections;
    2830using HeuristicLab.Data;
    2931using HeuristicLab.MainForm;
     32using HeuristicLab.MainForm.WindowsForms;
    3033
    3134namespace HeuristicLab.DataPreprocessing.Views {
     
    3336  [Content(typeof(PreprocessingChartContent), false)]
    3437  public partial class PreprocessingChartView : PreprocessingCheckedVariablesView {
    35 
    36     protected PreprocessingDataTable dataTable;
    37     protected List<PreprocessingDataTable> dataTablePerVariable;
    38     protected List<DataRow> dataRows;
    39     protected List<DataRow> selectedDataRows;
    40 
    41     protected DataRowVisualProperties.DataRowChartType chartType;
    42     protected string chartTitle;
    43 
    44     private const string DEFAULT_CHART_TITLE = "Chart";
    45     private const int FIXED_CHART_SIZE = 300;
    46     private const int MAX_TABLE_AUTO_SIZE_ROWS = 3;
    47 
    48 
    49     public IEnumerable<double> Classification { get; set; }
    50     public bool IsDetailedChartViewEnabled { get; set; }
     38    protected Dictionary<string, DataTable> dataTables;
     39    protected Dictionary<string, DataTableView> dataTableViews;
     40
     41    public static readonly Color[] Colors = {
     42      Color.FromArgb(59, 136, 239), Color.FromArgb(252, 177, 59), Color.FromArgb(226, 64, 10),
     43      Color.FromArgb(5, 100, 146), Color.FromArgb(191, 191, 191), Color.FromArgb(26, 59, 105),
     44      Color.FromArgb(255, 226, 126), Color.FromArgb(18, 156, 221), Color.FromArgb(202, 107, 75),
     45      Color.FromArgb(0, 92, 219), Color.FromArgb(243, 210, 136), Color.FromArgb(80, 99, 129),
     46      Color.FromArgb(241, 185, 168), Color.FromArgb(224, 131, 10), Color.FromArgb(120, 147, 190)
     47    };
    5148
    5249    public PreprocessingChartView() {
    5350      InitializeComponent();
    54       chartType = DataRowVisualProperties.DataRowChartType.Line;
    55       chartTitle = DEFAULT_CHART_TITLE;
     51      dataTables = new Dictionary<string, DataTable>();
     52      dataTableViews = new Dictionary<string, DataTableView>();
     53      scrollPanel.HorizontalScroll.Visible = false;
    5654    }
    5755
     
    6058      if (Content != null) {
    6159        InitData();
    62         GenerateChart();
    63 
    64         foreach (var row in dataRows) {
    65           string variableName = row.Name;
    66           if (!IsVariableChecked(variableName)) {
    67             dataTableView.SetRowEnabled(variableName, false);
    68             dataTable.SelectedRows.Remove(variableName);
    69             dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
    70           }
    71         }
    72       }
    73     }
    74 
    75     private void InitData() {
    76       //Create data tables and data rows
    77       dataRows = Content.CreateAllDataRows(chartType);
    78       dataTable = new PreprocessingDataTable(chartTitle);
    79       dataTablePerVariable = new List<PreprocessingDataTable>();
    80 
    81       //add data rows to data tables according to checked item list
    82       foreach (var row in dataRows) {
    83         string variableName = row.Name;
    84 
    85         //add row to data table
    86         dataTable.Rows.Add(row);
    87 
    88         //add row to data table per variable
    89         PreprocessingDataTable d = new PreprocessingDataTable(variableName);
    90         d.Rows.Add(row);
    91         dataTablePerVariable.Add(d);
    92       }
    93 
    94       UpdateSelection();
     60        GenerateLayout();
     61      }
     62    }
     63
     64    protected virtual int GetNumberOfVisibleDataTables() {
     65      return Content.VariableItemList.CheckedItems.Count();
     66    }
     67
     68    protected virtual IEnumerable<DataTableView> GetVisibleDataTables() {
     69      foreach (var name in Content.VariableItemList.CheckedItems) {
     70        if (!dataTableViews.ContainsKey(name.Value.Value))
     71          dataTableViews.Add(name.Value.Value, new DataTableView() { Content = dataTables[name.Value.Value], ShowChartOnly = true });
     72        yield return dataTableViews[name.Value.Value];
     73      }
     74    }
     75
     76    protected virtual DataTable CreateDataTable(string variableName) {
     77      return null;
     78    }
     79
     80    protected virtual void InitData() {
     81      dataTables.Clear();
     82      dataTableViews.Clear();
     83      foreach (var variable in Content.VariableItemList.Select(v => v.Value)) {
     84        dataTables.Add(variable, CreateDataTable(variable));
     85      }
    9586    }
    9687
     
    9889      base.CheckedItemsChanged(sender, checkedItems);
    9990
    100       foreach (IndexedItem<StringValue> item in checkedItems.Items) {
    101         string variableName = item.Value.Value;
    102 
    103 
    104         if (!IsVariableChecked(variableName)) {
    105           // not checked -> remove
    106           dataTableView.SetRowEnabled(variableName, false);
    107           dataTable.SelectedRows.Remove(variableName);
    108           dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
    109         } else {
    110           // checked -> add
    111           DataRow row = GetDataRow(variableName);
    112           DataRow selectedRow = GetSelectedDataRow(variableName);
    113           dataTableView.SetRowEnabled(variableName, true);
    114 
    115           PreprocessingDataTable pdt = new PreprocessingDataTable(variableName);
    116           pdt.Rows.Add(row);
    117           // dataTablePerVariable does not contain unchecked variables => reduce insert position by number of uncheckt variables to correct the index
    118           int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
    119           dataTablePerVariable.Insert(item.Index - uncheckedUntilVariable, pdt);
    120 
    121           //update selection
    122           if (selectedRow != null) {
    123             dataTable.SelectedRows.Add(selectedRow);
    124             pdt.SelectedRows.Add(selectedRow);
    125           }
    126         }
    127       }
    128 
    129       // update chart if not in all in one mode
    130       if (Content != null && !Content.AllInOneMode)
    131         GenerateChart();
    132     }
    133 
    134     private DataRow GetSelectedDataRow(string variableName) {
    135       foreach (DataRow row in selectedDataRows) {
    136         if (row.Name == variableName)
    137           return row;
    138       }
    139       return null;
    140     }
    141     private DataRow GetDataRow(string variableName) {
    142       foreach (DataRow row in dataRows) {
    143         if (row.Name == variableName)
    144           return row;
    145       }
    146       return null;
    147     }
     91      GenerateLayout();
     92    }
     93
    14894
    14995    #region Add/Remove/Update Variable, Reset
    15096    protected override void AddVariable(string name) {
    15197      base.AddVariable(name);
    152       DataRow row = Content.CreateDataRow(name, chartType);
    153       dataTable.Rows.Add(row);
    154       PreprocessingDataTable d = new PreprocessingDataTable(name);
    155       d.Rows.Add(row);
    156       dataTablePerVariable.Add(d);
    157 
    158       if (!Content.AllInOneMode)
    159         GenerateChart();
     98      dataTables.Add(name, CreateDataTable(name));
     99
     100      GenerateLayout();
    160101    }
    161102
     
    163104    protected override void RemoveVariable(string name) {
    164105      base.RemoveVariable(name);
    165       dataTable.Rows.Remove(name);
    166       dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == name)));
    167 
    168       if (!Content.AllInOneMode)
    169         GenerateChart();
     106      dataTables.Remove(name);
     107      dataTableViews.Remove(name);
     108
     109      GenerateLayout();
    170110    }
    171111
    172112    protected override void UpdateVariable(string name) {
    173113      base.UpdateVariable(name);
    174       DataRow newRow = Content.CreateDataRow(name, chartType);
    175       dataTable.Rows.Remove(name);
    176       dataTable.Rows.Add(newRow);
    177       DataTable dt = dataTablePerVariable.Find(x => x.Rows.Find(y => y.Name == name) != null);
    178       if (dt != null) {
    179         dt.Rows.Remove(name);
    180         dt.Rows.Add(newRow);
    181       }
     114      dataTables.Remove(name);
     115      var newDataTable = CreateDataTable(name);
     116      dataTables.Add(name, newDataTable);
     117      dataTableViews[name].Content = newDataTable;
     118      GenerateLayout();
    182119    }
    183120    protected override void ResetAllVariables() {
     
    186123    #endregion
    187124
    188     #region Generate Charts
    189     protected void GenerateChart() {
     125    protected override void CheckedChangedUpdate() {
     126      GenerateLayout();
     127    }
     128
     129    #region Generate Layout
     130    protected void GenerateLayout() {
     131      if (SuppressCheckedChangedUpdate)
     132        return;
     133
     134      scrollPanel.SuspendRepaint();
     135
    190136      ClearTableLayout();
    191       if (Content.AllInOneMode) {
    192         GenerateSingleChartLayout();
    193       } else
    194         GenerateMultiChartLayout();
    195     }
    196 
    197     private void GenerateSingleChartLayout() {
    198       tableLayoutPanel.ColumnCount = 1;
    199       tableLayoutPanel.RowCount = 1;
    200       tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
    201       tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
    202       tableLayoutPanel.Controls.Add(dataTableView, 0, 0);
    203       dataTableView.Content = dataTable;
    204     }
    205 
    206     private void GenerateMultiChartLayout() {
    207       int checkedItemsCnt = 0;
    208       foreach (var item in Content.VariableItemList.CheckedItems)
    209         checkedItemsCnt++;
    210 
    211       // set columns and rows based on number of items
    212       int columns = GetNrOfMultiChartColumns(checkedItemsCnt);
    213       int rows = GetNrOfMultiChartRows(checkedItemsCnt, columns);
    214 
    215       tableLayoutPanel.ColumnCount = columns;
    216       tableLayoutPanel.RowCount = rows;
    217 
    218       List<PreprocessingDataTable>.Enumerator enumerator = dataTablePerVariable.GetEnumerator();
    219       for (int x = 0; x < columns; x++) {
    220 
    221         if (rows <= MAX_TABLE_AUTO_SIZE_ROWS)
    222           tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / columns));
    223         else
    224           //scrollbar is shown if there are more than 3 rows -> remove scroll bar width from total width
    225           tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth) / columns));
    226         for (int y = 0; y < rows; y++) {
    227           //Add a row only when creating the first column
    228           if (x == 0) {
    229             // fixed chart size when there are more than 3 tables
    230             if (rows > MAX_TABLE_AUTO_SIZE_ROWS)
    231               tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE));
    232             else
    233               tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / rows));
     137
     138      int nrCharts = GetNumberOfVisibleDataTables();
     139
     140      // Set columns and rows based on number of items
     141      int columns = Math.Min(nrCharts, (int)columnsNumericUpDown.Value);
     142      int rows = (int)Math.Ceiling((float)nrCharts / columns);
     143
     144      tableLayoutPanel.ColumnCount = Math.Max(columns, 0);
     145      tableLayoutPanel.RowCount = Math.Max(rows, 0);
     146
     147      if (columns > 0 && rows > 0) {
     148        var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / columns;
     149        var height = width * 0.75f;
     150
     151        using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
     152          for (int row = 0; row < rows; row++) {
     153            tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, height));
     154            for (int col = 0; col < columns; col++) {
     155              if (row == 0) {
     156                // Add a column-style only when creating the first row
     157                tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, width));
     158              }
     159
     160              if (enumerator.MoveNext())
     161                AddDataTableToTableLayout(enumerator.Current, row, col);
     162
     163            }
    234164          }
    235 
    236           enumerator.MoveNext();
    237           PreprocessingDataTable d = enumerator.Current;
    238           AddDataTableToTableLayout(d, x, y);
    239 
    240165        }
    241       }
    242     }
    243     private int GetNrOfMultiChartColumns(int itemCount) {
    244       int columns = 0;
    245       if (itemCount <= 2)
    246         columns = 1;
    247       else if (itemCount <= 6)
    248         columns = 2;
    249       else
    250         columns = 3;
    251       return columns;
    252     }
    253     private int GetNrOfMultiChartRows(int itemCount, int columns) {
    254       int rows = 0;
    255       if (columns == 3)
    256         rows = (itemCount + 2) / columns;
    257       else if (columns == 2)
    258         rows = (itemCount + 1) / columns;
    259       else
    260         rows = itemCount / columns;
    261       return rows;
    262     }
    263 
    264     private void AddDataTableToTableLayout(PreprocessingDataTable dataTable, int x, int y) {
    265       PreprocessingDataTableView dataView = new PreprocessingDataTableView();
    266       dataView.Classification = Classification;
    267       dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
    268 
     166        tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 0));
     167      }
     168
     169      scrollPanel.ResumeRepaint(true);
     170    }
     171
     172    private void AddDataTableToTableLayout(DataTableView dataTable, int row, int col) {
    269173      if (dataTable == null) {
    270174        // dummy panel for empty field
    271         Panel p = new Panel();
    272         p.Dock = DockStyle.Fill;
    273         tableLayoutPanel.Controls.Add(p, y, x);
     175        Panel p = new Panel { Dock = DockStyle.Fill };
     176        tableLayoutPanel.Controls.Add(p, col, row);
    274177      } else {
    275         dataView.Content = dataTable;
    276         dataView.Dock = DockStyle.Fill;
    277         tableLayoutPanel.Controls.Add(dataView, y, x);
     178        dataTable.Dock = DockStyle.Fill;
     179        tableLayoutPanel.Controls.Add(dataTable, col, row);
    278180      }
    279181    }
     
    286188      tableLayoutPanel.ColumnStyles.Clear();
    287189      tableLayoutPanel.RowStyles.Clear();
    288       tableLayoutPanel.AutoScroll = false;
    289       tableLayoutPanel.AutoScroll = true;
    290190    }
    291191    //Remove horizontal scroll bar if visible
     
    293193      if (tableLayoutPanel.HorizontalScroll.Visible) {
    294194        // Add padding on the right in order to accomodate the vertical scrollbar
    295         int vWidth = SystemInformation.VerticalScrollBarWidth;
    296         tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
     195        tableLayoutPanel.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
    297196      } else {
    298197        // Reset padding
     
    302201    #endregion
    303202
    304     #region Update Selection
    305     protected override void PreprocessingData_SelctionChanged(object sender, EventArgs e) {
    306       base.PreprocessingData_SelctionChanged(sender, e);
    307       UpdateSelection();
    308     }
    309 
    310     private void UpdateSelection() {
    311       //update data table selection
    312       selectedDataRows = Content.CreateAllSelectedDataRows(chartType);
    313       dataTable.SelectedRows.Clear();
    314       foreach (var selectedRow in selectedDataRows) {
    315         if (IsVariableChecked(selectedRow.Name))
    316           dataTable.SelectedRows.Add(selectedRow);
    317       }
    318 
    319       //update data table per variable selection
    320       foreach (PreprocessingDataTable d in dataTablePerVariable) {
    321         d.SelectedRows.Clear();
    322         DataRow row = selectedDataRows.Find(x => x.Name == d.Name);
    323         if (row != null)
    324           d.SelectedRows.Add(row);
    325       }
    326     }
    327     #endregion
     203    private void columnsNumericUpDown_ValueChanged(object sender, System.EventArgs e) {
     204      GenerateLayout();
     205    }
     206
     207    private void splitContainer_Panel2_Resize(object sender, EventArgs e) {
     208      if (SuppressCheckedChangedUpdate)
     209        return;
     210
     211      scrollPanel.SuspendRepaint();
     212
     213      if (tableLayoutPanel.ColumnCount > 0 && tableLayoutPanel.RowCount > 0) {
     214        var width = (splitContainer.Panel2.Width - SystemInformation.VerticalScrollBarWidth) / tableLayoutPanel.ColumnCount;
     215        var height = width * 0.75f;
     216
     217        for (int i = 0; i < tableLayoutPanel.RowStyles.Count - 1; i++) {
     218          tableLayoutPanel.RowStyles[i].Height = height;
     219        }
     220        for (int i = 0; i < tableLayoutPanel.ColumnStyles.Count; i++) {
     221          tableLayoutPanel.ColumnStyles[i].Width = width;
     222        }
     223      }
     224
     225      scrollPanel.ResumeRepaint(true);
     226    }
    328227  }
    329228}
Note: See TracChangeset for help on using the changeset viewer.