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
RevLine 
[10882]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;
[10987]11using HeuristicLab.Common;
[10882]12using HeuristicLab.Core;
13using HeuristicLab.Core.Views;
14using HeuristicLab.Data;
15using HeuristicLab.DataPreprocessing.Implementations;
16using HeuristicLab.MainForm;
17
18namespace HeuristicLab.DataPreprocessing.Views {
19
[10987]20  [View("Scatter Plot Multi View")]
[10952]21  [Content(typeof(ScatterPlotContent), false)]
[10987]22  public partial class ScatterPlotMultiView : ItemView {
[10882]23
[10952]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
[10987]30    public ScatterPlotMultiView() {
[10882]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) {
[10915]42        GenerateMultiLayout();
[10882]43      }
44    }
45
[10952]46    //Add header elements to the table layout panel
47    private void addHeaderToTableLayoutPanels() {
48
[10987]49      List<string> variables = Content.GetVariableNames().ToList();
50
[10952]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
[10915]68    private void GenerateMultiLayout()
69    {
[10987]70      List<string> variables = Content.GetVariableNames().ToList();
71
[10915]72      tableLayoutPanel.Controls.Clear();
73      //Clear out the existing row and column styles
74      tableLayoutPanel.ColumnStyles.Clear();
75      tableLayoutPanel.RowStyles.Clear();
[10882]76
[10915]77      //Set row and column count
78      tableLayoutPanel.ColumnCount = variables.Count+1;
79      tableLayoutPanel.RowCount = variables.Count+1;
80
[10952]81      tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, HEADER_WIDTH));
82      tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, HEADER_HEIGHT));
[10915]83      // set column and row layout
84      for (int x = 0; x < variables.Count; x++)
85      {
86        // auto size
[10952]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));
[10915]90        }
91        // fixed size
92        else {
[10952]93          tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, FIXED_CHART_WIDTH));
94          tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, FIXED_CHART_HEIGHT));
[10915]95        }
[10882]96      }
[10915]97
[10952]98      addHeaderToTableLayoutPanels();
99      addChartsToTableLayoutPanel();
100 
101    }
[10915]102
[10952]103    private void addChartsToTableLayoutPanel() {
[10987]104
105      List<string> variables = Content.GetVariableNames().ToList();
106
[10915]107      //set scatter plots and histograms
[10952]108      for (int x = 1; x < variables.Count + 1; x++) {
[10915]109
[10952]110        for (int y = 1; y < variables.Count + 1; y++) {
111          // use historgram if x and y variable are equal
[10915]112          if (x == y) {
113            PreprocessingDataTable dataTable = new PreprocessingDataTable();
[10987]114            DataRow dataRow = Content.CreateDataRow(variables[x - 1], DataRowVisualProperties.DataRowChartType.Histogram);
[10915]115            dataTable.Rows.Add(dataRow);
116            PreprocessingDataTableView pcv = new PreprocessingDataTableView();
[10952]117            pcv.ChartDoubleClick += HistogramDoubleClick;
[10915]118            pcv.Content = dataTable;
119            tableLayoutPanel.Controls.Add(pcv, y, x);
120          }
121          //scatter plot
[10952]122          else {
[10987]123            ScatterPlot scatterPlot = Content.CreateScatterPlot(variables[x - 1], variables[y - 1]);
[10915]124            PreprocessingScatterPlotView pspv = new PreprocessingScatterPlotView();
[10952]125            pspv.ChartDoubleClick += ScatterPlotDoubleClick;
[10915]126            pspv.Content = scatterPlot;
127            pspv.Dock = DockStyle.Fill;
[10952]128            tableLayoutPanel.Controls.Add(pspv, x, y);
[10915]129          }
130        }
131      }
[10882]132    }
133
[10952]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;
[10987]137      ScatterPlotContent scatterContent = new ScatterPlotContent(Content, new Cloner());  // create new content
[10952]138      ScatterPlot scatterPlot = pspv.Content;
139      setVariablesInContentFromScatterPlot(scatterContent, scatterPlot);
[10915]140
[10987]141      MainFormManager.MainForm.ShowContent(scatterContent, typeof(ScatterPlotSingleView));  // open in new tab
[10952]142    }
[10915]143
[10952]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      }
[10915]153    }
[10952]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) {
[10987]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);
[10952]181
[10987]182      //MainFormManager.MainForm.ShowContent(histoContent, typeof(HistogramView));  // open in new tab
[10952]183    }
[10915]184   
185
[10882]186  }
187
188 
189}
Note: See TracBrowser for help on using the repository browser.