Free cookie consent management tool by TermsFeed Policy Generator

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, 8 years ago

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

File size: 24.8 KB
RevLine 
[13780]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;
[13836]24using System.Drawing;
[13817]25using System.Globalization;
[13780]26using System.Linq;
[13840]27using System.Threading;
[13837]28using System.Threading.Tasks;
[13780]29using System.Windows.Forms;
30using System.Windows.Forms.DataVisualization.Charting;
31using HeuristicLab.Common;
[13836]32using HeuristicLab.MainForm.WindowsForms;
[13780]33using HeuristicLab.Visualization.ChartControlsExtensions;
34
35namespace HeuristicLab.Problems.DataAnalysis.Views {
[13831]36  public partial class GradientChart : UserControl {
37    private ModifiableDataset sharedFixedVariables; // used for syncronising variable values between charts
[13837]38    private ModifiableDataset internalDataset; // holds the x values for each point drawn
[13780]39
[13842]40    private CancellationTokenSource cancelCurrentRecalculateSource;
[13840]41
[13842]42    private readonly List<IRegressionSolution> solutions;
43    private readonly Dictionary<IRegressionSolution, Series> seriesCache;
44    private readonly Dictionary<IRegressionSolution, Series> ciSeriesCache;
45
[13853]46    private readonly ToolStripMenuItem configToolStripMenuItem;
47    private readonly GradientChartConfigurationDialog configurationDialog;
48
[13842]49    #region Properties
[13831]50    public bool ShowLegend {
51      get { return chart.Legends[0].Enabled; }
52      set { chart.Legends[0].Enabled = value; }
[13780]53    }
[13831]54    public bool ShowCursor {
55      get { return chart.Annotations[0].Visible; }
[13853]56      set {
57        chart.Annotations[0].Visible = value;
58        if (!value) chart.ChartAreas[0].AxisX.Title = string.Empty;
59      }
[13831]60    }
[13780]61
[13855]62    public bool ShowConfigButton {
63      get { return configurationButton.Visible; }
64      set { configurationButton.Visible = value; }
65    }
66
[13831]67    private int xAxisTicks = 5;
68    public int XAxisTicks {
69      get { return xAxisTicks; }
[13843]70      set {
71        if (value != xAxisTicks) {
72          xAxisTicks = value;
73          SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
74          RecalculateInternalDataset();
75        }
76      }
[13780]77    }
[13842]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;
[14006]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          }
[13842]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;
[14006]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          }
[13842]113        }
114      }
115    }
116
[13831]117    private int yAxisTicks = 5;
[13842]118    public int YAxisTicks {
[13831]119      get { return yAxisTicks; }
[13843]120      set {
121        if (value != yAxisTicks) {
122          yAxisTicks = value;
123          SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
124          RecalculateInternalDataset();
125        }
126      }
[13831]127    }
[13842]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;
[13995]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);
[13842]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;
[13995]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);
[13842]149        }
150      }
151    }
[13780]152
[13843]153    private double trainingMin = 1;
154    private double trainingMax = -1;
[13780]155
[13831]156    private int drawingSteps = 1000;
157    public int DrawingSteps {
158      get { return drawingSteps; }
[13842]159      set {
160        if (value != drawingSteps) {
161          drawingSteps = value;
162          RecalculateInternalDataset();
163          ResizeAllSeriesData();
164        }
165      }
[13780]166    }
167
[13831]168    private string freeVariable;
169    public string FreeVariable {
170      get { return freeVariable; }
[13780]171      set {
[13831]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();
[13780]178      }
179    }
180
[13843]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
[13831]190    private VerticalLineAnnotation VerticalLineAnnotation {
191      get { return (VerticalLineAnnotation)chart.Annotations.SingleOrDefault(x => x is VerticalLineAnnotation); }
[13780]192    }
[13850]193
194    internal ElementPosition InnerPlotPosition {
195      get { return chart.ChartAreas[0].InnerPlotPosition; }
196    }
[13842]197    #endregion
[13780]198
199    public GradientChart() {
200      InitializeComponent();
[13836]201
[13842]202      solutions = new List<IRegressionSolution>();
203      seriesCache = new Dictionary<IRegressionSolution, Series>();
204      ciSeriesCache = new Dictionary<IRegressionSolution, Series>();
205
[13836]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;
[13840]215
[13853]216      configToolStripMenuItem = new ToolStripMenuItem("Configuration");
[13855]217      configToolStripMenuItem.Click += config_Click;
[13853]218      chart.ContextMenuStrip.Items.Add(new ToolStripSeparator());
219      chart.ContextMenuStrip.Items.Add(configToolStripMenuItem);
220
221      configurationDialog = new GradientChartConfigurationDialog(this);
222
[13842]223      Disposed += GradientChart_Disposed;
[13780]224    }
[13853]225
[13840]226    private void GradientChart_Disposed(object sender, EventArgs e) {
[13843]227      if (cancelCurrentRecalculateSource != null)
228        cancelCurrentRecalculateSource.Cancel();
[13840]229    }
230
[13842]231    public void Configure(IEnumerable<IRegressionSolution> solutions, ModifiableDataset sharedFixedVariables, string freeVariable, int drawingSteps, bool initializeAxisRanges = true) {
[13831]232      if (!SolutionsCompatible(solutions))
233        throw new ArgumentException("Solutions are not compatible with the problem data.");
234      this.freeVariable = freeVariable;
235      this.drawingSteps = drawingSteps;
[13780]236
[13842]237      this.solutions.Clear();
238      this.solutions.AddRange(solutions);
239
[13831]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;
[13780]246
[13842]247      RecalculateTrainingLimits(initializeAxisRanges);
[13831]248      RecalculateInternalDataset();
[13842]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();
[13780]262    }
263
[13843]264    public async Task RecalculateAsync(bool updateOnFinish = true, bool resetYAxis = true) {
[13842]265      if (IsDisposed
266        || sharedFixedVariables == null || !solutions.Any() || string.IsNullOrEmpty(freeVariable)
267        || trainingMin.IsAlmost(trainingMax) || trainingMin > trainingMax || drawingSteps == 0)
268        return;
[13829]269
[13853]270      calculationPendingTimer.Start();
271
[13842]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
[13995]280      // Make sure to allow a small offset to be able to distinguish the vertical line annotation from the axis
[13842]281      var defaultValue = sharedFixedVariables.GetDoubleValue(freeVariable, 0);
[13995]282      var step = (trainingMax - trainingMin) / drawingSteps;
283      var minimum = chart.ChartAreas[0].AxisX.Minimum;
284      var maximum = chart.ChartAreas[0].AxisX.Maximum;
[14006]285      if (defaultValue <= minimum)
[13995]286        VerticalLineAnnotation.X = minimum + step;
[14006]287      else if (defaultValue >= maximum)
[13995]288        VerticalLineAnnotation.X = maximum - step;
289      else
290        VerticalLineAnnotation.X = defaultValue;
291
[13853]292      if (ShowCursor)
293        chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + defaultValue.ToString("N3", CultureInfo.CurrentCulture);
[13842]294
295      // Update series
296      var cancellationToken = cancelCurrentRecalculateSource.Token;
297      try {
[13843]298        var limits = await UpdateAllSeriesDataAsync(cancellationToken);
299        //cancellationToken.ThrowIfCancellationRequested();
[13842]300
[13843]301        yMin = limits.Lower;
302        yMax = limits.Upper;
[13842]303        // Set y-axis
[13843]304        if (resetYAxis)
305          SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
[13842]306
307        UpdateOutOfTrainingRangeStripLines();
308
[13853]309        calculationPendingTimer.Stop();
310        calculationPendingLabel.Visible = false;
[13843]311        if (updateOnFinish)
312          Update();
[13842]313      }
314      catch (OperationCanceledException) { }
315      catch (AggregateException ae) {
316        if (!ae.InnerExceptions.Any(e => e is OperationCanceledException))
317          throw;
318      }
[13831]319    }
320
[13843]321    private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
[13842]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;
[13843]326      axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
327
[13845]328      try {
329        chart.ChartAreas[0].RecalculateAxesScale();
330      }
331      catch (InvalidOperationException) {
332        // Can occur if eg. axis min == axis max
333      }
[13842]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
[13831]348    private void RecalculateInternalDataset() {
[13843]349      if (sharedFixedVariables == null)
350        return;
351
[13831]352      // we expand the range in order to get nice tick intervals on the x axis
[13829]353      double xmin, xmax, xinterval;
[13831]354      ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
[13842]355
356      if (FixedXAxisMin.HasValue) xmin = FixedXAxisMin.Value;
357      if (FixedXAxisMax.HasValue) xmax = FixedXAxisMax.Value;
[13831]358      double step = (xmax - xmin) / drawingSteps;
359
[13829]360      var xvalues = new List<double>();
[13831]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      );
[13808]371    }
372
[13842]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;
[13837]379
[13842]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        };
[13836]389      }
[13842]390      return Tuple.Create(series, confidenceIntervalSeries);
391    }
[13836]392
[13842]393    private void OrderAndColorSeries() {
[13836]394      chart.SuspendRepaint();
[13842]395
[13836]396      chart.Series.Clear();
397      // Add mean series for applying palette colors
[13842]398      foreach (var solution in solutions) {
399        chart.Series.Add(seriesCache[solution]);
[13780]400      }
[13842]401
[13836]402      chart.Palette = ChartColorPalette.BrightPastel;
403      chart.ApplyPaletteColors();
404      chart.Palette = ChartColorPalette.None;
405
[13842]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];
[13995]411          ciSeries.Color = Color.FromArgb(40, series.Color);
[13842]412          int idx = chart.Series.IndexOf(seriesCache[solution]);
413          chart.Series.Insert(idx, ciSeries);
414        }
[13836]415      }
[13842]416
[13836]417      chart.ResumeRepaint(true);
[13842]418    }
[13836]419
[13843]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) {
[13842]434      return Task.Run(() => {
[13843]435        var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
436        var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList();
[13836]437
[13843]438        double min = double.MaxValue, max = double.MinValue;
[13820]439
[13843]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        }
[13820]446
[13843]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;
[13842]461          }
[13843]462        }
463
464        cancellationToken.ThrowIfCancellationRequested();
465        return new DoubleLimit(min, max);
[13842]466      }, cancellationToken);
467    }
[13840]468
[13842]469    private void ResizeAllSeriesData() {
[13843]470      if (internalDataset == null)
471        return;
472
[13842]473      var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
474      foreach (var solution in solutions)
475        ResizeSeriesData(solution, xvalues);
[13780]476    }
[13842]477    private void ResizeSeriesData(IRegressionSolution solution, IList<double> xvalues = null) {
478      if (xvalues == null)
479        xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
[13780]480
[13842]481      var series = seriesCache[solution];
482      series.Points.SuspendUpdates();
[13853]483      series.Points.Clear();
[13842]484      for (int i = 0; i < xvalues.Count; i++)
485        series.Points.Add(new DataPoint(xvalues[i], 0.0));
486      series.Points.ResumeUpdates();
[13780]487
[13842]488      Series confidenceIntervalSeries;
489      if (ciSeriesCache.TryGetValue(solution, out confidenceIntervalSeries)) {
490        confidenceIntervalSeries.Points.SuspendUpdates();
[13853]491        confidenceIntervalSeries.Points.Clear();
[13842]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      }
[13780]496    }
497
[13842]498    public async Task AddSolutionAsync(IRegressionSolution solution) {
[13831]499      if (!SolutionsCompatible(solutions.Concat(new[] { solution })))
500        throw new ArgumentException("The solution is not compatible with the problem data.");
[13842]501      if (solutions.Contains(solution))
502        return;
503
[13831]504      solutions.Add(solution);
[13842]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();
[13995]516      var args = new EventArgs<IRegressionSolution>(solution);
517      OnSolutionAdded(this, args);
[13780]518    }
[13995]519
[13842]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();
[13995]530      var args = new EventArgs<IRegressionSolution>(solution);
531      OnSolutionRemoved(this, args);
[13831]532    }
[13780]533
[13831]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;
[13780]546    }
547
[13842]548    private void UpdateOutOfTrainingRangeStripLines() {
[13831]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;
[14006]554      lowerStripLine.StripWidth = Math.Abs(trainingMin - axisX.Minimum);
[13831]555
556      upperStripLine.IntervalOffset = trainingMax;
[14006]557      upperStripLine.StripWidth = Math.Abs(axisX.Maximum - trainingMax);
[13780]558    }
559
[13842]560    #region Events
[13995]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
[13817]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
[13842]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;
[13831]587
[13842]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);
[13780]593    }
594
[13818]595    private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
[13840]596      var step = (trainingMax - trainingMin) / drawingSteps;
[13846]597      double newLocation = step * (long)Math.Round(e.NewLocationX / step);
[13840]598      var axisX = chart.ChartAreas[0].AxisX;
[13995]599      if (newLocation >= axisX.Maximum)
600        newLocation = axisX.Maximum - step;
601      if (newLocation <= axisX.Minimum)
602        newLocation = axisX.Minimum + step;
[13831]603
[13846]604      e.NewLocationX = newLocation;
[13831]605      var annotation = VerticalLineAnnotation;
606      var x = annotation.X;
607      sharedFixedVariables.SetVariableValue(x, FreeVariable, 0);
608
[13853]609      if (ShowCursor) {
610        chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + x.ToString("N3", CultureInfo.CurrentCulture);
611        chart.Update();
612      }
[13831]613
[13853]614      OnVariableValueChanged(this, EventArgs.Empty);
[13818]615    }
616
[13780]617    private void chart_MouseMove(object sender, MouseEventArgs e) {
[13842]618      bool hitCursor = chart.HitTest(e.X, e.Y).ChartElementType == ChartElementType.Annotation;
619      chart.Cursor = hitCursor ? Cursors.VSplit : Cursors.Default;
[13780]620    }
621
[13842]622    private void chart_DragDrop(object sender, DragEventArgs e) {
[13780]623      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
624      if (data != null) {
625        var solution = data as IRegressionSolution;
[13842]626        if (!solutions.Contains(solution))
627          AddSolutionAsync(solution);
[13780]628      }
629    }
[13842]630    private void chart_DragEnter(object sender, DragEventArgs e) {
[13780]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    }
[13853]640
641    private void calculationPendingTimer_Tick(object sender, EventArgs e) {
642      calculationPendingLabel.Visible = true;
643      Update();
644    }
645
[13855]646    private void config_Click(object sender, EventArgs e) {
[13853]647      configurationDialog.ShowDialog(this);
648    }
[13817]649    #endregion
[13780]650  }
651}
Note: See TracBrowser for help on using the repository browser.