Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1222 was 1194, checked in by mstoeger, 15 years ago

Implemented a continuous-, discrete- and string- label provider for the X/Y axis labels. (#433)

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