Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell function to create a single-series spline chart from .Net chart controls and display in a WPF window
Function New-SingleSeriesSplineChart {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
$Title,
[Parameter(Mandatory=$true)]
$Data,
[Parameter(Mandatory=$true)]
$AxisX,
[Parameter(Mandatory=$true)]
$AxisY
)
# Add required assemblies
Add-Type -AssemblyName PresentationFramework,System.Windows.Forms,System.Windows.Forms.DataVisualization
# Create a WPF Window
$Window = New-object System.Windows.Window
$Window.Title = $Title
$window.Height = 800
$Window.Width = 800
$Window.WindowStartupLocation = "CenterScreen"
# Add an image to the Window
$Image = New-Object System.Windows.Controls.Image
$Image.Height = "NaN"
$Image.Width = "NaN"
$Window.AddChild($Image)
# Function to create a .Net Spline Chart
Function Create-SplineChart {
param($Title,$Data,$AxisX,$AxisY)
# Create a chart object
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Width = 800
$Chart.Height = 800
$Chart.Left = 10
$Chart.Top = 10
# Create a chartarea to draw on and add this to the chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisY.Minimum = 0
$ChartArea.AxisX.Minimum = 0
$ChartArea.AxisX.Interval = 1
$ChartArea.AxisX.IsLabelAutoFit = $false
$ChartArea.AxisX.LabelStyle.Angle = -45
$ChartArea.Area3DStyle.Enable3D = $True
$ChartArea.Area3DStyle.Inclination = "10"
$ChartArea.Area3DStyle.Rotation = "10"
$ChartArea.BackColor = "AliceBlue"
$ChartArea.AxisX.LabelStyle.Font = (New-Object System.Drawing.Font -ArgumentList "Segui", "12")
$Chart.ChartAreas.Add($ChartArea)
[void]$Chart.Series.Add($AxisY)
# Add a legend
$Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend
$Chart.Legends.Add($Legend)
$Chart.Legends[0].Docking = "Bottom"
$Chart.Legends[0].Font = (New-Object System.Drawing.Font -ArgumentList "Segui", "12")
$Chart.Legends[0].Alignment = "Center"
# Add a datapoint for each value specified in the provided data
$Data | foreach {
$datapoint = new-object System.Windows.Forms.DataVisualization.Charting.DataPoint(0, $_.$AxisY)
$datapoint.AxisLabel = $_.$AxisX
$Chart.Series[$AxisY].Points.Add($datapoint)
}
# Set the chart type
$Chart.Series[$AxisY].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Spline
# Set the title of the Chart
$TitleObj = new-object System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($TitleObj)
$Chart.Titles[0].Font = (New-Object System.Drawing.Font -ArgumentList "Segui", "18")
$Chart.Titles[0].Text = $Title
# Save the chart to a memory stream
$Stream = New-Object System.IO.MemoryStream
$Chart.SaveImage($Stream,"png")
$script:ImageStream = $Stream.GetBuffer()
$Stream.Dispose()
}
# Add an event to display the chart when the window is opened
$Window.Add_ContentRendered({
# Create the Chart
Create-SplineChart -Title $Title -Data $Data -AxisX $AxisX -AxisY $AxisY
# Set the image source
$image.Source = $ImageStream
$This.Activate()
})
# Display window
$null = $window.Dispatcher.InvokeAsync{$window.ShowDialog()}.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.