Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/DataRow.cs @ 1255

Last change on this file since 1255 was 1233, checked in by mstoeger, 16 years ago

General housekeeping (#498) Removed some old unused Z-Order values. Replaced some var types by concrete types. Renamed some LineShape properties. Added caching of Pens and Brushes in LineShape and RectangleShape. Put axis tick calculation algorithm into its own class.

File size: 6.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 : IDataRow {
18    private string label = "";
19    private Color color = Color.Black;
20    private int thickness = 2;
21    private DrawingStyle style = DrawingStyle.Solid;
22    private DataRowType lineType = DataRowType.Normal;
23    private readonly List<double> dataRow = new List<double>();
24
25    private ILabelProvider labelProvider = new ContinuousLabelProvider("0.##");
26
27    public DataRowType LineType{
28      get { return lineType; }
29      set {
30        lineType = value;
31        OnDataRowChanged(this);
32      }
33    }
34
35    public ILabelProvider YAxisLabelProvider {
36      get { return labelProvider; }
37      set {
38        this.labelProvider = value;
39        OnDataRowChanged(this);
40      }
41    }
42
43    public DataRow() {
44    }
45   
46    public DataRow(string label) {
47      this.Label = label;
48    }
49
50    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
51     this.Label = label;
52      this.Color = color;
53      this.Thickness = thickness;
54      this.Style = style;
55      this.dataRow = dataRow;
56    }
57
58    public event DataRowChangedHandler DataRowChanged;
59
60    protected void OnDataRowChanged(IDataRow row) {
61      if (DataRowChanged != null) {
62        DataRowChanged(this);
63      }
64    }
65
66    public event ValuesChangedHandler ValuesChanged;
67
68    protected void OnValuesChanged(double[] values, int index, Action action) {
69      if (ValuesChanged != null) {
70        ValuesChanged(this, values, index, action);
71      }
72    }
73
74    public event ValueChangedHandler ValueChanged;
75
76    protected void OnValueChanged(double value, int index, Action action) {
77      if (ValueChanged != null) {
78        ValueChanged(this, value, index, action);
79      }
80    }
81
82 
83    public string Label {
84      get { return label; }
85      set {
86        label = value;
87        OnDataRowChanged(this);
88      }
89    }
90   
91
92    public Color Color {
93      get { return color; }
94      set {
95        color = value;
96        OnDataRowChanged(this);
97      }
98    }
99
100    public int Thickness {
101      get { return thickness; }
102      set {
103        thickness = value;
104        OnDataRowChanged(this);
105      }
106    }
107
108    public DrawingStyle Style {
109      get { return style; }
110      set {
111        style = value;
112        OnDataRowChanged(this);
113      }
114    }
115
116    public void AddValue(double value) {
117      dataRow.Add(value);
118      OnValueChanged(value, dataRow.Count - 1, Action.Added);
119    }
120
121    public void AddValue(double value, int index) {
122      //check if index is valid
123      if (index >= 0 && index < dataRow.Count) {
124        dataRow.Insert(index, value);
125        OnValueChanged(value, index, Action.Added);
126      } else {
127        throw new IndexOutOfRangeException();
128      }   
129    }
130
131    public void AddValues(double[] values) {
132      int startInd = dataRow.Count;
133
134      foreach (double d in values) {
135        dataRow.Add(d);
136      }
137      OnValuesChanged(values, startInd, Action.Added);
138    }
139
140    public void AddValues(double[] values, int index) {
141      int j = index;
142
143      //check if index to start changes is valid
144      if (index >=0 && (index + values.Length) < dataRow.Count) {
145        foreach (double d in values) {
146          dataRow.Insert(j, d);
147          j++;
148        }
149        OnValuesChanged(values, index, Action.Added);
150      } else {
151        throw new IndexOutOfRangeException();
152      }
153    }
154
155    public void ModifyValue(double value, int index) {
156      //check if index is valid
157      if (index >= 0 && index < dataRow.Count) {
158        dataRow[index] = value;
159        OnValueChanged(value, index, Action.Modified);
160      } else {
161        throw new IndexOutOfRangeException();
162      }
163    }
164
165    public void ModifyValues(double[] values, int index) {
166      int startInd = index;
167      int modInd = index;
168
169      //check if index to start modification is valid
170      if (startInd >=0 && startInd + values.Length < dataRow.Count) {
171        foreach (double d in values) {
172          dataRow[modInd] = d;
173          modInd++;
174        }
175        OnValuesChanged(values, startInd, Action.Modified);
176      } else {
177        throw new IndexOutOfRangeException();
178      }
179    }
180
181    public void RemoveValue(int index) {
182      double remVal = dataRow[index];
183      //check if index is valid
184      if (index >= 0 && index < dataRow.Count) {
185        dataRow.RemoveAt(index);
186        OnValueChanged(remVal, index, Action.Deleted);
187      } else {
188        throw new IndexOutOfRangeException();
189      }
190    }
191
192    public void RemoveValues(int index, int count) {
193      double[] remValues = new double[count]; //removed values
194      int j = 0;
195
196      //check if count is valid
197      if (count > 0) {
198        //check if index is valid
199        if ((index >= 0) && (index + count <= dataRow.Count)) {
200          for (int i = index; i < (index + count); i++) {
201            remValues.SetValue(i, j);
202            dataRow.RemoveAt(i);
203            j++;
204          }
205          OnValuesChanged(remValues, index, Action.Deleted);
206        } else {
207          throw new IndexOutOfRangeException();
208        }
209      } else {
210        throw new Exception("parameter count must be > 0!");
211      }
212    }
213
214    public int Count {
215      get { return dataRow.Count; }
216    }
217
218    public double this[int index] {
219      get { return dataRow[index]; }
220      set {
221        dataRow[index] = value;
222        OnValueChanged(value, index, Action.Modified);
223      }
224    }
225  }
226}
Note: See TracBrowser for help on using the repository browser.