1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Collections.Generic;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Visualization {
|
---|
6 | public enum Action {
|
---|
7 | Added,
|
---|
8 | Modified,
|
---|
9 | Deleted
|
---|
10 | }
|
---|
11 |
|
---|
12 | public delegate void DataRowChangedHandler(IDataRow row);
|
---|
13 | public delegate void ValuesChangedHandler(IDataRow row, double[] values, int index, Action action);
|
---|
14 | public delegate void ValueChangedHandler(IDataRow row, double value, int index, Action action);
|
---|
15 |
|
---|
16 | public class DataRow : IDataRow {
|
---|
17 | private string label = "";
|
---|
18 | private Color color = Color.Black;
|
---|
19 | private int thickness = 2;
|
---|
20 | private DrawingStyle style = DrawingStyle.Solid;
|
---|
21 | private List<double> dataRow = new List<double>();
|
---|
22 |
|
---|
23 | private ILabelProvider labelProvider = new DefaultLabelProvider("0.##");
|
---|
24 |
|
---|
25 | public ILabelProvider YAxisLabelProvider {
|
---|
26 | get { return labelProvider; }
|
---|
27 | set {
|
---|
28 | this.labelProvider = value;
|
---|
29 | OnDataRowChanged(this);
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | public DataRow() {
|
---|
35 | }
|
---|
36 |
|
---|
37 | public DataRow(string label) {
|
---|
38 | this.Label = label;
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | public DataRow(string label, Color color, int thickness, DrawingStyle style, List<double> dataRow) {
|
---|
43 | this.Label = label;
|
---|
44 | this.Color = color;
|
---|
45 | this.Thickness = thickness;
|
---|
46 | this.Style = style;
|
---|
47 | this.dataRow = dataRow;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Raised when data row data changed. Should cause redraw in the view.
|
---|
52 | /// </summary>
|
---|
53 | public event DataRowChangedHandler DataRowChanged;
|
---|
54 |
|
---|
55 | protected void OnDataRowChanged(IDataRow row) {
|
---|
56 | if (DataRowChanged != null) {
|
---|
57 | DataRowChanged(this);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public event ValuesChangedHandler ValuesChanged;
|
---|
62 |
|
---|
63 | protected void OnValuesChanged(double[] values, int index, Action action) {
|
---|
64 | if (ValuesChanged != null) {
|
---|
65 | ValuesChanged(this, values, index, action);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public event ValueChangedHandler ValueChanged;
|
---|
70 |
|
---|
71 | protected void OnValueChanged(double value, int index, Action action) {
|
---|
72 | if (ValueChanged != null) {
|
---|
73 | ValueChanged(this, value, index, action);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | public string Label {
|
---|
79 | get { return label; }
|
---|
80 | set {
|
---|
81 | label = value;
|
---|
82 | OnDataRowChanged(this);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | public Color Color {
|
---|
88 | get { return color; }
|
---|
89 | set {
|
---|
90 | color = value;
|
---|
91 | OnDataRowChanged(this);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public int Thickness {
|
---|
96 | get { return thickness; }
|
---|
97 | set {
|
---|
98 | thickness = value;
|
---|
99 | OnDataRowChanged(this);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public DrawingStyle Style {
|
---|
104 | get { return style; }
|
---|
105 | set {
|
---|
106 | style = value;
|
---|
107 | OnDataRowChanged(this);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void AddValue(double value) {
|
---|
112 | dataRow.Add(value);
|
---|
113 | OnValueChanged(value, dataRow.Count - 1, Action.Added);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void AddValue(double value, int index) {
|
---|
117 | //check if index is valid
|
---|
118 | if (index >= 0 && index < dataRow.Count) {
|
---|
119 | dataRow.Insert(index, value);
|
---|
120 | OnValueChanged(value, index, Action.Added);
|
---|
121 | } else {
|
---|
122 | throw new System.IndexOutOfRangeException();
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void AddValues(double[] values) {
|
---|
127 | int startInd = dataRow.Count;
|
---|
128 |
|
---|
129 | foreach (double d in values) {
|
---|
130 | dataRow.Add(d);
|
---|
131 | }
|
---|
132 | OnValuesChanged(values, startInd, Action.Added);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void AddValues(double[] values, int index) {
|
---|
136 | int j = index;
|
---|
137 |
|
---|
138 | //check if index to start changes is valid
|
---|
139 | if (index >=0 && (index + values.Length) < dataRow.Count) {
|
---|
140 | foreach (double d in values) {
|
---|
141 | dataRow.Insert(j, d);
|
---|
142 | j++;
|
---|
143 | }
|
---|
144 | OnValuesChanged(values, index, Action.Added);
|
---|
145 | } else {
|
---|
146 | throw new System.IndexOutOfRangeException();
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void ModifyValue(double value, int index) {
|
---|
151 | //check if index is valid
|
---|
152 | if (index >= 0 && index < dataRow.Count) {
|
---|
153 | dataRow[index] = value;
|
---|
154 | OnValueChanged(value, index, Action.Modified);
|
---|
155 | } else {
|
---|
156 | throw new System.IndexOutOfRangeException();
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void ModifyValues(double[] values, int index) {
|
---|
161 | int startInd = index;
|
---|
162 | int modInd = index;
|
---|
163 |
|
---|
164 | //check if index to start modification is valid
|
---|
165 | if (startInd >=0 && startInd + values.Length < dataRow.Count) {
|
---|
166 | foreach (double d in values) {
|
---|
167 | dataRow[modInd] = d;
|
---|
168 | modInd++;
|
---|
169 | }
|
---|
170 | OnValuesChanged(values, startInd, Action.Modified);
|
---|
171 | } else {
|
---|
172 | throw new System.IndexOutOfRangeException();
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public void RemoveValue(int index) {
|
---|
177 | double remVal = dataRow[index];
|
---|
178 | //check if index is valid
|
---|
179 | if (index >= 0 && index < dataRow.Count) {
|
---|
180 | dataRow.RemoveAt(index);
|
---|
181 | OnValueChanged(remVal, index, Action.Deleted);
|
---|
182 | } else {
|
---|
183 | throw new System.IndexOutOfRangeException();
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | public void RemoveValues(int index, int count) {
|
---|
188 | double[] remValues = new double[count]; //removed values
|
---|
189 | int j = 0;
|
---|
190 |
|
---|
191 | //check if count is valid
|
---|
192 | if (count > 0) {
|
---|
193 | //check if index is valid
|
---|
194 | if ((index >= 0) && (index + count <= dataRow.Count)) {
|
---|
195 | for (int i = index; i < (index + count); i++) {
|
---|
196 | remValues.SetValue(i, j);
|
---|
197 | dataRow.RemoveAt(i);
|
---|
198 | j++;
|
---|
199 | }
|
---|
200 | OnValuesChanged(remValues, index, Action.Deleted);
|
---|
201 | } else {
|
---|
202 | throw new System.IndexOutOfRangeException();
|
---|
203 | }
|
---|
204 | } else {
|
---|
205 | throw new System.Exception("parameter count must be > 0!");
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | public int Count {
|
---|
210 | get { return dataRow.Count; }
|
---|
211 | }
|
---|
212 |
|
---|
213 | public double this[int index] {
|
---|
214 | get { return dataRow[index]; }
|
---|
215 | set {
|
---|
216 | dataRow[index] = value;
|
---|
217 | OnValueChanged(value, index, Action.Modified);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | } |
---|