#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Analysis;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using RegressionType = HeuristicLab.Analysis.ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType;
namespace HeuristicLab.DataPreprocessing.Views {
[View("Scatter Plot Single View")]
[Content(typeof(SingleScatterPlotContent), true)]
public partial class ScatterPlotSingleView : ItemView {
public new SingleScatterPlotContent Content {
get { return (SingleScatterPlotContent)base.Content; }
set { base.Content = value; }
}
public ScatterPlotSingleView() {
InitializeComponent();
regressionTypeComboBox.DataSource = Enum.GetValues(typeof(RegressionType));
regressionTypeComboBox.SelectedItem = RegressionType.None;
}
public void InitData() {
IEnumerable variables = Content.PreprocessingData.GetDoubleVariableNames();
// add variables to combo boxes
comboBoxXVariable.Items.Clear();
comboBoxYVariable.Items.Clear();
comboBoxGroup.Items.Clear();
comboBoxXVariable.Items.AddRange(variables.ToArray());
comboBoxYVariable.Items.AddRange(variables.ToArray());
comboBoxGroup.Items.Add("-");
for (int i = 0; i < Content.PreprocessingData.Columns; ++i) {
if (Content.PreprocessingData.VariableHasType(i)) {
double distinctValueCount = Content.PreprocessingData.GetValues(i).GroupBy(x => x).Count();
if (distinctValueCount <= 20)
comboBoxGroup.Items.Add(Content.PreprocessingData.GetVariableName(i));
}
}
// use x and y variable from content
if (Content.SelectedXVariable != null && Content.SelectedYVariable != null && Content.SelectedGroupVariable != null) {
comboBoxXVariable.SelectedItem = Content.SelectedXVariable;
comboBoxYVariable.SelectedItem = Content.SelectedYVariable;
comboBoxGroup.SelectedItem = Content.SelectedGroupVariable;
} else {
if (variables.Count() >= 2) {
comboBoxXVariable.SelectedIndex = 0;
comboBoxYVariable.SelectedIndex = 1;
comboBoxGroup.SelectedIndex = 0;
UpdateScatterPlot();
}
}
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
InitData();
}
}
private void UpdateScatterPlot() {
if (comboBoxXVariable.SelectedItem != null && comboBoxYVariable.SelectedItem != null && comboBoxGroup.SelectedItem != null) {
var xVariable = (string)comboBoxXVariable.SelectedItem;
var yVariable = (string)comboBoxYVariable.SelectedItem;
var groupVariable = (string)comboBoxGroup.SelectedItem;
ScatterPlot scatterPlot = Content.CreateScatterPlot(xVariable, yVariable, groupVariable);
var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
int order = (int)polynomialRegressionOrderNumericUpDown.Value;
foreach (var row in scatterPlot.Rows) {
row.VisualProperties.PointSize = 6;
row.VisualProperties.IsRegressionVisibleInLegend = false;
row.VisualProperties.RegressionType = regressionType;
row.VisualProperties.PolynomialRegressionOrder = order;
}
var vp = scatterPlot.VisualProperties;
vp.Title = string.Empty;
vp.XAxisTitle = xVariable;
vp.YAxisTitle = yVariable;
scatterPlotControl.Content = scatterPlot;
//save selected x and y variable in content
this.Content.SelectedXVariable = (string)comboBoxXVariable.SelectedItem;
this.Content.SelectedYVariable = (string)comboBoxYVariable.SelectedItem;
this.Content.SelectedGroupVariable = (string)comboBoxGroup.SelectedItem;
}
}
private void comboBoxXVariable_SelectedIndexChanged(object sender, EventArgs e) {
var oldPlot = scatterPlotControl.Content;
UpdateScatterPlot();
var newPlot = scatterPlotControl.Content;
if (oldPlot == null || newPlot == null) return;
newPlot.VisualProperties.YAxisMinimumAuto = oldPlot.VisualProperties.YAxisMinimumAuto;
newPlot.VisualProperties.YAxisMaximumAuto = oldPlot.VisualProperties.YAxisMaximumAuto;
newPlot.VisualProperties.YAxisMinimumFixedValue = oldPlot.VisualProperties.YAxisMinimumFixedValue;
newPlot.VisualProperties.YAxisMaximumFixedValue = oldPlot.VisualProperties.YAxisMaximumFixedValue;
foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) {
var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone();
newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName;
x.nr.VisualProperties = newVisuapProperties;
}
}
private void comboBoxYVariable_SelectedIndexChanged(object sender, EventArgs e) {
var oldPlot = scatterPlotControl.Content;
UpdateScatterPlot();
var newPlot = scatterPlotControl.Content;
if (oldPlot == null || newPlot == null) return;
newPlot.VisualProperties.XAxisMinimumAuto = oldPlot.VisualProperties.XAxisMinimumAuto;
newPlot.VisualProperties.XAxisMaximumAuto = oldPlot.VisualProperties.XAxisMaximumAuto;
newPlot.VisualProperties.XAxisMinimumFixedValue = oldPlot.VisualProperties.XAxisMinimumFixedValue;
newPlot.VisualProperties.XAxisMaximumFixedValue = oldPlot.VisualProperties.XAxisMaximumFixedValue;
foreach (var x in newPlot.Rows.Zip(oldPlot.Rows, (nr, or) => new { nr, or })) {
var newVisuapProperties = (ScatterPlotDataRowVisualProperties)x.or.VisualProperties.Clone();
newVisuapProperties.DisplayName = x.nr.VisualProperties.DisplayName;
x.nr.VisualProperties = newVisuapProperties;
}
}
private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) {
UpdateScatterPlot();
}
#region Regression Line
private void regressionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
polynomialRegressionOrderNumericUpDown.Enabled = regressionType == RegressionType.Polynomial;
UpdateRegressionLine();
}
private void polynomialRegressionOrderNumericUpDown_ValueChanged(object sender, EventArgs e) {
UpdateRegressionLine();
}
private void UpdateRegressionLine() {
var regressionType = (RegressionType)regressionTypeComboBox.SelectedValue;
int order = (int)polynomialRegressionOrderNumericUpDown.Value;
foreach (var row in scatterPlotControl.Content.Rows) {
row.VisualProperties.IsRegressionVisibleInLegend = false;
row.VisualProperties.RegressionType = regressionType;
row.VisualProperties.PolynomialRegressionOrder = order;
}
}
#endregion
}
}