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