source: branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/GradientChart.cs @ 14006

Last change on this file since 14006 was 14006, checked in by bburlacu, 5 years ago

#2597: Fixed a couple of bugs in the GradientChart related to the configuration of axis limits.

File size: 24.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Globalization;
26using System.Linq;
27using System.Threading;
28using System.Threading.Tasks;
29using System.Windows.Forms;
30using System.Windows.Forms.DataVisualization.Charting;
31using HeuristicLab.Common;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.Visualization.ChartControlsExtensions;
34
35namespace HeuristicLab.Problems.DataAnalysis.Views {
36  public partial class GradientChart : UserControl {
37    private ModifiableDataset sharedFixedVariables; // used for syncronising variable values between charts
38    private ModifiableDataset internalDataset; // holds the x values for each point drawn
39
40    private CancellationTokenSource cancelCurrentRecalculateSource;
41
42    private readonly List<IRegressionSolution> solutions;
43    private readonly Dictionary<IRegressionSolution, Series> seriesCache;
44    private readonly Dictionary<IRegressionSolution, Series> ciSeriesCache;
45
46    private readonly ToolStripMenuItem configToolStripMenuItem;
47    private readonly GradientChartConfigurationDialog configurationDialog;
48
49    #region Properties
50    public bool ShowLegend {
51      get { return chart.Legends[0].Enabled; }
52      set { chart.Legends[0].Enabled = value; }
53    }
54    public bool ShowCursor {
55      get { return chart.Annotations[0].Visible; }
56      set {
57        chart.Annotations[0].Visible = value;
58        if (!value) chart.ChartAreas[0].AxisX.Title = string.Empty;
59      }
60    }
61
62    public bool ShowConfigButton {
63      get { return configurationButton.Visible; }
64      set { configurationButton.Visible = value; }
65    }
66
67    private int xAxisTicks = 5;
68    public int XAxisTicks {
69      get { return xAxisTicks; }
70      set {
71        if (value != xAxisTicks) {
72          xAxisTicks = value;
73          SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
74          RecalculateInternalDataset();
75        }
76      }
77    }
78    private double? fixedXAxisMin;
79    public double? FixedXAxisMin {
80      get { return fixedXAxisMin; }
81      set {
82        if ((value.HasValue && fixedXAxisMin.HasValue && !value.Value.IsAlmost(fixedXAxisMin.Value)) || (value.HasValue != fixedXAxisMin.HasValue)) {
83          fixedXAxisMin = value;
84          if (trainingMin < trainingMax) {
85            SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
86            RecalculateInternalDataset();
87            // set the vertical line position
88            if (VerticalLineAnnotation.X <= fixedXAxisMin) {
89              var axisX = chart.ChartAreas[0].AxisX;
90              var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
91              VerticalLineAnnotation.X = axisX.Minimum + step;
92            }
93          }
94        }
95      }
96    }
97    private double? fixedXAxisMax;
98    public double? FixedXAxisMax {
99      get { return fixedXAxisMax; }
100      set {
101        if ((value.HasValue && fixedXAxisMax.HasValue && !value.Value.IsAlmost(fixedXAxisMax.Value)) || (value.HasValue != fixedXAxisMax.HasValue)) {
102          fixedXAxisMax = value;
103          if (trainingMin < trainingMax) {
104            SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
105            RecalculateInternalDataset();
106            // set the vertical line position
107            if (VerticalLineAnnotation.X >= fixedXAxisMax) {
108              var axisX = chart.ChartAreas[0].AxisX;
109              var step = (axisX.Maximum - axisX.Minimum) / drawingSteps;
110              VerticalLineAnnotation.X = axisX.Maximum - step;
111            }
112          }
113        }
114      }
115    }
116
117    private int yAxisTicks = 5;
118    public int YAxisTicks {
119      get { return yAxisTicks; }
120      set {
121        if (value != yAxisTicks) {
122          yAxisTicks = value;
123          SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
124          RecalculateInternalDataset();
125        }
126      }
127    }
128    private double? fixedYAxisMin;
129    public double? FixedYAxisMin {
130      get { return fixedYAxisMin; }
131      set {
132        if ((value.HasValue && fixedYAxisMin.HasValue && !value.Value.IsAlmost(fixedYAxisMin.Value)) || (value.HasValue != fixedYAxisMin.HasValue)) {
133          fixedYAxisMin = value;
134          // the check below prevents an exception when yMin >= yMax
135          if (yMin < yMax)
136            SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
137        }
138      }
139    }
140    private double? fixedYAxisMax;
141    public double? FixedYAxisMax {
142      get { return fixedYAxisMax; }
143      set {
144        if ((value.HasValue && fixedYAxisMax.HasValue && !value.Value.IsAlmost(fixedYAxisMax.Value)) || (value.HasValue != fixedYAxisMax.HasValue)) {
145          fixedYAxisMax = value;
146          // the check below prevents an exception when yMin >= yMax
147          if (yMin < yMax)
148            SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
149        }
150      }
151    }
152
153    private double trainingMin = 1;
154    private double trainingMax = -1;
155
156    private int drawingSteps = 1000;
157    public int DrawingSteps {
158      get { return drawingSteps; }
159      set {
160        if (value != drawingSteps) {
161          drawingSteps = value;
162          RecalculateInternalDataset();
163          ResizeAllSeriesData();
164        }
165      }
166    }
167
168    private string freeVariable;
169    public string FreeVariable {
170      get { return freeVariable; }
171      set {
172        if (value == freeVariable) return;
173        if (solutions.Any(s => !s.ProblemData.Dataset.DoubleVariables.Contains(value))) {
174          throw new ArgumentException("Variable does not exist in the ProblemData of the Solutions.");
175        }
176        freeVariable = value;
177        RecalculateInternalDataset();
178      }
179    }
180
181    private double yMin;
182    public double YMin {
183      get { return yMin; }
184    }
185    private double yMax;
186    public double YMax {
187      get { return yMax; }
188    }
189
190    private VerticalLineAnnotation VerticalLineAnnotation {
191      get { return (VerticalLineAnnotation)chart.Annotations.SingleOrDefault(x => x is VerticalLineAnnotation); }
192    }
193
194    internal ElementPosition InnerPlotPosition {
195      get { return chart.ChartAreas[0].InnerPlotPosition; }
196    }
197    #endregion
198
199    public GradientChart() {
200      InitializeComponent();
201
202      solutions = new List<IRegressionSolution>();
203      seriesCache = new Dictionary<IRegressionSolution, Series>();
204      ciSeriesCache = new Dictionary<IRegressionSolution, Series>();
205
206      // Configure axis
207      chart.CustomizeAllChartAreas();
208      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
209      chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
210      chart.ChartAreas[0].CursorX.Interval = 0;
211
212      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
213      chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
214      chart.ChartAreas[0].CursorY.Interval = 0;
215
216      configToolStripMenuItem = new ToolStripMenuItem("Configuration");
217      configToolStripMenuItem.Click += config_Click;
218      chart.ContextMenuStrip.Items.Add(new ToolStripSeparator());
219      chart.ContextMenuStrip.Items.Add(configToolStripMenuItem);
220
221      configurationDialog = new GradientChartConfigurationDialog(this);
222
223      Disposed += GradientChart_Disposed;
224    }
225
226    private void GradientChart_Disposed(object sender, EventArgs e) {
227      if (cancelCurrentRecalculateSource != null)
228        cancelCurrentRecalculateSource.Cancel();
229    }
230
231    public void Configure(IEnumerable<IRegressionSolution> solutions, ModifiableDataset sharedFixedVariables, string freeVariable, int drawingSteps, bool initializeAxisRanges = true) {
232      if (!SolutionsCompatible(solutions))
233        throw new ArgumentException("Solutions are not compatible with the problem data.");
234      this.freeVariable = freeVariable;
235      this.drawingSteps = drawingSteps;
236
237      this.solutions.Clear();
238      this.solutions.AddRange(solutions);
239
240      // add an event such that whenever a value is changed in the shared dataset,
241      // this change is reflected in the internal dataset (where the value becomes a whole column)
242      if (this.sharedFixedVariables != null)
243        this.sharedFixedVariables.ItemChanged -= sharedFixedVariables_ItemChanged;
244      this.sharedFixedVariables = sharedFixedVariables;
245      this.sharedFixedVariables.ItemChanged += sharedFixedVariables_ItemChanged;
246
247      RecalculateTrainingLimits(initializeAxisRanges);
248      RecalculateInternalDataset();
249
250      chart.Series.Clear();
251      seriesCache.Clear();
252      ciSeriesCache.Clear();
253      foreach (var solution in this.solutions) {
254        var series = CreateSeries(solution);
255        seriesCache.Add(solution, series.Item1);
256        if (series.Item2 != null)
257          ciSeriesCache.Add(solution, series.Item2);
258      }
259
260      ResizeAllSeriesData();
261      OrderAndColorSeries();
262    }
263
264    public async Task RecalculateAsync(bool updateOnFinish = true, bool resetYAxis = true) {
265      if (IsDisposed
266        || sharedFixedVariables == null || !solutions.Any() || string.IsNullOrEmpty(freeVariable)
267        || trainingMin.IsAlmost(trainingMax) || trainingMin > trainingMax || drawingSteps == 0)
268        return;
269
270      calculationPendingTimer.Start();
271
272      Update(); // immediately show label
273
274      // cancel previous recalculate call
275      if (cancelCurrentRecalculateSource != null)
276        cancelCurrentRecalculateSource.Cancel();
277      cancelCurrentRecalculateSource = new CancellationTokenSource();
278
279      // Set cursor and x-axis
280      // Make sure to allow a small offset to be able to distinguish the vertical line annotation from the axis
281      var defaultValue = sharedFixedVariables.GetDoubleValue(freeVariable, 0);
282      var step = (trainingMax - trainingMin) / drawingSteps;
283      var minimum = chart.ChartAreas[0].AxisX.Minimum;
284      var maximum = chart.ChartAreas[0].AxisX.Maximum;
285      if (defaultValue <= minimum)
286        VerticalLineAnnotation.X = minimum + step;
287      else if (defaultValue >= maximum)
288        VerticalLineAnnotation.X = maximum - step;
289      else
290        VerticalLineAnnotation.X = defaultValue;
291
292      if (ShowCursor)
293        chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + defaultValue.ToString("N3", CultureInfo.CurrentCulture);
294
295      // Update series
296      var cancellationToken = cancelCurrentRecalculateSource.Token;
297      try {
298        var limits = await UpdateAllSeriesDataAsync(cancellationToken);
299        //cancellationToken.ThrowIfCancellationRequested();
300
301        yMin = limits.Lower;
302        yMax = limits.Upper;
303        // Set y-axis
304        if (resetYAxis)
305          SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
306
307        UpdateOutOfTrainingRangeStripLines();
308
309        calculationPendingTimer.Stop();
310        calculationPendingLabel.Visible = false;
311        if (updateOnFinish)
312          Update();
313      }
314      catch (OperationCanceledException) { }
315      catch (AggregateException ae) {
316        if (!ae.InnerExceptions.Any(e => e is OperationCanceledException))
317          throw;
318      }
319    }
320
321    private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
322      double axisMin, axisMax, axisInterval;
323      ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
324      axis.Minimum = fixedAxisMin ?? axisMin;
325      axis.Maximum = fixedAxisMax ?? axisMax;
326      axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
327
328      try {
329        chart.ChartAreas[0].RecalculateAxesScale();
330      }
331      catch (InvalidOperationException) {
332        // Can occur if eg. axis min == axis max
333      }
334    }
335
336    private void RecalculateTrainingLimits(bool initializeAxisRanges) {
337      trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Min()).Max();
338      trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Max()).Min();
339
340      if (initializeAxisRanges) {
341        double xmin, xmax, xinterval;
342        ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
343        FixedXAxisMin = xmin;
344        FixedXAxisMax = xmax;
345      }
346    }
347
348    private void RecalculateInternalDataset() {
349      if (sharedFixedVariables == null)
350        return;
351
352      // we expand the range in order to get nice tick intervals on the x axis
353      double xmin, xmax, xinterval;
354      ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
355
356      if (FixedXAxisMin.HasValue) xmin = FixedXAxisMin.Value;
357      if (FixedXAxisMax.HasValue) xmax = FixedXAxisMax.Value;
358      double step = (xmax - xmin) / drawingSteps;
359
360      var xvalues = new List<double>();
361      for (int i = 0; i < drawingSteps; i++)
362        xvalues.Add(xmin + i * step);
363
364      var variables = sharedFixedVariables.DoubleVariables.ToList();
365      internalDataset = new ModifiableDataset(variables,
366        variables.Select(x => x == FreeVariable
367          ? xvalues
368          : Enumerable.Repeat(sharedFixedVariables.GetDoubleValue(x, 0), xvalues.Count).ToList()
369        )
370      );
371    }
372
373    private Tuple<Series, Series> CreateSeries(IRegressionSolution solution) {
374      var series = new Series {
375        ChartType = SeriesChartType.Line,
376        Name = solution.ProblemData.TargetVariable + " " + solutions.IndexOf(solution)
377      };
378      series.LegendText = series.Name;
379
380      var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution;
381      Series confidenceIntervalSeries = null;
382      if (confidenceBoundSolution != null) {
383        confidenceIntervalSeries = new Series {
384          ChartType = SeriesChartType.Range,
385          YValuesPerPoint = 2,
386          Name = "95% Conf. Interval " + series.Name,
387          IsVisibleInLegend = false
388        };
389      }
390      return Tuple.Create(series, confidenceIntervalSeries);
391    }
392
393    private void OrderAndColorSeries() {
394      chart.SuspendRepaint();
395
396      chart.Series.Clear();
397      // Add mean series for applying palette colors
398      foreach (var solution in solutions) {
399        chart.Series.Add(seriesCache[solution]);
400      }
401
402      chart.Palette = ChartColorPalette.BrightPastel;
403      chart.ApplyPaletteColors();
404      chart.Palette = ChartColorPalette.None;
405
406      // Add confidence interval series before its coresponding series for correct z index
407      foreach (var solution in solutions) {
408        Series ciSeries;
409        if (ciSeriesCache.TryGetValue(solution, out ciSeries)) {
410          var series = seriesCache[solution];
411          ciSeries.Color = Color.FromArgb(40, series.Color);
412          int idx = chart.Series.IndexOf(seriesCache[solution]);
413          chart.Series.Insert(idx, ciSeries);
414        }
415      }
416
417      chart.ResumeRepaint(true);
418    }
419
420    private async Task<DoubleLimit> UpdateAllSeriesDataAsync(CancellationToken cancellationToken) {
421      var updateTasks = solutions.Select(solution => UpdateSeriesDataAsync(solution, cancellationToken));
422
423      double min = double.MaxValue, max = double.MinValue;
424      foreach (var update in updateTasks) {
425        var limit = await update;
426        if (limit.Lower < min) min = limit.Lower;
427        if (limit.Upper > max) max = limit.Upper;
428      }
429
430      return new DoubleLimit(min, max);
431    }
432
433    private Task<DoubleLimit> UpdateSeriesDataAsync(IRegressionSolution solution, CancellationToken cancellationToken) {
434      return Task.Run(() => {
435        var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
436        var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList();
437
438        double min = double.MaxValue, max = double.MinValue;
439
440        var series = seriesCache[solution];
441        for (int i = 0; i < xvalues.Count; i++) {
442          series.Points[i].SetValueXY(xvalues[i], yvalues[i]);
443          if (yvalues[i] < min) min = yvalues[i];
444          if (yvalues[i] > max) max = yvalues[i];
445        }
446
447        var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution;
448        if (confidenceBoundSolution != null) {
449          var confidenceIntervalSeries = ciSeriesCache[solution];
450
451          cancellationToken.ThrowIfCancellationRequested();
452          var variances =
453            confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset,
454              Enumerable.Range(0, internalDataset.Rows)).ToList();
455          for (int i = 0; i < xvalues.Count; i++) {
456            var lower = yvalues[i] - 1.96 * Math.Sqrt(variances[i]);
457            var upper = yvalues[i] + 1.96 * Math.Sqrt(variances[i]);
458            confidenceIntervalSeries.Points[i].SetValueXY(xvalues[i], lower, upper);
459            if (lower < min) min = lower;
460            if (upper > max) max = upper;
461          }
462        }
463
464        cancellationToken.ThrowIfCancellationRequested();
465        return new DoubleLimit(min, max);
466      }, cancellationToken);
467    }
468
469    private void ResizeAllSeriesData() {
470      if (internalDataset == null)
471        return;
472
473      var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
474      foreach (var solution in solutions)
475        ResizeSeriesData(solution, xvalues);
476    }
477    private void ResizeSeriesData(IRegressionSolution solution, IList<double> xvalues = null) {
478      if (xvalues == null)
479        xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
480
481      var series = seriesCache[solution];
482      series.Points.SuspendUpdates();
483      series.Points.Clear();
484      for (int i = 0; i < xvalues.Count; i++)
485        series.Points.Add(new DataPoint(xvalues[i], 0.0));
486      series.Points.ResumeUpdates();
487
488      Series confidenceIntervalSeries;
489      if (ciSeriesCache.TryGetValue(solution, out confidenceIntervalSeries)) {
490        confidenceIntervalSeries.Points.SuspendUpdates();
491        confidenceIntervalSeries.Points.Clear();
492        for (int i = 0; i < xvalues.Count; i++)
493          confidenceIntervalSeries.Points.Add(new DataPoint(xvalues[i], new[] { -1.0, 1.0 }));
494        confidenceIntervalSeries.Points.ResumeUpdates();
495      }
496    }
497
498    public async Task AddSolutionAsync(IRegressionSolution solution) {
499      if (!SolutionsCompatible(solutions.Concat(new[] { solution })))
500        throw new ArgumentException("The solution is not compatible with the problem data.");
501      if (solutions.Contains(solution))
502        return;
503
504      solutions.Add(solution);
505      RecalculateTrainingLimits(true);
506
507      var series = CreateSeries(solution);
508      seriesCache.Add(solution, series.Item1);
509      if (series.Item2 != null)
510        ciSeriesCache.Add(solution, series.Item2);
511
512      ResizeSeriesData(solution);
513      OrderAndColorSeries();
514
515      await RecalculateAsync();
516      var args = new EventArgs<IRegressionSolution>(solution);
517      OnSolutionAdded(this, args);
518    }
519
520    public async Task RemoveSolutionAsync(IRegressionSolution solution) {
521      if (!solutions.Remove(solution))
522        return;
523
524      RecalculateTrainingLimits(true);
525
526      seriesCache.Remove(solution);
527      ciSeriesCache.Remove(solution);
528
529      await RecalculateAsync();
530      var args = new EventArgs<IRegressionSolution>(solution);
531      OnSolutionRemoved(this, args);
532    }
533
534    private static bool SolutionsCompatible(IEnumerable<IRegressionSolution> solutions) {
535      foreach (var solution1 in solutions) {
536        var variables1 = solution1.ProblemData.Dataset.DoubleVariables;
537        foreach (var solution2 in solutions) {
538          if (solution1 == solution2)
539            continue;
540          var variables2 = solution2.ProblemData.Dataset.DoubleVariables;
541          if (!variables1.All(variables2.Contains))
542            return false;
543        }
544      }
545      return true;
546    }
547
548    private void UpdateOutOfTrainingRangeStripLines() {
549      var axisX = chart.ChartAreas[0].AxisX;
550      var lowerStripLine = axisX.StripLines[0];
551      var upperStripLine = axisX.StripLines[1];
552
553      lowerStripLine.IntervalOffset = axisX.Minimum;
554      lowerStripLine.StripWidth = Math.Abs(trainingMin - axisX.Minimum);
555
556      upperStripLine.IntervalOffset = trainingMax;
557      upperStripLine.StripWidth = Math.Abs(axisX.Maximum - trainingMax);
558    }
559
560    #region Events
561    public event EventHandler<EventArgs<IRegressionSolution>> SolutionAdded;
562    public void OnSolutionAdded(object sender, EventArgs<IRegressionSolution> args) {
563      var added = SolutionAdded;
564      if (added == null) return;
565      added(sender, args);
566    }
567
568    public event EventHandler<EventArgs<IRegressionSolution>> SolutionRemoved;
569    public void OnSolutionRemoved(object sender, EventArgs<IRegressionSolution> args) {
570      var removed = SolutionRemoved;
571      if (removed == null) return;
572      removed(sender, args);
573    }
574
575    public event EventHandler VariableValueChanged;
576    public void OnVariableValueChanged(object sender, EventArgs args) {
577      var changed = VariableValueChanged;
578      if (changed == null) return;
579      changed(sender, args);
580    }
581
582    private void sharedFixedVariables_ItemChanged(object o, EventArgs<int, int> e) {
583      if (o != sharedFixedVariables) return;
584      var variables = sharedFixedVariables.DoubleVariables.ToList();
585      var rowIndex = e.Value;
586      var columnIndex = e.Value2;
587
588      var variableName = variables[columnIndex];
589      if (variableName == FreeVariable) return;
590      var v = sharedFixedVariables.GetDoubleValue(variableName, rowIndex);
591      var values = new List<double>(Enumerable.Repeat(v, DrawingSteps));
592      internalDataset.ReplaceVariable(variableName, values);
593    }
594
595    private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
596      var step = (trainingMax - trainingMin) / drawingSteps;
597      double newLocation = step * (long)Math.Round(e.NewLocationX / step);
598      var axisX = chart.ChartAreas[0].AxisX;
599      if (newLocation >= axisX.Maximum)
600        newLocation = axisX.Maximum - step;
601      if (newLocation <= axisX.Minimum)
602        newLocation = axisX.Minimum + step;
603
604      e.NewLocationX = newLocation;
605      var annotation = VerticalLineAnnotation;
606      var x = annotation.X;
607      sharedFixedVariables.SetVariableValue(x, FreeVariable, 0);
608
609      if (ShowCursor) {
610        chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + x.ToString("N3", CultureInfo.CurrentCulture);
611        chart.Update();
612      }
613
614      OnVariableValueChanged(this, EventArgs.Empty);
615    }
616
617    private void chart_MouseMove(object sender, MouseEventArgs e) {
618      bool hitCursor = chart.HitTest(e.X, e.Y).ChartElementType == ChartElementType.Annotation;
619      chart.Cursor = hitCursor ? Cursors.VSplit : Cursors.Default;
620    }
621
622    private void chart_DragDrop(object sender, DragEventArgs e) {
623      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
624      if (data != null) {
625        var solution = data as IRegressionSolution;
626        if (!solutions.Contains(solution))
627          AddSolutionAsync(solution);
628      }
629    }
630    private void chart_DragEnter(object sender, DragEventArgs e) {
631      if (!e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) return;
632      e.Effect = DragDropEffects.None;
633
634      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
635      var regressionSolution = data as IRegressionSolution;
636      if (regressionSolution != null) {
637        e.Effect = DragDropEffects.Copy;
638      }
639    }
640
641    private void calculationPendingTimer_Tick(object sender, EventArgs e) {
642      calculationPendingLabel.Visible = true;
643      Update();
644    }
645
646    private void config_Click(object sender, EventArgs e) {
647      configurationDialog.ShowDialog(this);
648    }
649    #endregion
650  }
651}
Note: See TracBrowser for help on using the repository browser.