1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Optimization.Views {
|
---|
35 | [View("RunCollection BubbleChart")]
|
---|
36 | [Content(typeof(RunCollection), false)]
|
---|
37 | public partial class RunCollectionBubbleChartView : AsynchronousContentView {
|
---|
38 | private enum SizeDimension { Constant = 0 }
|
---|
39 | private enum AxisDimension { Index = 0 }
|
---|
40 |
|
---|
41 | private string xAxisValue;
|
---|
42 | private string yAxisValue;
|
---|
43 | private string sizeAxisValue;
|
---|
44 |
|
---|
45 | private Dictionary<IRun, List<DataPoint>> runToDataPointMapping;
|
---|
46 | private Dictionary<IRun, int> runToIndexMapping;
|
---|
47 | private Dictionary<int, Dictionary<object, double>> categoricalMapping;
|
---|
48 | private Dictionary<IRun, double> xJitter;
|
---|
49 | private Dictionary<IRun, double> yJitter;
|
---|
50 | private double xJitterFactor = 0.0;
|
---|
51 | private double yJitterFactor = 0.0;
|
---|
52 | private Random random;
|
---|
53 | private bool isSelecting = false;
|
---|
54 | private bool suppressUpdates = false;
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | public RunCollectionBubbleChartView() {
|
---|
59 | InitializeComponent();
|
---|
60 |
|
---|
61 | chart.ContextMenuStrip.Items.Insert(0, hideRunToolStripMenuItem);
|
---|
62 | chart.ContextMenuStrip.Items.Insert(1, openBoxPlotViewToolStripMenuItem);
|
---|
63 | chart.ContextMenuStrip.Items.Add(getDataAsMatrixToolStripMenuItem);
|
---|
64 | chart.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);
|
---|
65 |
|
---|
66 | runToDataPointMapping = new Dictionary<IRun, List<DataPoint>>();
|
---|
67 | categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
|
---|
68 | xJitter = new Dictionary<IRun, double>();
|
---|
69 | yJitter = new Dictionary<IRun, double>();
|
---|
70 | random = new Random();
|
---|
71 |
|
---|
72 | colorDialog.Color = Color.Black;
|
---|
73 | colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
|
---|
74 | isSelecting = false;
|
---|
75 |
|
---|
76 | chart.CustomizeAllChartAreas();
|
---|
77 | chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
78 | chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
79 | chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
|
---|
80 | chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public new RunCollection Content {
|
---|
84 | get { return (RunCollection)base.Content; }
|
---|
85 | set { base.Content = value; }
|
---|
86 | }
|
---|
87 | public IStringConvertibleMatrix Matrix {
|
---|
88 | get { return this.Content; }
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void RegisterContentEvents() {
|
---|
92 | base.RegisterContentEvents();
|
---|
93 | Content.Reset += new EventHandler(Content_Reset);
|
---|
94 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
95 | Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
96 | Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
97 | Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
98 | Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
|
---|
99 | Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
|
---|
100 | RegisterRunEvents(Content);
|
---|
101 | }
|
---|
102 | protected override void DeregisterContentEvents() {
|
---|
103 | base.DeregisterContentEvents();
|
---|
104 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
105 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
106 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
107 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
108 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
109 | Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
|
---|
110 | Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
|
---|
111 | DeregisterRunEvents(Content);
|
---|
112 | }
|
---|
113 | protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
|
---|
114 | foreach (IRun run in runs)
|
---|
115 | run.Changed += new EventHandler(run_Changed);
|
---|
116 | }
|
---|
117 | protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
|
---|
118 | foreach (IRun run in runs)
|
---|
119 | run.Changed -= new EventHandler(run_Changed);
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
123 | DeregisterRunEvents(e.OldItems);
|
---|
124 | RegisterRunEvents(e.Items);
|
---|
125 | }
|
---|
126 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
127 | DeregisterRunEvents(e.Items);
|
---|
128 | }
|
---|
129 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
130 | RegisterRunEvents(e.Items);
|
---|
131 | }
|
---|
132 | private void run_Changed(object sender, EventArgs e) {
|
---|
133 | if (InvokeRequired)
|
---|
134 | this.Invoke(new EventHandler(run_Changed), sender, e);
|
---|
135 | else {
|
---|
136 | IRun run = (IRun)sender;
|
---|
137 | UpdateRun(run);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void UpdateRun(IRun run) {
|
---|
142 | if (!suppressUpdates) {
|
---|
143 | if (runToDataPointMapping.ContainsKey(run)) {
|
---|
144 | foreach (DataPoint point in runToDataPointMapping[run]) {
|
---|
145 | point.Color = run.Color;
|
---|
146 | if (!run.Visible) {
|
---|
147 | this.chart.Series[0].Points.Remove(point);
|
---|
148 | UpdateCursorInterval();
|
---|
149 | chart.ChartAreas[0].RecalculateAxesScale();
|
---|
150 | }
|
---|
151 | }
|
---|
152 | if (!run.Visible) runToDataPointMapping.Remove(run);
|
---|
153 | } else {
|
---|
154 | AddDataPoint(run);
|
---|
155 | UpdateCursorInterval();
|
---|
156 | chart.ChartAreas[0].RecalculateAxesScale();
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (this.chart.Series[0].Points.Count == 0)
|
---|
160 | noRunsLabel.Visible = true;
|
---|
161 | else
|
---|
162 | noRunsLabel.Visible = false;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | protected override void OnContentChanged() {
|
---|
167 | base.OnContentChanged();
|
---|
168 | this.categoricalMapping.Clear();
|
---|
169 | UpdateComboBoxes();
|
---|
170 | UpdateDataPoints();
|
---|
171 | UpdateCaption();
|
---|
172 | RebuildInverseIndex();
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void RebuildInverseIndex() {
|
---|
176 | if (Content != null) {
|
---|
177 | runToIndexMapping = new Dictionary<IRun, int>(Content.Count);
|
---|
178 | int i = 0;
|
---|
179 | foreach (var run in Content) {
|
---|
180 | runToIndexMapping.Add(run, i);
|
---|
181 | i++;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
187 | if (InvokeRequired)
|
---|
188 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
189 | else
|
---|
190 | UpdateComboBoxes();
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void UpdateCaption() {
|
---|
194 | Caption = Content != null ? Content.OptimizerName + " Bubble Chart" : ViewAttribute.GetViewName(GetType());
|
---|
195 | }
|
---|
196 |
|
---|
197 | private void UpdateComboBoxes() {
|
---|
198 | string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
|
---|
199 | string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
|
---|
200 | string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
|
---|
201 | this.xAxisComboBox.Items.Clear();
|
---|
202 | this.yAxisComboBox.Items.Clear();
|
---|
203 | this.sizeComboBox.Items.Clear();
|
---|
204 | if (Content != null) {
|
---|
205 | string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
|
---|
206 | this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
|
---|
207 | this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
|
---|
208 | this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
|
---|
209 | this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
|
---|
210 | string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
|
---|
211 | this.sizeComboBox.Items.AddRange(additionalSizeDimension);
|
---|
212 | this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
|
---|
213 | this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
|
---|
214 |
|
---|
215 | bool changed = false;
|
---|
216 | if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
|
---|
217 | xAxisComboBox.SelectedItem = selectedXAxis;
|
---|
218 | changed = true;
|
---|
219 | }
|
---|
220 | if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
|
---|
221 | yAxisComboBox.SelectedItem = selectedYAxis;
|
---|
222 | changed = true;
|
---|
223 | }
|
---|
224 | if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
|
---|
225 | sizeComboBox.SelectedItem = selectedSizeAxis;
|
---|
226 | changed = true;
|
---|
227 | }
|
---|
228 | if (changed) {
|
---|
229 | UpdateDataPoints();
|
---|
230 | UpdateAxisLabels();
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
237 | if (InvokeRequired)
|
---|
238 | Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
|
---|
239 | else {
|
---|
240 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
241 | if (!suppressUpdates) UpdateDataPoints();
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
|
---|
246 | if (InvokeRequired)
|
---|
247 | Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
|
---|
248 | else UpdateCaption();
|
---|
249 | }
|
---|
250 |
|
---|
251 | private void Content_Reset(object sender, EventArgs e) {
|
---|
252 | if (InvokeRequired)
|
---|
253 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
254 | else {
|
---|
255 | this.categoricalMapping.Clear();
|
---|
256 | RebuildInverseIndex();
|
---|
257 | UpdateDataPoints();
|
---|
258 | UpdateAxisLabels();
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | private void UpdateDataPoints() {
|
---|
263 | Series series = this.chart.Series[0];
|
---|
264 | series.Points.Clear();
|
---|
265 | runToDataPointMapping.Clear();
|
---|
266 |
|
---|
267 | chart.ChartAreas[0].AxisX.IsMarginVisible = xAxisValue != AxisDimension.Index.ToString();
|
---|
268 | chart.ChartAreas[0].AxisY.IsMarginVisible = yAxisValue != AxisDimension.Index.ToString();
|
---|
269 |
|
---|
270 | if (Content != null) {
|
---|
271 | foreach (IRun run in this.Content)
|
---|
272 | this.AddDataPoint(run);
|
---|
273 |
|
---|
274 | if (this.chart.Series[0].Points.Count == 0)
|
---|
275 | noRunsLabel.Visible = true;
|
---|
276 | else {
|
---|
277 | noRunsLabel.Visible = false;
|
---|
278 | UpdateMarkerSizes();
|
---|
279 | UpdateCursorInterval();
|
---|
280 | }
|
---|
281 | }
|
---|
282 | xTrackBar.Value = 0;
|
---|
283 | yTrackBar.Value = 0;
|
---|
284 |
|
---|
285 | //needed to set axis back to automatic and refresh them, otherwise their values may remain NaN
|
---|
286 | var xAxis = chart.ChartAreas[0].AxisX;
|
---|
287 | var yAxis = chart.ChartAreas[0].AxisY;
|
---|
288 | SetAutomaticUpdateOfAxis(xAxis, true);
|
---|
289 | SetAutomaticUpdateOfAxis(yAxis, true);
|
---|
290 | chart.Refresh();
|
---|
291 | }
|
---|
292 |
|
---|
293 | private void UpdateMarkerSizes() {
|
---|
294 | var series = chart.Series[0];
|
---|
295 | var sizeValues = series.Points.Select(p => p.YValues[1]);
|
---|
296 |
|
---|
297 | double minSizeValue = sizeValues.Min();
|
---|
298 | double maxSizeValue = sizeValues.Max();
|
---|
299 | double sizeRange = maxSizeValue - minSizeValue;
|
---|
300 |
|
---|
301 | const int smallestBubbleSize = 5;
|
---|
302 |
|
---|
303 | foreach (DataPoint point in series.Points) {
|
---|
304 | //calculates the relative size of the data point 0 <= relativeSize <= 1
|
---|
305 | double relativeSize = (point.YValues[1] - minSizeValue);
|
---|
306 | if (sizeRange > double.Epsilon) {
|
---|
307 | relativeSize /= sizeRange;
|
---|
308 |
|
---|
309 | //invert bubble sizes if the value of the trackbar is negative
|
---|
310 | if (sizeTrackBar.Value < 0) relativeSize = Math.Abs(relativeSize - 1);
|
---|
311 | } else relativeSize = 1;
|
---|
312 |
|
---|
313 | double sizeChange = Math.Abs(sizeTrackBar.Value) * relativeSize;
|
---|
314 | point.MarkerSize = (int)Math.Round(sizeChange + smallestBubbleSize);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | private void UpdateDataPointJitter() {
|
---|
319 | var xAxis = this.chart.ChartAreas[0].AxisX;
|
---|
320 | var yAxis = this.chart.ChartAreas[0].AxisY;
|
---|
321 |
|
---|
322 | SetAutomaticUpdateOfAxis(xAxis, false);
|
---|
323 | SetAutomaticUpdateOfAxis(yAxis, false);
|
---|
324 |
|
---|
325 | double xAxisRange = xAxis.Maximum - xAxis.Minimum;
|
---|
326 | double yAxisRange = yAxis.Maximum - yAxis.Minimum;
|
---|
327 |
|
---|
328 | foreach (DataPoint point in chart.Series[0].Points) {
|
---|
329 | IRun run = (IRun)point.Tag;
|
---|
330 | double xValue = GetValue(run, xAxisValue).Value;
|
---|
331 | double yValue = GetValue(run, yAxisValue).Value;
|
---|
332 |
|
---|
333 | if (!xJitterFactor.IsAlmost(0.0))
|
---|
334 | xValue += 0.1 * GetXJitter(run) * xJitterFactor * (xAxisRange);
|
---|
335 | if (!yJitterFactor.IsAlmost(0.0))
|
---|
336 | yValue += 0.1 * GetYJitter(run) * yJitterFactor * (yAxisRange);
|
---|
337 |
|
---|
338 | point.XValue = xValue;
|
---|
339 | point.YValues[0] = yValue;
|
---|
340 | }
|
---|
341 |
|
---|
342 | }
|
---|
343 |
|
---|
344 | // sets an axis to automatic or restrains it to its current values
|
---|
345 | // this is used that none of the set values is changed when jitter is applied, so that the chart stays the same
|
---|
346 | private void SetAutomaticUpdateOfAxis(Axis axis, bool enabled) {
|
---|
347 | if (enabled) {
|
---|
348 | axis.Maximum = double.NaN;
|
---|
349 | axis.Minimum = double.NaN;
|
---|
350 | axis.MajorGrid.Interval = double.NaN;
|
---|
351 | axis.MajorTickMark.Interval = double.NaN;
|
---|
352 | axis.LabelStyle.Interval = double.NaN;
|
---|
353 | } else {
|
---|
354 | axis.Minimum = axis.Minimum;
|
---|
355 | axis.Maximum = axis.Maximum;
|
---|
356 | axis.MajorGrid.Interval = axis.MajorGrid.Interval;
|
---|
357 | axis.MajorTickMark.Interval = axis.MajorTickMark.Interval;
|
---|
358 | axis.LabelStyle.Interval = axis.LabelStyle.Interval;
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | private void AddDataPoint(IRun run) {
|
---|
363 | double? xValue;
|
---|
364 | double? yValue;
|
---|
365 | double? sizeValue;
|
---|
366 | Series series = this.chart.Series[0];
|
---|
367 |
|
---|
368 | xValue = GetValue(run, xAxisValue);
|
---|
369 | yValue = GetValue(run, yAxisValue);
|
---|
370 | sizeValue = GetValue(run, sizeAxisValue);
|
---|
371 |
|
---|
372 | if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
|
---|
373 | xValue = xValue.Value;
|
---|
374 |
|
---|
375 | yValue = yValue.Value;
|
---|
376 |
|
---|
377 | if (run.Visible) {
|
---|
378 | DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
|
---|
379 | point.Tag = run;
|
---|
380 | point.Color = run.Color;
|
---|
381 | series.Points.Add(point);
|
---|
382 | if (!runToDataPointMapping.ContainsKey(run)) runToDataPointMapping.Add(run, new List<DataPoint>());
|
---|
383 | runToDataPointMapping[run].Add(point);
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 | private double? GetValue(IRun run, string columnName) {
|
---|
388 | if (run == null || string.IsNullOrEmpty(columnName))
|
---|
389 | return null;
|
---|
390 |
|
---|
391 | if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
|
---|
392 | AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
|
---|
393 | return GetValue(run, axisDimension);
|
---|
394 | } else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
|
---|
395 | SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
|
---|
396 | return GetValue(run, sizeDimension);
|
---|
397 | } else {
|
---|
398 | int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
|
---|
399 | IItem value = Content.GetValue(run, columnIndex);
|
---|
400 | if (value == null)
|
---|
401 | return null;
|
---|
402 |
|
---|
403 | DoubleValue doubleValue = value as DoubleValue;
|
---|
404 | IntValue intValue = value as IntValue;
|
---|
405 | TimeSpanValue timeSpanValue = value as TimeSpanValue;
|
---|
406 | double? ret = null;
|
---|
407 | if (doubleValue != null) {
|
---|
408 | if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
|
---|
409 | ret = doubleValue.Value;
|
---|
410 | } else if (intValue != null)
|
---|
411 | ret = intValue.Value;
|
---|
412 | else if (timeSpanValue != null) {
|
---|
413 | ret = timeSpanValue.Value.TotalSeconds;
|
---|
414 | } else
|
---|
415 | ret = GetCategoricalValue(columnIndex, value.ToString());
|
---|
416 |
|
---|
417 | return ret;
|
---|
418 | }
|
---|
419 | }
|
---|
420 | private double GetCategoricalValue(int dimension, string value) {
|
---|
421 | if (!this.categoricalMapping.ContainsKey(dimension)) {
|
---|
422 | this.categoricalMapping[dimension] = new Dictionary<object, double>();
|
---|
423 | var orderedCategories = Content.Select(r => Content.GetValue(r, dimension).ToString())
|
---|
424 | .Distinct()
|
---|
425 | .OrderBy(x => x, new NaturalStringComparer());
|
---|
426 | int count = 1;
|
---|
427 | foreach (var category in orderedCategories) {
|
---|
428 | this.categoricalMapping[dimension].Add(category, count);
|
---|
429 | count++;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | return this.categoricalMapping[dimension][value];
|
---|
433 | }
|
---|
434 |
|
---|
435 | private double GetValue(IRun run, AxisDimension axisDimension) {
|
---|
436 | double value = double.NaN;
|
---|
437 | switch (axisDimension) {
|
---|
438 | case AxisDimension.Index: {
|
---|
439 | value = runToIndexMapping[run];
|
---|
440 | break;
|
---|
441 | }
|
---|
442 | default: {
|
---|
443 | throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
|
---|
444 | }
|
---|
445 | }
|
---|
446 | return value;
|
---|
447 | }
|
---|
448 | private double GetValue(IRun run, SizeDimension sizeDimension) {
|
---|
449 | double value = double.NaN;
|
---|
450 | switch (sizeDimension) {
|
---|
451 | case SizeDimension.Constant: {
|
---|
452 | value = 2;
|
---|
453 | break;
|
---|
454 | }
|
---|
455 | default: {
|
---|
456 | throw new ArgumentException("No handling strategy for " + sizeDimension.ToString() + " is defined.");
|
---|
457 | }
|
---|
458 | }
|
---|
459 | return value;
|
---|
460 | }
|
---|
461 | private void UpdateCursorInterval() {
|
---|
462 | Series series = chart.Series[0];
|
---|
463 | double[] xValues = (from point in series.Points
|
---|
464 | where !point.IsEmpty
|
---|
465 | select point.XValue)
|
---|
466 | .DefaultIfEmpty(1.0)
|
---|
467 | .ToArray();
|
---|
468 | double[] yValues = (from point in series.Points
|
---|
469 | where !point.IsEmpty
|
---|
470 | select point.YValues[0])
|
---|
471 | .DefaultIfEmpty(1.0)
|
---|
472 | .ToArray();
|
---|
473 |
|
---|
474 | double xRange = xValues.Max() - xValues.Min();
|
---|
475 | double yRange = yValues.Max() - yValues.Min();
|
---|
476 | if (xRange.IsAlmost(0.0)) xRange = 1.0;
|
---|
477 | if (yRange.IsAlmost(0.0)) yRange = 1.0;
|
---|
478 | double xDigits = (int)Math.Log10(xRange) - 3;
|
---|
479 | double yDigits = (int)Math.Log10(yRange) - 3;
|
---|
480 | double xZoomInterval = Math.Pow(10, xDigits);
|
---|
481 | double yZoomInterval = Math.Pow(10, yDigits);
|
---|
482 | this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
|
---|
483 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
484 |
|
---|
485 | //code to handle TimeSpanValues correct
|
---|
486 | int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
|
---|
487 | int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
488 | if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
|
---|
489 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
490 | columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
491 | if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
|
---|
492 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
493 | }
|
---|
494 |
|
---|
495 | #region Drag & drop and tooltip
|
---|
496 | private void chart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
497 | HitTestResult h = this.chart.HitTest(e.X, e.Y, ChartElementType.DataPoint);
|
---|
498 | if (h.ChartElementType == ChartElementType.DataPoint) {
|
---|
499 | IRun run = (IRun)((DataPoint)h.Object).Tag;
|
---|
500 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
501 | if (view != null) {
|
---|
502 | view.ReadOnly = this.ReadOnly;
|
---|
503 | view.Locked = this.Locked;
|
---|
504 | }
|
---|
505 |
|
---|
506 | this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
|
---|
507 | this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
|
---|
508 | }
|
---|
509 | UpdateAxisLabels();
|
---|
510 | }
|
---|
511 |
|
---|
512 | private void chart_MouseUp(object sender, MouseEventArgs e) {
|
---|
513 | if (isSelecting) {
|
---|
514 | System.Windows.Forms.DataVisualization.Charting.Cursor xCursor = chart.ChartAreas[0].CursorX;
|
---|
515 | System.Windows.Forms.DataVisualization.Charting.Cursor yCursor = chart.ChartAreas[0].CursorY;
|
---|
516 |
|
---|
517 | double minX = Math.Min(xCursor.SelectionStart, xCursor.SelectionEnd);
|
---|
518 | double maxX = Math.Max(xCursor.SelectionStart, xCursor.SelectionEnd);
|
---|
519 | double minY = Math.Min(yCursor.SelectionStart, yCursor.SelectionEnd);
|
---|
520 | double maxY = Math.Max(yCursor.SelectionStart, yCursor.SelectionEnd);
|
---|
521 |
|
---|
522 | //check for click to select model
|
---|
523 | if (minX == maxX && minY == maxY) {
|
---|
524 | HitTestResult hitTest = chart.HitTest(e.X, e.Y);
|
---|
525 | if (hitTest.ChartElementType == ChartElementType.DataPoint) {
|
---|
526 | int pointIndex = hitTest.PointIndex;
|
---|
527 | IRun run = (IRun)this.chart.Series[0].Points[pointIndex].Tag;
|
---|
528 | run.Color = colorDialog.Color;
|
---|
529 | }
|
---|
530 | } else {
|
---|
531 | List<DataPoint> selectedPoints = new List<DataPoint>();
|
---|
532 | foreach (DataPoint p in this.chart.Series[0].Points) {
|
---|
533 | if (p.XValue >= minX && p.XValue < maxX &&
|
---|
534 | p.YValues[0] >= minY && p.YValues[0] < maxY) {
|
---|
535 | selectedPoints.Add(p);
|
---|
536 | }
|
---|
537 | }
|
---|
538 | foreach (DataPoint p in selectedPoints) {
|
---|
539 | IRun run = (IRun)p.Tag;
|
---|
540 | run.Color = colorDialog.Color;
|
---|
541 | }
|
---|
542 | }
|
---|
543 | this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
|
---|
544 | this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
549 | HitTestResult h = this.chart.HitTest(e.X, e.Y);
|
---|
550 | string newTooltipText = string.Empty;
|
---|
551 | string oldTooltipText;
|
---|
552 | if (h.ChartElementType == ChartElementType.DataPoint) {
|
---|
553 | IRun run = (IRun)((DataPoint)h.Object).Tag;
|
---|
554 | newTooltipText = BuildTooltip(run);
|
---|
555 | } else if (h.ChartElementType == ChartElementType.AxisLabels) {
|
---|
556 | newTooltipText = ((CustomLabel)h.Object).ToolTip;
|
---|
557 | }
|
---|
558 |
|
---|
559 | oldTooltipText = this.tooltip.GetToolTip(chart);
|
---|
560 | if (newTooltipText != oldTooltipText)
|
---|
561 | this.tooltip.SetToolTip(chart, newTooltipText);
|
---|
562 | }
|
---|
563 |
|
---|
564 | private string BuildTooltip(IRun run) {
|
---|
565 | string tooltip;
|
---|
566 | tooltip = run.Name + System.Environment.NewLine;
|
---|
567 |
|
---|
568 | double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
|
---|
569 | double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
|
---|
570 | double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
|
---|
571 |
|
---|
572 | string xString = xValue == null ? string.Empty : xValue.Value.ToString();
|
---|
573 | string yString = yValue == null ? string.Empty : yValue.Value.ToString();
|
---|
574 | string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
|
---|
575 |
|
---|
576 | //code to handle TimeSpanValues correct
|
---|
577 | int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
|
---|
578 | int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
579 | if (xValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
|
---|
580 | TimeSpan time = TimeSpan.FromSeconds(xValue.Value);
|
---|
581 | xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
|
---|
582 | }
|
---|
583 | columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
584 | if (yValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
|
---|
585 | TimeSpan time = TimeSpan.FromSeconds(yValue.Value);
|
---|
586 | yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.Seconds);
|
---|
587 | }
|
---|
588 |
|
---|
589 | tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
|
---|
590 | tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
|
---|
591 | tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
|
---|
592 |
|
---|
593 | return tooltip;
|
---|
594 | }
|
---|
595 | #endregion
|
---|
596 |
|
---|
597 | #region GUI events and updating
|
---|
598 | private double GetXJitter(IRun run) {
|
---|
599 | if (!this.xJitter.ContainsKey(run))
|
---|
600 | this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
|
---|
601 | return this.xJitter[run];
|
---|
602 | }
|
---|
603 | private double GetYJitter(IRun run) {
|
---|
604 | if (!this.yJitter.ContainsKey(run))
|
---|
605 | this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
|
---|
606 | return this.yJitter[run];
|
---|
607 | }
|
---|
608 | private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
|
---|
609 | this.xJitterFactor = xTrackBar.Value / 100.0;
|
---|
610 | this.yJitterFactor = yTrackBar.Value / 100.0;
|
---|
611 | UpdateDataPointJitter();
|
---|
612 | }
|
---|
613 | private void sizeTrackBar_ValueChanged(object sender, EventArgs e) {
|
---|
614 | UpdateMarkerSizes();
|
---|
615 | }
|
---|
616 |
|
---|
617 | private void AxisComboBox_SelectedValueChanged(object sender, EventArgs e) {
|
---|
618 | bool axisSelected = xAxisComboBox.SelectedIndex != -1 && yAxisComboBox.SelectedIndex != -1;
|
---|
619 | xTrackBar.Enabled = yTrackBar.Enabled = axisSelected;
|
---|
620 | colorXAxisButton.Enabled = colorYAxisButton.Enabled = axisSelected;
|
---|
621 |
|
---|
622 | xAxisValue = (string)xAxisComboBox.SelectedItem;
|
---|
623 | yAxisValue = (string)yAxisComboBox.SelectedItem;
|
---|
624 | sizeAxisValue = (string)sizeComboBox.SelectedItem;
|
---|
625 |
|
---|
626 | UpdateDataPoints();
|
---|
627 | UpdateAxisLabels();
|
---|
628 | }
|
---|
629 | private void UpdateAxisLabels() {
|
---|
630 | Axis xAxis = this.chart.ChartAreas[0].AxisX;
|
---|
631 | Axis yAxis = this.chart.ChartAreas[0].AxisY;
|
---|
632 | int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
|
---|
633 | SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
|
---|
634 | SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
|
---|
635 | if (xAxisComboBox.SelectedItem != null)
|
---|
636 | xAxis.Title = xAxisComboBox.SelectedItem.ToString();
|
---|
637 | if (yAxisComboBox.SelectedItem != null)
|
---|
638 | yAxis.Title = yAxisComboBox.SelectedItem.ToString();
|
---|
639 | }
|
---|
640 |
|
---|
641 | private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
|
---|
642 | this.UpdateAxisLabels();
|
---|
643 | }
|
---|
644 |
|
---|
645 | private void SetCustomAxisLabels(Axis axis, int dimension) {
|
---|
646 | axis.CustomLabels.Clear();
|
---|
647 | if (categoricalMapping.ContainsKey(dimension)) {
|
---|
648 | foreach (var pair in categoricalMapping[dimension]) {
|
---|
649 | string labelText = pair.Key.ToString();
|
---|
650 | CustomLabel label = new CustomLabel();
|
---|
651 | label.ToolTip = labelText;
|
---|
652 | if (labelText.Length > 25)
|
---|
653 | labelText = labelText.Substring(0, 25) + " ... ";
|
---|
654 | label.Text = labelText;
|
---|
655 | label.GridTicks = GridTickTypes.TickMark;
|
---|
656 | label.FromPosition = pair.Value - 0.5;
|
---|
657 | label.ToPosition = pair.Value + 0.5;
|
---|
658 | axis.CustomLabels.Add(label);
|
---|
659 | }
|
---|
660 | } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
|
---|
661 | this.chart.ChartAreas[0].RecalculateAxesScale();
|
---|
662 | for (double i = axis.Minimum; i <= axis.Maximum; i += axis.LabelStyle.Interval) {
|
---|
663 | TimeSpan time = TimeSpan.FromSeconds(i);
|
---|
664 | string x = string.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
|
---|
665 | axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
|
---|
666 | }
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | private void zoomButton_CheckedChanged(object sender, EventArgs e) {
|
---|
671 | this.isSelecting = selectButton.Checked;
|
---|
672 | this.colorButton.Enabled = this.isSelecting;
|
---|
673 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
|
---|
674 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
|
---|
675 | }
|
---|
676 | private void colorButton_Click(object sender, EventArgs e) {
|
---|
677 | if (colorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
678 | this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
|
---|
679 | }
|
---|
680 | }
|
---|
681 | private Image GenerateImage(int width, int height, Color fillColor) {
|
---|
682 | Image colorImage = new Bitmap(width, height);
|
---|
683 | using (Graphics gfx = Graphics.FromImage(colorImage)) {
|
---|
684 | using (SolidBrush brush = new SolidBrush(fillColor)) {
|
---|
685 | gfx.FillRectangle(brush, 0, 0, width, height);
|
---|
686 | }
|
---|
687 | }
|
---|
688 | return colorImage;
|
---|
689 | }
|
---|
690 |
|
---|
691 | private IRun runToHide = null;
|
---|
692 | private void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
693 | var pos = Control.MousePosition;
|
---|
694 | var chartPos = chart.PointToClient(pos);
|
---|
695 |
|
---|
696 | HitTestResult h = this.chart.HitTest(chartPos.X, chartPos.Y);
|
---|
697 | if (h.ChartElementType == ChartElementType.DataPoint) {
|
---|
698 | runToHide = (IRun)((DataPoint)h.Object).Tag;
|
---|
699 | hideRunToolStripMenuItem.Visible = true;
|
---|
700 | } else {
|
---|
701 | runToHide = null;
|
---|
702 | hideRunToolStripMenuItem.Visible = false;
|
---|
703 | }
|
---|
704 |
|
---|
705 | }
|
---|
706 | private void hideRunToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
707 | var constraint = Content.Constraints.OfType<RunCollectionContentConstraint>().Where(c => c.Active).FirstOrDefault();
|
---|
708 | if (constraint == null) {
|
---|
709 | constraint = new RunCollectionContentConstraint();
|
---|
710 | Content.Constraints.Add(constraint);
|
---|
711 | constraint.Active = true;
|
---|
712 | }
|
---|
713 | constraint.ConstraintData.Add(runToHide);
|
---|
714 | }
|
---|
715 |
|
---|
716 | private void openBoxPlotViewToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
717 | RunCollectionBoxPlotView boxplotView = new RunCollectionBoxPlotView();
|
---|
718 | boxplotView.Content = this.Content;
|
---|
719 | boxplotView.xAxisComboBox.SelectedItem = xAxisComboBox.SelectedItem;
|
---|
720 | boxplotView.yAxisComboBox.SelectedItem = yAxisComboBox.SelectedItem;
|
---|
721 | boxplotView.Show();
|
---|
722 | }
|
---|
723 |
|
---|
724 | private void getDataAsMatrixToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
725 | int xCol = Matrix.ColumnNames.ToList().IndexOf(xAxisValue);
|
---|
726 | var grouped = new Dictionary<string, List<string>>();
|
---|
727 | Dictionary<double, string> reverseMapping = null;
|
---|
728 | if (categoricalMapping.ContainsKey(xCol))
|
---|
729 | reverseMapping = categoricalMapping[xCol].ToDictionary(x => x.Value, y => y.Key.ToString());
|
---|
730 | foreach (var run in Content.Where(r => r.Visible)) {
|
---|
731 | var x = GetValue(run, xAxisValue);
|
---|
732 | var y = GetValue(run, yAxisValue);
|
---|
733 | if (!(x.HasValue && y.HasValue)) continue;
|
---|
734 |
|
---|
735 | var category = reverseMapping == null ? x.Value.ToString() : reverseMapping[x.Value];
|
---|
736 | if (!grouped.ContainsKey(category)) grouped[category] = new List<string>();
|
---|
737 | grouped[category].Add(y.Value.ToString());
|
---|
738 | }
|
---|
739 |
|
---|
740 | if (!grouped.Any()) return;
|
---|
741 | var matrix = new StringMatrix(grouped.Values.Max(x => x.Count), grouped.Count) {
|
---|
742 | ColumnNames = grouped.Keys.ToArray()
|
---|
743 | };
|
---|
744 | int i = 0;
|
---|
745 | foreach (var col in matrix.ColumnNames) {
|
---|
746 | int j = 0;
|
---|
747 | foreach (var y in grouped[col])
|
---|
748 | matrix[j++, i] = y;
|
---|
749 | i++;
|
---|
750 | }
|
---|
751 |
|
---|
752 | MainFormManager.MainForm.ShowContent(matrix);
|
---|
753 | }
|
---|
754 | #endregion
|
---|
755 |
|
---|
756 | #region Automatic coloring
|
---|
757 | private void colorXAxisButton_Click(object sender, EventArgs e) {
|
---|
758 | ColorRuns(xAxisValue);
|
---|
759 | }
|
---|
760 |
|
---|
761 | private void colorYAxisButton_Click(object sender, EventArgs e) {
|
---|
762 | ColorRuns(yAxisValue);
|
---|
763 | }
|
---|
764 |
|
---|
765 | private void ColorRuns(string axisValue) {
|
---|
766 | var runs = Content.Where(r => r.Visible).Select(r => new { Run = r, Value = GetValue(r, axisValue) }).Where(r => r.Value.HasValue).ToList();
|
---|
767 | double minValue = runs.Min(r => r.Value.Value);
|
---|
768 | double maxValue = runs.Max(r => r.Value.Value);
|
---|
769 | double range = maxValue - minValue;
|
---|
770 |
|
---|
771 | if (range.IsAlmost(0)) {
|
---|
772 | Color c = ColorGradient.Colors[0];
|
---|
773 | runs.ForEach(r => r.Run.Color = c);
|
---|
774 | } else {
|
---|
775 | int maxColorIndex = ColorGradient.Colors.Count - 1;
|
---|
776 | foreach (var r in runs) {
|
---|
777 | int colorIndex = (int)(maxColorIndex * (r.Value - minValue) / (range));
|
---|
778 | r.Run.Color = ColorGradient.Colors[colorIndex];
|
---|
779 | }
|
---|
780 | }
|
---|
781 | }
|
---|
782 | #endregion
|
---|
783 | }
|
---|
784 | }
|
---|