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 | if (Content == null) {
|
---|
98 | Caption = "DataTable";
|
---|
99 | chart.Enabled = false;
|
---|
100 | } else {
|
---|
101 | Caption = Content.Name + " (" + Content.GetType().Name + ")";
|
---|
102 | chart.Enabled = true;
|
---|
103 | chart.Titles.Add(new Title(Content.Name, Docking.Top));
|
---|
104 | foreach (DataRow row in Content.Rows)
|
---|
105 | AddDataRow(row);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void AddDataRow(DataRow row) {
|
---|
110 | Series series = new Series(row.Name);
|
---|
111 | series.ChartType = SeriesChartType.FastLine;
|
---|
112 | series.ToolTip = "#VAL";
|
---|
113 | for (int i = 0; i < row.Values.Count; i++)
|
---|
114 | series.Points.Add(row.Values[i]);
|
---|
115 | chart.Series.Add(series);
|
---|
116 | }
|
---|
117 | private void RemoveDataRow(DataRow row) {
|
---|
118 | Series series = chart.Series[row.Name];
|
---|
119 | chart.Series.Remove(series);
|
---|
120 | }
|
---|
121 |
|
---|
122 | #region Content Events
|
---|
123 | private void RegisterDataRowEvents(DataRow row) {
|
---|
124 | row.NameChanged += new EventHandler(Row_NameChanged);
|
---|
125 | valuesRowsTable.Add(row.Values, row);
|
---|
126 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
127 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
128 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
129 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
130 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
131 | }
|
---|
132 | private void DeregisterDataRowEvents(DataRow row) {
|
---|
133 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
134 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
135 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
136 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
137 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
138 | valuesRowsTable.Remove(row.Values);
|
---|
139 | row.NameChanged -= new EventHandler(Row_NameChanged);
|
---|
140 | }
|
---|
141 | protected override void Content_NameChanged(object sender, EventArgs e) {
|
---|
142 | if (InvokeRequired)
|
---|
143 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
144 | else {
|
---|
145 | chart.Titles[0].Text = Content.Name;
|
---|
146 | base.Content_NameChanged(sender, e);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
150 | if (InvokeRequired)
|
---|
151 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded), sender, e);
|
---|
152 | else {
|
---|
153 | foreach (DataRow row in e.Items) {
|
---|
154 | AddDataRow(row);
|
---|
155 | RegisterDataRowEvents(row);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
160 | if (InvokeRequired)
|
---|
161 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved), sender, e);
|
---|
162 | else {
|
---|
163 | foreach (DataRow row in e.Items) {
|
---|
164 | DeregisterDataRowEvents(row);
|
---|
165 | RemoveDataRow(row);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
170 | if (InvokeRequired)
|
---|
171 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced), sender, e);
|
---|
172 | else {
|
---|
173 | foreach (DataRow row in e.OldItems) {
|
---|
174 | DeregisterDataRowEvents(row);
|
---|
175 | RemoveDataRow(row);
|
---|
176 | }
|
---|
177 | foreach (DataRow row in e.Items) {
|
---|
178 | AddDataRow(row);
|
---|
179 | RegisterDataRowEvents(row);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
184 | if (InvokeRequired)
|
---|
185 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset), 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 Row_NameChanged(object sender, EventArgs e) {
|
---|
198 | if (InvokeRequired)
|
---|
199 | Invoke(new EventHandler(Row_NameChanged), sender, e);
|
---|
200 | else {
|
---|
201 | DataRow row = (DataRow)sender;
|
---|
202 | chart.Series[row.Name].Name = row.Name;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
206 | if (InvokeRequired)
|
---|
207 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded), sender, e);
|
---|
208 | else {
|
---|
209 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
210 | foreach (IndexedItem<double> item in e.Items)
|
---|
211 | chart.Series[row.Name].Points.InsertY(item.Index, item.Value);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
215 | if (InvokeRequired)
|
---|
216 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved), sender, e);
|
---|
217 | else {
|
---|
218 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
219 | List<DataPoint> points = new List<DataPoint>();
|
---|
220 | foreach (IndexedItem<double> item in e.Items)
|
---|
221 | points.Add(chart.Series[row.Name].Points[item.Index]);
|
---|
222 | foreach (DataPoint point in points)
|
---|
223 | chart.Series[row.Name].Points.Remove(point);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
227 | if (InvokeRequired)
|
---|
228 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced), sender, e);
|
---|
229 | else {
|
---|
230 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
231 | foreach (IndexedItem<double> item in e.Items)
|
---|
232 | chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
|
---|
233 | }
|
---|
234 | }
|
---|
235 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
236 | if (InvokeRequired)
|
---|
237 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved), sender, e);
|
---|
238 | else {
|
---|
239 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
240 | foreach (IndexedItem<double> item in e.Items)
|
---|
241 | chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
|
---|
242 | }
|
---|
243 | }
|
---|
244 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
245 | if (InvokeRequired)
|
---|
246 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset), sender, e);
|
---|
247 | else {
|
---|
248 | DataRow row = valuesRowsTable[(IObservableList<double>)sender];
|
---|
249 | chart.Series[row.Name].Points.Clear();
|
---|
250 | foreach (IndexedItem<double> item in e.Items)
|
---|
251 | chart.Series[row.Name].Points[item.Index].YValues = new double[] { item.Value };
|
---|
252 | }
|
---|
253 | }
|
---|
254 | #endregion
|
---|
255 | }
|
---|
256 | }
|
---|