Index: /branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs
===================================================================
--- /branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs	(revision 13827)
+++ /branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs	(revision 13828)
@@ -20,4 +20,5 @@
 #endregion
 
+using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -31,7 +32,5 @@
   [Content(typeof(IRegressionSolution))]
   public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
-    private ModifiableDataset internalDataset;
     private Dictionary<string, GradientChart> charts;
-
     private const int Points = 1000;
 
@@ -63,16 +62,11 @@
       var pd = Content.ProblemData;
       var ds = pd.Dataset;
-
-      // update variable list
-      variableListView.Items.Clear();
-      variableListView.Items.AddRange(pd.AllowedInputVariables.Select(x => new ListViewItem(x, 0)).ToArray());
-
-      // update internal dataset
-      var variableNames = pd.AllowedInputVariables.ToList();
+      // create dataset
+      var variableNames = Content.GetUsedVariablesForPrediction().ToList();
       var variableValues = variableNames.Select(x => new List<double> { pd.Dataset.GetDoubleValues(x, pd.TrainingIndices).Median() });
-      internalDataset = new ModifiableDataset(variableNames, variableValues);
-      // update charts and variable limits
+      var dataset = new ModifiableDataset(variableNames, variableValues);
+      // create charts
       charts = new Dictionary<string, GradientChart>();
-      foreach (var x in pd.AllowedInputVariables) {
+      foreach (var x in variableNames) {
         double min = 0, max = 0;
         foreach (var v in ds.GetDoubleValues(x, pd.TrainingIndices)) {
@@ -94,7 +88,22 @@
           }
         };
-        gradientChart.Configure(new[] { Content }, pd, internalDataset, x, min, max, Points);
+        gradientChart.Configure(new[] { Content }, pd, dataset, x, min, max, Points);
         charts[x] = gradientChart;
       }
+      // update variable list
+      variableListView.Items.Clear();
+      variableListView.Items.AddRange(variableNames.Select(x => new ListViewItem(x, 0)).ToArray());
+    }
+    // sort chart controls so that they always appear in the same order as in the list view
+    // the gradient chart layout should be suspended before calling this method
+    private void SortControls() {
+      var tl = gradientChartTableLayout;
+      var indices = variableListView.CheckedItems.Cast<ListViewItem>().ToDictionary(checkedItem => checkedItem.Text, checkedItem => checkedItem.Index);
+      var count = tl.Controls.Count;
+      var controls = new GradientChart[count];
+      for (int i = 0; i < count; ++i) controls[i] = (GradientChart)tl.Controls[i];
+      Array.Sort(controls, (a, b) => indices[a.Variable].CompareTo(indices[b.Variable]));
+      tl.Controls.Clear();
+      tl.Controls.AddRange(controls);
     }
 
@@ -109,28 +118,20 @@
       var rowHeight = 0.8f * columnWidth;
       tl.RowStyles.Clear();
-
       if (item.Checked) {
         tl.Controls.Add(chart);
-        var index = tl.Controls.Count - 1;
-        int rowIndex = index / tl.ColumnCount;
-        int columnIndex = index % tl.ColumnCount;
-        tl.SetRow(chart, rowIndex);
-        tl.SetColumn(chart, columnIndex);
         chart.UpdateChart();
-        tl.RowCount = rowIndex + 1;
       } else {
         tl.Controls.Remove(chart);
-        // relayout
-        var count = tl.Controls.Count;
-        tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
-        for (int i = 0; i < count; ++i) {
-          var control = tl.Controls[i];
-          int rowIndex = i / tl.ColumnCount;
-          int columnIndex = i % tl.ColumnCount;
-          tl.SetRow(control, rowIndex);
-          tl.SetColumn(control, columnIndex);
-        }
       }
-      // set absolute row sizes
+      var count = tl.Controls.Count;
+      SortControls();
+      tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
+      for (int i = 0; i < count; ++i) {
+        var control = tl.Controls[i];
+        int rowIndex = i / tl.ColumnCount;
+        int columnIndex = i % tl.ColumnCount;
+        tl.SetRow(control, rowIndex);
+        tl.SetColumn(control, columnIndex);
+      }
       for (int i = 0; i < tl.RowCount; ++i) {
         tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
