PoshCode Logo PowerShell Code Repository

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

    
11
# ---------------------------------------------------------------------------
22
### <Script>
33
### <Author>
44
### Chad Miller
55
### </Author>
66
### <Description>
77
### Defines functions for wokring with  Microsoft Chart Control for .NET 3.5 Framework
88
### Pipe output of Powershell command to Out-Chart function and specify chart type
99
### Chart will display in form or save to image file
1010
### Real-time charts are supported by passing in a script block
1111
### My thanks to Richard MacDonald for his wonderful post on Charting with PowerShell
1212
### http://blogs.technet.com/richard_macdonald/archive/2009/04/28/3231887.aspx
1313
### Note: Requires NET Framework 3.5 and Microsoft Chart Controls for Microsoft .NET Framework 3.5
1414
### </Description>
1515
### <Usage>
1616
### . ./LibraryChart.ps1
1717
###  -------------------------- EXAMPLE 1 --------------------------
1818
### Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 5  | out-chart -xField 'name' -yField 'WS'
1919
###
2020
### This command will produce a default column chart
2121
###
2222
###  -------------------------- EXAMPLE 2 --------------------------
2323
### 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'
2424
### This command will output the chart to a file
2525
###
2626
###  -------------------------- EXAMPLE 3 --------------------------
2727
### Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 5  | out-chart -xField 'name' -yField 'WS' -chartType 'Pie'
2828
###
2929
### This command will produce a pie chart
3030
###
3131
###  -------------------------- EXAMPLE 4 --------------------------
3232
### out-chart -xField 'name' -yField 'WS' -scriptBlock {Get-Process | Sort-Object -Property WS | Select-Object Name,WS -Last 1} -chartType 'line'
3333
###
3434
### This command will produce a real-time line chart
3535
###
3636
### </Usage>
3737
### </Script>
3838
# --------------------------------------------------------------------------
3939
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
4040
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
4242
#######################
4343
function New-Chart
4444
{
4545
    param ([int]$width,[int]$height,[int]$left,[int]$top,$chartTitle)
4646
    # create chart object
4747
    $global:Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
4848
    $global:Chart.Width = $width
4949
    $global:Chart.Height = $height
5050
    $global:Chart.Left = $left
5151
    $global:Chart.Top = $top
5252
   # create a chartarea to draw on and add to chart
5353
    $chartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
5454
    $global:chart.ChartAreas.Add($chartArea)
5656
    [void]$global:Chart.Titles.Add($chartTitle)
5858
    # change chart area colour
5959
    $global:Chart.BackColor = [System.Drawing.Color]::Transparent
6161
} #New-Chart
6363
#######################
6464
function New-BarColumnChart
6565
{
6666
    param ([hashtable]$ht, $chartType='Column', $chartTitle,$xTitle,$yTitle, [int]$width,[int]$height,[int]$left,[int]$top,[bool]$asc)
6868
    New-Chart -width $width -height $height -left $left -top $top -chartTile $chartTitle
7070
    $chart.ChartAreas[0].AxisX.Title = $xTitle
7171
    $chart.ChartAreas[0].AxisY.Title = $yTitle
7373
    [void]$global:Chart.Series.Add("Data")
7474
    $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys, $ht.Values)
7676
    $global:Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::$chartType
7878
    if ($asc)
7979
    { $global:Chart.Series["Data"].Sort([System.Windows.Forms.DataVisualization.Charting.PointSortOrder]::Ascending, "Y") }
8080
    else
8181
    { $global:Chart.Series["Data"].Sort([System.Windows.Forms.DataVisualization.Charting.PointSortOrder]::Descending, "Y") }
8383
    $global:Chart.Series["Data"]["DrawingStyle"] = "Cylinder"
8484
    $global:chart.Series["Data"].IsValueShownAsLabel = $true
8585
    $global:chart.Series["Data"]["LabelStyle"] = "Top"
