Using Sho Visualization in WinForms from C#
Over on the Forum of Sho drobertson asked how to create a Contour Chart from C# within a form. I thought that would make good example code, so here it is:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ShoNS.Visualization;
using ShoNS.Array;
using ShoNS.MathFunc;
namespace testForm
{
// Create a composite visualization by deriving from System.Windows.Forms.Form
public class testForm : Form
{
private ShoChart ch; // this will hold a Sho contour chart
private Button button1; // a button to update the contour chart
private Label label1; // a label to show how many times the button has been clicked
private int num; // a counter
public testForm()
{
num = 0;
InitializeComponent();
}
// code to generate (slightly random) data and create a contour chart from it
// this method shows examples of calling into the Sho math libraries and the Sho viz libraries
private ShoChart GenChart()
{
// first, create a range object that spans from 0 to pi, with 100 elements
DoubleRange b = new DoubleRange(0.0, Math.PI);
b.Count = 100;
// now, create a row vector and fill it with random numbers
DoubleArray x = new DoubleArray(1, 100);
ArrayRandom.FillRandom<double>(x);
// shift and scale the random numbers, and add sin(b) to it, to make a noisy sinusoid
x = (x-0.5) * 0.1 + ArrayMath.Sin(b);
// analogously, we'll make a noisy cosine row vector, 100 long
DoubleArray y = new DoubleArray(1, 100);
ArrayRandom.FillRandom<double>(y);
y = (y-0.5) * 0.1 + ArrayMath.Cos(b);
// now, make a matrix from the outer product of x and y.
DoubleArray arr = x.T * y;
// from the matrix, create a contour plot. Second argument says automatically choose the contours
ShoChart ch = new ShoContourChart(arr, null);
// all of the Sho viz classes inherit from control, so we can set WinForms control parameters
// to size the contour plot and place it within the parent form.
ch.AutoSize = true;
ch.Location = new Point(25, 75);
ch.Name = "ContourChart";
ch.Size = new Size(375, 225);
return ch;
}
// a button click will create a new contour chart with slightly randomized data
private void buttonClick(object sender, EventArgs e)
{
SuspendLayout();
// take the old contour plot out of the form
Controls.Remove(ch);
// generate a new one, and put it into the form
ch = GenChart();
Controls.Add(ch);
// increment the label
label1.Text = num.ToString();
num++;
// relayout and refresh the form
ResumeLayout(false);
PerformLayout();
Refresh();
}
private void InitializeComponent()
{
// code derived from form wizard: create a button and label
button1 = new Button();
label1 = new Label();
SuspendLayout();
// position and parameterize the button
button1.Location = new Point(175, 50);
button1.Name = "button1";
button1.Size = new Size(100, 20);
button1.TabIndex = 0;
button1.Text = "click me";
button1.UseVisualStyleBackColor = true;
button1.Click += new EventHandler(buttonClick); // hook up button click handler
// position and parameterize the label
label1.AutoSize = true;
label1.Location = new Point(215, 25);
label1.Name = "label1";
label1.Size = new Size(50, 20);
label1.Text = num.ToString();
label1.TextAlign = ContentAlignment.MiddleCenter;
num++;
// create a new contour plot
ch = GenChart();
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(425, 325);
Controls.Add(label1);
Controls.Add(button1);
Controls.Add(ch);
Name = "Form1";
Text = "Example showing ShoChart embedded in WinForm using C#";
// layout the controls: now we're ready to start the message pump
ResumeLayout(false);
PerformLayout();
}
}
}
From inside of Sho, you can test this by dynamically loading the dll, and running the form in its own thread (made easy with ShoThread):
>>> load('testForm.dll')
<Assembly testForm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...>
>>> from testForm import *
>>> t = ShoThread(lambda : System.Windows.Forms.Application.Run(testForm()))
>>> t.Start()
Comments
Anonymous
February 15, 2011
I'm getting an access denied error on that linkAnonymous
February 15, 2011
My mistake... The Forum of Sho is available here: social.msdn.microsoft.com/.../threadsAnonymous
July 11, 2013
I can find library of sho. Where ShoViz.dll, MathFunc.dll... ?Anonymous
December 07, 2014
When running the program, it alerts it cannot load library ArrayT.dll, but I already add Array dll into the references. What did I miss? Thanks!Anonymous
April 07, 2015
The comment has been removed