Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1988 was 1988, checked in by cbahner, 15 years ago

#639 added color and y axis assignment to persistance

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