1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Visualization.LabelProvider;
|
---|
5 |
|
---|
6 | namespace 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 | public string Label {
|
---|
87 | get { return label; }
|
---|
88 | set {
|
---|
89 | label = value;
|
---|
90 | OnDataRowChanged(this);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | public Color Color {
|
---|
96 | get { return color; }
|
---|
97 | set {
|
---|
98 | color = value;
|
---|
99 | OnDataRowChanged(this);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public int Thickness {
|
---|
104 | get { return thickness; }
|
---|
105 | set {
|
---|
106 | thickness = value;
|
---|
107 | OnDataRowChanged(this);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public DrawingStyle Style {
|
---|
112 | get { return style; }
|
---|
113 | set {
|
---|
114 | style = value;
|
---|
115 | OnDataRowChanged(this);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private bool showYAxis = true;
|
---|
120 |
|
---|
121 | public virtual bool ShowYAxis {
|
---|
122 | get { return showYAxis; }
|
---|
123 | set {
|
---|
124 | showYAxis = value;
|
---|
125 | OnDataRowChanged(this);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void AddValue(double value) {
|
---|
130 | UpdateMinMaxValue(value);
|
---|
131 |
|
---|
132 | dataRow.Add(value);
|
---|
133 | OnValueChanged(value, dataRow.Count - 1, Action.Added);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void AddValue(double value, int index) {
|
---|
137 | //check if index is valid
|
---|
138 | if (index >= 0 && index < dataRow.Count) {
|
---|
139 | dataRow.Insert(index, value);
|
---|
140 | OnValueChanged(value, index, Action.Added);
|
---|
141 | } else {
|
---|
142 | throw new IndexOutOfRangeException();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void AddValues(double[] values) {
|
---|
147 | int startInd = dataRow.Count;
|
---|
148 |
|
---|
149 | foreach (double d in values) {
|
---|
150 | dataRow.Add(d);
|
---|
151 | }
|
---|
152 | OnValuesChanged(values, startInd, Action.Added);
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void AddValues(double[] values, int index) {
|
---|
156 | int j = index;
|
---|
157 |
|
---|
158 | //check if index to start changes is valid
|
---|
159 | if (index >=0 && (index + values.Length) < dataRow.Count) {
|
---|
160 | foreach (double d in values) {
|
---|
161 | dataRow.Insert(j, d);
|
---|
162 | j++;
|
---|
163 | }
|
---|
164 | OnValuesChanged(values, index, Action.Added);
|
---|
165 | } else {
|
---|
166 | throw new IndexOutOfRangeException();
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void ModifyValue(double value, int index) {
|
---|
171 | //check if index is valid
|
---|
172 | if (index >= 0 && index < dataRow.Count) {
|
---|
173 | dataRow[index] = value;
|
---|
174 | OnValueChanged(value, index, Action.Modified);
|
---|
175 | } else {
|
---|
176 | throw new IndexOutOfRangeException();
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public void ModifyValues(double[] values, int index) {
|
---|
181 | int startInd = index;
|
---|
182 | int modInd = index;
|
---|
183 |
|
---|
184 | //check if index to start modification is valid
|
---|
185 | if (startInd >=0 && startInd + values.Length < dataRow.Count) {
|
---|
186 | foreach (double d in values) {
|
---|
187 | dataRow[modInd] = d;
|
---|
188 | modInd++;
|
---|
189 | }
|
---|
190 | OnValuesChanged(values, startInd, Action.Modified);
|
---|
191 | } else {
|
---|
192 | throw new IndexOutOfRangeException();
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void RemoveValue(int index) {
|
---|
197 | double remVal = dataRow[index];
|
---|
198 | //check if index is valid
|
---|
199 | if (index >= 0 && index < dataRow.Count) {
|
---|
200 | dataRow.RemoveAt(index);
|
---|
201 | OnValueChanged(remVal, index, Action.Deleted);
|
---|
202 | } else {
|
---|
203 | throw new IndexOutOfRangeException();
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void RemoveValues(int index, int count) {
|
---|
208 | double[] remValues = new double[count]; //removed values
|
---|
209 | int j = 0;
|
---|
210 |
|
---|
211 | //check if count is valid
|
---|
212 | if (count > 0) {
|
---|
213 | //check if index is valid
|
---|
214 | if ((index >= 0) && (index + count <= dataRow.Count)) {
|
---|
215 | for (int i = index; i < (index + count); i++) {
|
---|
216 | remValues.SetValue(i, j);
|
---|
217 | dataRow.RemoveAt(i);
|
---|
218 | j++;
|
---|
219 | }
|
---|
220 | OnValuesChanged(remValues, index, Action.Deleted);
|
---|
221 | } else {
|
---|
222 | throw new IndexOutOfRangeException();
|
---|
223 | }
|
---|
224 | } else {
|
---|
225 | throw new Exception("parameter count must be > 0!");
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | public int Count {
|
---|
230 | get { return dataRow.Count; }
|
---|
231 | }
|
---|
232 |
|
---|
233 | public double this[int index] {
|
---|
234 | get { return dataRow[index]; }
|
---|
235 | set {
|
---|
236 | dataRow[index] = value;
|
---|
237 | OnValueChanged(value, index, Action.Modified);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | public double MinValue {
|
---|
242 | get { return minValue; }
|
---|
243 | }
|
---|
244 |
|
---|
245 | public double MaxValue {
|
---|
246 | get { return maxValue; }
|
---|
247 | }
|
---|
248 |
|
---|
249 | private void UpdateMinMaxValue(double value) {
|
---|
250 | maxValue = Math.Max(value, maxValue);
|
---|
251 | minValue = Math.Min(value, minValue);
|
---|
252 | }
|
---|
253 | }
|
---|
254 | } |
---|