Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Visualization/3.2/AvgLineAggregator.cs @ 6004

Last change on this file since 6004 was 1818, checked in by cbahner, 16 years ago

#519 added floating average

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