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