Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented multiple Y-Axes. (#433) Panning & Zooming is broken.

File size: 6.4 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    // TODO implement calculation of min and max values
28    private double minValue = double.MaxValue;
29    private double maxValue = double.MinValue;
30
31    public DataRowType LineType{
32      get { return lineType; }
33      set {
34        lineType = value;
35        OnDataRowChanged(this);
36      }
37    }
38
39    public ILabelProvider YAxisLabelProvider {
40      get { return labelProvider; }
41      set {
42        this.labelProvider = value;
43        OnDataRowChanged(this);
44      }
45    }
46
47    public DataRow() {
48    }
49   
50    public DataRow(string label) {
51      this.Label = label;
52    }
53
54    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
55     this.Label = label;
56      this.Color = color;
57      this.Thickness = thickness;
58      this.Style = style;
59      this.dataRow = dataRow;
60    }
61
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      UpdateMinMaxValue(value);
122
123      dataRow.Add(value);
124      OnValueChanged(value, dataRow.Count - 1, Action.Added);
125    }
126
127    public void AddValue(double value, int index) {
128      //check if index is valid
129      if (index >= 0 && index < dataRow.Count) {
130        dataRow.Insert(index, value);
131        OnValueChanged(value, index, Action.Added);
132      } else {
133        throw new IndexOutOfRangeException();
134      }   
135    }
136
137    public void AddValues(double[] values) {
138      int startInd = dataRow.Count;
139
140      foreach (double d in values) {
141        dataRow.Add(d);
142      }
143      OnValuesChanged(values, startInd, Action.Added);
144    }
145
146    public void AddValues(double[] values, int index) {
147      int j = index;
148
149      //check if index to start changes is valid
150      if (index >=0 && (index + values.Length) < dataRow.Count) {
151        foreach (double d in values) {
152          dataRow.Insert(j, d);
153          j++;
154        }
155        OnValuesChanged(values, index, Action.Added);
156      } else {
157        throw new IndexOutOfRangeException();
158      }
159    }
160
161    public void ModifyValue(double value, int index) {
162      //check if index is valid
163      if (index >= 0 && index < dataRow.Count) {
164        dataRow[index] = value;
165        OnValueChanged(value, index, Action.Modified);
166      } else {
167        throw new IndexOutOfRangeException();
168      }
169    }
170
171    public void ModifyValues(double[] values, int index) {
172      int startInd = index;
173      int modInd = index;
174
175      //check if index to start modification is valid
176      if (startInd >=0 && startInd + values.Length < dataRow.Count) {
177        foreach (double d in values) {
178          dataRow[modInd] = d;
179          modInd++;
180        }
181        OnValuesChanged(values, startInd, Action.Modified);
182      } else {
183        throw new IndexOutOfRangeException();
184      }
185    }
186
187    public void RemoveValue(int index) {
188      double remVal = dataRow[index];
189      //check if index is valid
190      if (index >= 0 && index < dataRow.Count) {
191        dataRow.RemoveAt(index);
192        OnValueChanged(remVal, index, Action.Deleted);
193      } else {
194        throw new IndexOutOfRangeException();
195      }
196    }
197
198    public void RemoveValues(int index, int count) {
199      double[] remValues = new double[count]; //removed values
200      int j = 0;
201
202      //check if count is valid
203      if (count > 0) {
204        //check if index is valid
205        if ((index >= 0) && (index + count <= dataRow.Count)) {
206          for (int i = index; i < (index + count); i++) {
207            remValues.SetValue(i, j);
208            dataRow.RemoveAt(i);
209            j++;
210          }
211          OnValuesChanged(remValues, index, Action.Deleted);
212        } else {
213          throw new IndexOutOfRangeException();
214        }
215      } else {
216        throw new Exception("parameter count must be > 0!");
217      }
218    }
219
220    public int Count {
221      get { return dataRow.Count; }
222    }
223
224    public double this[int index] {
225      get { return dataRow[index]; }
226      set {
227        dataRow[index] = value;
228        OnValueChanged(value, index, Action.Modified);
229      }
230    }
231
232    public double MinValue {
233      get { return minValue; }
234    }
235
236    public double MaxValue {
237      get { return maxValue; }
238    }
239
240    private void UpdateMinMaxValue(double value) {
241      maxValue = Math.Max(value, maxValue);
242      minValue = Math.Min(value, minValue);
243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.