#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Analysis;
using HeuristicLab.Analysis.Views;
using HeuristicLab.Collections;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.DataPreprocessing.Views {
[View("Preprocessing Chart View")]
[Content(typeof(PreprocessingChartContent), false)]
public partial class PreprocessingChartView : PreprocessingCheckedVariablesView {
protected Dictionary dataTables;
protected Dictionary dataTableControls;
private const int FIXED_CHART_SIZE = 300;
private const int MAX_TABLE_AUTO_SIZE_ROWS = 3;
protected static readonly Color[] Colors = {
Color.FromArgb(59, 136, 239), Color.FromArgb(252, 177, 59), Color.FromArgb(226, 64, 10),
Color.FromArgb(5, 100, 146), Color.FromArgb(191, 191, 191), Color.FromArgb(26, 59, 105),
Color.FromArgb(255, 226, 126), Color.FromArgb(18, 156, 221), Color.FromArgb(202, 107, 75),
Color.FromArgb(0, 92, 219), Color.FromArgb(243, 210, 136), Color.FromArgb(80, 99, 129),
Color.FromArgb(241, 185, 168), Color.FromArgb(224, 131, 10), Color.FromArgb(120, 147, 190)
};
public PreprocessingChartView() {
InitializeComponent();
dataTables = new Dictionary();
dataTableControls = new Dictionary();
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
InitData();
GenerateLayout();
}
}
protected virtual int GetNumberOfVisibleDataTables() {
return Content.VariableItemList.CheckedItems.Count();
}
protected virtual IEnumerable GetVisibleDataTables() {
foreach (var name in Content.VariableItemList.CheckedItems) {
if (!dataTableControls.ContainsKey(name.Value.Value))
dataTableControls.Add(name.Value.Value, new DataTableControl() { Content = dataTables[name.Value.Value] });
yield return dataTableControls[name.Value.Value];
}
}
protected virtual DataTable CreateDataTable(string variableName) {
return null;
}
protected virtual void InitData() {
dataTables.Clear();
dataTableControls.Clear();
foreach (var variable in Content.VariableItemList.Select(v => v.Value)) {
dataTables.Add(variable, CreateDataTable(variable));
}
}
protected override void CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs> checkedItems) {
base.CheckedItemsChanged(sender, checkedItems);
foreach (IndexedItem item in checkedItems.Items) {
string variableName = item.Value.Value;
if (!IsVariableChecked(variableName)) {
// not checked -> remove
//dataTableView.SetRowEnabled(variableName, false);
//dataTablePerVariable.Remove(dataTablePerVariable.Find(x => (x.Name == variableName)));
} else {
// checked -> add
//DataRow row = GetDataRow(variableName);
//dataTableView.SetRowEnabled(variableName, true);
//var pdt = new DataTable(variableName);
//pdt.VisualProperties.Title = string.Empty;
//pdt.Rows.Add(row);
// dataTablePerVariable does not contain unchecked variables => reduce insert position by number of uncheckt variables to correct the index
//int uncheckedUntilVariable = checkedItemList.Content.TakeWhile(x => x.Value != variableName).Count(x => !checkedItemList.Content.ItemChecked(x));
//dataTables.Insert(item.Index - uncheckedUntilVariable, pdt);
}
}
// update chart if not in all in one mode
//if (Content != null && !Content.AllInOneMode)
//GenerateChart();??
GenerateLayout();
}
#region Add/Remove/Update Variable, Reset
protected override void AddVariable(string name) {
base.AddVariable(name);
dataTables.Add(name, CreateDataTable(name));
GenerateLayout();
}
// remove variable from data table and item list
protected override void RemoveVariable(string name) {
base.RemoveVariable(name);
dataTables.Remove(name);
dataTableControls.Remove(name);
GenerateLayout();
}
protected override void UpdateVariable(string name) {
base.UpdateVariable(name);
dataTables.Remove(name);
var newDataTable = CreateDataTable(name);
dataTables.Add(name, newDataTable);
dataTableControls[name].Content = newDataTable;
GenerateLayout();
}
protected override void ResetAllVariables() {
InitData();
}
#endregion
protected override void CheckedChangedUpdate() {
GenerateLayout();
}
#region Generate Layout
protected void GenerateLayout() {
if (suppressCheckedChangedUpdate)
return;
tableLayoutPanel.SuspendRepaint();
ClearTableLayout();
int nrCharts = GetNumberOfVisibleDataTables();
// Set columns and rows based on number of items
int columns = GetNrOfMultiChartColumns(nrCharts);
int rows = GetNrOfMultiChartRows(nrCharts, columns);
tableLayoutPanel.ColumnCount = columns;
tableLayoutPanel.RowCount = rows;
using (var enumerator = GetVisibleDataTables().GetEnumerator()) {
for (int x = 0; x < columns; x++) {
var columnStyle = rows <= MAX_TABLE_AUTO_SIZE_ROWS
? new ColumnStyle(SizeType.Percent, 100 / columns)
: new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - SystemInformation.VerticalScrollBarWidth) / columns);
tableLayoutPanel.ColumnStyles.Add(columnStyle);
for (int y = 0; y < rows; y++) {
// Add a row only when creating the first column
if (x == 0) {
var rowStyle = rows > MAX_TABLE_AUTO_SIZE_ROWS
? new RowStyle(SizeType.Absolute, FIXED_CHART_SIZE)
: new RowStyle(SizeType.Percent, 100 / rows);
tableLayoutPanel.RowStyles.Add(rowStyle);
}
if (enumerator.MoveNext())
AddDataTableToTableLayout(enumerator.Current, x, y);
}
}
}
tableLayoutPanel.ResumeRepaint(true);
}
private int GetNrOfMultiChartColumns(int itemCount) {
int columns = 0;
if (itemCount <= 2)
columns = 1;
else if (itemCount <= 6)
columns = 2;
else
columns = 3;
return columns;
}
private int GetNrOfMultiChartRows(int itemCount, int columns) {
int rows = 0;
if (columns == 3)
rows = (itemCount + 2) / columns;
else if (columns == 2)
rows = (itemCount + 1) / columns;
else
rows = itemCount / columns;
return rows;
}
private void AddDataTableToTableLayout(DataTableControl dataTable, int x, int y) {
//dataView.Classification = Classification;
//dataView.IsDetailedChartViewEnabled = IsDetailedChartViewEnabled;
if (dataTable == null) {
// dummy panel for empty field
Panel p = new Panel { Dock = DockStyle.Fill };
tableLayoutPanel.Controls.Add(p, y, x);
} else {
dataTable.Dock = DockStyle.Fill;
tableLayoutPanel.Controls.Add(dataTable, y, x);
}
}
protected void ClearTableLayout() {
//Clear out the existing controls
tableLayoutPanel.Controls.Clear();
//Clear out the existing row and column styles
tableLayoutPanel.ColumnStyles.Clear();
tableLayoutPanel.RowStyles.Clear();
tableLayoutPanel.Width = 0;
tableLayoutPanel.Height = 0;
tableLayoutPanel.AutoScroll = false;
tableLayoutPanel.AutoScroll = true;
}
//Remove horizontal scroll bar if visible
private void tableLayoutPanel_Layout(object sender, LayoutEventArgs e) {
if (tableLayoutPanel.HorizontalScroll.Visible) {
// Add padding on the right in order to accomodate the vertical scrollbar
int vWidth = SystemInformation.VerticalScrollBarWidth;
tableLayoutPanel.Padding = new Padding(0, 0, vWidth, 0);
} else {
// Reset padding
tableLayoutPanel.Padding = new Padding(0);
}
}
#endregion
}
}