Difference between
modified post 1205 by Chad Miller on Wed 8th Jul 06:35 and
original post 1204 by Chad Miller on Tue 7th Jul 19:40
Showold version | new version | both versions
| 1 | 1 | # --------------------------------------------------------------------------- | |
| 2 | 2 | ### <Script> | |
| 3 | 3 | ### <Author> | |
| 4 | 4 | ### Chad Miller | |
| 5 | 5 | ### </Author> | |
| 6 | 6 | ### <Description> | |
| 7 | 7 | ### Defines functions for wokring with Microsoft Chart Control for .NET 3.5 Framework | |
| 8 | 8 | ### Pipe output of Powershell command to Out-Chart function and specify chart type | |
| 9 | 9 | ### Chart will display in form or save to image file | |
| 10 | 10 | ### Real-time charts are supported by passing in a script block | |
| 11 | 11 | ### My thanks to Richard MacDonald for his wonderful post on Charting with PowerShell | |
| 12 | 12 | ### http://blogs.technet.com/richard_macdonald/archive/2009/04/28/3231887.aspx | |
| 13 | 13 | ### Note: Requires NET Framework 3.5 and Microsoft Chart Controls for Microsoft .NET Framework 3.5 | |
| 14 | 14 | ### </Description> | |
| 15 | 15 | ### <Usage> | |
| 16 | 16 | ### . ./LibraryChart.ps1 | |
| 17 | 17 | ### -------------------------- EXAMPLE 1 -------------------------- | |
| 18 | 18 | ### Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 5 | out-chart -xField 'name' -yField 'WS' | |
| 19 | 19 | ### | |
| 20 | 20 | ### This command will produce a default column chart | |
| 21 | 21 | ### | |
| 22 | 22 | ### -------------------------- EXAMPLE 2 -------------------------- | |
| 23 | 23 | ### Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 5 | out-chart -xField 'name' -yField 'WS' -filename 'c:\users\u00\documents\process.png' | |
| 24 | 24 | ### This command will output the chart to a file | |
| 25 | 25 | ### | |
| 26 | 26 | ### -------------------------- EXAMPLE 3 -------------------------- | |
| 27 | 27 | ### Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 5 | out-chart -xField 'name' -yField 'WS' -chartType 'Pie' | |
| 28 | 28 | ### | |
| 29 | 29 | ### This command will produce a pie chart | |
| 30 | 30 | ### | |
| 31 | 31 | ### -------------------------- EXAMPLE 4 -------------------------- | |
| 32 | 32 | ### out-chart -xField 'name' -yField 'WS' -scriptBlock {Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 1} -chartType 'line' | |
| 33 | 33 | ### | |
| 34 | 34 | ### This command will produce a real-time line chart | |
| 35 | 35 | ### | |
| 36 | 36 | ### </Usage> | |
| 37 | 37 | ### </Script> | |
| 38 | 38 | # -------------------------------------------------------------------------- | |
| 39 | 39 | [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | |
| 40 | 40 | [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") | |
| 42 | 42 | ####################### | |
| 43 | 43 | function New-Chart | |
| 44 | 44 | { | |
| 45 | 45 | param ([int]$width,[int]$height,[int]$left,[int]$top,$chartTitle) | |
| 46 | 46 | # create chart object | |
| 47 | 47 | $global:Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart | |
| 48 | 48 | $global:Chart.Width = $width | |
| 49 | 49 | $global:Chart.Height = $height | |
| 50 | 50 | $global:Chart.Left = $left | |
| 51 | 51 | $global:Chart.Top = $top | |
| 52 | 52 | # create a chartarea to draw on and add to chart | |
| 53 | 53 | $chartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea | |
| 54 | 54 | $global:chart.ChartAreas.Add($chartArea) | |
| 56 | 56 | [void]$global:Chart.Titles.Add($chartTitle) | |
| 58 | 58 | # change chart area colour | |
| 59 | 59 | $global:Chart.BackColor = [System.Drawing.Color]::Transparent | |
| 61 | 61 | } #New-Chart | |
| 63 | 63 | ####################### | |
| 64 | 64 | function New-BarColumnChart | |
| 65 | 65 | { | |
| 66 | 66 | param ([hashtable]$ht, $chartType='Column', $chartTitle,$xTitle,$yTitle, [int]$width,[int]$height,[int]$left,[int]$top,[bool]$asc) | |
| 68 | 68 | New-Chart -width $width -height $height -left $left -top $top -chartTile $chartTitle | |
| 70 | 70 | $chart.ChartAreas[0].AxisX.Title = $xTitle | |
| 71 | 71 | $chart.ChartAreas[0].AxisY.Title = $yTitle | |
| 73 | 73 | [void]$global:Chart.Series.Add("Data") | |
| 74 | 74 | $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys, $ht.Values) | |
| 76 | 76 | $global:Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::$chartType | |
| 78 | 78 | if ($asc) | |
| 79 | 79 | { $global:Chart.Series["Data"].Sort([System.Windows.Forms.DataVisualization.Charting.PointSortOrder]::Ascending, "Y") } | |
| 80 | 80 | else | |
| 81 | 81 | { $global:Chart.Series["Data"].Sort([System.Windows.Forms.DataVisualization.Charting.PointSortOrder]::Descending, "Y") } | |
| 83 | 83 | $global:Chart.Series["Data"]["DrawingStyle"] = "Cylinder" | |
| 84 | 84 | $global:chart.Series["Data"].IsValueShownAsLabel = $true | |
| 85 | 85 | $global:chart.Series["Data"]["LabelStyle"] = "Top" | |
| 88 | 88 | } #New-BarColumnChart | |
| 90 | 90 | ####################### | |
| 91 | 91 | function New-LineChart | |
| 92 | 92 | { | |
| 94 | 94 | param ([hashtable]$ht,$chartTitle, [int]$width,[int]$height,[int]$left,[int]$top) | |
| 96 | 96 | New-Chart -width $width -height $height -left $left -top $top -chartTile $chartTitle | |
| 98 | 98 | [void]$global:Chart.Series.Add("Data") | |
| 99 | - | $global:Chart.Series["Data"].Points.DataBindY($ht.Values) | |
| 99 | + | # $global:Chart.Series["Data"].Points.AddXY($(get-date), $($ht.Values)) | |
| 100 | + | $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys,$ht.Values) | |
| 102 | + | #$global:Chart.Series["Data"].XValueType = [System.Windows.Forms.DataVisualization.Charting.ChartValueType]::Time | |
| 103 | + | #$global:Chart.chartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss" | |
| 104 | + | #$global:Chart.chartAreas[0].AxisX.LabelStyle.Interval = 1 | |
| 105 | + | #$global:Chart.chartAreas[0].AxisX.LabelStyle.IntervalType = [System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType]::Seconds | |
| 101 | 106 | $global:Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line | |
| 102 | - | $global:chart.Series["Data"].IsValueShownAsLabel = $false | |
| 107 | + | #$global:chart.Series["Data"].IsValueShownAsLabel = $false | |
| 104 | 109 | } #New-LineChart | |
| 106 | 111 | ####################### | |
| 107 | 112 | function New-PieChart | |
| 108 | 113 | { | |
| 110 | 115 | param ([hashtable]$ht,$chartTitle, [int]$width,[int]$height,[int]$left,[int]$top) | |
| 112 | 117 | New-Chart -width $width -height $height -left $left -top $top -chartTile $chartTitle | |
| 114 | 119 | [void]$global:Chart.Series.Add("Data") | |
| 115 | 120 | $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys, $ht.Values) | |
| 117 | 122 | $global:Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie | |
| 119 | 124 | $global:Chart.Series["Data"]["PieLabelStyle"] = "Outside" | |
| 120 | 125 | $global:Chart.Series["Data"]["PieLineColor"] = "Black" | |
| 121 | 126 | #$global:chart.Series["Data"].IsValueShownAsLabel = $true | |
| 122 | 127 | #$global:chart.series["Data"].Label = "#PERCENT{P1}" | |
| 123 | 128 | #$legend = New-object System.Windows.Forms.DataVisualization.Charting.Legend | |
| 124 | 129 | #$global:Chart.Legends.Add($legend) | |
| 125 | 130 | #$Legend.Name = "Default" | |
| 127 | 132 | } #New-PieChart | |
| 129 | 134 | ####################### | |
| 130 | 135 | function Remove-Points | |
| 131 | 136 | { | |
| 132 | 137 | param($name='Data',[int]$maxPoints=200) | |
| 134 | 139 | while ( $global:chart.Series["$name"].Points.Count > $maxPoints ) | |
| 135 | 140 | { $global:chart.Series["$name"].Points.RemoveAT(0) } | |
| 137 | 142 | } #Add-Series | |
| 139 | 144 | ####################### | |
| 140 | 145 | function Out-Form | |
| 141 | 146 | { | |
| 142 | 147 | param($interval,$scriptBlock,$xField,$yField) | |
| 144 | 149 | # display the chart on a form | |
| 145 | 150 | $global:Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor | |
| 146 | 151 | [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left | |
| 147 | 152 | $Form = New-Object Windows.Forms.Form | |
| 148 | 153 | $Form.Text = 'PowerCharts' | |
| 149 | 154 | $Form.Width = 600 | |
| 150 | 155 | $Form.Height = 600 | |
| 151 | 156 | $Form.controls.add($global:Chart) | |
| 152 | 157 | if ($scriptBlock -is [ScriptBlock]) | |
| 153 | 158 | { | |
| 154 | 159 | if (!($xField -or $yField)) | |
| 155 | 160 | { throw 'xField and yField required with scriptBlock parameter.' } | |
| 156 | 161 | $timer = New-Object System.Windows.Forms.Timer | |
| 157 | 162 | $timer.Interval = $interval | |
| 158 | 163 | $timer.add_Tick({ | |
| 160 | 165 | $ht = &$scriptBlock | ConvertTo-HashTable $xField $yField | |
| 161 | 166 | if ($global:Chart.Series["Data"].ChartTypeName -eq 'Line') | |
| 162 | 167 | { | |
| 163 | 168 | Remove-Points | |
| 164 | - | #$ht | foreach { $global:Chart.Series["Data"].Points.AddXY($($_.Keys), $($_.Values)) } | |
| 169 | + | $ht | foreach { $global:Chart.Series["Data"].Points.AddXY($($_.Keys), $($_.Values)) } | |
| 165 | - | $ht | foreach { $global:Chart.Series["Data"].Points.AddY($ht[$xField]) } | |
| 166 | 170 | } | |
| 167 | 171 | else | |
| 168 | 172 | { $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys, $ht.Values) } | |
| 169 | 173 | $global:chart.ResetAutoValues() | |
| 170 | 174 | $global:chart.Invalidate() | |
| 172 | 176 | }) | |
| 173 | 177 | $timer.Enabled = $true | |
| 174 | 178 | $timer.Start() | |
| 176 | 180 | } | |
| 177 | 181 | $Form.Add_Shown({$Form.Activate()}) | |
| 178 | 182 | $Form.ShowDialog() | |
| 180 | 184 | } #Out-Form | |
| 182 | 186 | ####################### | |
| 183 | 187 | function Out-ImageFile | |
| 184 | 188 | { | |
| 185 | 189 | param($fileName,$fileformat) | |
| 187 | 191 | $global:Chart.SaveImage($fileName,$fileformat) | |
| 188 | 192 | } | |
| 189 | 193 | ####################### | |
| 190 | 194 | ### ConvertTo-Hashtable function based on code by Jeffery Snover | |
| 191 | 195 | ### http://blogs.msdn.com/powershell/archive/2008/11/23/poshboard-and-convertto-hashtable.aspx | |
| 192 | 196 | ####################### | |
| 193 | 197 | function ConvertTo-Hashtable | |
| 194 | 198 | { | |
| 195 | 199 | param([string]$key, $value) | |
| 197 | 201 | Begin | |
| 198 | 202 | { | |
| 199 | 203 | $hash = @{} | |
| 200 | 204 | } | |
| 201 | 205 | Process | |
| 202 | 206 | { | |
| 203 | 207 | $thisKey = $_.$Key | |
| 204 | 208 | $hash.$thisKey = $_.$Value | |
| 205 | 209 | } | |
| 206 | 210 | End | |
| 207 | 211 | { | |
| 208 | 212 | Write-Output $hash | |
| 209 | 213 | } | |
| 211 | 215 | } #ConvertTo-Hashtable | |
| 213 | 217 | ####################### | |
| 214 | 218 | function Out-Chart | |
| 215 | 219 | { | |
| 216 | 220 | param( $xField=$(throw 'Out-Chart:xField is required'), | |
| 217 | 221 | $yField=$(throw 'Out-Chart:yField is required'), | |
| 218 | 222 | $chartType='Column',$chartTitle, | |
| 219 | 223 | [int]$width=500, | |
| 220 | 224 | [int]$height=400, | |
| 221 | 225 | [int]$left=40, | |
| 222 | 226 | [int]$top=30, | |
| 223 | 227 | $filename, | |
| 224 | 228 | $fileformat='png', | |
| 225 | 229 | [int]$interval=1000, | |
| 226 | 230 | $scriptBlock, | |
| 227 | 231 | [switch]$asc | |
| 228 | 232 | ) | |
| 230 | 234 | Begin | |
| 231 | 235 | { | |
| 232 | 236 | $ht = @{} | |
| 233 | 237 | } | |
| 234 | 238 | Process | |
| 235 | 239 | { | |
| 236 | 240 | if ($_) | |
| 237 | 241 | { | |
| 238 | 242 | $thisKey = $_.$xField | |
| 239 | 243 | $ht.$thisKey = $_.$yField | |
| 240 | 244 | } | |
| 241 | 245 | } | |
| 242 | 246 | End | |
| 243 | 247 | { | |
| 244 | 248 | if ($scriptBlock) | |
| 245 | 249 | { $ht = &$scriptBlock | ConvertTo-HashTable $xField $yField } | |
| 246 | 250 | switch ($chartType) | |
| 247 | 251 | { | |
| 248 | 252 | 'Bar' {New-BarColumnChart -ht $ht -chartType $chartType -chartTitle $chartTitle -width $width -height $height -left $left -top $top -asc $($asc.IsPresent)} | |
| 249 | 253 | 'Column' {New-BarColumnChart -ht $ht -chartType $chartType -chartTitle $chartTitle -width $width -height $height -left $left -top $top -asc $($asc.IsPresent)} | |
| 250 | 254 | 'Pie' {New-PieChart -chartType -ht $ht -chartTitle $chartTitle -width $width -height $height -left $left -top $top } | |
| 251 | 255 | 'Line' {New-LineChart -chartType -ht $ht -chartTitle $chartTitle -width $width -height $height -left $left -top $top } | |
| 253 | 257 | } | |
| 255 | 259 | if ($filename) | |
| 256 | 260 | { Out-ImageFile $filename $fileformat } | |
| 257 | 261 | elseif ($scriptBlock ) | |
| 258 | 262 | { Out-Form $interval $scriptBlock $xField $yField } | |
| 259 | 263 | else | |
| 260 | 264 | { Out-Form } | |
| 261 | 265 | } | |
| 263 | 267 | } #Out-Chart |
ContributeMost Recent Contributions (feed)
- VerifyCategoryRule.ps1
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Use-Culture.ps1
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Start-ProcessAsUser.ps1
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Show-HtmlHelp.ps1
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Show-ColorizedContent.ps
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Set-RemoteRegistryKeyPro
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Set-PsBreakPointLastErro
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Set-ConsoleProperties.ps
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Set-Clipboard.ps1
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- Send-TcpRequest.ps1
- 18 hours ago
-
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
PowerShell Code Repository