Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/22/16 12:42:09 (7 years ago)
Author:
pfleck
Message:

#2709 Added an option for the preprocessing scatterplot to use a color gradient instead of the chart color palette.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotSingleView.cs

    r14514 r14521  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    2526using HeuristicLab.Analysis;
     27using HeuristicLab.Common;
    2628using HeuristicLab.Core.Views;
    2729using HeuristicLab.MainForm;
     30using HeuristicLab.MainForm.WindowsForms;
    2831using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType;
    2932
     
    3336  [Content(typeof(SingleScatterPlotContent), true)]
    3437  public partial class ScatterPlotSingleView : ItemView {
     38
     39    private readonly string NoGroupItem = "-";
    3540
    3641    public new SingleScatterPlotContent Content {
     
    5560      comboBoxXVariable.Items.AddRange(variables.ToArray());
    5661      comboBoxYVariable.Items.AddRange(variables.ToArray());
    57       comboBoxGroup.Items.Add("-");
     62      comboBoxGroup.Items.Add(NoGroupItem);
    5863      for (int i = 0; i < Content.PreprocessingData.Columns; ++i) {
    5964        if (Content.PreprocessingData.VariableHasType<double>(i)) {
    60           double distinctValueCount = Content.PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
    61           if (distinctValueCount <= 20)
    62             comboBoxGroup.Items.Add(Content.PreprocessingData.GetVariableName(i));
     65          //double distinctValueCount = Content.PreprocessingData.GetValues<double>(i).GroupBy(x => x).Count();
     66          //if (distinctValueCount <= 20)
     67          comboBoxGroup.Items.Add(Content.PreprocessingData.GetVariableName(i));
    6368        }
    6469      }
     
    8691    }
    8792
     93    protected override void SetEnabledStateOfControls() {
     94      base.SetEnabledStateOfControls();
     95      useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem;
     96      gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked; ;
     97    }
     98
    8899    private void UpdateScatterPlot() {
    89100      if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxGroup.SelectedItem != null) {
     
    91102        var yVariable = (string)comboBoxYVariable.SelectedItem;
    92103        var groupVariable = (string)comboBoxGroup.SelectedItem;
     104
     105        bool groupingActive = useGradientCheckBox.Checked && groupVariable != NoGroupItem;
     106        double min = 0, max = 1;
     107        if (groupingActive) {
     108          var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable))
     109            .Distinct().OrderBy(x => x).ToList();
     110          min = groupValues.First();
     111          max = groupValues.Last();
     112        }
    93113        ScatterPlot scatterPlot = Content.CreateScatterPlot(xVariable, yVariable, groupVariable);
     114        var rows = scatterPlot.Rows.ToList();
     115        scatterPlot.Rows.Clear();
    94116        var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
    95117        int order = (int)polynomialRegressionOrderNumericUpDown.Value;
    96         foreach (var row in scatterPlot.Rows) {
     118        foreach (var row in rows) {
    97119          row.VisualProperties.PointSize = 6;
    98120          row.VisualProperties.IsRegressionVisibleInLegend = false;
    99121          row.VisualProperties.RegressionType = regressionType;
    100122          row.VisualProperties.PolynomialRegressionOrder = order;
    101         }
     123          row.VisualProperties.IsVisibleInLegend = !useGradientCheckBox.Checked;
     124          if (groupingActive)
     125            row.VisualProperties.Color = GetColor(double.Parse(row.Name), min, max);
     126        }
     127        scatterPlot.Rows.AddRange(rows);
    102128        var vp = scatterPlot.VisualProperties;
    103129        vp.Title = string.Empty;
     
    106132
    107133        scatterPlotControl.Content = scatterPlot;
     134
     135        if (groupingActive) {
     136          gradientMinimumLabel.Text = min.ToString("G5");
     137          gradientMaximumLabel.Text = max.ToString("G5");
     138        }
    108139
    109140        //save selected x and y variable in content
     
    152183
    153184    private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) {
     185      useGradientCheckBox.Enabled = (string)comboBoxGroup.SelectedItem != NoGroupItem;
     186      gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked;
    154187      UpdateScatterPlot();
    155188    }
     
    159192      var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
    160193      polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
     194
    161195      UpdateRegressionLine();
    162196    }
     
    170204      int order = (int)polynomialRegressionOrderNumericUpDown.Value;
    171205
    172       foreach (var row in scatterPlotControl.Content.Rows) {
     206      var rows = scatterPlotControl.Content.Rows.ToList();
     207      scatterPlotControl.Content.Rows.Clear();
     208      foreach (var row in rows) {
    173209        row.VisualProperties.IsRegressionVisibleInLegend = false;
    174210        row.VisualProperties.RegressionType = regressionType;
    175211        row.VisualProperties.PolynomialRegressionOrder = order;
    176212      }
     213      scatterPlotControl.Content.Rows.AddRange(rows);
    177214    }
    178215    #endregion
     216
     217    private void useGradientCheckBox_CheckedChanged(object sender, EventArgs e) {
     218      gradientPanel.Visible = useGradientCheckBox.Enabled && useGradientCheckBox.Checked;
     219
     220      // remove rows and re-add them later to avoid firing visual property changd events
     221      var rows = scatterPlotControl.Content.Rows.ToDictionary(r => r.Name, r => r);
     222      scatterPlotControl.Content.Rows.Clear();
     223
     224      if (useGradientCheckBox.Checked) {
     225        var groupVariable = (string)comboBoxGroup.SelectedItem;
     226        if (groupVariable == NoGroupItem) return;
     227
     228        var groupValues = Content.PreprocessingData.GetValues<double>(Content.PreprocessingData.GetColumnIndex(groupVariable))
     229          .Distinct().OrderBy(x => x).ToList();
     230        double min = groupValues.First(), max = groupValues.Last();
     231        foreach (var group in groupValues) {
     232          ScatterPlotDataRow row;
     233          if (rows.TryGetValue(group.ToString("R"), out row)) {
     234            row.VisualProperties.Color = GetColor(group, min, max);
     235            row.VisualProperties.IsVisibleInLegend = false;
     236          }
     237        }
     238        gradientMinimumLabel.Text = min.ToString("G5");
     239        gradientMaximumLabel.Text = max.ToString("G5");
     240      } else {
     241        foreach (var row in rows.Values) {
     242          row.VisualProperties.Color = Color.Empty;
     243          row.VisualProperties.IsVisibleInLegend = true;
     244        }
     245      }
     246      scatterPlotControl.Content.Rows.AddRange(rows.Values);
     247    }
     248
     249    private static Color GetColor(double value, double min, double max) {
     250      if (double.IsNaN(value)) {
     251        return Color.DarkGray;
     252      }
     253      var colors = ColorGradient.Colors;
     254      int index = (int)((colors.Count - 1) * (value - min) / (max - min));
     255      if (index >= colors.Count) index = colors.Count - 1;
     256      if (index < 0) index = 0;
     257      return colors[index];
     258    }
     259
     260    private void BatchRowUpdate(Action<ScatterPlotDataRow> rowAction) {
     261      var scatterPlot = scatterPlotControl.Content;
     262      var rows = scatterPlot.Rows.ToList();
     263      // remove rows and re-add them later to avoid firing visual property changd events
     264      scatterPlot.Rows.Clear();
     265      foreach (var row in rows) {
     266        rowAction(row);
     267      }
     268      scatterPlot.Rows.AddRange(rows);
     269    }
    179270  }
    180271}
Note: See TracChangeset for help on using the changeset viewer.