Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/AvgAggregator.cs @ 2479

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

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

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