Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/DataRow.cs @ 1975

Last change on this file since 1975 was 1972, checked in by shofstad, 15 years ago

DataRowSettings updated (#636)

File size: 5.1 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
[1350]17  public class DataRow : DataRowBase {
[1233]18    private readonly List<double> dataRow = new List<double>();
[761]19
[1285]20    private double minValue = double.MaxValue;
21    private double maxValue = double.MinValue;
22
[868]23    public DataRow() {
24    }
[1190]25   
[867]26    public DataRow(string label) {
[1962]27      this.RowSettings.Label = label;
[867]28    }
29
30    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
[1962]31      this.RowSettings.Label = label;
32      this.RowSettings.Color = color;
33      this.RowSettings.Thickness = thickness;
[1972]34      this.RowSettings.Style = style;
[867]35      this.dataRow = dataRow;
[1972]36      this.RowSettings.ShowMarkers = true;
[867]37    }
38
[1561]39    public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow, bool showMarkers) {
[1962]40      this.RowSettings.Label = label;
41      this.RowSettings.Color = color;
42      this.RowSettings.Thickness = thickness;
[1972]43      this.RowSettings.Style = style;
44      this.RowSettings.ShowMarkers = showMarkers;
[1561]45      this.dataRow = dataRow;
46    }
47
[1350]48    public override void AddValue(double value) {
[1285]49      UpdateMinMaxValue(value);
50
[860]51      dataRow.Add(value);
[867]52      OnValueChanged(value, dataRow.Count - 1, Action.Added);
[761]53    }
54
[1350]55    public override void AddValue(double value, int index) {
[867]56      //check if index is valid
57      if (index >= 0 && index < dataRow.Count) {
58        dataRow.Insert(index, value);
59        OnValueChanged(value, index, Action.Added);
60      } else {
[1233]61        throw new IndexOutOfRangeException();
[867]62      }   
[761]63    }
64
[1350]65    public override void AddValues(double[] values) {
[860]66      int startInd = dataRow.Count;
67
68      foreach (double d in values) {
69        dataRow.Add(d);
70      }
[867]71      OnValuesChanged(values, startInd, Action.Added);
[761]72    }
73
[1350]74    public override void AddValues(double[] values, int index) {
[867]75      int j = index;
76
[860]77      //check if index to start changes is valid
[867]78      if (index >=0 && (index + values.Length) < dataRow.Count) {
[860]79        foreach (double d in values) {
[867]80          dataRow.Insert(j, d);
81          j++;
[860]82        }
[867]83        OnValuesChanged(values, index, Action.Added);
[860]84      } else {
[1233]85        throw new IndexOutOfRangeException();
[860]86      }
[761]87    }
88
[1350]89    public override void ModifyValue(double value, int index) {
[867]90      //check if index is valid
91      if (index >= 0 && index < dataRow.Count) {
92        dataRow[index] = value;
93        OnValueChanged(value, index, Action.Modified);
94      } else {
[1233]95        throw new IndexOutOfRangeException();
[867]96      }
[761]97    }
98
[1350]99    public override void ModifyValues(double[] values, int index) {
[867]100      int startInd = index;
101      int modInd = index;
102
103      //check if index to start modification is valid
104      if (startInd >=0 && startInd + values.Length < dataRow.Count) {
105        foreach (double d in values) {
106          dataRow[modInd] = d;
107          modInd++;
108        }
109        OnValuesChanged(values, startInd, Action.Modified);
110      } else {
[1233]111        throw new IndexOutOfRangeException();
[867]112      }
[761]113    }
114
[1350]115    public override void RemoveValue(int index) {
[867]116      double remVal = dataRow[index];
117      //check if index is valid
118      if (index >= 0 && index < dataRow.Count) {
119        dataRow.RemoveAt(index);
120        OnValueChanged(remVal, index, Action.Deleted);
121      } else {
[1233]122        throw new IndexOutOfRangeException();
[867]123      }
[761]124    }
125
[1350]126    public override void RemoveValues(int index, int count) {
[867]127      double[] remValues = new double[count]; //removed values
128      int j = 0;
129
130      //check if count is valid
131      if (count > 0) {
132        //check if index is valid
133        if ((index >= 0) && (index + count <= dataRow.Count)) {
134          for (int i = index; i < (index + count); i++) {
135            remValues.SetValue(i, j);
136            dataRow.RemoveAt(i);
137            j++;
138          }
139          OnValuesChanged(remValues, index, Action.Deleted);
140        } else {
[1233]141          throw new IndexOutOfRangeException();
[867]142        }
143      } else {
[1233]144        throw new Exception("parameter count must be > 0!");
[867]145      }
[761]146    }
147
[1350]148    public override int Count {
[860]149      get { return dataRow.Count; }
[761]150    }
151
[1350]152    public override double this[int index] {
[860]153      get { return dataRow[index]; }
[761]154      set {
[860]155        dataRow[index] = value;
[867]156        OnValueChanged(value, index, Action.Modified);
[761]157      }
158    }
[1285]159
[1350]160    public override double MinValue {
[1285]161      get { return minValue; }
162    }
163
[1350]164    public override double MaxValue {
[1285]165      get { return maxValue; }
166    }
167
168    private void UpdateMinMaxValue(double value) {
169      maxValue = Math.Max(value, maxValue);
170      minValue = Math.Min(value, minValue);
171    }
[761]172  }
173}
Note: See TracBrowser for help on using the repository browser.