Skip to content

Instantly share code, notes, and snippets.

@Tcip
Last active April 16, 2025 18:42
Show Gist options
  • Save Tcip/676b8b2f8ec786ce85ee5db5a06dda6d to your computer and use it in GitHub Desktop.
Save Tcip/676b8b2f8ec786ce85ee5db5a06dda6d to your computer and use it in GitHub Desktop.
Displays top 10 processes in a Pie chart with PowerShell
# Building a Chart Using PowerShell and Chart Controls
# https://learn-powershell.net/2016/09/18/building-a-chart-using-powershell-and-chart-controls/
#region Helper Functions
Function Invoke-SaveDialog {
$FileTypes = [enum]::GetNames('System.Windows.Forms.DataVisualization.Charting.ChartImageFormat') | ForEach {
$_.Insert(0, '*.')
}
$SaveFileDlg = New-Object System.Windows.Forms.SaveFileDialog
$SaveFileDlg.DefaultExt = 'PNG'
$SaveFileDlg.Filter = "Image Files ($($FileTypes))|$($FileTypes)|All Files (*.*)|*.*"
$return = $SaveFileDlg.ShowDialog()
If ($Return -eq 'OK') {
[pscustomobject]@{
FileName = $SaveFileDlg.FileName
Extension = $SaveFileDlg.FileName -replace '.*\.(.*)', '$1'
}
}
}
#endregion Helper Functions
$Processes = Get-Process | Sort-Object WS -Descending | Select-Object -First 10
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$Series = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series
$ChartTypes = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]
$Series.ChartType = $ChartTypes::Pie
$Chart.Series.Add($Series)
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series['Series1'].Points.DataBindXY($Processes.Name, $Processes.WS)
$Chart.Width = 700
$Chart.Height = 400
$Chart.Left = 10
$Chart.Top = 10
$Chart.BackColor = [System.Drawing.Color]::White
$Chart.BorderColor = 'Black'
$Chart.BorderDashStyle = 'Solid'
$ChartTitle = New-Object System.Windows.Forms.DataVisualization.Charting.Title
$ChartTitle.Text = 'Top 5 Processes by Working Set Memory'
$Font = New-Object System.Drawing.Font @('Microsoft Sans Serif', '12', [System.Drawing.FontStyle]::Bold)
$ChartTitle.Font = $Font
$Chart.Titles.Add($ChartTitle)
$Chart.Series['Series1']['PieLabelStyle'] = 'Disabled'
$Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend
$Legend.IsEquallySpacedItems = $True
$Legend.BorderColor = 'Black'
$Chart.Legends.Add($Legend)
$chart.Series["Series1"].LegendText = "#VALX (#VALY)"
$Chart.Series['Series1']['PieLineColor'] = 'Black'
$Chart.Series['Series1']['PieLabelStyle'] = 'Outside'
$Chart.Series['Series1'].Label = "#VALX (#VALY)"
#region Windows Form to Display Chart
$AnchorAll = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Width = 740
$Form.Height = 490
$Form.controls.add($Chart)
$Chart.Anchor = $AnchorAll
# add a save button
$SaveButton = New-Object Windows.Forms.Button
$SaveButton.Text = "Save"
$SaveButton.Top = 420
$SaveButton.Left = 600
$SaveButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
# [enum]::GetNames('System.Windows.Forms.DataVisualization.Charting.ChartImageFormat')
$SaveButton.add_click({
$Result = Invoke-SaveDialog
If ($Result) {
$Chart.SaveImage($Result.FileName, $Result.Extension)
}
})
$Form.controls.add($SaveButton)
$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
#endregion Windows Form to Display Chart
@Tcip
Copy link
Author

Tcip commented Apr 16, 2025

Displays top 10 processes in a Pie chart with PowerShell

This is a working demo of build a chart in PowerShell
original code

Copy and Paste ready

Displays top 10 processes in a Pie chart with PowerShell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment