{"id":2857,"date":"2011-05-05T10:51:59","date_gmt":"2011-05-05T18:51:59","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=2857"},"modified":"2016-06-14T16:42:59","modified_gmt":"2016-06-14T23:42:59","slug":"primalforms-2011-designing-charts-for-powershell","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/05\/05\/primalforms-2011-designing-charts-for-powershell\/","title":{"rendered":"Designing Charts for PowerShell"},"content":{"rendered":"<p>Recently a blog post titled <a href=\"http:\/\/blogs.technet.com\/b\/richard_macdonald\/archive\/2009\/04\/28\/3231887.aspx\" target=\"_blank\">Charting with PowerShell<\/a> was brought to my attention. The blog article demonstrates how to create graphs in PowerShell using WinForms. After reading this article, I thought it would be much easier to have the option to visually design your charts instead of coding it by hand. Therefore in the last service release we added support for the Microsoft Chart Controls.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/05\/Designing-Charts.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"Designing Charts\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/05\/Designing-Charts_thumb.png\" alt=\"Designing Charts\" width=\"244\" height=\"161\" border=\"0\" \/><\/a><\/p>\n<p>Because the\u00a0chart controls are not part of the core .NET Library, you\u00a0need to download the library:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.microsoft.com\/downloads\/en\/details.aspx?FamilyId=130F7986-BF49-4FE5-9CA8-910AE6EA442C&amp;displaylang=en\" target=\"_blank\">Microsoft Chart Controls for Microsoft .NET 3.5 Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.microsoft.com\/downloads\/en\/details.aspx?familyid=EE8F6F35-B087-4324-9DBA-6DD5E844FD9F&amp;displaylang=en\" target=\"_blank\">Microsoft Chart Controls Documentation<\/a><\/li>\n<\/ul>\n<p>To demonstrate the chart\u00a0control, I created two sample forms:<\/p>\n<p><strong>Top Processes Bar Graph Sample<\/strong><\/p>\n<p>The first sample is based on Richard\u2019s Blog post which displays the top five processes with the highest Working Set in a Bar graph. The form also includes a button to export the chart into a PNG image.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/05\/Process-Chart.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"Process Chart\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/05\/Process-Chart_thumb.png\" alt=\"Process Chart\" width=\"244\" height=\"175\" border=\"0\" \/><\/a><\/p>\n<p>Since the Chart is not the easiest control to use, I created helper functions to make it simpler to display your results in a chart (See sample files for function definitions):<\/p>\n<p><span style=\"color: #008080;\">Load-Chart<\/span> \u2013 By using this function you need not worry about what properties to set or what objects to create. It will handle most of the dirty work for you. All you need to do is pass the points to plot.<\/p>\n<p><span style=\"color: #008080;\">Clear-Chart<\/span> \u2013 This function will remove all the chart areas, series, and points.<\/p>\n<p>With these helper functions loading the chart boils down to the following:<\/p>\n<pre>    <span style=\"color: #8b0000;\">$Processes<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #0000ff; font-weight: bold;\">Get-Process<\/span> <span style=\"color: #0000ff;\">|<\/span> <span style=\"color: #0000ff; font-weight: bold;\">Sort-Object<\/span> <span style=\"color: #0000ff;\">-<\/span><span style=\"color: #000000;\">Property WS <\/span><span style=\"color: #0000ff;\">|<\/span> <span style=\"color: #0000ff; font-weight: bold;\">Select-Object<\/span><span style=\"color: #000000;\"> Name,WS,ID <\/span><span style=\"color: #0000ff;\">-<\/span><span style=\"color: #000000;\">Last <\/span><span style=\"color: #000000;\">5<\/span>\r\n    <span style=\"color: #8b0000;\">$ProcNames<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\"> @(<\/span><span style=\"color: #0000ff;\">foreach<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #8b0000;\">$Proc<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$Processes<\/span><span style=\"color: #000000;\">){<\/span><span style=\"color: #8b0000;\">$Proc<\/span><span style=\"color: #000000;\">.Name <\/span><span style=\"color: #0000ff;\">+<\/span> <span style=\"color: #ff0000;\">\" (\"<\/span> <span style=\"color: #0000ff;\">+<\/span> <span style=\"color: #8b0000;\">$Proc<\/span><span style=\"color: #000000;\">.ID <\/span><span style=\"color: #0000ff;\">+<\/span> <span style=\"color: #ff0000;\">\")\"<\/span><span style=\"color: #000000;\">})\r\n    <\/span><span style=\"color: #8b0000;\">$WS<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\"> @(<\/span><span style=\"color: #0000ff;\">foreach<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #8b0000;\">$Proc<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$Processes<\/span><span style=\"color: #000000;\">){<\/span><span style=\"color: #8b0000;\">$Proc<\/span><span style=\"color: #000000;\">.WS<\/span><span style=\"color: #0000ff;\">\/<\/span><span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">MB}) \r\n\r\n    <\/span><span style=\"color: #008080;\">Load-Chart<\/span> <span style=\"color: #8b0000;\">$chart1<\/span> <span style=\"color: #0000ff;\">-<\/span><span style=\"color: #000000;\">XPoints <\/span><span style=\"color: #8b0000;\">$ProcNames<\/span> <span style=\"color: #0000ff;\">-<\/span><span style=\"color: #000000;\">YPoints <\/span><span style=\"color: #8b0000;\">$WS<\/span><\/pre>\n<p>To save the chart as an image, you can use the following method:<\/p>\n<blockquote>\n<pre><span style=\"color: #8b0000;\">$Chart1<\/span><span style=\"color: #000000;\">.SaveImage(<\/span><span style=\"color: #ff0000;\">\"Filepath\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"PNG\"<\/span><span style=\"color: #000000;\">)<\/span><\/pre>\n<\/blockquote>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Disk Space Pie Chart Sample<\/strong><\/p>\n<p>The second sample gathers information about the system\u2019s hard drives and displays them using pie charts.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/05\/Disk-Space-Pie-Chart.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;\" title=\"Disk Space Pie Chart\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/05\/Disk-Space-Pie-Chart_thumb.png\" alt=\"Disk Space Pie Chart\" width=\"244\" height=\"175\" border=\"0\" \/><\/a><\/p>\n<p>Using the following script you can display a pie chart for each hard drive:<\/p>\n<pre>    <span style=\"color: #008000;\">#Get Disk space using WMI and make sure it is an array<\/span>\r\n    <span style=\"color: #8b0000;\">$Disks<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"background-color: #ffffff; color: #000000;\"> @(<\/span><span style=\"color: #0000ff; font-weight: bold;\">Get-WMIObject<\/span><span style=\"background-color: #ffffff; color: #000000;\"> Win32_LogicalDisk <\/span><span style=\"color: #0000ff;\">-<\/span><span style=\"color: #0000ff;\">filter<\/span> <span style=\"color: #ff0000;\">\"DriveType=3\"<\/span><span style=\"background-color: #ffffff; color: #000000;\"> )\r\n\r\n    <\/span><span style=\"color: #008000;\">#Remove all the current charts<\/span>\r\n    <span style=\"color: #008080;\">Clear-Chart<\/span> <span style=\"color: #8b0000;\">$chart1<\/span>\r\n\r\n    <span style=\"color: #008000;\">#Loop through each drive<\/span>\r\n    <span style=\"color: #0000ff;\">foreach<\/span><span style=\"background-color: #ffffff; color: #000000;\">(<\/span><span style=\"color: #8b0000;\">$disk<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$Disks<\/span><span style=\"background-color: #ffffff; color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #8b0000;\">$UsedSpace<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"background-color: #ffffff; color: #000000;\">((<\/span><span style=\"color: #8b0000;\">$disk<\/span><span style=\"background-color: #ffffff; color: #000000;\">.size <\/span><span style=\"color: #0000ff;\">-<\/span> <span style=\"color: #8b0000;\">$disk<\/span><span style=\"background-color: #ffffff; color: #000000;\">.freespace)<\/span><span style=\"color: #0000ff;\">\/<\/span><span style=\"color: #000000;\">1<\/span><span style=\"background-color: #ffffff; color: #000000;\">gb)\r\n        <\/span><span style=\"color: #8b0000;\">$FreeSpace<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"background-color: #ffffff; color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$disk<\/span><span style=\"background-color: #ffffff; color: #000000;\">.freespace<\/span><span style=\"color: #0000ff;\">\/<\/span><span style=\"color: #000000;\">1<\/span><span style=\"background-color: #ffffff; color: #000000;\">gb)\r\n\r\n        <\/span><span style=\"color: #008000;\">#Load a Chart for each Drive<\/span>\r\n        <span style=\"color: #008080;\">Load-Chart<\/span> <span style=\"color: #8b0000;\">$chart1<\/span> <span style=\"color: #0000ff;\">-<\/span><span style=\"background-color: #ffffff; color: #000000;\">XPoints (<\/span><span style=\"color: #ff0000;\">\"Used ({0:N1} GB)\"<\/span> <span style=\"color: #0000ff;\">-f<\/span> <span style=\"color: #8b0000;\">$UsedSpace<\/span><span style=\"background-color: #ffffff; color: #000000;\">),`\r\n        (<\/span><span style=\"color: #ff0000;\">\"Free Space ({0:N1} GB)\"<\/span> <span style=\"color: #0000ff;\">-f<\/span> <span style=\"color: #8b0000;\">$FreeSpace<\/span><span style=\"background-color: #ffffff; color: #000000;\">) <\/span><span style=\"color: #0000ff;\">-<\/span><span style=\"background-color: #ffffff; color: #000000;\">YPoints <\/span><span style=\"color: #8b0000;\">$UsedSpace<\/span><span style=\"background-color: #ffffff; color: #000000;\">, <\/span><span style=\"color: #8b0000;\">$FreeSpace<\/span><span style=\"background-color: #ffffff; color: #000000;\"> `\r\n        <\/span><span style=\"color: #0000ff;\">-<\/span><span style=\"background-color: #ffffff; color: #000000;\">ChartType <\/span><span style=\"color: #ff0000;\">\"Pie\"<\/span><span style=\"background-color: #ffffff; color: #000000;\"> `\r\n        <\/span><span style=\"color: #0000ff;\">-<\/span><span style=\"background-color: #ffffff; color: #000000;\">Title (<\/span><span style=\"color: #ff0000;\">\"Volume: {0} ({1:N1} GB)\"<\/span> <span style=\"color: #0000ff;\">-f<\/span> <span style=\"color: #8b0000;\">$disk<\/span><span style=\"background-color: #ffffff; color: #000000;\">.VolumeName,(<\/span><span style=\"color: #8b0000;\">$disk<\/span><span style=\"background-color: #ffffff; color: #000000;\">.size<\/span><span style=\"color: #0000ff;\">\/<\/span><span style=\"color: #000000;\">1<\/span><span style=\"background-color: #ffffff; color: #000000;\">gb) )`\r\n        <\/span><span style=\"color: #0000ff;\">-<\/span><span style=\"background-color: #ffffff; color: #000000;\">Append\r\n    }\r\n\r\n    <\/span><span style=\"color: #008000;\">#Set Custom Style<\/span>\r\n    <span style=\"color: #0000ff;\">foreach<\/span><span style=\"background-color: #ffffff; color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$Series<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$chart1<\/span><span style=\"background-color: #ffffff; color: #000000;\">.Series)\r\n    {\r\n        <\/span><span style=\"color: #8b0000;\">$Series<\/span><span style=\"background-color: #ffffff; color: #000000;\">.CustomProperties <\/span><span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #ff0000;\">\"PieDrawingStyle=Concave\"<\/span><span style=\"background-color: #ffffff; color: #000000;\">\r\n    }<\/span><\/pre>\n<p>When you specify the -Append parameter, the <span style=\"color: #008080;\">Load-Chart<\/span> function will add another chart area to the control instead of replacing the first chart.<\/p>\n<p>Since these forms are dependent on the Microsoft Chart Controls, any machine running the script (including any packaged executable) will require the controls to be installed beforehand.<\/p>\n<p>To check for the Microsoft Chart Controls I added the following lines to the <span style=\"color: #008080;\">OnApplicationLoad <\/span>function:<\/p>\n<pre><span style=\"color: #0000ff;\">if<\/span><span style=\"background-color: #ffffff; color: #000000;\">([Reflection.Assembly]<\/span><span style=\"color: #0000ff;\">::<\/span><span style=\"background-color: #ffffff; color: #000000;\">LoadWithPartialName(<\/span><span style=\"color: #ff0000;\">\"System.Windows.Forms.DataVisualization\"<\/span><span style=\"background-color: #ffffff; color: #000000;\">) <\/span><span style=\"color: #0000ff;\">-eq<\/span> <span style=\"color: #8b0000;\">$null<\/span><span style=\"background-color: #ffffff; color: #000000;\">)\r\n{\r\n    <\/span><span style=\"color: #008000;\">#Microsoft Chart Controls are not installed<\/span><span style=\"background-color: #ffffff; color: #000000;\">\r\n    [void][reflection.assembly]<\/span><span style=\"color: #0000ff;\">::<\/span><span style=\"background-color: #ffffff; color: #000000;\">Load(<\/span><span style=\"color: #ff0000;\">\"System.Windows.Forms, Version=2.0.0.0,`\r\n    Culture=neutral, PublicKeyToken=b77a5c561934e089\"<\/span><span style=\"background-color: #ffffff; color: #000000;\">)\r\n    [void][System.Windows.Forms.MessageBox]<\/span><span style=\"color: #0000ff;\">::<\/span><span style=\"background-color: #ffffff; color: #000000;\">Show(<\/span><span style=\"color: #ff0000;\">\"Microsoft Chart Controls for Microsoft .NET 3.5`\r\n    Framework is required\"<\/span><span style=\"background-color: #ffffff; color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"Microsoft Chart Controls Required\"<\/span><span style=\"background-color: #ffffff; color: #000000;\">)\r\n    <\/span><span style=\"color: #008000;\">#Open the URL<\/span><span style=\"background-color: #ffffff; color: #000000;\">\r\n    [System.Diagnostics.Process]<\/span><span style=\"color: #0000ff;\">::<\/span><span style=\"background-color: #ffffff; color: #000000;\">Start(<\/span><span style=\"color: #ff0000;\">\"http:\/\/www.microsoft.com\/downloads\/en\/`\r\n    details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&amp;displaylang=en\"<\/span><span style=\"background-color: #ffffff; color: #000000;\">);\r\n    <\/span><span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #8b0000;\">$false<\/span><span style=\"background-color: #ffffff; color: #000000;\">\r\n}<\/span><\/pre>\n<p>To download the Sample Charts <a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PrimalForms 2011\/ChartSamples.zip\" target=\"_blank\">here<\/a>.\u00a0 The samples are located under Script Samples folder.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently a blog post titled Charting with PowerShell was brought to my attention. The blog article demonstrates how to create graphs in PowerShell using WinForms. After reading this article, I thought it would be much easier to have the option to visually design your charts instead of coding it by hand. Therefore in the last [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[575,21,25],"tags":[564,636,28,314,312],"class_list":["post-2857","post","type-post","status-publish","format-standard","hentry","category-primalforms-2011-software-news","category-software-news","category-windows-powershell","tag-564","tag-charts","tag-powershell","tag-primalforms","tag-winforms"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/2857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=2857"}],"version-history":[{"count":22,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/2857\/revisions"}],"predecessor-version":[{"id":12254,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/2857\/revisions\/12254"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=2857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=2857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=2857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}