Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionTargetResponseGradientView.cs @ 13825

Last change on this file since 13825 was 13820, checked in by bburlacu, 9 years ago

#2597: Added flags for controlling the display of legend, axis labels, or vertical cursor on the chart.

File size: 5.1 KB
RevLine 
[13817]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.Collections.Generic;
[13808]23using System.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.MainForm;
[13817]27using HeuristicLab.MainForm.WindowsForms;
[13808]28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Target Response Gradient View")]
31  [Content(typeof(IRegressionSolution))]
32  public partial class RegressionSolutionTargetResponseGradientView : DataAnalysisSolutionEvaluationView {
33    private ModifiableDataset internalDataset;
34    private Dictionary<string, GradientChart> charts;
35
36    private const int Points = 1000;
37
38    public RegressionSolutionTargetResponseGradientView() {
39      InitializeComponent();
40    }
41
42    public new IRegressionSolution Content {
43      get { return (IRegressionSolution)base.Content; }
44      set {
45        if (value == null || value == Content) return;
46        base.Content = value;
47      }
48    }
49
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      variableListView.ItemChecked += variableListView_ItemChecked;
53    }
54
55    protected override void DeregisterContentEvents() {
56      variableListView.ItemChecked -= variableListView_ItemChecked;
57      base.DeregisterContentEvents();
58    }
59
60    protected override void OnContentChanged() {
61      base.OnContentChanged();
[13817]62      if (Content == null) return;
[13808]63      var pd = Content.ProblemData;
64      var ds = pd.Dataset;
65
66      // update variable list
67      variableListView.Items.Clear();
68      variableListView.Items.AddRange(pd.AllowedInputVariables.Select(x => new ListViewItem(x, 0)).ToArray());
69
70      // update internal dataset
71      var variableNames = pd.AllowedInputVariables.ToList();
72      var variableValues = variableNames.Select(x => new List<double> { pd.Dataset.GetDoubleValues(x, pd.TrainingIndices).Median() });
73      internalDataset = new ModifiableDataset(variableNames, variableValues);
74      // update charts and variable limits
75      charts = new Dictionary<string, GradientChart>();
76      foreach (var x in pd.AllowedInputVariables) {
77        double min = 0, max = 0;
78        foreach (var v in ds.GetDoubleValues(x, pd.TrainingIndices)) {
79          if (v > max) max = v;
80          if (v < min) min = v;
81        }
[13820]82        var gradientChart = new GradientChart {
83          Dock = DockStyle.Fill,
84          Margin = Padding.Empty,
85          ShowLegend = false,
86          ShowCursor = true,
87          ShowXAxisLabel = true,
88          ShowYAxisLabel = true
89        };
[13817]90        gradientChart.VariableValueChanged += (o, e) => {
91          foreach (var chart in gradientChartTableLayout.Controls.Cast<GradientChart>()) {
92            if (chart == (GradientChart)o) continue;
93            chart.UpdateChart();
94          }
95        };
[13808]96        gradientChart.Configure(new[] { Content }, pd, internalDataset, x, min, max, Points);
97        charts[x] = gradientChart;
98      }
99    }
100
101    private void variableListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
102      if (charts == null) return;
103      var item = e.Item;
104      var variable = item.Text;
105      var chart = charts[variable];
[13817]106      var tl = gradientChartTableLayout;
107      tl.SuspendLayout();
108      var columnWidth = tl.GetColumnWidths()[0]; // all columns have the same width
109      var rowHeight = 0.8f * columnWidth;
110      tl.RowStyles.Clear();
111
[13808]112      if (item.Checked) {
[13817]113        tl.Controls.Add(chart);
114        var index = tl.Controls.Count - 1;
115        int rowIndex = index / tl.ColumnCount;
116        int columnIndex = index % tl.ColumnCount;
117        tl.SetRow(chart, rowIndex);
118        tl.SetColumn(chart, columnIndex);
[13808]119        chart.UpdateChart();
[13817]120        tl.RowCount = rowIndex + 1;
[13808]121      } else {
[13817]122        tl.Controls.Remove(chart);
123        // relayout
124        var count = tl.Controls.Count;
125        tl.RowCount = count / tl.ColumnCount + (count % tl.ColumnCount == 0 ? 0 : 1);
126        for (int i = 0; i < count; ++i) {
127          var control = tl.Controls[i];
128          int rowIndex = i / tl.ColumnCount;
129          int columnIndex = i % tl.ColumnCount;
130          tl.SetRow(control, rowIndex);
131          tl.SetColumn(control, columnIndex);
132        }
[13808]133      }
[13817]134      // set absolute row sizes
135      for (int i = 0; i < tl.RowCount; ++i) {
136        tl.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
137      }
138      tl.ResumeLayout();
[13808]139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.