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

Last change on this file since 14021 was 14021, checked in by bburlacu, 6 years ago

#2597: Fixed some additional issues with the density charts (which are now turned on by default).

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