Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1190 was 1190, checked in by gkragl, 15 years ago

Implemented XAxisLabelProvider and YAxisLabelProvider (#434, #435 )

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