8888
} #New-BarColumnChart
9090
#######################
9191
function New-LineChart
9292
{
9494
    param ([hashtable]$ht,$chartTitle, [int]$width,[int]$height,[int]$left,[int]$top)
9696
    New-Chart -width $width -height $height -left $left -top $top -chartTile $chartTitle
9898
    [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
101106
    $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
104109
} #New-LineChart
106111
#######################
107112
function New-PieChart
108113
{
110115
    param ([hashtable]$ht,$chartTitle, [int]$width,[int]$height,[int]$left,[int]$top)
112117
    New-Chart -width $width -height $height -left $left -top $top -chartTile $chartTitle
114119
    [void]$global:Chart.Series.Add("Data")
115120
    $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys, $ht.Values)
117122
    $global:Chart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
119124
    $global:Chart.Series["Data"]["PieLabelStyle"] = "Outside"
120125
    $global:Chart.Series["Data"]["PieLineColor"] = "Black"
121126
    #$global:chart.Series["Data"].IsValueShownAsLabel = $true
122127
    #$global:chart.series["Data"].Label =  "#PERCENT{P1}"
123128
    #$legend = New-object System.Windows.Forms.DataVisualization.Charting.Legend
124129
    #$global:Chart.Legends.Add($legend)
125130
    #$Legend.Name = "Default"
127132
} #New-PieChart
129134
#######################
130135
function Remove-Points
131136
{
132137
    param($name='Data',[int]$maxPoints=200)
134139
    while ( $global:chart.Series["$name"].Points.Count > $maxPoints )
135140
    { $global:chart.Series["$name"].Points.RemoveAT(0) }
137142
} #Add-Series
139144
#######################
140145
function Out-Form
141146
{
142147
    param($interval,$scriptBlock,$xField,$yField)
144149
    # display the chart on a form
145150
    $global:Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor
146151
                    [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
147152
    $Form = New-Object Windows.Forms.Form
148153
    $Form.Text = 'PowerCharts'
149154
    $Form.Width = 600
150155
    $Form.Height = 600
151156
    $Form.controls.add($global:Chart)
152157
    if ($scriptBlock -is [ScriptBlock])
153158
    {
154159
        if (!($xField -or $yField))
155160
        { throw 'xField and yField required with scriptBlock parameter.' }
156161
        $timer = New-Object System.Windows.Forms.Timer
157162
        $timer.Interval = $interval
158163
        $timer.add_Tick({
160165
        $ht = &$scriptBlock | ConvertTo-HashTable $xField $yField
161166
        if ($global:Chart.Series["Data"].ChartTypeName -eq 'Line')
162167
        {
163168
            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]) }
166170
        }
167171
        else
168172
        { $global:Chart.Series["Data"].Points.DataBindXY($ht.Keys, $ht.Values) }
169173
        $global:chart.ResetAutoValues()
170174
        $global:chart.Invalidate()
172176
        })
173177
        $timer.Enabled = $true
174178
        $timer.Start()
176180
    }
177181
    $Form.Add_Shown({$Form.Activate()})
178182
    $Form.ShowDialog()
180184
} #Out-Form
182186
#######################
183187
function Out-ImageFile
184188
{
185189
    param($fileName,$fileformat)
187191
    $global:Chart.SaveImage($fileName,$fileformat)
188192
}
189193
#######################
190194
### ConvertTo-Hashtable function based on code by Jeffery Snover
191195
### http://blogs.msdn.com/powershell/archive/2008/11/23/poshboard-and-convertto-hashtable.aspx
192196
#######################
193197
function ConvertTo-Hashtable
194198
{
195199
    param([string]$key, $value)
197201
    Begin
198202
    {
199203
        $hash = @{}
200204
    }
201205
    Process
202206
    {
203207
        $thisKey = $_.$Key
204208
        $hash.$thisKey = $_.$Value
205209
    }
206210
    End
207211
    {
208212
        Write-Output $hash
209213
    }
211215
} #ConvertTo-Hashtable
213217
#######################
214218
function Out-Chart
215219
{
216220
    param(  $xField=$(throw 'Out-Chart:xField is required'),
217221
            $yField=$(throw 'Out-Chart:yField is required'),
218222
            $chartType='Column',$chartTitle,
219223
            [int]$width=500,
220224
            [int]$height=400,
221225
            [int]$left=40,
222226
            [int]$top=30,
223227
            $filename,
224228
            $fileformat='png',
225229
            [int]$interval=1000,
226230
            $scriptBlock,
227231
            [switch]$asc
228232
        )
230234
    Begin
231235
    {
232236
        $ht = @{}
233237
    }
234238
    Process
235239
    {
236240
       if ($_)
237241
       {
238242
        $thisKey = $_.$xField
239243
        $ht.$thisKey = $_.$yField
240244
       }
241245
    }
242246
    End
243247
    {
244248
        if ($scriptBlock)
245249
        { $ht = &$scriptBlock | ConvertTo-HashTable $xField $yField }
246250
        switch ($chartType)
247251
        {
248252
            'Bar' {New-BarColumnChart -ht $ht -chartType $chartType -chartTitle $chartTitle -width $width -height $height -left $left -top $top -asc $($asc.IsPresent)}
249253
            'Column' {New-BarColumnChart -ht $ht -chartType $chartType -chartTitle $chartTitle -width $width -height $height -left $left -top $top -asc $($asc.IsPresent)}
250254
            'Pie' {New-PieChart -chartType -ht $ht  -chartTitle $chartTitle -width $width -height $height -left $left -top $top }
251255
            'Line' {New-LineChart -chartType -ht $ht -chartTitle $chartTitle -width $width -height $height -left $left -top $top }
253257
        }
255259
        if ($filename)
256260
        { Out-ImageFile $filename $fileformat }
257261
        elseif ($scriptBlock )
258262
        { Out-Form $interval $scriptBlock $xField $yField }
259263
        else
260264
        { Out-Form }
261265
    }
263267
} #Out-Chart

ContributeMost Recent Contributions (feed)

Contribute ... Next Page