Creating a Chart image in PowerShell
As many of you know that PowerShell can tap into potential of .Net methods easily available with Microsoft Windows machines, the same can be utilized to create a pie chart or some other kind of chart image using a given dataset.
The below is just a sample that how it works and how it can be used. Further use cases might be creating pictures and then embedding them into HTML report
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
function New-Chart {
[cmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$xAxis,
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$yAxis,
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$width = 700,
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$height = 400,
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$left = 10,
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$top = 10,
[Parameter(ValueFromPipeline = $true, mandatory = $false)]$title = "",
[Parameter(ValueFromPipeline = $true, mandatory = $false)][ValidateScript({ $_ -in ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).Name })]$chartColor = "Transparent",
[Parameter(ValueFromPipeline = $true, mandatory = $false)][ValidateScript({ $_ -in ([System.Windows.Forms.DataVisualization.Charting.SeriesChartType] | Get-Member -Static -MemberType Properties).Name })]$chartType,
[Parameter(ValueFromPipeline = $true, ParameterSetName = "extra", mandatory = $true)][bool]$generateImage,
[Parameter(ValueFromPipeline = $true, ParameterSetName = "extra", mandatory = $false)][string]$imgExt = "png",
[Parameter(ValueFromPipeline = $true, ParameterSetName = "extra", mandatory = $false)][string]$saveImagePath = "$env:TEMP\image.png"
)
$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::$chartType
$Chart.Series.Add($Series)
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series['Series1'].Points.DataBindXY($xAxis, $yAxis)
$Chart.Width = $width
$Chart.Height = $height
$Chart.Left = $left
$Chart.Top = $top
$Chart.BackColor = [System.Drawing.Color]::$chartColor
$Chart.BorderColor = 'Black'
$Chart.BorderDashStyle = 'Solid'
$ChartTitle = New-Object System.Windows.Forms.DataVisualization.Charting.Title
$ChartTitle.Text = $title
$Font = New-Object System.Drawing.Font @('Microsoft Sans Serif', '12', [System.Drawing.FontStyle]::Bold)
$ChartTitle.Font = $Font
$Chart.Titles.Add($ChartTitle)
$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)"
$ChartArea.Area3DStyle.Enable3D = $True
$ChartArea.Area3DStyle.Inclination = 60
if ($generateImage) {
$Chart.SaveImage($saveImagePath, $imgExt)
}
Return $Chart
}
$Processes = Get-Process | Sort-Object WS -Descending | Select-Object -First 10
$s = New-Chart -xAxis $Processes.Name -yAxis $Processes.WS -chartType "Pie" -generateImage $true
?IT Systeembeheerder / Azure Cloud Administrator
3 个月So the sources of this visualisation of this pie, are the memories amounts of your local "own started" executables, within windows started up by yourself? Can you easily change that by/with a some value of cells of an .MDB or an .XLSX??