Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.4/ScatterPlotMultiView.cs @ 10987

Last change on this file since 10987 was 10987, checked in by aesterer, 10 years ago

Refactored scatter plot

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