Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/FloatingAvgAggregator.cs @ 3075

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

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

File size: 5.1 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
5  public class FloatingAvgAggregator : DataRowBase, IAggregator {
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          refreshValue();
39          break;
40        default:
41          throw new ArgumentOutOfRangeException("action");
42      }
43    }
44
45    void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
46      refreshValue();
47    }
48
49    void dataRow_DataRowChanged(IDataRow row) {
50      refreshValue();
51    }
52
53    private int area = 5;
54
55    public int Area{
56      get { return area; }
57      set { area = value; }
58    }
59
60    private void refreshValue() {
61
62      if (dataRowWatches.Count >= 1) {
63        IDataRow watchedRow = dataRowWatches[0];
64       
65        dataRow.Clear();
66        OnDataRowChanged(this);
67
68        for (int i = 0; i < watchedRow.Count; i++) {
69
70          double avgVal = 0;
71          int count = 0;
72          for (int j = Math.Max(0, i-area); j < Math.Min(watchedRow.Count, i+area); j++) {
73            avgVal += watchedRow[j];
74            count++;
75          }
76
77          if (count >= 1)
78            avgVal /= count;
79
80          dataRow.Add(avgVal);
81
82          OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
83        }
84      }
85      //OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
86      OnValuesChanged(dataRow.ToArray(), 0, Action.Modified);
87      //OnDataRowChanged(this);                                     
88    }
89
90
91    private void refreshLastValues(IDataRow row) {
92      if (dataRowWatches.Count >= 1) {
93        IDataRow watchedRow = dataRowWatches[0];
94
95        dataRow.Clear();
96
97        for (int i = 0; i < watchedRow.Count; i++) {
98
99          double avgVal = 0;
100          int count = 0;
101          for (int j = Math.Max(0, i - area); j < Math.Min(watchedRow.Count, i + area); j++) {
102            avgVal += watchedRow[j];
103            count++;
104          }
105
106          if (count >= 1)
107            avgVal /= count;
108
109          dataRow.Add(avgVal);
110
111         
112        }
113      }
114      //OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
115      OnValueChanged(dataRow[dataRow.Count-1], dataRow.Count - 1, Action.Added);
116     
117      double[] changeVals = new double[Math.Min(area, dataRow.Count-1)];
118      for (int i = 0; i < Math.Min(area, dataRow.Count-1); i++)
119      {
120        changeVals[i] = dataRow[Math.Max(0, dataRow.Count - area - 1 + i)];
121      }
122      OnValuesChanged(changeVals, Math.Max(dataRow.Count-area-1, 0), Action.Modified);
123      //OnDataRowChanged(this);       
124     
125    }
126
127    #region IDataRow Members
128
129    public override void AddValue(double value) {
130      throw new NotSupportedException();
131//      dataRow.Add(value);
132//      OnValueChanged(value, dataRow.Count - 1, Action.Added);
133    }
134
135    public override void AddValue(double value, int index) {
136      throw new NotSupportedException();
137    }
138
139    public override void AddValues(double[] values) {
140      throw new NotSupportedException();
141    }
142
143    public override void AddValues(double[] values, int index) {
144      throw new NotSupportedException();
145    }
146
147    public override void ModifyValue(double value, int index) {
148      throw new NotSupportedException();
149    }
150
151    public override void ModifyValues(double[] values, int index) {
152      throw new NotSupportedException();
153    }
154
155    public override void RemoveValue(int index) {
156      throw new NotSupportedException();
157    }
158
159    public override void RemoveValues(int index, int countVals) {
160      throw new NotSupportedException();
161    }
162
163    public override int Count {
164      get { return dataRow.Count; } //return dataRowWatches.Count; }
165    }
166
167    public override double this[int index] {
168      get { return dataRow[index]; }
169      set {
170        dataRow[index] = value;
171        OnValueChanged(value, index, Action.Modified);
172      }
173    }
174
175    public override double MinValue {
176      get { return 0; }
177    }
178
179    public override double MaxValue {
180      get { return 0; }
181    }
182
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.