Index: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/PreprocessingChartView.cs
===================================================================
--- branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/PreprocessingChartView.cs	(revision 10712)
+++ branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/PreprocessingChartView.cs	(revision 10717)
@@ -20,6 +20,10 @@
 #endregion
 
+using System;
+using System.Collections.Generic;
+using System.Drawing;
 using System.Windows.Forms;
 using HeuristicLab.Analysis;
+using HeuristicLab.Analysis.Views;
 using HeuristicLab.Collections;
 using HeuristicLab.Core;
@@ -36,7 +40,11 @@
     private IChartLogic logic;
     private DataTable dataTable;
+    private List<DataTable> dataTablePerVariable;
     private ICheckedItemList<StringValue> variableItemList;
+    private List<DataRow> dataRows;
+
     protected DataRowVisualProperties.DataRowChartType chartType;
     protected string chartTitle;
+    protected bool allInOneMode = true;
 
     private const string DEFAULT_CHART_TITLE = "Chart";
@@ -54,11 +62,29 @@
         if (VariableIsDisplayed(variableName)) {
           dataTable.Rows.Remove(variableName);
+          dataTablePerVariable.Remove(dataTablePerVariable.Find( x => (x.Name == variableName)));
         } else {
-          dataTable.Rows.Add(logic.CreateDataRow(variableName, chartType));
+          DataRow row = GetDataRow(variableName);
+          //row.VisualProperties.Color = GetRandomColor();
+          dataTable.Rows.Add(row);
+          DataTable d = new DataTable(variableName);
+          d.Rows.Add(row);
+          dataTablePerVariable.Add(d);
         }
       }
-    }
-
-    public bool VariableIsDisplayed(string name) {
+
+      if (!allInOneMode)
+        GenerateChart();
+
+    }
+
+    private Color GetRandomColor() {
+      Random randomGen = new Random();
+      KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
+      KnownColor randomColorName = names[randomGen.Next(names.Length)];
+      Color randomColor = Color.FromKnownColor(randomColorName);
+      return randomColor;
+    }
+
+    private bool VariableIsDisplayed(string name) {
 
       foreach (var item in dataTable.Rows) {
@@ -69,14 +95,4 @@
     }
 
-    private void InitDataTable() {
-      dataTable = logic.CreateDataTable(chartTitle, chartType);
-      dataTableView.Content = dataTable;
-    }
-
-    private void InitVariablesListBox() {
-      variableItemList = logic.CreateVariableItemList();
-      checkedItemList.Content = variableItemList;
-    }
-
     protected override void RegisterContentEvents() {
       base.RegisterContentEvents();
@@ -96,4 +112,33 @@
       get { return (PreprocessingChartContent)base.Content; }
       set { base.Content = value; }
+    }
+
+    private void InitData() {
+      variableItemList = logic.CreateVariableItemList();
+      dataRows = logic.CreateAllDataRows(chartType);
+
+      // init data table
+      dataTable = new DataTable(chartTitle);
+      dataTable.Rows.AddRange(dataRows);
+
+      // init data table per variable
+      dataTablePerVariable = new List<DataTable>();
+      foreach(var checkedItem in variableItemList.CheckedItems)  {
+        string variableName = variableItemList[checkedItem.Index].Value;
+        DataTable d = new DataTable(variableName);
+        DataRow row = GetDataRow(variableName);
+        //row.VisualProperties.Color = GetRandomColor();
+        d.Rows.Add(row);
+        dataTablePerVariable.Add(d);
+      }
+
+    }
+
+    private DataRow GetDataRow(string variableName) {
+      foreach (DataRow row in dataRows) {
+        if (row.Name == variableName)
+          return row;
+      }
+      return null;
     }
 
@@ -102,6 +147,7 @@
       if (Content != null) {
         logic = Content.ChartLogic;
-        InitDataTable();
-        InitVariablesListBox();
+        InitData();
+        checkedItemList.Content = variableItemList;
+        GenerateChart();
         variableItemList.CheckedItemsChanged += CheckedItemsChanged;
         logic.Changed += PreprocessingData_Changed;
@@ -109,23 +155,31 @@
     }
 
-    //TODO: refactor: possible code duplication with HistogramLogic
+    // TODO : handle also other changed events
     void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
       switch (e.Type) {
         case DataPreprocessingChangedEventType.DeleteColumn:
-          RemoveVariable(logic.GetVariableNameByIndex(e.Column));
+          //RemoveVariable(logic.GetVariableNameByIndex(e.Column));
           break;
         case DataPreprocessingChangedEventType.AddColumn:
-          AddVariable(logic.GetVariableNameByIndex(e.Column));
+          //AddVariable(logic.GetVariableNameByIndex(e.Column));
           break;
         case DataPreprocessingChangedEventType.ChangeColumn:
         case DataPreprocessingChangedEventType.ChangeItem:
-          dataTable.Rows.Remove(logic.GetVariableNameByIndex(e.Column));
-          dataTable.Rows.Add(logic.CreateDataRow(logic.GetVariableNameByIndex(e.Column), chartType));
+          //UpdateDataForVariable(logic.GetVariableNameByIndex(e.Column));
+
           break;
         case DataPreprocessingChangedEventType.DeleteRow:
         case DataPreprocessingChangedEventType.AddRow:
-          InitDataTable();
           break;
       }
+    }
+
+    private void UpdateDataForVariable(string variableName) {
+      DataRow newRow = logic.CreateDataRow(variableName, chartType);
+      dataTable.Rows.Remove(variableName);
+      dataTable.Rows.Add(newRow);
+      DataTable dt = dataTablePerVariable.Find(x => (x.Rows.ItemName == variableName));
+      dt.Rows.Remove(variableName);
+      dt.Rows.Add(newRow);
     }
 
