Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/AvgLineAggregator.cs @ 3188

Last change on this file since 3188 was 2040, checked in by dwagner, 15 years ago

dw: Added new dialog for adding lines using the options dialog. (#478)

File size: 4.3 KB
RevLine 
[1325]1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
[2040]5  public class AvgLineAggregator : DataRowBase, IAggregator {
[1325]6
7    private readonly List<double> dataRow = new List<double>();
[1607]8    readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
[1325]9
10    #region IAggregator Members
11
[1607]12    public void AddWatch(IDataRow watchDataRow) {
13      dataRowWatches.Add(watchDataRow);
14      watchDataRow.ValueChanged += dataRow_ValueChanged;
15      watchDataRow.ValuesChanged += dataRow_ValuesChanged;
16      watchDataRow.DataRowChanged += dataRow_DataRowChanged;
[1325]17    }
18
[1607]19    public void RemoveWatch(IDataRow watchDataRow) {
20      dataRowWatches.Remove(watchDataRow);
21      watchDataRow.DataRowChanged -= dataRow_DataRowChanged;
22      watchDataRow.ValuesChanged -= dataRow_ValuesChanged;
23      watchDataRow.ValueChanged -= dataRow_ValueChanged;
[1325]24    }
25
26    #endregion
27
28    void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
[1605]29      switch (action) {
30        case Action.Added:
[1607]31          refreshLastValues(row);
[1605]32          break;
33        case Action.Modified:
34          refreshValue();
35          break;
36        case Action.Deleted:
[1607]37          refreshLastValues(row);
[1605]38          break;
39        default:
40          throw new ArgumentOutOfRangeException("action");
41      }
[1325]42    }
43
44    void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
[1818]45//      for (int i = 0; i < values.Length; i++) {
46//        refreshValue();
47//      }
48      refreshValue();
[1325]49    }
50
51    void dataRow_DataRowChanged(IDataRow row) {
[1605]52      refreshValue();
[1325]53    }
54
[1605]55    private void refreshValue() {
[1607]56     
[1325]57      int count = dataRowWatches.Count;
58
59      IDataRow firstRow = dataRowWatches[0];
60      int count1 = firstRow.Count;
[1607]61      Console.WriteLine("count: " + count1);
[1325]62
63      dataRow.Clear();
64
65      if (dataRowWatches.Count >= 2) {
66        for (int i = 0; i < count1; i++) {
[1607]67          double tmpSum = 0;
[1325]68          for (int j = 0; j < count; j++) {
69            if (dataRowWatches[j].Count > i) {
70              tmpSum += dataRowWatches[j][i];
71            }
72          }
73
[1607]74          dataRow.Add(tmpSum / count);
[1325]75          OnValueChanged(tmpSum / count, dataRow.Count - 1, Action.Added);
76        }
77      }
[1605]78    }
[1325]79
[1607]80    private void refreshLastValues(IDataRow row) {
[1605]81
82      int index = row.Count - 1;
83      double curAvg = 0;
84
85      foreach (IDataRow watch in dataRowWatches) {
86        if (watch.Count >= index +1) {
[1607]87          curAvg += watch[index];
[1605]88        }
89      }
90
91      if (dataRowWatches.Count > 0)
92        curAvg /= dataRowWatches.Count;
93
94
95      if (dataRow.Count <= index) {
96        dataRow.Add(curAvg);
97        OnValueChanged(curAvg, dataRow.Count - 1, Action.Added);     
98      }
99      else {
100        dataRow[index] = curAvg;
101        OnValueChanged(curAvg, dataRow.Count - 1, Action.Modified); 
102      }
[1325]103    }
104
105    #region IDataRow Members
106
[1350]107    public override void AddValue(double value) {
[1325]108      dataRow.Add(value);
109      OnValueChanged(value, dataRow.Count - 1, Action.Added);
110    }
111
[1350]112    public override void AddValue(double value, int index) {
[1607]113      throw new NotSupportedException();
[1325]114    }
115
[1350]116    public override void AddValues(double[] values) {
[1325]117      throw new NotSupportedException();
118    }
119
[1350]120    public override void AddValues(double[] values, int index) {
[1325]121      throw new NotSupportedException();
122    }
123
[1350]124    public override void ModifyValue(double value, int index) {
[1325]125      throw new NotSupportedException();
126    }
127
[1350]128    public override void ModifyValues(double[] values, int index) {
[1325]129      throw new NotSupportedException();
130    }
131
[1350]132    public override void RemoveValue(int index) {
[1325]133      throw new NotSupportedException();
134    }
135
[1607]136    public override void RemoveValues(int index, int countVals) {
[1325]137      throw new NotSupportedException();
138    }
139
[1350]140    public override int Count {
[1383]141      get { return dataRow.Count; } //return dataRowWatches.Count; }
[1325]142    }
143
[1350]144    public override double this[int index] {
[1325]145      get { return dataRow[index]; }
146      set {
147        dataRow[index] = value;
148        OnValueChanged(value, index, Action.Modified);
149      }
150    }
151
[1350]152    public override double MinValue {
153      get { return 0; }
[1327]154    }
155
[1350]156    public override double MaxValue {
157      get { return 0; }
[1327]158    }
159
[1325]160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.