Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/DataRow.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: 7.6 KB
Line 
1using System;
2using System.Drawing;
3using System.Collections.Generic;
4using System.Globalization;
5using System.Text;
6using System.Xml;
7
8namespace HeuristicLab.Visualization {
9  public enum Action {
10    Added,
11    Modified,
12    Deleted
13  }
14
15  public delegate void DataRowChangedHandler(IDataRow row);
16  public delegate void ValuesChangedHandler(IDataRow row, double[] values, int index, Action action);
17  public delegate void ValueChangedHandler(IDataRow row, double value, int index, Action action);
18
19  public class DataRow : DataRowBase {
20    private readonly List<double> dataRow = new List<double>();
21
22    private double minValue = double.MaxValue;
23    private double maxValue = double.MinValue;
24
25    public DataRow() {
26    }
27   
28    public DataRow(string label) {
29      this.RowSettings.Label = label;
30    }
31
32    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
33      this.RowSettings.Label = label;
34      this.RowSettings.Color = color;
35      this.RowSettings.Thickness = thickness;
36      this.RowSettings.Style = style;
37      this.dataRow = dataRow;
38      this.RowSettings.ShowMarkers = true;
39    }
40
41    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow, bool showMarkers) {
42      this.RowSettings.Label = label;
43      this.RowSettings.Color = color;
44      this.RowSettings.Thickness = thickness;
45      this.RowSettings.Style = style;
46      this.RowSettings.ShowMarkers = showMarkers;
47      this.dataRow = dataRow;
48    }
49
50    public override void AddValue(double value) {
51      UpdateMinMaxValue(value);
52
53      dataRow.Add(value);
54      OnValueChanged(value, dataRow.Count - 1, Action.Added);
55    }
56
57    public override void AddValue(double value, int index) {
58      //check if index is valid
59      if (index >= 0 && index < dataRow.Count) {
60        UpdateMinMaxValue(value);
61        dataRow.Insert(index, value);
62        OnValueChanged(value, index, Action.Added);
63      } else {
64        throw new IndexOutOfRangeException();
65      }   
66    }
67
68    public override void AddValues(double[] values) {
69      int startIndex = dataRow.Count;
70
71      foreach (double value in values) {
72        UpdateMinMaxValue(value);
73        dataRow.Add(value);
74      }
75
76      OnValuesChanged(values, startIndex, Action.Added);
77    }
78
79    public override void AddValues(double[] values, int index) {
80      if (index >= 0 && index < dataRow.Count) {
81        for (int i = 0; i < values.Length; i++) {
82          double value = values[i];
83          UpdateMinMaxValue(value);
84          dataRow.Insert(index + i, value);
85        }
86        OnValuesChanged(values, index, Action.Added);
87      } else {
88        throw new IndexOutOfRangeException();
89      }
90    }
91
92    public override void ModifyValue(double value, int index) {
93      //check if index is valid
94      if (index >= 0 && index < dataRow.Count) {
95        UpdateMinMaxValue(value, index); // bad runtime but works
96        dataRow[index] = value;
97        OnValueChanged(value, index, Action.Modified);
98      } else {
99        throw new IndexOutOfRangeException();
100      }
101    }
102
103    public override void ModifyValues(double[] values, int index) {
104      //check if index to start modification is valid
105      if (index >= 0 && index + values.Length < dataRow.Count) {
106        for (int i = 0; i < values.Length; i++) {
107          double value = values[i];
108          UpdateMinMaxValue(value, index + i); // bad runtime but works
109          dataRow[index+i] = value;
110        }
111        OnValuesChanged(values, index, Action.Modified);
112      } else {
113        throw new IndexOutOfRangeException();
114      }
115    }
116
117    public override void RemoveValue(int index) {
118      if (index >= 0 && index < dataRow.Count) {
119        UpdateMinMaxValueForRemovedValue(index); // bad runtime but works
120        double removedValue = dataRow[index];
121        dataRow.RemoveAt(index);
122        OnValueChanged(removedValue, index, Action.Deleted);
123      } else {
124        throw new IndexOutOfRangeException();
125      }
126    }
127
128    public override void RemoveValues(int index, int count) {
129      if (count > 0) {
130        if ((index >= 0) && (index + count <= dataRow.Count)) {
131          double[] removedValues = new double[count];
132          for (int i = 0; i < count; i++) {
133            removedValues[i] = dataRow[index + i];
134            UpdateMinMaxValueForRemovedValue(index); // bad runtime but works
135            dataRow.RemoveAt(index);
136          }
137          OnValuesChanged(removedValues, index, Action.Deleted);
138        } else {
139          throw new IndexOutOfRangeException();
140        }
141      } else {
142        throw new Exception("parameter count must be > 0!");
143      }
144    }
145
146    public override int Count {
147      get { return dataRow.Count; }
148    }
149
150    public override double this[int index] {
151      get { return dataRow[index]; }
152      set {
153        dataRow[index] = value;
154        OnValueChanged(value, index, Action.Modified);
155      }
156    }
157
158    public override double MinValue {
159      get { return minValue; }
160    }
161
162    public override double MaxValue {
163      get { return maxValue; }
164    }
165
166    private void UpdateMinMaxValueForRemovedValue(int removedValueIndex) {
167      if (minValue == dataRow[removedValueIndex] || maxValue == dataRow[removedValueIndex]) {
168        minValue = double.MaxValue;
169        maxValue = double.MinValue;
170
171        for (int i = 0; i < dataRow.Count; i++) {
172          if (i != removedValueIndex) {
173            UpdateMinMaxValue(dataRow[i]);
174          }
175        }
176      }
177    }
178
179    public override XmlNode ToXml(IDataRow row, XmlDocument document)
180    {
181      XmlNode columnElement = document.CreateNode(XmlNodeType.Element, "row", null);
182
183      XmlAttribute idAttr = document.CreateAttribute("label");
184      idAttr.Value = row.RowSettings.Label;
185      columnElement.Attributes.Append(idAttr);
186
187      XmlAttribute attrColor = document.CreateAttribute("color");
188      attrColor.Value = row.RowSettings.Color.Name;
189      columnElement.Attributes.Append(attrColor);
190
191      XmlAttribute attrYAxis = document.CreateAttribute("yAxis");
192      attrYAxis.Value = row.YAxis.Label;
193      columnElement.Attributes.Append(attrYAxis);
194
195      StringBuilder builder = new StringBuilder();
196
197      for (int i = 0; i < row.Count; i++)
198      {
199        if (i == 0)
200        {
201          builder.Append(row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
202          //columnElement.InnerText += row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
203        }
204        else
205        {
206          builder.Append(";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat));
207          //columnElement.InnerText += ";" + row[i].ToString(CultureInfo.InvariantCulture.NumberFormat);
208        }
209      }
210      columnElement.InnerText += builder.ToString();
211      return columnElement;
212    }
213
214    public override IDataRow FromXml(XmlNode xmlNode)
215    {
216      throw new System.NotImplementedException();
217    }
218
219    private void UpdateMinMaxValue(double newValue, int oldValueIndex) {
220      if (minValue != dataRow[oldValueIndex] && maxValue != dataRow[oldValueIndex])
221        UpdateMinMaxValue(newValue);
222      else {
223        minValue = double.MaxValue;
224        maxValue = double.MinValue;
225
226        for (int i = 0; i < dataRow.Count; i++) {
227          double value = oldValueIndex != i ? dataRow[i] : newValue;
228          UpdateMinMaxValue(value);
229        }
230      }
231    }
232
233    private void UpdateMinMaxValue(double value) {
234      maxValue = Math.Max(value, maxValue);
235      minValue = Math.Min(value, minValue);
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.