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;

// 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);
// 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);
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)
// take the old contour plot out of the form
// generate a new one, and put it into the form
ch = GenChart();
// increment the label
label1.Text = num.ToString();
// relayout and refresh the form

private void InitializeComponent()
// code derived from form wizard: create a button and label
button1 = new Button();
label1 = new Label();


// 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;

// 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);
Name = "Form1";
Text = "Example showing ShoChart embedded in WinForm using C#";

// layout the controls: now we're ready to start the message pump


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=, Culture=neutral, PublicKeyToken=...>
>>> from testForm import *
>>> t = ShoThread(lambda : System.Windows.Forms.Application.Run(testForm()))
>>> t.Start()



Comments (5)

  1. pnewhook says:

    I'm getting an access denied error on that link

  2. Sho Team says:

    My mistake… The Forum of Sho is available here:…/threads

  3. GiangVT says:

    I can find library of sho. Where ShoViz.dll, MathFunc.dll… ?

  4. samh says:

    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?


  5. Drew says:

    A few questions:

    The colorbar keyword displays a colorbar but does not display the values each color maps to? Is there anyway to fix this? Please let me know

    Also is there a way to extract the contour line coordinates from within the code i.e. if I were to plot 10 contour lines is it possible to extract the points they represent on the figure or alternatively to trigger a mouseclick event and display x,y coordinate on the plot

Skip to main content