Creating and Editing a Box & Whisker Plot in C#
This example demonstrates selecting a data set, running a box & whisker plot, and customizing the output graph:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
namespace CSharpExample
{
/// <summary> /// Summary description for CSharpForm. /// </summary> public class MainForm : System.Windows.Forms.Form
{ private System.Windows.Forms.Button IDD_OPEN_DATASET;
/// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null;
public MainForm()
{ // Required for Windows Form Designer support InitializeComponent(); }
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing )
{ if ( disposing )
{
if (components != null)
{ components.Dispose(); }
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent()
{ this.IDD_OPEN_DATASET = new System.Windows.Forms.Button(); this.SuspendLayout();
// IDD_OPEN_DATASET
this.IDD_OPEN_DATASET.Location = new System.Drawing.Point(8, 8); this.IDD_OPEN_DATASET.Name = "IDD_OPEN_DATASET"; this.IDD_OPEN_DATASET.RightToLeft = System.Windows.Forms.RightToLeft.No; this.IDD_OPEN_DATASET.Size = new System.Drawing.Size(184, 40); this.IDD_OPEN_DATASET.TabIndex = 0; this.IDD_OPEN_DATASET.Text = "&Open Spreadsheet"; this.IDD_OPEN_DATASET.Click += new System.EventHandler(this.IDD_OPEN_DATASET_Click);
// MainForm
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(200, 53); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.IDD_OPEN_DATASET}); this.Name = "MainForm"; this.Text = "Box and Whisker Plot"; this.ResumeLayout(false); }
#endregion
/// <summary> /// The main entry point for the application. /// </summary> [STAThread]
static void Main()
{ Application.Run(new MainForm()); }
private void IDD_OPEN_DATASET_Click(object sender, System.EventArgs e)
{ //see which spreadsheet the user wishes to use OpenFileDialog FileDlg = new OpenFileDialog(); FileDlg.InitialDirectory = "c:\\" ; FileDlg.Filter = "Spreadsheet file (*.sta)|*.sta" ; FileDlg.FilterIndex = 2 ;
//quit if user presses the cancel button if (FileDlg.ShowDialog() == DialogResult.Cancel)
{ return; }
//get the file pathway string FilePath = FileDlg.FileName;
//launch an instance of Statistica Statistica.Application StatApp = new Statistica.Application(); StatApp.Visible = true;
/*open the spreadsheet--note that a cast is necessary because Spreadsheets.open() returns as an Object*/ Statistica.Spreadsheet spr = (Statistica.Spreadsheet)StatApp.Spreadsheets.Open(FilePath,true);
//create a box and whisker plot CreateBoxAndWhisker(StatApp, spr); }
/// <summary> /// creates a box and whisker plot with outliers calculated from standard /// error plus means /// </summary> public void CreateBoxAndWhisker(Statistica.Application StatApp, Statistica.Spreadsheet DataSet)
{ Statistica.Analysis newanalysis = StatApp.Analysis((Statistica.AnalysisIdentifier)11010,DataSet); Statistica.BoxPlot2D BoxPlotDlg = (Statistica.BoxPlot2D)newanalysis.Dialog;
//set the box plot's options BoxPlotDlg.set_DependentVariable("2"); BoxPlotDlg.BoxMode = 0; //regular mode BoxPlotDlg.MiddlePointValue = 0; //midpoint is mean BoxPlotDlg.MiddlePointStyle = 0; BoxPlotDlg.MiddlePointPooledVariance = false; BoxPlotDlg.MultipleBoxLayout = 0; //multiple plot shift BoxPlotDlg.TrimDistribExtremes = 0; BoxPlotDlg.ConnectMiddlePoint = false; BoxPlotDlg.MeanBoxValue = 0; //means + std. err. BoxPlotDlg.MeanWhiskerValue = (Statistica.MeanWhiskerValueType)4; //Min-Max values BoxPlotDlg.Outliers = (Statistica.Outlier)3; //outliers and extremes BoxPlotDlg.OutliersCoefficient = 1.500000; BoxPlotDlg.DisplayFTestAndP = false; BoxPlotDlg.DisplayKruskalWallisTest = false; BoxPlotDlg.set_BoxCoefficient(0, 1); BoxPlotDlg.set_WhiskerCoefficient(0, 1.000000);
//get access to the box and whisker plot within the analysis Statistica.StaDocuments docs = (Statistica.StaDocuments)BoxPlotDlg.Graphs; StatisticaGraphics.Graph grph = (StatisticaGraphics.Graph)docs[1];
//customize the graph CustomizeBoxPlot(grph); }
/// <summary> /// customizes the box plot /// </summary> public void CustomizeBoxPlot(StatisticaGraphics.Graph BoxPlot)
{ StatisticaGraphics.Layout2D GraphLayout = (StatisticaGraphics.Layout2D)BoxPlot.Content;
//add your company name to the title BoxPlot.Titles[1].Text = "Acme corporation";
//set the background to be transparent GraphLayout.Attribute.Background.Color.Transparent = true;
//present the box plot BoxPlot.Visible = true; }
}
}