1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Visualization {
|
---|
5 | public class AvgLineAggregator : DataRowBase {
|
---|
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 | break;
|
---|
39 | default:
|
---|
40 | throw new ArgumentOutOfRangeException("action");
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
|
---|
45 | for (int i = 0; i < values.Length; i++) {
|
---|
46 | refreshValue();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | void dataRow_DataRowChanged(IDataRow row) {
|
---|
51 | refreshValue();
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void refreshValue() {
|
---|
55 |
|
---|
56 | int count = dataRowWatches.Count;
|
---|
57 |
|
---|
58 | IDataRow firstRow = dataRowWatches[0];
|
---|
59 | int count1 = firstRow.Count;
|
---|
60 | Console.WriteLine("count: " + count1);
|
---|
61 |
|
---|
62 | dataRow.Clear();
|
---|
63 |
|
---|
64 | if (dataRowWatches.Count >= 2) {
|
---|
65 | for (int i = 0; i < count1; i++) {
|
---|
66 | double tmpSum = 0;
|
---|
67 | for (int j = 0; j < count; j++) {
|
---|
68 | if (dataRowWatches[j].Count > i) {
|
---|
69 | tmpSum += dataRowWatches[j][i];
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | dataRow.Add(tmpSum / count);
|
---|
74 | OnValueChanged(tmpSum / count, dataRow.Count - 1, Action.Added);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void refreshLastValues(IDataRow row) {
|
---|
80 |
|
---|
81 | int index = row.Count - 1;
|
---|
82 | double curAvg = 0;
|
---|
83 |
|
---|
84 | foreach (IDataRow watch in dataRowWatches) {
|
---|
85 | if (watch.Count >= index +1) {
|
---|
86 | curAvg += watch[index];
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (dataRowWatches.Count > 0)
|
---|
91 | curAvg /= dataRowWatches.Count;
|
---|
92 |
|
---|
93 |
|
---|
94 | if (dataRow.Count <= index) {
|
---|
95 | dataRow.Add(curAvg);
|
---|
96 | OnValueChanged(curAvg, dataRow.Count - 1, Action.Added);
|
---|
97 | }
|
---|
98 | else {
|
---|
99 | dataRow[index] = curAvg;
|
---|
100 | OnValueChanged(curAvg, dataRow.Count - 1, Action.Modified);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | #region IDataRow Members
|
---|
105 |
|
---|
106 | public override void AddValue(double value) {
|
---|
107 | dataRow.Add(value);
|
---|
108 | OnValueChanged(value, dataRow.Count - 1, Action.Added);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override void AddValue(double value, int index) {
|
---|
112 | throw new NotSupportedException();
|
---|
113 | }
|
---|
114 |
|
---|
115 | public override void AddValues(double[] values) {
|
---|
116 | throw new NotSupportedException();
|
---|
117 | }
|
---|
118 |
|
---|
119 | public override void AddValues(double[] values, int index) {
|
---|
120 | throw new NotSupportedException();
|
---|
121 | }
|
---|
122 |
|
---|
123 | public override void ModifyValue(double value, int index) {
|
---|
124 | throw new NotSupportedException();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override void ModifyValues(double[] values, int index) {
|
---|
128 | throw new NotSupportedException();
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override void RemoveValue(int index) {
|
---|
132 | throw new NotSupportedException();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override void RemoveValues(int index, int countVals) {
|
---|
136 | throw new NotSupportedException();
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override int Count {
|
---|
140 | get { return dataRow.Count; } //return dataRowWatches.Count; }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public override double this[int index] {
|
---|
144 | get { return dataRow[index]; }
|
---|
145 | set {
|
---|
146 | dataRow[index] = value;
|
---|
147 | OnValueChanged(value, index, Action.Modified);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public override double MinValue {
|
---|
152 | get { return 0; }
|
---|
153 | }
|
---|
154 |
|
---|
155 | public override double MaxValue {
|
---|
156 | get { return 0; }
|
---|
157 | }
|
---|
158 |
|
---|
159 | #endregion
|
---|
160 | }
|
---|
161 | }
|
---|