Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/DataRow.cs @ 2040

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

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

File size: 8.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Globalization;
5using System.Xml;
6using HeuristicLab.Core;
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    public DataRow(string label) {
28      this.RowSettings.Label = label;
29    }
30
31    public override void AddValue(double value) {
32      UpdateMinMaxValue(value);
33
34      dataRow.Add(value);
35      OnValueChanged(value, dataRow.Count - 1, Action.Added);
36    }
37
38    public override void AddValue(double value, int index) {
39      //check if index is valid
40      if (index >= 0 && index < dataRow.Count) {
41        UpdateMinMaxValue(value);
42        dataRow.Insert(index, value);
43        OnValueChanged(value, index, Action.Added);
44      } else {
45        throw new IndexOutOfRangeException();
46      }   
47    }
48
49    public override void AddValues(double[] values) {
50      int startIndex = dataRow.Count;
51
52      foreach (double value in values) {
53        UpdateMinMaxValue(value);
54        dataRow.Add(value);
55      }
56
57      OnValuesChanged(values, startIndex, Action.Added);
58    }
59
60    public override void AddValues(double[] values, int index) {
61      if (index >= 0 && index < dataRow.Count) {
62        for (int i = 0; i < values.Length; i++) {
63          double value = values[i];
64          UpdateMinMaxValue(value);
65          dataRow.Insert(index + i, value);
66        }
67        OnValuesChanged(values, index, Action.Added);
68      } else {
69        throw new IndexOutOfRangeException();
70      }
71    }
72
73    public override void ModifyValue(double value, int index) {
74      //check if index is valid
75      if (index >= 0 && index < dataRow.Count) {
76        UpdateMinMaxValue(value, index); // bad runtime but works
77        dataRow[index] = value;
78        OnValueChanged(value, index, Action.Modified);
79      } else {
80        throw new IndexOutOfRangeException();
81      }
82    }
83
84    public override void ModifyValues(double[] values, int index) {
85      //check if index to start modification is valid
86      if (index >= 0 && index + values.Length < dataRow.Count) {
87        for (int i = 0; i < values.Length; i++) {
88          double value = values[i];
89          UpdateMinMaxValue(value, index + i); // bad runtime but works
90          dataRow[index+i] = value;
91        }
92        OnValuesChanged(values, index, Action.Modified);
93      } else {
94        throw new IndexOutOfRangeException();
95      }
96    }
97
98    public override void RemoveValue(int index) {
99      if (index >= 0 && index < dataRow.Count) {
100        UpdateMinMaxValueForRemovedValue(index); // bad runtime but works
101        double removedValue = dataRow[index];
102        dataRow.RemoveAt(index);
103        OnValueChanged(removedValue, index, Action.Deleted);
104      } else {
105        throw new IndexOutOfRangeException();
106      }
107    }
108
109    public override void RemoveValues(int index, int count) {
110      if (count > 0) {
111        if ((index >= 0) && (index + count <= dataRow.Count)) {
112          double[] removedValues = new double[count];
113          for (int i = 0; i < count; i++) {
114            removedValues[i] = dataRow[index + i];
115            UpdateMinMaxValueForRemovedValue(index); // bad runtime but works
116            dataRow.RemoveAt(index);
117          }
118          OnValuesChanged(removedValues, index, Action.Deleted);
119        } else {
120          throw new IndexOutOfRangeException();
121        }
122      } else {
123        throw new Exception("parameter count must be > 0!");
124      }
125    }
126
127    public override int Count {
128      get { return dataRow.Count; }
129    }
130
131    public override double this[int index] {
132      get { return dataRow[index]; }
133      set {
134        dataRow[index] = value;
135        OnValueChanged(value, index, Action.Modified);
136      }
137    }
138
139    public override double MinValue {
140      get { return minValue; }
141    }
142
143    public override double MaxValue {
144      get { return maxValue; }
145    }
146
147    private void UpdateMinMaxValueForRemovedValue(int removedValueIndex) {
148      if (minValue == dataRow[removedValueIndex] || maxValue == dataRow[removedValueIndex]) {
149        minValue = double.MaxValue;
150        maxValue = double.MinValue;
151
152        for (int i = 0; i < dataRow.Count; i++) {
153          if (i != removedValueIndex) {
154            UpdateMinMaxValue(dataRow[i]);
155          }
156        }
157      }
158    }
159
160    private void UpdateMinMaxValue(double newValue, int oldValueIndex) {
161      if (minValue != dataRow[oldValueIndex] && maxValue != dataRow[oldValueIndex])
162        UpdateMinMaxValue(newValue);
163      else {
164        minValue = double.MaxValue;
165        maxValue = double.MinValue;
166
167        for (int i = 0; i < dataRow.Count; i++) {
168          double value = oldValueIndex != i ? dataRow[i] : newValue;
169          UpdateMinMaxValue(value);
170        }
171      }
172    }
173
174    private void UpdateMinMaxValue(double value) {
175      maxValue = Math.Max(value, maxValue);
176      minValue = Math.Min(value, minValue);
177    }
178
179    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
180      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
181
182      XmlSupport.SetAttribute("Label", RowSettings.Label, node);
183      XmlSupport.SetAttribute("Color", RowSettings.Color.ToArgb().ToString(), node);
184      XmlSupport.SetAttribute("LineType", RowSettings.LineType.ToString(), node);
185      XmlSupport.SetAttribute("Thickness", RowSettings.Thickness.ToString(), node);
186      XmlSupport.SetAttribute("ShowMarkers", RowSettings.ShowMarkers ? "true" : "false", node);
187      XmlSupport.SetAttribute("Style", RowSettings.Style.ToString(), node);
188
189      node.AppendChild(PersistenceManager.Persist("YAxis", this.YAxis, document, persistedObjects));
190
191      List<string> strValues = new List<string>();
192      for (int i = 0; i < this.Count; i++) {
193        strValues.Add(this[i].ToString(CultureInfo.InvariantCulture));
194      }
195      XmlElement valuesElement = document.CreateElement("Values");
196      valuesElement.InnerText = string.Join(";", strValues.ToArray());
197      node.AppendChild(valuesElement);
198
199      return node;
200    }
201
202    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
203      base.Populate(node, restoredObjects);
204
205      this.RowSettings.Label = XmlSupport.GetAttribute("Label", "", node);
206      this.RowSettings.Color = Color.FromArgb(Int32.Parse(XmlSupport.GetAttribute("Color", Color.Black.ToArgb().ToString(), node)));
207      this.RowSettings.LineType = (DataRowType)Enum.Parse(typeof(DataRowType), XmlSupport.GetAttribute("LineType", "Normal", node));
208      this.RowSettings.Thickness = Int32.Parse(XmlSupport.GetAttribute("Thickness", "2", node));
209      this.RowSettings.ShowMarkers = XmlSupport.GetAttribute("ShowMarkers", "true", node) == "true";
210      this.RowSettings.Style = (DrawingStyle)Enum.Parse(typeof(DrawingStyle), XmlSupport.GetAttribute("Style", DrawingStyle.Solid.ToString(), node));
211
212      XmlNode yAxisNode = node.SelectSingleNode("YAxis");
213      if (yAxisNode != null) {
214        this.YAxis = (YAxisDescriptor)PersistenceManager.Restore(yAxisNode, restoredObjects);
215      }
216
217      XmlNode valuesNode = node.SelectSingleNode("Values");
218      if (valuesNode != null) {
219        string[] strValues = valuesNode.InnerText.Split(';');
220        foreach (string strValue in strValues) {
221          double value = double.Parse(strValue, CultureInfo.InvariantCulture);
222          this.AddValue(value);
223        }
224      }
225    }
226  }
227}
Note: See TracBrowser for help on using the repository browser.