Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1193 was 1193, checked in by cbahner, 16 years ago

#320 added DataRowType: Normal or SingleValue for Aggregators (performance for drawing)

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