1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Xml;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Visualization {
|
---|
6 | public class FloatingAvgAggregator : DataRowBase {
|
---|
7 |
|
---|
8 | private readonly List<double> dataRow = new List<double>();
|
---|
9 | readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
|
---|
10 |
|
---|
11 | #region IAggregator Members
|
---|
12 |
|
---|
13 | public void AddWatch(IDataRow watchDataRow) {
|
---|
14 | dataRowWatches.Add(watchDataRow);
|
---|
15 | watchDataRow.ValueChanged += dataRow_ValueChanged;
|
---|
16 | watchDataRow.ValuesChanged += dataRow_ValuesChanged;
|
---|
17 | watchDataRow.DataRowChanged += dataRow_DataRowChanged;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public void RemoveWatch(IDataRow watchDataRow) {
|
---|
21 | dataRowWatches.Remove(watchDataRow);
|
---|
22 | watchDataRow.DataRowChanged -= dataRow_DataRowChanged;
|
---|
23 | watchDataRow.ValuesChanged -= dataRow_ValuesChanged;
|
---|
24 | watchDataRow.ValueChanged -= dataRow_ValueChanged;
|
---|
25 | }
|
---|
26 |
|
---|
27 | #endregion
|
---|
28 |
|
---|
29 | void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
|
---|
30 | switch (action) {
|
---|
31 | case Action.Added:
|
---|
32 | refreshLastValues(row);
|
---|
33 | break;
|
---|
34 | case Action.Modified:
|
---|
35 | refreshValue();
|
---|
36 | break;
|
---|
37 | case Action.Deleted:
|
---|
38 | //refreshLastValues(row);
|
---|
39 | refreshValue();
|
---|
40 | break;
|
---|
41 | default:
|
---|
42 | throw new ArgumentOutOfRangeException("action");
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
|
---|
47 | refreshValue();
|
---|
48 | }
|
---|
49 |
|
---|
50 | void dataRow_DataRowChanged(IDataRow row) {
|
---|
51 | refreshValue();
|
---|
52 | }
|
---|
53 |
|
---|
54 | private int area = 5;
|
---|
55 |
|
---|
56 | public int Area{
|
---|
57 | get { return area; }
|
---|
58 | set { area = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private void refreshValue() {
|
---|
62 |
|
---|
63 | if (dataRowWatches.Count >= 1) {
|
---|
64 | IDataRow watchedRow = dataRowWatches[0];
|
---|
65 |
|
---|
66 | dataRow.Clear();
|
---|
67 | OnDataRowChanged(this);
|
---|
68 |
|
---|
69 | for (int i = 0; i < watchedRow.Count; i++) {
|
---|
70 |
|
---|
71 | double avgVal = 0;
|
---|
72 | int count = 0;
|
---|
73 | for (int j = Math.Max(0, i-area); j < Math.Min(watchedRow.Count, i+area); j++) {
|
---|
74 | avgVal += watchedRow[j];
|
---|
75 | count++;
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (count >= 1)
|
---|
79 | avgVal /= count;
|
---|
80 |
|
---|
81 | dataRow.Add(avgVal);
|
---|
82 |
|
---|
83 | OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | //OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
|
---|
87 | OnValuesChanged(dataRow.ToArray(), 0, Action.Modified);
|
---|
88 | //OnDataRowChanged(this);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | private void refreshLastValues(IDataRow row) {
|
---|
93 | if (dataRowWatches.Count >= 1) {
|
---|
94 | IDataRow watchedRow = dataRowWatches[0];
|
---|
95 |
|
---|
96 | dataRow.Clear();
|
---|
97 |
|
---|
98 | for (int i = 0; i < watchedRow.Count; i++) {
|
---|
99 |
|
---|
100 | double avgVal = 0;
|
---|
101 | int count = 0;
|
---|
102 | for (int j = Math.Max(0, i - area); j < Math.Min(watchedRow.Count, i + area); j++) {
|
---|
103 | avgVal += watchedRow[j];
|
---|
104 | count++;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (count >= 1)
|
---|
108 | avgVal /= count;
|
---|
109 |
|
---|
110 | dataRow.Add(avgVal);
|
---|
111 |
|
---|
112 |
|
---|
113 | }
|
---|
114 | }
|
---|
115 | //OnValueChanged(avgVal, dataRow.Count - 1, Action.Added);
|
---|
116 | OnValueChanged(dataRow[dataRow.Count-1], dataRow.Count - 1, Action.Added);
|
---|
117 |
|
---|
118 | double[] changeVals = new double[Math.Min(area, dataRow.Count-1)];
|
---|
119 | for (int i = 0; i < Math.Min(area, dataRow.Count-1); i++)
|
---|
120 | {
|
---|
121 | changeVals[i] = dataRow[Math.Max(0, dataRow.Count - area - 1 + i)];
|
---|
122 | }
|
---|
123 | OnValuesChanged(changeVals, Math.Max(dataRow.Count-area-1, 0), Action.Modified);
|
---|
124 | //OnDataRowChanged(this);
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | #region IDataRow Members
|
---|
129 |
|
---|
130 | public override void AddValue(double value) {
|
---|
131 | throw new NotSupportedException();
|
---|
132 | // dataRow.Add(value);
|
---|
133 | // OnValueChanged(value, dataRow.Count - 1, Action.Added);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public override void AddValue(double value, int index) {
|
---|
137 | throw new NotSupportedException();
|
---|
138 | }
|
---|
139 |
|
---|
140 | public override void AddValues(double[] values) {
|
---|
141 | throw new NotSupportedException();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override void AddValues(double[] values, int index) {
|
---|
145 | throw new NotSupportedException();
|
---|
146 | }
|
---|
147 |
|
---|
148 | public override void ModifyValue(double value, int index) {
|
---|
149 | throw new NotSupportedException();
|
---|
150 | }
|
---|
151 |
|
---|
152 | public override void ModifyValues(double[] values, int index) {
|
---|
153 | throw new NotSupportedException();
|
---|
154 | }
|
---|
155 |
|
---|
156 | public override void RemoveValue(int index) {
|
---|
157 | throw new NotSupportedException();
|
---|
158 | }
|
---|
159 |
|
---|
160 | public override void RemoveValues(int index, int countVals) {
|
---|
161 | throw new NotSupportedException();
|
---|
162 | }
|
---|
163 |
|
---|
164 | public override int Count {
|
---|
165 | get { return dataRow.Count; } //return dataRowWatches.Count; }
|
---|
166 | }
|
---|
167 |
|
---|
168 | public override double this[int index] {
|
---|
169 | get { return dataRow[index]; }
|
---|
170 | set {
|
---|
171 | dataRow[index] = value;
|
---|
172 | OnValueChanged(value, index, Action.Modified);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public override double MinValue {
|
---|
177 | get { return 0; }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public override double MaxValue {
|
---|
181 | get { return 0; }
|
---|
182 | }
|
---|
183 |
|
---|
184 | public override XmlNode ToXml(IDataRow row, XmlDocument document)
|
---|
185 | {
|
---|
186 | throw new System.NotImplementedException();
|
---|
187 | }
|
---|
188 |
|
---|
189 | public override IDataRow FromXml(XmlNode xmlNode)
|
---|
190 | {
|
---|
191 | throw new System.NotImplementedException();
|
---|
192 | }
|
---|
193 |
|
---|
194 | #endregion
|
---|
195 | }
|
---|
196 | }
|
---|