1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
25 | using HeuristicLab.Collections;
|
---|
26 | using HeuristicLab.Core.Views;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Analysis.Views {
|
---|
30 | /// <summary>
|
---|
31 | /// The visual representation of a <see cref="Variable"/>.
|
---|
32 | /// </summary>
|
---|
33 | [View("DataTable View")]
|
---|
34 | [Content(typeof(DataTable), true)]
|
---|
35 | public sealed partial class DataTableView : NamedItemView {
|
---|
36 | Dictionary<IObservableList<double>, DataRow> valuesRowsTable;
|
---|
37 | /// <summary>
|
---|
38 | /// Gets or sets the variable to represent visually.
|
---|
39 | /// </summary>
|
---|
40 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
41 | /// No own data storage present.</remarks>
|
---|
42 | public new DataTable Content {
|
---|
43 | get { return (DataTable)base.Content; }
|
---|
44 | set { base.Content = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
|
---|
49 | /// </summary>
|
---|
50 | public DataTableView() {
|
---|
51 | InitializeComponent();
|
---|
52 | Caption = "DataTable";
|
---|
53 | valuesRowsTable = new Dictionary<IObservableList<double>, DataRow>();
|
---|
54 | }
|
---|
55 | /// <summary>
|
---|
56 | /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
|
---|
57 | /// </summary>
|
---|
58 | /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
|
---|
59 | /// <param name="variable">The variable to represent visually.</param>
|
---|
60 | public DataTableView(DataTable content)
|
---|
61 | : this() {
|
---|
62 | Content = content;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
|
---|
67 | /// </summary>
|
---|
68 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
69 | protected override void DeregisterContentEvents() {
|
---|
70 | foreach (DataRow row in Content.Rows)
|
---|
71 | DeregisterDataRowEvents(row);
|
---|
72 | Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
|
---|
73 | Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
|
---|
74 | Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
|
---|
75 | Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
|
---|
76 | base.DeregisterContentEvents();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Adds eventhandlers to the underlying <see cref="Variable"/>.
|
---|
81 | /// </summary>
|
---|
82 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
83 | protected override void RegisterContentEvents() {
|
---|
84 | base.RegisterContentEvents();
|
---|
85 | Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
|
---|
86 | Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
|
---|
87 | Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
|
---|
88 | Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
|
---|
89 | foreach (DataRow row in Content.Rows)
|
---|
90 | RegisterDataRowEvents(row);
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected override void OnContentChanged() {
|
---|
94 | base.OnContentChanged();
|
---|
95 | chart.Titles.Clear();
|
---|
96 | chart.Series.Clear();
|
---|
97 | Caption = "DataTable";
|
---|
98 | if (Content != null) {
|
---|
99 | Caption = Content.Name + " (" + Content.GetType().Name + ")";
|
---|
100 | chart.Titles.Add(new Title(Content.Name, Docking.Top));
|
---|
101 | foreach (DataRow row in Content.Rows)
|
---|
102 | AddDataRow(row);
|
---|
103 | }
|
---|
104 | SetEnabledStateOfControls();
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected override void OnReadOnlyChanged() {
|
---|
108 | base.OnReadOnlyChanged();
|
---|
109 | SetEnabledStateOfControls();
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void SetEnabledStateOfControls() {
|
---|
113 | chart.Enabled = Content != null;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void AddDataRow(DataRow row) {
|
---|
117 | Series series = new Series(row.Name);
|
---|
118 | series.ChartType = SeriesChartType.FastLine;
|
---|
119 | series.ToolTip = "#VAL";
|
---|
120 | for (int i = 0; i < row.Values.Count; i++) {
|
---|
121 | if (double.IsNaN(row.Values[i])) {
|
---|
122 | DataPoint point = new DataPoint();
|
---|
123 | point.IsEmpty = true;
|
---|
124 | series.Points.Add(point);
|
---|
125 | } else {
|
---|
126 | series.Points.Add(row.Values[i]);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | chart.Series.Add(series);
|
---|
130 | }
|
---|
131 | private void RemoveDataRow(DataRow row) {
|
---|
132 | Series series = chart.Series[row.Name];
|
---|
133 | chart.Series.Remove(series);
|
---|
134 | }
|
---|
135 |
|
---|
136 | #region Content Events
|
---|
137 | private void RegisterDataRowEvents(DataRow row) {
|
---|
138 | row.NameChanged += new EventHandler(Row_NameChanged);
|
---|
139 | valuesRowsTable.Add(row.Values, row);
|
---|
140 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
141 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
142 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
143 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
144 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
145 | }
|
---|
146 | private void DeregisterDataRowEvents(DataRow row) {
|
---|
147 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
148 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
149 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
150 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
151 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
152 | valuesRowsTable.Remove(row.Values);
|
---|
153 | row.NameChanged -= new EventHandler(Row_NameChanged);
|
---|
154 | }
|
---|
155 | protected override void Content_NameChanged(object sender, EventArgs e) {
|
---|
156 | if (InvokeRequired)
|
---|
157 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
158 | else {
|
---|
159 | chart.Titles[0].Text = Content.Name;
|
---|
160 | base.Content_NameChanged(sender, e);
|
---|
161 | }
|
---|
162 | }
|
---|
163 | private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
164 | if (InvokeRequired)
|
---|
165 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded), sender, e);
|
---|
166 | else {
|
---|
167 | foreach (DataRow row in e.Items) {
|
---|
168 | AddDataRow(row);
|
---|
169 | RegisterDataRowEvents(row);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
174 | if (InvokeRequired)
|
---|
175 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved), sender, e);
|
---|
176 | else {
|
---|
177 | foreach (DataRow row in e.Items) {
|
---|
178 | DeregisterDataRowEvents(row);
|
---|
179 | RemoveDataRow(row);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
184 | if (InvokeRequired)
|
---|
185 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced), sender, e);
|
---|
186 | else {
|
---|
187 | foreach (DataRow row in e.OldItems) {
|
---|
188 | DeregisterDataRowEvents(row);
|
---|
189 | RemoveDataRow(row);
|
---|
190 | }
|
---|
191 | foreach (DataRow row in e.Items) {
|
---|
192 | AddDataRow(row);
|
---|
193 | RegisterDataRowEvents(row);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
198 | if (InvokeRequired)
|
---|
199 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset), sender, e);
|
---|
200 | else {
|
---|
201 | foreach (DataRow row in e.OldItems) {
|
---|
202 | DeregisterDataRowEvents(row);
|
---|
203 | RemoveDataRow(row);
|
---|
204 | }
|
---|
205 | foreach (DataRow row in e.Items) {
|
---|
206 | AddDataRow(row);
|
---|
207 | RegisterDataRowEvents(row);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | private void Row_NameChanged(object sender, EventArgs e) {
|
---|
212 | if (InvokeRequired)
|
---|
213 | Invoke(new EventHandler(Row_NameChanged), sender, e);
|
---|
214 | else {
|
---|
215 | DataRow row = (DataRow)sender;
|
---|
216 | chart.Series[row.Name].Name = row.Name;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
220 | if (InvokeRequired)
|
---|
221 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded), sender, e);
|
---|
222 | else {
|
---|
223 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
224 | foreach (IndexedItem<double> item in e.Items) {
|
---|
225 | if (double.IsNaN(item.Value)) {
|
---|
226 | DataPoint point = new DataPoint();
|
---|
227 | point.IsEmpty = true;
|
---|
228 | chart.Series[row.Name].Points.Insert(item.Index, point);
|
---|
229 | } else {
|
---|
230 | chart.Series[row.Name].Points.InsertY(item.Index, item.Value);
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
236 | if (InvokeRequired)
|
---|
237 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved), sender, e);
|
---|
238 | else {
|
---|
239 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
240 | List<DataPoint> points = new List<DataPoint>();
|
---|
241 | foreach (IndexedItem<double> item in e.Items)
|
---|
242 | points.Add(chart.Series[row.Name].Points[item.Index]);
|
---|
243 | foreach (DataPoint point in points)
|
---|
244 | chart.Series[row.Name].Points.Remove(point);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
248 | if (InvokeRequired)
|
---|
249 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced), sender, e);
|
---|
250 | else {
|
---|
251 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
252 | foreach (IndexedItem<double> item in e.Items) {
|
---|
253 | if (double.IsNaN(item.Value))
|
---|
254 | chart.Series[row.Name].Points[item.Index].IsEmpty = true;
|
---|
255 | else
|
---|
256 | chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
261 | if (InvokeRequired)
|
---|
262 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved), sender, e);
|
---|
263 | else {
|
---|
264 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
265 | foreach (IndexedItem<double> item in e.Items) {
|
---|
266 | if (double.IsNaN(item.Value))
|
---|
267 | chart.Series[row.Name].Points[item.Index].IsEmpty = true;
|
---|
268 | else
|
---|
269 | chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
274 | if (InvokeRequired)
|
---|
275 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset), sender, e);
|
---|
276 | else {
|
---|
277 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
278 | chart.Series[row.Name].Points.Clear();
|
---|
279 | foreach (IndexedItem<double> item in e.Items) {
|
---|
280 | if (double.IsNaN(item.Value))
|
---|
281 | chart.Series[row.Name].Points[item.Index].IsEmpty = true;
|
---|
282 | else
|
---|
283 | chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 | #endregion
|
---|
288 | }
|
---|
289 | }
|
---|