Wednesday, March 19, 2008

Using ZedGraph From PowerShell

I was messing around the other day and decided to see if I could use the ZedGraph graphing library from the command line with PowerShell. I took one of the samples in C# and converted it to PowerShell. It was actually pretty straight forward and I got a big grin on my face when a graph popped up. Nice!! The C# sample is here.

The Result

The Code
#Load the zedgraph dll

$ZedGraphDll = "J:\src\zedgraph_dll_v514_464\zedgraph_dll_v5.1.4\ZedGraph.dll"
[System.Reflection.Assembly]::LoadFrom($ZedGraphDll) | out-null

#create a new form and a ZedGraphControl
$global:form = new-object Windows.Forms.form
$form.Size = new-object System.Drawing.Size @(1000,600)

$zgc = new-object -typename ZedGraph.ZedGraphControl

#// Set the titles and axis labels
$zgc.GraphPane.Title.Text = "Demo of BaseTic property"
$zgc.GraphPane.XAxis.Title.Text = "Time, Days"
$zgc.GraphPane.YAxis.Title.Text = "Widget Production (units/hour)"

#// Build a PointPairList with points based on Sine wave
$list = new-object -typename Zedgraph.PointPairList
$pi = [System.Math]::pi
for ( $i=0; $i -lt 36; $i++ )
{
$x = $i * 10.0 + 50.0
$y = ([System.Math]::sin( ($i * ($pi) / 15.0) ) * 16.0)
$list.Add( $x, $y)
}

#// Hide the legend
$zgc.GraphPane.Legend.IsVisible = $false;

#// Add a curve
$curve = $zgc.GraphPane.AddCurve( "label", $list, [System.Drawing.Color]::Red,`
[ZedGraph.SymbolType]::Circle )
$curve.Line.Width = 1.5
$curve.Symbol.Fill = new-object -typename ZedGraph.Fill([System.Drawing.Color]::White )
$curve.Symbol.Size = 5
#// Make the XAxis start with the first label at 50
$zgc.GraphPane.XAxis.Scale.BaseTic = 50

#// Fill the axis background with a gradient
$zgc.GraphPane.Chart.Fill = new-object -typename ZedGraph.Fill(`
[System.Drawing.Color]::White,`
[System.Drawing.Color]::SteelBlue, 45.0 )


#// Calculate the Axis Scale Ranges
$zgc.AxisChange()

#add the graph to the forms control
$Form.Controls.Add($zgc)
$zgc.dock = [System.Windows.Forms.DockStyle]::Fill

#display the form
$Form.Add_Shown({$form.Activate()})
[void]$form.showdialog()

-bc

No comments: