Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/03/16 17:21:44 (9 years ago)
Author:
bburlacu
Message:

#2597: Simplify code and ensure charts are sorted according to the variable order in the list view.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs

    r13820 r13828  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    3132  [Content(typeof(IRegressionSolution))]
    3233  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
    33     private ModifiableDataset internalDataset;
    3434    private Dictionary<string, GradientChart> charts;
    35 
    3635    private const int Points = 1000;
    3736
     
    6362      var pd = Content.ProblemData;
    6463      var ds = pd.Dataset;
    65 
    66       // update variable list
    67       variableListView.Items.Clear();
    68       variableListView.Items.AddRange(pd.AllowedInputVariables.Select(x => new ListViewItem(x, 0)).ToArray());
    69 
    70       // update internal dataset
    71       var variableNames = pd.AllowedInputVariables.ToList();
     64      // create dataset
     65      var variableNames = Content.GetUsedVariablesForPrediction().ToList();
    7266      var variableValues = variableNames.Select(x => new List<double> { pd.Dataset.GetDoubleValues(x, pd.TrainingIndices).Median() });
    73       internalDataset = new ModifiableDataset(variableNames, variableValues);
    74       // update charts and variable limits
     67      var dataset = new ModifiableDataset(variableNames, variableValues);
     68      // create charts
    7569      charts = new Dictionary<string, GradientChart>();
    76       foreach (var x in pd.AllowedInputVariables) {
     70      foreach (var x in variableNames) {
    7771        double min = 0, max = 0;
    7872        foreach (var v in ds.GetDoubleValues(x, pd.TrainingIndices)) {
     
    9488          }
    9589        };
    96         gradientChart.Configure(new[] { Content }, pd, internalDataset, x, min, max, Points);
     90        gradientChart.Configure(new[] { Content }, pd, dataset, x, min, max, Points);
    9791        charts[x] = gradientChart;
    9892      }
     93      // update variable list
     94      variableListView.Items.Clear();
     95      variableListView.Items.AddRange(variableNames.Select(x => new ListViewItem(x, 0)).ToArray());
     96    }
     97    // sort chart controls so that they always appear in the same order as in the list view
     98    // the gradient chart layout should be suspended before calling this method
     99    private void SortControls() {
     100      var tl = gradientChartTableLayout;
     101      var indices = variableListView.CheckedItems.Cast<ListViewItem>().ToDictionary(checkedItem => checkedItem.Text, checkedItem => checkedItem.Index);
     102      var count = tl.Controls.Count;
     103      var controls = new GradientChart[count];
     104      for (int i = 0; i < count; ++i) controls[i] = (GradientChart)tl.Controls[i];
     105      Array.Sort(controls, (a, b) => indices[a.Variable].CompareTo(indices[b.Variable]));
     106      tl.Controls.Clear();
     107      tl.Controls.AddRange(controls);
    99108    }
    100109
     
    109118      var rowHeight = 0.8f * columnWidth;
    110119      tl.RowStyles.Clear();
    111 
    112120      if (item.Checked) {
    113121        tl.Controls.Add(chart);
    114         var index = tl.Controls.Count - 1;
    115         int rowIndex = index / tl.ColumnCount;
    116         int columnIndex = index % tl.ColumnCount;
    117         tl.SetRow(chart, rowIndex);
    118         tl.SetColumn(chart, columnIndex);
    119122        chart.UpdateChart();
    120         tl.RowCount = rowIndex + 1;
    121123      } else {
    122124        tl.Controls.Remove(chart);
    123         // relayout
    124         var count = tl.Controls.Count;
    125         tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
    126         for (int i = 0; i < count; ++i) {
    127           var control = tl.Controls[i];
    128           int rowIndex = i / tl.ColumnCount;
    129           int columnIndex = i % tl.ColumnCount;
    130           tl.SetRow(control, rowIndex);
    131           tl.SetColumn(control, columnIndex);
    132         }
    133125      }
    134       // set absolute row sizes
     126      var count = tl.Controls.Count;
     127      SortControls();
     128      tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
     129      for (int i = 0; i < count; ++i) {
     130        var control = tl.Controls[i];
     131        int rowIndex = i / tl.ColumnCount;
     132        int columnIndex = i % tl.ColumnCount;
     133        tl.SetRow(control, rowIndex);
     134        tl.SetColumn(control, columnIndex);
     135      }
    135136      for (int i = 0; i < tl.RowCount; ++i) {
    136137        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
Note: See TracChangeset for help on using the changeset viewer.