Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 6.4 KB
RevLine 
[761]1using System;
2using System.Drawing;
[860]3using System.Collections.Generic;
[1194]4using HeuristicLab.Visualization.LabelProvider;
[761]5
6namespace HeuristicLab.Visualization {
[867]7  public enum Action {
8    Added,
9    Modified,
10    Deleted
11  }
12
[761]13  public delegate void DataRowChangedHandler(IDataRow row);
[867]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);
[761]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;
[1193]22    private DataRowType lineType = DataRowType.Normal;
[1233]23    private readonly List<double> dataRow = new List<double>();
[761]24
[1194]25    private ILabelProvider labelProvider = new ContinuousLabelProvider("0.##");
[1190]26
[1285]27    // TODO implement calculation of min and max values
28    private double minValue = double.MaxValue;
29    private double maxValue = double.MinValue;
30
[1193]31    public DataRowType LineType{
32      get { return lineType; }
33      set {
34        lineType = value;
35        OnDataRowChanged(this);
36      }
37    }
38
[1190]39    public ILabelProvider YAxisLabelProvider {
40      get { return labelProvider; }
[1191]41      set {
42        this.labelProvider = value;
43        OnDataRowChanged(this);
44      }
[1190]45    }
46
[868]47    public DataRow() {
48    }
[1190]49   
[867]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) {
[1190]55     this.Label = label;
[867]56      this.Color = color;
57      this.Thickness = thickness;
58      this.Style = style;
59      this.dataRow = dataRow;
60    }
61
[761]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
[867]72    protected void OnValuesChanged(double[] values, int index, Action action) {
[761]73      if (ValuesChanged != null) {
[867]74        ValuesChanged(this, values, index, action);
[761]75      }
76    }
77
78    public event ValueChangedHandler ValueChanged;
79
[867]80    protected void OnValueChanged(double value, int index, Action action) {
[761]81      if (ValueChanged != null) {
[867]82        ValueChanged(this, value, index, action);
[761]83      }
84    }
85
[1190]86 
[761]87    public string Label {
88      get { return label; }
89      set {
90        label = value;
91        OnDataRowChanged(this);
92      }
93    }
[1190]94   
[761]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) {
[1285]121      UpdateMinMaxValue(value);
122
[860]123      dataRow.Add(value);
[867]124      OnValueChanged(value, dataRow.Count - 1, Action.Added);
[761]125    }
126
127    public void AddValue(double value, int index) {
[867]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 {
[1233]133        throw new IndexOutOfRangeException();
[867]134      }   
[761]135    }
136
137    public void AddValues(double[] values) {
[860]138      int startInd = dataRow.Count;
139
140      foreach (double d in values) {
141        dataRow.Add(d);
142      }
[867]143      OnValuesChanged(values, startInd, Action.Added);
[761]144    }
145
146    public void AddValues(double[] values, int index) {
[867]147      int j = index;
148
[860]149      //check if index to start changes is valid
[867]150      if (index >=0 && (index + values.Length) < dataRow.Count) {
[860]151        foreach (double d in values) {
[867]152          dataRow.Insert(j, d);
153          j++;
[860]154        }
[867]155        OnValuesChanged(values, index, Action.Added);
[860]156      } else {
[1233]157        throw new IndexOutOfRangeException();
[860]158      }
[761]159    }
160
161    public void ModifyValue(double value, int index) {
[867]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 {
[1233]167        throw new IndexOutOfRangeException();
[867]168      }
[761]169    }
170
171    public void ModifyValues(double[] values, int index) {
[867]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 {
[1233]183        throw new IndexOutOfRangeException();
[867]184      }
[761]185    }
186
187    public void RemoveValue(int index) {
[867]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 {
[1233]194        throw new IndexOutOfRangeException();
[867]195      }
[761]196    }
197
198    public void RemoveValues(int index, int count) {
[867]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 {
[1233]213          throw new IndexOutOfRangeException();
[867]214        }
215      } else {
[1233]216        throw new Exception("parameter count must be > 0!");
[867]217      }
[761]218    }
219
220    public int Count {
[860]221      get { return dataRow.Count; }
[761]222    }
223
224    public double this[int index] {
[860]225      get { return dataRow[index]; }
[761]226      set {
[860]227        dataRow[index] = value;
[867]228        OnValueChanged(value, index, Action.Modified);
[761]229      }
230    }
[1285]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    }
[761]244  }
245}
Note: See TracBrowser for help on using the repository browser.