Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1962 was 1962, checked in by cbahner, 15 years ago

#636 first impl. of drawingStyle (DataRowSettings)

File size: 5.0 KB
Line 
1using System;
2using System.Drawing;
3using System.Collections.Generic;
4using HeuristicLab.Visualization.LabelProvider;
5
6namespace HeuristicLab.Visualization {
7  public enum Action {
8    Added,
9    Modified,
10    Deleted
11  }
12
13  public delegate void DataRowChangedHandler(IDataRow row);
14  public delegate void ValuesChangedHandler(IDataRow row, double[] values, int index, Action action);
15  public delegate void ValueChangedHandler(IDataRow row, double value, int index, Action action);
16
17  public class DataRow : DataRowBase {
18    private readonly List<double> dataRow = new List<double>();
19
20    private double minValue = double.MaxValue;
21    private double maxValue = double.MinValue;
22
23    public DataRow() {
24    }
25   
26    public DataRow(string label) {
27      this.RowSettings.Label = label;
28    }
29
30    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
31      this.RowSettings.Label = label;
32      this.RowSettings.Color = color;
33      this.RowSettings.Thickness = thickness;
34      this.Style = style;
35      this.dataRow = dataRow;
36      this.ShowMarkers = true;
37    }
38
39    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow, bool showMarkers) {
40      this.RowSettings.Label = label;
41      this.RowSettings.Color = color;
42      this.RowSettings.Thickness = thickness;
43      this.Style = style;
44      this.ShowMarkers = showMarkers;
45      this.dataRow = dataRow;
46    }
47
48    public override void AddValue(double value) {
49      UpdateMinMaxValue(value);
50
51      dataRow.Add(value);
52      OnValueChanged(value, dataRow.Count - 1, Action.Added);
53    }
54
55    public override void AddValue(double value, int index) {
56      //check if index is valid
57      if (index >= 0 && index < dataRow.Count) {
58        dataRow.Insert(index, value);
59        OnValueChanged(value, index, Action.Added);
60      } else {
61        throw new IndexOutOfRangeException();
62      }   
63    }
64
65    public override void AddValues(double[] values) {
66      int startInd = dataRow.Count;
67
68      foreach (double d in values) {
69        dataRow.Add(d);
70      }
71      OnValuesChanged(values, startInd, Action.Added);
72    }
73
74    public override void AddValues(double[] values, int index) {
75      int j = index;
76
77      //check if index to start changes is valid
78      if (index >=0 && (index + values.Length) < dataRow.Count) {
79        foreach (double d in values) {
80          dataRow.Insert(j, d);
81          j++;
82        }
83        OnValuesChanged(values, index, Action.Added);
84      } else {
85        throw new IndexOutOfRangeException();
86      }
87    }
88
89    public override void ModifyValue(double value, int index) {
90      //check if index is valid
91      if (index >= 0 && index < dataRow.Count) {
92        dataRow[index] = value;
93        OnValueChanged(value, index, Action.Modified);
94      } else {
95        throw new IndexOutOfRangeException();
96      }
97    }
98
99    public override void ModifyValues(double[] values, int index) {
100      int startInd = index;
101      int modInd = index;
102
103      //check if index to start modification is valid
104      if (startInd >=0 && startInd + values.Length < dataRow.Count) {
105        foreach (double d in values) {
106          dataRow[modInd] = d;
107          modInd++;
108        }
109        OnValuesChanged(values, startInd, Action.Modified);
110      } else {
111        throw new IndexOutOfRangeException();
112      }
113    }
114
115    public override void RemoveValue(int index) {
116      double remVal = dataRow[index];
117      //check if index is valid
118      if (index >= 0 && index < dataRow.Count) {
119        dataRow.RemoveAt(index);
120        OnValueChanged(remVal, index, Action.Deleted);
121      } else {
122        throw new IndexOutOfRangeException();
123      }
124    }
125
126    public override void RemoveValues(int index, int count) {
127      double[] remValues = new double[count]; //removed values
128      int j = 0;
129
130      //check if count is valid
131      if (count > 0) {
132        //check if index is valid
133        if ((index >= 0) && (index + count <= dataRow.Count)) {
134          for (int i = index; i < (index + count); i++) {
135            remValues.SetValue(i, j);
136            dataRow.RemoveAt(i);
137            j++;
138          }
139          OnValuesChanged(remValues, index, Action.Deleted);
140        } else {
141          throw new IndexOutOfRangeException();
142        }
143      } else {
144        throw new Exception("parameter count must be > 0!");
145      }
146    }
147
148    public override int Count {
149      get { return dataRow.Count; }
150    }
151
152    public override double this[int index] {
153      get { return dataRow[index]; }
154      set {
155        dataRow[index] = value;
156        OnValueChanged(value, index, Action.Modified);
157      }
158    }
159
160    public override double MinValue {
161      get { return minValue; }
162    }
163
164    public override double MaxValue {
165      get { return maxValue; }
166    }
167
168    private void UpdateMinMaxValue(double value) {
169      maxValue = Math.Max(value, maxValue);
170      minValue = Math.Min(value, minValue);
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.