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.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<int, Dictionary<object, double>> categoricalMapping;
|
---|
46 | private Dictionary<IRun, double> xJitter;
|
---|
47 | private Dictionary<IRun, double> yJitter;
|
---|
48 | private double xJitterFactor = 0.0;
|
---|
49 | private double yJitterFactor = 0.0;
|
---|
50 | private Random random;
|
---|
51 | private bool isSelecting = false;
|
---|
52 |
|
---|
53 | public RunCollectionBubbleChartView() {
|
---|
54 | InitializeComponent();
|
---|
55 |
|
---|
56 | this.categoricalMapping = new Dictionary<int, Dictionary<object, double>>();
|
---|
57 | this.xJitter = new Dictionary<IRun, double>();
|
---|
58 | this.yJitter = new Dictionary<IRun, double>();
|
---|
59 | this.random = new Random();
|
---|
60 | this.colorDialog.Color = Color.Black;
|
---|
61 | this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
|
---|
62 | this.isSelecting = false;
|
---|
63 |
|
---|
64 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
65 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
66 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
67 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
68 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !this.isSelecting;
|
---|
69 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !this.isSelecting;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public new RunCollection Content {
|
---|
73 | get { return (RunCollection)base.Content; }
|
---|
74 | set { base.Content = value; }
|
---|
75 | }
|
---|
76 | public IStringConvertibleMatrix Matrix {
|
---|
77 | get { return this.Content; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override void RegisterContentEvents() {
|
---|
81 | base.RegisterContentEvents();
|
---|
82 | Content.Reset += new EventHandler(Content_Reset);
|
---|
83 | Content.ColumnNamesChanged += new EventHandler(Content_ColumnNamesChanged);
|
---|
84 | Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
85 | Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
86 | Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
87 | RegisterRunEvents(Content);
|
---|
88 | }
|
---|
89 | protected override void DeregisterContentEvents() {
|
---|
90 | base.DeregisterContentEvents();
|
---|
91 | Content.Reset -= new EventHandler(Content_Reset);
|
---|
92 | Content.ColumnNamesChanged -= new EventHandler(Content_ColumnNamesChanged);
|
---|
93 | Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
94 | Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
95 | Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
96 | DeregisterRunEvents(Content);
|
---|
97 | }
|
---|
98 | protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
|
---|
99 | foreach (IRun run in runs)
|
---|
100 | run.Changed += new EventHandler(run_Changed);
|
---|
101 | }
|
---|
102 | protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
|
---|
103 | foreach (IRun run in runs)
|
---|
104 | run.Changed -= new EventHandler(run_Changed);
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
108 | DeregisterRunEvents(e.OldItems);
|
---|
109 | RegisterRunEvents(e.Items);
|
---|
110 | }
|
---|
111 | private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
112 | DeregisterRunEvents(e.Items);
|
---|
113 | }
|
---|
114 | private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
|
---|
115 | RegisterRunEvents(e.Items);
|
---|
116 | }
|
---|
117 | private void run_Changed(object sender, EventArgs e) {
|
---|
118 | if (InvokeRequired)
|
---|
119 | this.Invoke(new EventHandler(run_Changed), sender, e);
|
---|
120 | else {
|
---|
121 | IRun run = (IRun)sender;
|
---|
122 | UpdateRun(run);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void UpdateRun(IRun run) {
|
---|
127 | DataPoint point = this.chart.Series[0].Points.Where(p => p.Tag == run).SingleOrDefault();
|
---|
128 | if (point != null) {
|
---|
129 | point.Color = run.Color;
|
---|
130 | if (!run.Visible)
|
---|
131 | this.chart.Series[0].Points.Remove(point);
|
---|
132 | } else
|
---|
133 | AddDataPoint(run);
|
---|
134 | UpdateCursorInterval();
|
---|
135 |
|
---|
136 |
|
---|
137 | if (this.chart.Series[0].Points.Count == 0)
|
---|
138 | noRunsLabel.Visible = true;
|
---|
139 | else
|
---|
140 | noRunsLabel.Visible = false;
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected override void OnContentChanged() {
|
---|
144 | base.OnContentChanged();
|
---|
145 | this.categoricalMapping.Clear();
|
---|
146 | UpdateComboBoxes();
|
---|
147 | UpdateDataPoints();
|
---|
148 | }
|
---|
149 | private void Content_ColumnNamesChanged(object sender, EventArgs e) {
|
---|
150 | if (InvokeRequired)
|
---|
151 | Invoke(new EventHandler(Content_ColumnNamesChanged), sender, e);
|
---|
152 | else
|
---|
153 | UpdateComboBoxes();
|
---|
154 | }
|
---|
155 |
|
---|
156 | private void UpdateComboBoxes() {
|
---|
157 | string selectedXAxis = (string)this.xAxisComboBox.SelectedItem;
|
---|
158 | string selectedYAxis = (string)this.yAxisComboBox.SelectedItem;
|
---|
159 | string selectedSizeAxis = (string)this.sizeComboBox.SelectedItem;
|
---|
160 | this.xAxisComboBox.Items.Clear();
|
---|
161 | this.yAxisComboBox.Items.Clear();
|
---|
162 | this.sizeComboBox.Items.Clear();
|
---|
163 | if (Content != null) {
|
---|
164 | string[] additionalAxisDimension = Enum.GetNames(typeof(AxisDimension));
|
---|
165 | this.xAxisComboBox.Items.AddRange(additionalAxisDimension);
|
---|
166 | this.xAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
|
---|
167 | this.yAxisComboBox.Items.AddRange(additionalAxisDimension);
|
---|
168 | this.yAxisComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
|
---|
169 | string[] additionalSizeDimension = Enum.GetNames(typeof(SizeDimension));
|
---|
170 | this.sizeComboBox.Items.AddRange(additionalSizeDimension);
|
---|
171 | this.sizeComboBox.Items.AddRange(Matrix.ColumnNames.ToArray());
|
---|
172 | this.sizeComboBox.SelectedItem = SizeDimension.Constant.ToString();
|
---|
173 |
|
---|
174 | bool changed = false;
|
---|
175 | if (selectedXAxis != null && xAxisComboBox.Items.Contains(selectedXAxis)) {
|
---|
176 | xAxisComboBox.SelectedItem = selectedXAxis;
|
---|
177 | changed = true;
|
---|
178 | }
|
---|
179 | if (selectedYAxis != null && yAxisComboBox.Items.Contains(selectedYAxis)) {
|
---|
180 | yAxisComboBox.SelectedItem = selectedYAxis;
|
---|
181 | changed = true;
|
---|
182 | }
|
---|
183 | if (selectedSizeAxis != null && sizeComboBox.Items.Contains(selectedSizeAxis)) {
|
---|
184 | sizeComboBox.SelectedItem = selectedSizeAxis;
|
---|
185 | changed = true;
|
---|
186 | }
|
---|
187 | if (changed)
|
---|
188 | UpdateDataPoints();
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void Content_Reset(object sender, EventArgs e) {
|
---|
193 | if (InvokeRequired)
|
---|
194 | Invoke(new EventHandler(Content_Reset), sender, e);
|
---|
195 | else {
|
---|
196 | this.categoricalMapping.Clear();
|
---|
197 | UpdateDataPoints();
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void UpdateDataPoints() {
|
---|
202 | Series series = this.chart.Series[0];
|
---|
203 | series.Points.Clear();
|
---|
204 | if (Content != null) {
|
---|
205 | foreach (IRun run in this.Content)
|
---|
206 | this.AddDataPoint(run);
|
---|
207 |
|
---|
208 | //check to correct max bubble size
|
---|
209 | if (this.chart.Series[0].Points.Select(p => p.YValues[1]).Distinct().Count() == 1)
|
---|
210 | this.chart.Series[0]["BubbleMaxSize"] = "2";
|
---|
211 | else
|
---|
212 | this.chart.Series[0]["BubbleMaxSize"] = "7";
|
---|
213 |
|
---|
214 | if (this.chart.Series[0].Points.Count == 0)
|
---|
215 | noRunsLabel.Visible = true;
|
---|
216 | else
|
---|
217 | noRunsLabel.Visible = false;
|
---|
218 | UpdateCursorInterval();
|
---|
219 | }
|
---|
220 | }
|
---|
221 | private void AddDataPoint(IRun run) {
|
---|
222 | double? xValue;
|
---|
223 | double? yValue;
|
---|
224 | double? sizeValue;
|
---|
225 | Series series = this.chart.Series[0];
|
---|
226 | int row = this.Content.ToList().IndexOf(run);
|
---|
227 |
|
---|
228 | if (!xAxisComboBox.DroppedDown)
|
---|
229 | this.xAxisValue = (string)xAxisComboBox.SelectedItem;
|
---|
230 | if (!yAxisComboBox.DroppedDown)
|
---|
231 | this.yAxisValue = (string)yAxisComboBox.SelectedItem;
|
---|
232 | if (!sizeComboBox.DroppedDown)
|
---|
233 | this.sizeAxisValue = (string)sizeComboBox.SelectedItem;
|
---|
234 |
|
---|
235 | xValue = GetValue(run, this.xAxisValue);
|
---|
236 | yValue = GetValue(run, this.yAxisValue);
|
---|
237 | sizeValue = GetValue(run, this.sizeAxisValue);
|
---|
238 |
|
---|
239 | if (xValue.HasValue && yValue.HasValue && sizeValue.HasValue) {
|
---|
240 | xValue = xValue.Value;
|
---|
241 | if (!xJitterFactor.IsAlmost(0.0))
|
---|
242 | xValue += 0.1 * GetXJitter(run) * xJitterFactor * (this.chart.ChartAreas[0].AxisX.Maximum - this.chart.ChartAreas[0].AxisX.Minimum);
|
---|
243 | yValue = yValue.Value;
|
---|
244 | if (!yJitterFactor.IsAlmost(0.0))
|
---|
245 | yValue += 0.1 * GetYJitter(run) * yJitterFactor * (this.chart.ChartAreas[0].AxisY.Maximum - this.chart.ChartAreas[0].AxisY.Minimum);
|
---|
246 | if (run.Visible) {
|
---|
247 | DataPoint point = new DataPoint(xValue.Value, new double[] { yValue.Value, sizeValue.Value });
|
---|
248 | point.Tag = run;
|
---|
249 | point.Color = run.Color;
|
---|
250 | series.Points.Add(point);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 | private double? GetValue(IRun run, string columnName) {
|
---|
255 | if (run == null || string.IsNullOrEmpty(columnName))
|
---|
256 | return null;
|
---|
257 |
|
---|
258 | if (Enum.IsDefined(typeof(AxisDimension), columnName)) {
|
---|
259 | AxisDimension axisDimension = (AxisDimension)Enum.Parse(typeof(AxisDimension), columnName);
|
---|
260 | return GetValue(run, axisDimension);
|
---|
261 | } else if (Enum.IsDefined(typeof(SizeDimension), columnName)) {
|
---|
262 | SizeDimension sizeDimension = (SizeDimension)Enum.Parse(typeof(SizeDimension), columnName);
|
---|
263 | return GetValue(run, sizeDimension);
|
---|
264 | } else {
|
---|
265 | int columnIndex = Matrix.ColumnNames.ToList().IndexOf(columnName);
|
---|
266 | IItem value = Content.GetValue(run, columnIndex);
|
---|
267 | if (value == null)
|
---|
268 | return null;
|
---|
269 |
|
---|
270 | DoubleValue doubleValue = value as DoubleValue;
|
---|
271 | IntValue intValue = value as IntValue;
|
---|
272 | TimeSpanValue timeSpanValue = value as TimeSpanValue;
|
---|
273 | double? ret = null;
|
---|
274 | if (doubleValue != null) {
|
---|
275 | if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value))
|
---|
276 | ret = doubleValue.Value;
|
---|
277 | } else if (intValue != null)
|
---|
278 | ret = intValue.Value;
|
---|
279 | else if (timeSpanValue != null) {
|
---|
280 | ret = timeSpanValue.Value.TotalSeconds;
|
---|
281 | } else
|
---|
282 | ret = GetCategoricalValue(columnIndex, value.ToString());
|
---|
283 |
|
---|
284 | return ret;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | private double GetCategoricalValue(int dimension, string value) {
|
---|
288 | if (!this.categoricalMapping.ContainsKey(dimension))
|
---|
289 | this.categoricalMapping[dimension] = new Dictionary<object, double>();
|
---|
290 | if (!this.categoricalMapping[dimension].ContainsKey(value)) {
|
---|
291 | if (this.categoricalMapping[dimension].Values.Count == 0)
|
---|
292 | this.categoricalMapping[dimension][value] = 1.0;
|
---|
293 | else
|
---|
294 | this.categoricalMapping[dimension][value] = this.categoricalMapping[dimension].Values.Max() + 1.0;
|
---|
295 | }
|
---|
296 | return this.categoricalMapping[dimension][value];
|
---|
297 | }
|
---|
298 | private double GetValue(IRun run, AxisDimension axisDimension) {
|
---|
299 | double value = double.NaN;
|
---|
300 | switch (axisDimension) {
|
---|
301 | case AxisDimension.Index: {
|
---|
302 | value = Content.ToList().IndexOf(run);
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | default: {
|
---|
306 | throw new ArgumentException("No handling strategy for " + axisDimension.ToString() + " is defined.");
|
---|
307 | }
|
---|
308 | }
|
---|
309 | return value;
|
---|
310 | }
|
---|
311 | private double GetValue(IRun run, SizeDimension sizeDimension) {
|
---|
312 | double value = double.NaN;
|
---|
313 | switch (sizeDimension) {
|
---|
314 | case SizeDimension.Constant: {
|
---|
315 | value = 2;
|
---|
316 | break;
|
---|
317 | }
|
---|
318 | default: {
|
---|
319 | throw new ArgumentException("No handling strategy for " + sizeDimension.ToString() + " is defined.");
|
---|
320 | }
|
---|
321 | }
|
---|
322 | return value;
|
---|
323 | }
|
---|
324 | private void UpdateCursorInterval() {
|
---|
325 | Series series = chart.Series[0];
|
---|
326 | double[] xValues = (from point in series.Points
|
---|
327 | where !point.IsEmpty
|
---|
328 | select point.XValue)
|
---|
329 | .DefaultIfEmpty(1.0)
|
---|
330 | .ToArray();
|
---|
331 | double[] yValues = (from point in series.Points
|
---|
332 | where !point.IsEmpty
|
---|
333 | select point.YValues[0])
|
---|
334 | .DefaultIfEmpty(1.0)
|
---|
335 | .ToArray();
|
---|
336 |
|
---|
337 | double xRange = xValues.Max() - xValues.Min();
|
---|
338 | double yRange = yValues.Max() - yValues.Min();
|
---|
339 | if (xRange.IsAlmost(0.0)) xRange = 1.0;
|
---|
340 | if (yRange.IsAlmost(0.0)) yRange = 1.0;
|
---|
341 | double xDigits = (int)Math.Log10(xRange) - 3;
|
---|
342 | double yDigits = (int)Math.Log10(yRange) - 3;
|
---|
343 | double xZoomInterval = Math.Pow(10, xDigits);
|
---|
344 | double yZoomInterval = Math.Pow(10, yDigits);
|
---|
345 | this.chart.ChartAreas[0].CursorX.Interval = xZoomInterval;
|
---|
346 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
347 |
|
---|
348 | //code to handle TimeSpanValues correct
|
---|
349 | int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
|
---|
350 | int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
351 | if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
|
---|
352 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
353 | columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
354 | if (columnIndex >= 0 && Content.GetValue(0, columnIndex) is TimeSpanValue)
|
---|
355 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
356 | }
|
---|
357 |
|
---|
358 | #region drag and drop and tooltip
|
---|
359 | private IRun draggedRun;
|
---|
360 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
361 | HitTestResult h = this.chart.HitTest(e.X, e.Y);
|
---|
362 | if (h.ChartElementType == ChartElementType.DataPoint) {
|
---|
363 | IRun run = (IRun)((DataPoint)h.Object).Tag;
|
---|
364 | if (e.Clicks >= 2) {
|
---|
365 | IContentView view = MainFormManager.MainForm.ShowContent(run);
|
---|
366 | if (view != null) {
|
---|
367 | view.ReadOnly = this.ReadOnly;
|
---|
368 | view.Locked = this.Locked;
|
---|
369 | }
|
---|
370 | } else
|
---|
371 | this.draggedRun = run;
|
---|
372 | this.chart.ChartAreas[0].CursorX.SetSelectionPosition(double.NaN, double.NaN);
|
---|
373 | this.chart.ChartAreas[0].CursorY.SetSelectionPosition(double.NaN, double.NaN);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | private void chart_MouseUp(object sender, MouseEventArgs e) {
|
---|
378 | if (isSelecting) {
|
---|
379 | System.Windows.Forms.DataVisualization.Charting.Cursor xCursor = chart.ChartAreas[0].CursorX;
|
---|
380 | System.Windows.Forms.DataVisualization.Charting.Cursor yCursor = chart.ChartAreas[0].CursorY;
|
---|
381 |
|
---|
382 | double minX = Math.Min(xCursor.SelectionStart, xCursor.SelectionEnd);
|
---|
383 | double maxX = Math.Max(xCursor.SelectionStart, xCursor.SelectionEnd);
|
---|
384 | double minY = Math.Min(yCursor.SelectionStart, yCursor.SelectionEnd);
|
---|
385 | double maxY = Math.Max(yCursor.SelectionStart, yCursor.SelectionEnd);
|
---|
386 |
|
---|
387 | //check for click to select model
|
---|
388 | if (minX == maxX && minY == maxY) {
|
---|
389 | HitTestResult hitTest = chart.HitTest(e.X, e.Y);
|
---|
390 | if (hitTest.ChartElementType == ChartElementType.DataPoint) {
|
---|
391 | int pointIndex = hitTest.PointIndex;
|
---|
392 | IRun run = (IRun)this.chart.Series[0].Points[pointIndex].Tag;
|
---|
393 | run.Color = colorDialog.Color;
|
---|
394 | }
|
---|
395 | } else {
|
---|
396 | List<DataPoint> selectedPoints = new List<DataPoint>();
|
---|
397 | foreach (DataPoint p in this.chart.Series[0].Points) {
|
---|
398 | if (p.XValue >= minX && p.XValue < maxX &&
|
---|
399 | p.YValues[0] >= minY && p.YValues[0] < maxY) {
|
---|
400 | selectedPoints.Add(p);
|
---|
401 | }
|
---|
402 | }
|
---|
403 | foreach (DataPoint p in selectedPoints) {
|
---|
404 | IRun run = (IRun)p.Tag;
|
---|
405 | run.Color = colorDialog.Color;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | this.chart.ChartAreas[0].CursorX.SelectionStart = this.chart.ChartAreas[0].CursorX.SelectionEnd;
|
---|
409 | this.chart.ChartAreas[0].CursorY.SelectionStart = this.chart.ChartAreas[0].CursorY.SelectionEnd;
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
414 | HitTestResult h = this.chart.HitTest(e.X, e.Y);
|
---|
415 | if (!Locked) {
|
---|
416 | if (this.draggedRun != null && h.ChartElementType != ChartElementType.DataPoint) {
|
---|
417 | DataObject data = new DataObject();
|
---|
418 | data.SetData("Type", draggedRun.GetType());
|
---|
419 | data.SetData("Value", draggedRun);
|
---|
420 | if (ReadOnly)
|
---|
421 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
422 | else {
|
---|
423 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
424 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
425 | Content.Remove(draggedRun);
|
---|
426 | }
|
---|
427 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
|
---|
428 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
|
---|
429 | this.draggedRun = null;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | string newTooltipText = string.Empty;
|
---|
433 | string oldTooltipText;
|
---|
434 | if (h.ChartElementType == ChartElementType.DataPoint) {
|
---|
435 | IRun run = (IRun)((DataPoint)h.Object).Tag;
|
---|
436 | newTooltipText = BuildTooltip(run);
|
---|
437 | }
|
---|
438 |
|
---|
439 | oldTooltipText = this.tooltip.GetToolTip(chart);
|
---|
440 | if (newTooltipText != oldTooltipText)
|
---|
441 | this.tooltip.SetToolTip(chart, newTooltipText);
|
---|
442 | }
|
---|
443 |
|
---|
444 | private string BuildTooltip(IRun run) {
|
---|
445 | string tooltip;
|
---|
446 | tooltip = run.Name + System.Environment.NewLine;
|
---|
447 |
|
---|
448 | double? xValue = this.GetValue(run, (string)xAxisComboBox.SelectedItem);
|
---|
449 | double? yValue = this.GetValue(run, (string)yAxisComboBox.SelectedItem);
|
---|
450 | double? sizeValue = this.GetValue(run, (string)sizeComboBox.SelectedItem);
|
---|
451 |
|
---|
452 | string xString = xValue == null ? string.Empty : xValue.Value.ToString();
|
---|
453 | string yString = yValue == null ? string.Empty : yValue.Value.ToString();
|
---|
454 | string sizeString = sizeValue == null ? string.Empty : sizeValue.Value.ToString();
|
---|
455 |
|
---|
456 | //code to handle TimeSpanValues correct
|
---|
457 | int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
|
---|
458 | int columnIndex = xAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
459 | if (xValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
|
---|
460 | TimeSpan time = TimeSpan.FromSeconds(xValue.Value);
|
---|
461 | xString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.TotalSeconds);
|
---|
462 | }
|
---|
463 | columnIndex = yAxisComboBox.SelectedIndex - axisDimensionCount;
|
---|
464 | if (yValue.HasValue && columnIndex > 0 && Content.GetValue(0, columnIndex) is TimeSpanValue) {
|
---|
465 | TimeSpan time = TimeSpan.FromSeconds(yValue.Value);
|
---|
466 | yString = string.Format("{0:00}:{1:00}:{2:00.00}", (int)time.TotalHours, time.Minutes, time.TotalSeconds);
|
---|
467 | }
|
---|
468 |
|
---|
469 | tooltip += xAxisComboBox.SelectedItem + " : " + xString + Environment.NewLine;
|
---|
470 | tooltip += yAxisComboBox.SelectedItem + " : " + yString + Environment.NewLine;
|
---|
471 | tooltip += sizeComboBox.SelectedItem + " : " + sizeString + Environment.NewLine;
|
---|
472 |
|
---|
473 | return tooltip;
|
---|
474 | }
|
---|
475 | #endregion
|
---|
476 |
|
---|
477 | #region GUI events and updating
|
---|
478 | private double GetXJitter(IRun run) {
|
---|
479 | if (!this.xJitter.ContainsKey(run))
|
---|
480 | this.xJitter[run] = random.NextDouble() * 2.0 - 1.0;
|
---|
481 | return this.xJitter[run];
|
---|
482 | }
|
---|
483 | private double GetYJitter(IRun run) {
|
---|
484 | if (!this.yJitter.ContainsKey(run))
|
---|
485 | this.yJitter[run] = random.NextDouble() * 2.0 - 1.0;
|
---|
486 | return this.yJitter[run];
|
---|
487 | }
|
---|
488 | private void jitterTrackBar_ValueChanged(object sender, EventArgs e) {
|
---|
489 | this.xJitterFactor = xTrackBar.Value / 100.0;
|
---|
490 | this.yJitterFactor = yTrackBar.Value / 100.0;
|
---|
491 | this.UpdateDataPoints();
|
---|
492 | }
|
---|
493 |
|
---|
494 | private void AxisComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
495 | UpdateDataPoints();
|
---|
496 | UpdateAxisLabels();
|
---|
497 | }
|
---|
498 | private void UpdateAxisLabels() {
|
---|
499 | Axis xAxis = this.chart.ChartAreas[0].AxisX;
|
---|
500 | Axis yAxis = this.chart.ChartAreas[0].AxisY;
|
---|
501 | int axisDimensionCount = Enum.GetNames(typeof(AxisDimension)).Count();
|
---|
502 | SetCustomAxisLabels(xAxis, xAxisComboBox.SelectedIndex - axisDimensionCount);
|
---|
503 | SetCustomAxisLabels(yAxis, yAxisComboBox.SelectedIndex - axisDimensionCount);
|
---|
504 | }
|
---|
505 |
|
---|
506 | private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
|
---|
507 | this.UpdateAxisLabels();
|
---|
508 | }
|
---|
509 |
|
---|
510 | private void SetCustomAxisLabels(Axis axis, int dimension) {
|
---|
511 | axis.CustomLabels.Clear();
|
---|
512 | if (categoricalMapping.ContainsKey(dimension)) {
|
---|
513 | CustomLabel label = null;
|
---|
514 | foreach (var pair in categoricalMapping[dimension]) {
|
---|
515 | string labelText = pair.Key.ToString();
|
---|
516 | if (labelText.Length > 25)
|
---|
517 | labelText = labelText.Substring(0, 25) + " ... ";
|
---|
518 | label = axis.CustomLabels.Add(pair.Value - 0.5, pair.Value + 0.5, labelText);
|
---|
519 | label.GridTicks = GridTickTypes.TickMark;
|
---|
520 | }
|
---|
521 | axis.IsLabelAutoFit = false;
|
---|
522 | axis.LabelStyle.Enabled = true;
|
---|
523 | axis.LabelStyle.Angle = 0;
|
---|
524 | axis.LabelStyle.TruncatedLabels = true;
|
---|
525 | } else if (dimension > 0 && Content.GetValue(0, dimension) is TimeSpanValue) {
|
---|
526 | this.chart.ChartAreas[0].RecalculateAxesScale();
|
---|
527 | for (double i = axis.Minimum; i <= axis.Maximum; i += axis.LabelStyle.Interval) {
|
---|
528 | TimeSpan time = TimeSpan.FromSeconds(i);
|
---|
529 | string x = string.Format("{0:00}:{1:00}:{2:00}", (int)time.Hours, time.Minutes, time.Seconds);
|
---|
530 | axis.CustomLabels.Add(i - axis.LabelStyle.Interval / 2, i + axis.LabelStyle.Interval / 2, x);
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | private void zoomButton_CheckedChanged(object sender, EventArgs e) {
|
---|
536 | this.isSelecting = selectButton.Checked;
|
---|
537 | this.colorButton.Enabled = this.isSelecting;
|
---|
538 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = !isSelecting;
|
---|
539 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = !isSelecting;
|
---|
540 | }
|
---|
541 | private void colorButton_Click(object sender, EventArgs e) {
|
---|
542 | if (colorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
543 | this.colorButton.Image = this.GenerateImage(16, 16, this.colorDialog.Color);
|
---|
544 | }
|
---|
545 | }
|
---|
546 | private Image GenerateImage(int width, int height, Color fillColor) {
|
---|
547 | Image colorImage = new Bitmap(width, height);
|
---|
548 | using (Graphics gfx = Graphics.FromImage(colorImage)) {
|
---|
549 | using (SolidBrush brush = new SolidBrush(fillColor)) {
|
---|
550 | gfx.FillRectangle(brush, 0, 0, width, height);
|
---|
551 | }
|
---|
552 | }
|
---|
553 | return colorImage;
|
---|
554 | }
|
---|
555 | #endregion
|
---|
556 | }
|
---|
557 | }
|
---|