Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotMultiView.cs @ 14029

Last change on this file since 14029 was 14029, checked in by gkronber, 8 years ago

#2434: merged trunk changes r12934:14026 from trunk to branch

File size: 7.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Windows.Forms;
6using HeuristicLab.Analysis;
7using HeuristicLab.Common;
8using HeuristicLab.Core.Views;
9using HeuristicLab.MainForm;
10
11namespace HeuristicLab.DataPreprocessing.Views {
12  [View("Scatter Plot Multi View")]
13  [Content(typeof(ScatterPlotContent), false)]
14  public partial class ScatterPlotMultiView : ItemView {
15
16    private const int HEADER_WIDTH = 50;
17    private const int HEADER_HEIGHT = 50;
18    private const int MAX_AUTO_SIZE_ELEMENTS = 6;
19    private const int FIXED_CHART_WIDTH = 250;
20    private const int FIXED_CHART_HEIGHT = 150;
21
22    public ScatterPlotMultiView() {
23      InitializeComponent();
24    }
25
26    public new ScatterPlotContent Content {
27      get { return (ScatterPlotContent)base.Content; }
28      set { base.Content = value; }
29    }
30
31    protected override void OnContentChanged() {
32      base.OnContentChanged();
33      if (Content != null) {
34        GenerateMultiLayout();
35      }
36    }
37
38    //Add header elements to the table layout panel
39    private void addHeaderToTableLayoutPanels() {
40
41      List<string> variables = Content.PreprocessingData.GetDoubleVariableNames().ToList();
42
43      for (int i = 1; i < variables.Count + 1; i++) {
44        // Use buttons for header elements
45        Button xButton = new Button();
46        xButton.Enabled = false;
47        xButton.BackColor = Color.Gainsboro;
48        xButton.Text = variables[i - 1];
49        xButton.Dock = DockStyle.Fill;
50        tableLayoutPanel.Controls.Add(xButton, 0, i);
51        Button yButton = new Button();
52        yButton.Enabled = false;
53        yButton.BackColor = Color.Gainsboro;
54        yButton.Text = variables[i - 1];
55        yButton.Dock = DockStyle.Fill;
56        tableLayoutPanel.Controls.Add(yButton, i, 0);
57      }
58    }
59
60    private void GenerateMultiLayout() {
61      List<string> variables = Content.PreprocessingData.GetDoubleVariableNames().ToList();
62
63      tableLayoutPanel.Controls.Clear();
64      //Clear out the existing row and column styles
65      tableLayoutPanel.ColumnStyles.Clear();
66      tableLayoutPanel.RowStyles.Clear();
67
68      //Set row and column count
69      tableLayoutPanel.ColumnCount = variables.Count + 1;
70      tableLayoutPanel.RowCount = variables.Count + 1;
71
72      tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, HEADER_WIDTH));
73      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, HEADER_HEIGHT));
74      // set column and row layout
75      for (int x = 0; x < variables.Count; x++) {
76        // auto size
77        if (variables.Count <= MAX_AUTO_SIZE_ELEMENTS) {
78          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, (tableLayoutPanel.Width - HEADER_WIDTH) / variables.Count));
79          tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, (tableLayoutPanel.Height - HEADER_HEIGHT) / variables.Count));
80        }
81        // fixed size
82        else {
83          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, FIXED_CHART_WIDTH));
84          tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, FIXED_CHART_HEIGHT));
85        }
86      }
87
88      addHeaderToTableLayoutPanels();
89      addChartsToTableLayoutPanel();
90
91    }
92
93    private void addChartsToTableLayoutPanel() {
94
95      List<string> variables = Content.PreprocessingData.GetDoubleVariableNames().ToList();
96
97      //set scatter plots and histograms
98      for (int x = 1; x < variables.Count + 1; x++) {
99
100        for (int y = 1; y < variables.Count + 1; y++) {
101          // use historgram if x and y variable are equal
102          if (x == y) {
103            PreprocessingDataTable dataTable = new PreprocessingDataTable();
104            DataRow dataRow = Content.CreateDataRow(variables[x - 1], DataRowVisualProperties.DataRowChartType.Histogram);
105            dataTable.Rows.Add(dataRow);
106            PreprocessingDataTableView pcv = new PreprocessingDataTableView();
107            pcv.ChartDoubleClick += HistogramDoubleClick;
108            pcv.Content = dataTable;
109            tableLayoutPanel.Controls.Add(pcv, y, x);
110          }
111          //scatter plot
112          else {
113            ScatterPlot scatterPlot = Content.CreateScatterPlot(variables[x - 1], variables[y - 1]);
114            PreprocessingScatterPlotView pspv = new PreprocessingScatterPlotView();
115            pspv.ChartDoubleClick += ScatterPlotDoubleClick;
116            pspv.Content = scatterPlot;
117            pspv.Dock = DockStyle.Fill;
118            tableLayoutPanel.Controls.Add(pspv, x, y);
119          }
120        }
121      }
122    }
123
124    //Open scatter plot in new tab with new content when double clicked
125    private void ScatterPlotDoubleClick(object sender, EventArgs e) {
126      PreprocessingScatterPlotView pspv = (PreprocessingScatterPlotView)sender;
127      ScatterPlotContent scatterContent = new ScatterPlotContent(Content, new Cloner());  // create new content
128      ScatterPlot scatterPlot = pspv.Content;
129      setVariablesInContentFromScatterPlot(scatterContent, scatterPlot);
130
131      MainFormManager.MainForm.ShowContent(scatterContent, typeof(ScatterPlotSingleView));  // open in new tab
132    }
133
134    //Extract variable names from scatter plot and set them in content
135    private void setVariablesInContentFromScatterPlot(ScatterPlotContent scatterContent, ScatterPlot scatterPlot) {
136
137      // only one data row should be in scatter plot
138      if (scatterPlot.Rows.Count == 1) {
139        string[] variables = scatterPlot.Rows.ElementAt(0).Name.Split(new string[] { " - " }, StringSplitOptions.None); // extract variable names from string
140        scatterContent.SelectedXVariable = variables[0];
141        scatterContent.SelectedYVariable = variables[1];
142      }
143    }
144
145    //Set variable item list from with variable from data table
146    private void setVariableItemListFromDataTable(HistogramContent histoContent, PreprocessingDataTable dataTable) {
147
148      // only one data row should be in data table
149      if (dataTable.Rows.Count == 1) {
150        string variableName = dataTable.Rows.ElementAt(0).Name;
151
152        // set only variable name checked
153        foreach (var checkedItem in histoContent.VariableItemList) {
154          if (checkedItem.Value == variableName)
155            histoContent.VariableItemList.SetItemCheckedState(checkedItem, true);
156          else
157            histoContent.VariableItemList.SetItemCheckedState(checkedItem, false);
158
159        }
160      }
161    }
162
163    //open histogram in new tab with new content when double clicked
164    private void HistogramDoubleClick(object sender, EventArgs e) {
165      PreprocessingDataTableView pcv = (PreprocessingDataTableView)sender;
166      HistogramContent histoContent = new HistogramContent(Content.PreprocessingData);  // create new content     
167      histoContent.VariableItemList = Content.CreateVariableItemList();
168      PreprocessingDataTable dataTable = pcv.Content;
169      setVariableItemListFromDataTable(histoContent, dataTable);
170
171      MainFormManager.MainForm.ShowContent(histoContent, typeof(HistogramView));  // open in new tab
172    }
173
174
175  }
176
177
178}
Note: See TracBrowser for help on using the repository browser.