Pie charts using PowerShell

Version 1

    In some instances pie charts are better suited than line or bar.  And it just looks cool.     We can use the built in charting of PowerShell to query Orions's database for infomation then powershell charts to draw the pie chart.  Then use the custom html to link to the chart.  To make it dynamic, run the powershell every 5 minutes to keep the chart updated.

     

    As as example, we used vendor information, but it can really be any swql query.   It also doesn't have to be a pie chart, it can be a small chart, a 3D chart, etc.

     

    Add-Type -AssemblyName System.Windows.Forms

    Add-Type -AssemblyName System.Windows.Forms.DataVisualization

    $ErrorActionPreference = 'Stop'

     

    # solarwinds access information

    $hostname = '1.1.1.1'

     

    # load the swis snapin

    if (-not (Get-PSSnapin | ?{ $_.Name -ceq 'SwisSnapin' })) {

        Add-PSSnapin 'SwisSnapin'

    }

     

    # connect to the solarwinds server

    #$swis = Connect-Swis -Hostname $hostname -Trusted

    $swis = Connect-Swis -Hostname $hostname -Username xxxxxxx -Password yyyyyyyy

     

    Get-NetIPAddress | Select IPAddress | %{

        $query = 'select Vendor, count(*) as Value

    from Orion.Nodes

    group by Vendor'

     

        $Datas = Get-SwisData $swis $query

    }

     

    if (-not $Datas) {}

    else {

    #Write-Output ("Start Manage")

    # create chart object

          $Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart

          $Chart.Width = 400

          $Chart.Height = 400

          $Chart.Left = 20

          $Chart.Top = 20

    # create a chartarea to draw on and add to chart

          $ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea

          $Chart.ChartAreas.Add($ChartArea)

    # add data to chart

          #$Cities = @{London=7556900; Berlin=3429900; Madrid=3213271; Rome=2726539; Paris=2188500}

          [void]$Chart.Series.Add("Data")

          $Chart.Series["Data"].Points.DataBindXY($Datas.Vendor, $Datas.Value)

          $chart.Series["Data"].ChartType = "Pie"

          $Chart.Series["Data"]["PieLabelStyle"] = "Outside"

          $Chart.Series["Data"]["PieLineColor"] = "Black"

          #$Chart.Series["Data"]["PieDrawingStyle"] = "Concave"

          ($Chart.Series["Data"].Points.FindMaxByValue())["Exploded"] = $true

          $Chart.Series["Data"][‘PieLabelStyle’] = ‘Disabled’

          # Legend

          $Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend

          $Legend.IsEquallySpacedItems = $True

          $Legend.BorderColor = 'Black'

          $Chart.Legends.Add($Legend)

          $chart.Series["Data"].LegendText = "#VALX (#VALY)"

          # save chart to file

          $Chart.SaveImage("D:\InetPub\SolarWinds\Orion\Images\Vendor.png", "PNG")

        }

     

     

     

    References:

    https://bytecookie.wordpress.com/2012/04/13/tutorial-powershell-and-microsoft-chart-controls-or-how-to-spice-up-your-reports/

    http://www.ciprianpadurariu.ro/tag/powershell/

    https://blogs.technet.microsoft.com/richard_macdonald/2009/04/28/charting-with-powershell/

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart(v=vs.110).aspx

     

    Thanks

    Amit