Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2592 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
RevLine 
[1325]1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
[2040]5  public interface IAggregator {
6    void AddWatch(IDataRow dataRow);
7    void RemoveWatch(IDataRow dataRow);
8  }
9
10  public class AvgAggregator : DataRowBase, IAggregator {
[1325]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
[1607]29    private readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
[1350]30    private double curAvgValue;
[1325]31
[1350]32    private void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
[1605]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
[1325]48    }
49
[1350]50    private void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
[1325]51      for (int i = 0; i < values.Length; i++) {
[1605]52        refreshValue();
[1325]53      }
54    }
55
[1350]56    private void dataRow_DataRowChanged(IDataRow row) {
[1605]57      refreshValue();
[1325]58    }
59
[1605]60    private int count;
61   
62    public AvgAggregator() {
63      curAvgValue = 0;
64      count = 0;
[2040]65      this.RowSettings.LineType = DataRowType.SingleValue;
[1605]66    }
[1325]67
[1605]68    private void refreshValue() {
[1325]69      curAvgValue = double.MinValue;
70      double tmpSum = 0;
[1605]71      count = 0;
[1350]72      foreach (IDataRow rows in dataRowWatches) {
[1325]73        for (int i = 0; i < rows.Count; i++) {
74          tmpSum += rows[i];
75          count++;
76        }
77      }
[1605]78      if (count == 0) curAvgValue = 0;
79      else curAvgValue = tmpSum / count;
[1325]80      OnValueChanged(curAvgValue, 0, Action.Modified);
81    }
[1605]82    private void refreshValue(double newVal, Action action) {
83      double temp = curAvgValue * count;
[1325]84
[1605]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
[1325]104    #region IDataRow Members
105
[1350]106    public override void AddValue(double value) {
[1325]107      throw new NotSupportedException();
108    }
109
[1350]110    public override void AddValue(double value, int index) {
[1325]111      throw new NotSupportedException();
112    }
113
[1350]114    public override void AddValues(double[] values) {
[1325]115      throw new NotSupportedException();
116    }
117
[1350]118    public override void AddValues(double[] values, int index) {
[1325]119      throw new NotSupportedException();
120    }
121
[1350]122    public override void ModifyValue(double value, int index) {
[1325]123      throw new NotSupportedException();
124    }
125
[1350]126    public override void ModifyValues(double[] values, int index) {
[1325]127      throw new NotSupportedException();
128    }
129
[1350]130    public override void RemoveValue(int index) {
[1325]131      throw new NotSupportedException();
132    }
133
[1607]134    public override void RemoveValues(int index, int countVals) {
[1325]135      throw new NotSupportedException();
136    }
137
[1350]138    public override int Count {
[1325]139      get { return 1; }
140    }
141
[1350]142    public override double this[int index] {
143      get { return curAvgValue; }
144      set { throw new NotSupportedException(); }
[1325]145    }
146
[1350]147    public override double MinValue {
148      get { return curAvgValue; }
[1327]149    }
150
[1350]151    public override double MaxValue {
152      get { return curAvgValue; }
[1327]153    }
154
[1325]155    #endregion
156  }
[1350]157}
Note: See TracBrowser for help on using the repository browser.