PoshCode Logo PowerShell Code Repository

LibraryChart by Chad Miller 31 months ago
View followups from Chad Miller | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1204"></script>download | new post

Defines functions for wokring with Microsoft Chart Control for .NET 3.5 Framework.Pipe output to Out-Chart function and specify chart type. Chart will display in form or save to image file. Real-time charts are supported by passing in a script block

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

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me