Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12165


Ignore:
Timestamp:
03/09/15 15:52:59 (9 years ago)
Author:
ehopf
Message:

#2335: Encapsulated sort column and statistics generation behavior in StringConvertibleMatrixView.cs. Additionally fixed the statistic measures regarding missing values.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessingImprovements/HeuristicLab.Data.Views/3.3/StringConvertibleMatrixView.cs

    r12151 r12165  
    295295    }
    296296
    297     private void dataGridView_KeyDown(object sender, KeyEventArgs e) {
     297    protected virtual void dataGridView_KeyDown(object sender, KeyEventArgs e) {
    298298      if (!ReadOnly && e.Control && e.KeyCode == Keys.V)
    299299        PasteValuesToDataGridView();
     
    398398    }
    399399
    400     private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
     400    protected virtual void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
    401401      if (Content != null) {
    402402        if (e.Button == MouseButtons.Left && Content.SortableView) {
    403           bool addToSortedIndices = (Control.ModifierKeys & Keys.Control) == Keys.Control;
    404           SortOrder newSortOrder = SortOrder.Ascending;
    405           if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
    406             SortOrder oldSortOrder = sortedColumnIndices.Where(x => x.Key == e.ColumnIndex).First().Value;
    407             int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
    408             newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
    409           }
    410 
    411           if (!addToSortedIndices)
    412             sortedColumnIndices.Clear();
    413 
    414           if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
    415             int sortedIndex = sortedColumnIndices.FindIndex(x => x.Key == e.ColumnIndex);
    416             if (newSortOrder != SortOrder.None)
    417               sortedColumnIndices[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
    418             else
    419               sortedColumnIndices.RemoveAt(sortedIndex);
    420           } else
    421             if (newSortOrder != SortOrder.None)
    422               sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
    423           Sort();
     403          SortColumn(e.ColumnIndex);
    424404        }
    425405      }
     
    438418      dataGridView.Invalidate();
    439419    }
     420
     421    protected virtual void SortColumn(int columnIndex) {
     422      bool addToSortedIndices = (Control.ModifierKeys & Keys.Control) == Keys.Control;
     423      SortOrder newSortOrder = SortOrder.Ascending;
     424      if (sortedColumnIndices.Any(x => x.Key == columnIndex)) {
     425        SortOrder oldSortOrder = sortedColumnIndices.Where(x => x.Key == columnIndex).First().Value;
     426        int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
     427        newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
     428      }
     429
     430      if (!addToSortedIndices)
     431        sortedColumnIndices.Clear();
     432
     433      if (sortedColumnIndices.Any(x => x.Key == columnIndex)) {
     434        int sortedIndex = sortedColumnIndices.FindIndex(x => x.Key == columnIndex);
     435        if (newSortOrder != SortOrder.None)
     436          sortedColumnIndices[sortedIndex] = new KeyValuePair<int, SortOrder>(columnIndex, newSortOrder);
     437        else
     438          sortedColumnIndices.RemoveAt(sortedIndex);
     439      } else
     440        if (newSortOrder != SortOrder.None)
     441          sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(columnIndex, newSortOrder));
     442      Sort();
     443    }
     444
    440445    protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
    441446      int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
     
    510515    }
    511516
    512     private void dataGridView_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
     517    protected virtual void dataGridView_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {
    513518      if (Content == null) return;
    514519      if (e.Button == MouseButtons.Right && Content.ColumnNames.Count() != 0)
     
    543548
    544549    protected virtual void dataGridView_SelectionChanged(object sender, EventArgs e) {
    545       string stringFormat = "{0,20:0.0000}";
    546550      statisticsTextBox.Text = string.Empty;
    547551      if (dataGridView.SelectedCells.Count > 1) {
     
    553557        }
    554558        if (selectedValues.Count > 1) {
    555           StringBuilder labelText = new StringBuilder();
    556           labelText.Append("Count: " + string.Format(stringFormat, selectedValues.Count) + "    ");
    557           labelText.Append("Sum: " + string.Format(stringFormat, selectedValues.Sum()) + "    ");
    558           labelText.Append("Min: " + string.Format(stringFormat, selectedValues.Min()) + "    ");
    559           labelText.Append("Max: " + string.Format(stringFormat, selectedValues.Max()) + "    ");
    560           labelText.Append("Average: " + string.Format(stringFormat, selectedValues.Average()) + "    ");
    561           labelText.Append("Standard Deviation: " + string.Format(stringFormat, selectedValues.StandardDeviation()) + "    ");
    562 
    563           statisticsTextBox.Text = labelText.ToString();
    564         }
    565       }
     559          statisticsTextBox.Text = CreateStatisticsText(selectedValues);
     560        }
     561      }
     562    }
     563
     564    protected virtual string CreateStatisticsText(ICollection<double> values) {
     565      string stringFormat = "{0,20:0.0000}";
     566      int overallCount = values.Count;
     567      values = values.Where(x => !double.IsNaN(x)).ToList();
     568      StringBuilder statisticsText = new StringBuilder();
     569      statisticsText.Append("Count: " + values.Count + "    ");
     570      statisticsText.Append("Sum: " + string.Format(stringFormat, values.Sum()) + "    ");
     571      statisticsText.Append("Min: " + string.Format(stringFormat, values.Min()) + "    ");
     572      statisticsText.Append("Max: " + string.Format(stringFormat, values.Max()) + "    ");
     573      statisticsText.Append("Average: " + string.Format(stringFormat, values.Average()) + "    ");
     574      statisticsText.Append("Standard Deviation: " + string.Format(stringFormat, values.StandardDeviation()) + "    ");
     575      if (overallCount > 0)
     576        statisticsText.Append("Missing Values: " + string.Format(stringFormat, ((overallCount - values.Count) / (double)overallCount) * 100) + "%    ");
     577      return statisticsText.ToString();
    566578    }
    567579  }
Note: See TracChangeset for help on using the changeset viewer.