@@ -154,4 +208,115 @@
     }
 
+    protected void GenerateChart() {
+
+      //Clear out the existing controls
+      tableLayoutPanel.Controls.Clear();
+
+      //Clear out the existing row and column styles
+      tableLayoutPanel.ColumnStyles.Clear();
+      tableLayoutPanel.RowStyles.Clear();
+      tableLayoutPanel.AutoScroll = false;
+      tableLayoutPanel.AutoScroll = true;    
+
+      if (allInOneMode) {
+        GenerateSingleChartLayout();
+      } else
+        GenerateMultiChartLayout();
+    }
+
+    private void GenerateSingleChartLayout() {
+      tableLayoutPanel.ColumnCount = 1;
+      tableLayoutPanel.RowCount = 1;
+      tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
+      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
+      tableLayoutPanel.Controls.Add(dataTableView, 0, 0);
+      dataTableView.Content = dataTable;
+    }
+
+    private void GenerateMultiChartLayout() {
+      int checkedItemsCnt = 0;
+      foreach(var item in variableItemList.CheckedItems)
+        checkedItemsCnt++;
+
+      int rows = 0;
+      int columns = 0;
+
+      // set columns and rows based on number of items
+      if (checkedItemsCnt <= 2)
+        columns = 1;
+      else if (checkedItemsCnt <= 6)
+        columns = 2;
+      else
+        columns = 3;
+
+      if (columns == 3)
+        rows = (checkedItemsCnt+2) / columns;
+      else if(columns == 2)
+        rows = (checkedItemsCnt + 1) / columns;
+      else
+        rows = checkedItemsCnt / columns;
+
+      //Now we will generate the table, setting up the row and column counts first
+      tableLayoutPanel.ColumnCount = columns;
+      tableLayoutPanel.RowCount = rows;
+
+      List<DataTable>.Enumerator enumerator = dataTablePerVariable.GetEnumerator();
+      for (int x = 0; x < columns; x++) {
+        
+        if(rows <= 3)
+          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / columns));
+        else
+          //scrollbar is shown if there are more than 3 rows -> remove scroll bar width from total width
+          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth) / columns));
+        for (int y = 0; y < rows; y++) {
+          //Add a row only when creating the first column
+          if (x == 0) {
+            // fixed chart size when there are more than 3 tables
+            if (rows > 3)
+              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 300));
+            else
+              tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / rows));
+          }
+
+          DataTableView dataView = new DataTableView();
+          enumerator.MoveNext();
+          DataTable d = enumerator.Current;
+          if (d == null) {
+            // dummy panel for empty field 
+            Panel p = new Panel();
+            p.Dock = DockStyle.Fill;
+            tableLayoutPanel.Controls.Add(p, y, x);
+          }
+          else {
+            dataView.Content = d;
+            dataView.Dock = DockStyle.Fill;
+            tableLayoutPanel.Controls.Add(dataView, y, x);
+          }
+        }
+      }
+    }
+
+    //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);
+      }
+    }
+
+    private void radioButton1_CheckedChanged(object sender, EventArgs e) {
+      if (allInOneMode == false)
+        allInOneMode = true;
+      else
+        allInOneMode = false;
+      GenerateChart();
+    }
+
+
+
   }
 }
