Sunday, August 23, 2009

PowerShell and ZedGraph – Example 1

Was searching for a PowerShell Mathematics library when I stumbled upon a cool .NET drawing library that can be easily consumed in PowerShell –ZedGraph. This is a fairly robust library that will enable you to graph multiple types of charts, including overlays. I plan on writing a series of posts that will demonstrate these capabilities.

Lets get started!

For the first chart, we are going to keep it simple. We will create a standard Bar chart with some fictitious data. The end result will look something like this.


Now to the code:

# Load the ZedGraph dll   
$ZedGraphDll = "C:\zedgraph_dll_v515\zedgraph_dll_v5.1.5\ZedGraph.dll"
[System.Reflection.Assembly]::LoadFrom($ZedGraphDll) | out-null

# Create a WinForm to serve as a container
$global:form = new-object Windows.Forms.form
$form.Size = new-object System.Drawing.Size @(500,400)

# Create a ZedGraphControl
$zgc = new-object -typename ZedGraph.ZedGraphControl
$zgc.GraphPane.Title.Text = "Yuengling Consumption"
$zgc.GraphPane.XAxis.Title.Text = "Month"
$zgc.GraphPane.YAxis.Title.Text = "Bottles"

$xLabels = "April", "May", "June","July", "August"
$yLabels = 60, 60, 75, 70, 15

$zgc.GraphPane.AddBar("Ying",$null,$yLabels,[System.Drawing.Color]::Red)
$zgc.GraphPane.XAxis.Type = 'Text'
$zgc.GraphPane.XAxis.Scale.TextLabels = $xLabels

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

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

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

As you can see, it is fairly simple to get a standard graph output. The class documentation on the ZedGraph site was very helpfull. Make sure you spend some time looking over it as you start to explore this dll. I will continue to build on this example over the next few days.

Enjoy!

No comments: