Charts and PowerShell !! PowerShell Wrapper “Light Edition”

Check Out the Light Edition of this PowerShell Wrapper : Get-CorpCharts-FullEdition

I think one of the most interesting things when writing PowerShell scripts, is to make the results appear in a nice chart or couple of them. If you are going to show disk space info, nothing more than a nice chart will worth looking at. If you are sending a report to your management, nothing will take their attention more than charts. I love getting charts as  a high level output, with more tables and data for extra details.

Why this is different than others?

Because this script is a wrapper around the dirty stuff that you do not want to worry about, or may be you are new to PowerShell and  just what the chart to get generated.

You can just give it some data, and charts will start decorating your screen , ready to be emailed maybe to someone else to look at the data in a nice way.

The script

The script is FULL with examples and information of how to use it. Do not panic if you find the code long or complex for you. Just look at the examples and you will find your way.

Do not forget that you need to install two things before running the script on your machine :

So how the script works?

You have to enter a parameter called (-data) that is essentially an array of objects.

Suppose you have an object called (City) with two properties : City_Name, and City_Population

and let us say you have ten of those objects and they are stored in an array called $Cities

So now, all what you have to do is to give the $Cities array to the function as -Data parameter, and supply the -Obj_Key parameter as “City_Name”, and -Obj_Value as “City_Population”

So the command will be : Get-CorpCharts-LightEdition -data $Cities  -Obj_Key  City_Name  -Obj_Value City_Population -FilePath C:\graph.png

Charts_PowerShell

Download version 2

You can download version 2 of the script from here: Get-CorpCharts

New in Version 2

  • Sortable data option via -sort parameter.
  • Fix label alignment via -fix_label_alignment parameter.
  • Exposing X and Y axis intervals via parameters.
  • Show data as percentage via -show_percentage_pie parameter.
  • Collected threshold to group data items below the threshold into one item called ‘Others’.
  • Customizing chart data column colors via -chart_color parameter.
  • Dynamic dimension depending on the number of items in the input data.

Charts with PowerShell 1

Charts with PowerShell 2

Charts with PowerShell 3

Charts with PowerShell 4Charts with PowerShell 5

Charts with PowerShell 6

 Examples

.EXAMPLE   

Chart by supplying array of objects as input. We are interested in the Name and Population properties of the input objects. In this case, we should also use the -obj_key and -obj_value parameters to tell the function which properties to draw. Default chart type ‘column’ is used.

PS C:\> Get-Corpchart-LightEdition -data $array_of_city_objects  -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png”

.EXAMPLE 

Specifying chart type as pie chart type.

PS C:\> Get-Corpchart-LightEdition -data  $cities  -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png” -type Pie

.EXAMPLE

Specifying chart type as pie chart type. legend is shown.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png” -type Pie -showlegend

.EXAMPLE 

Specifying chart type as SplineArea chart type.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png” -type SplineArea

.EXAMPLE   

Specifying chart type as bar chart type, and specifying the title for the chart and x/y axis.

PS C:\> Get-Corpchart-LightEdition -data $cities  -obj_key  “Name” -obj_value  “Population”

-filepath “c:\chart.png”  -type Bar  -title_text  “people per country”  -chartarea_Xtitle  “cities”

 -chartarea_Ytitle  “population”

.EXAMPLE  

Specifying chart type as column chart type. Applying the -showHighLow switch to highlight the max and min values with different colors.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png” -type Column -showHighLow

 .EXAMPLE 

Chart with percentages shown on the pie/doughnut charts.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png” -type Doughnut -Show_percentage_pie

.EXAMPLE  

If the chart type is pie or doughnut, you can specify a threshold (percentage) that all data values below it, will be shown as one data item called (Others).

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key “Name” -obj_value “Population”

 -filepath “c:\chart.png” -type Doughnut  -CollectedThreshold 16

.EXAMPLE 

Column chart with green columns.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key “Name” -obj_value “Population”

-filepath “c:\chart.png” -chart_color Green

16 comments on “Charts and PowerShell !! PowerShell Wrapper “Light Edition”

  1. Hello Ammar,

    thanks a lot for this function 🙂 .

    however i have been trying to use your script, but it seems something is wrong

    here is an example:

    Missing expression after ‘,’.
    At C:\Users\whkc4736\Desktop\Get-CorpChart_v2.ps1:240 char:33
    + [Parameter(Position = 2, <<<< `
    + CategoryInfo : ParserError: (,:String) [], ParseException
    + FullyQualifiedErrorId : MissingExpressionAfterToken

    could you please advice?

    thanks alot

  2. Hello Ammar,

    thanks for your reply

    i was trying to load the function [copy/past into the power-shell console] that’s where the error came up

    am i doing something wrong?

    and yes MScharts control is installed

    thanks for your help

  3. Hi Ammar,

    Looks like I’ve got it working fine! I’ve got some nice graphs of my greenhouse, telling me about temperature, light (IR & LUX), Humidity, air pressure (millibar), battery levels, solar input (voltage). All thanks to your graphs script!

    I noticed that I needed to make a few small modifications to run it on Windows 7 (ran fine on win8.1).

    It compains about line 615 (color) and I had to comment out line 207. Also some errors reported where you used ` to go to a new line, which seemed fine to me, but ISE complained about some spaces being used there, not sure why. As I said: it’s minor.

    Anyhow, that’s not what I’m writing, just thought I’d mention it, as you may want to fine tune you source.

    My question: I’d like to ask if the following to options are possible somehow:

    1. I’d like to put emphasis on a certain area in the graph; for example, I’m using millibar in a line graph, which is a value typically somewhere between 900 and 1100. Righ now the Y value of the graph goes from 0 – 1100.. I’d like it to have a MIN (900) and MAX (1100). Is that possible?

    2. I’m setting time (X axis) against a certain value (humidity, temperature, dewpoint)
    As dewpoint and temperature are both in Celcius on the Y axis, would it be possible to show both lines somehow? And to take that a bit further (but I guess that’s is quite a bit harder) could I somehow add humidity (a percentage) also? (for example with it’s scale from 0 to 10% o the right)… I know I know that last one is a tough one 😉

    ————————–
    note related to the above, but since I’m rambing now anyway…
    By the way, I found that hashvalues & date/time do’n sort well. For thise who may find it useful; this how I ended up fixing that:

    First simply fill a $hashtable with data staring with a time field. For example:

    DateTime;BatteryVolume;Temp;Humidity%;milliBar;LightFull;LightIR;LightLux
    18-04-2014 18:01;099;21.00;72.10;1017;2959;1338;856

    Now use the following:

    $template = 'dd-MM-yyyy HH:mm'
    $mytable = $HashTable.GetEnumerator() | Sort-Object {[DateTime]::ParseExact($_.Name, $template, $null)}
    

    Now use $mytable in Get-Corpchart, Corpchart may complain it’s not a hashtable, simply use -obj_key ‘Name’ -obj_value ‘Value’ to make it work.

    • Ya i can understand your comment. I spent long time trying to draw in powershell and finally i got it working for me with lots of tweaks. What i tried to do here is to build a wrapper as perfect as possible.
      When you want to input data, the most logical way to do it is by using hash tables as they are key pairs. The problem is that only PowerSehll 3.0 i guess where they introduced sorted hashtable. Not everyone has this version, that is why i came out with an object input with two properties of your own selections. you can sort an array of objects and input them to the function to get a sorted data.

      After all, to get professional graphs, some sort of skills is required to do your own tweaking. My script aims to give you the starting point and all my experience so you can reverse engineer it and tweak it the way you like.
      I use graphs as much as i can in my scripts as people especially management people would like to see nice graphs instead of long raw data.
      I am preparing my next version of Exchange Dashbord with this new function as now graphs will expand automatically depending on the number of input pairs. This is HUGE step as i got so many feedback from people asking about so crowded graphs on my previous Exchange report.
      I am glad you took the time to go through the code and figure your own way

  4. Regarding your request about emphasis, im only using .NET to do the graphing. The options that is available is to emphasis high and low only.

    If you go through the .NET library, or ask a true developer who deals with the charts library, you will certainly get his input as it is large area.

    And if this is an option, well, you can easily integrate it with my code and it will reflect your new chart needs. You can do magic with that library, just browse the documentation or ask a dev person and he may help. For my case, i did not need that.

    Other option that i will go for here is to leave bring the data using PowerShell or any other means and send them to native .NET code where a developer can take that array of object data and use it to do the graphs in native .NET using any framework he liked. I did one time such integration where i pulled the data using PowerShell to a SQL DB, and then a .NET code inspect the data from SQL and view it in ASP page for nice GUI web interface.

    Again, with PowerShell , you will not get the full features since in the background it uses .NET to do the GUI part.

  5. Pingback: New Light PowerShell Wrapper for drawing Charts | Ammar Hasayen - Blog

  6. Pingback: Charts and PowerShell !! PowerShell Wrapper “Full Edition” | Ammar Hasayen - Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s