WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

PowerShell
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Create a Chart from Data Using Google Chart API

 Keywords:  Google Chart API Get-GoogleChart System.Management.Automation  System.Management.Automation.PowerShell AddScript BeginInvoke EndInvoke Invoke


;/////////////////////////////////////////////////////////////////////////////////////////////////////////
;Winbatch 2013A - Example of creating a chart from data using Google Chart API
;                
;
;Stan Littlefield, May 7, 2013
;/////////////////////////////////////////////////////////////////////////////////////////////////////////

If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf


cScript = Dirscript():"googlechart.ps1"
If ! FileExist(cScript) Then Terminate(@TRUE,"Cannot Continue",cScript:" not found")
BoxOpen("Please Wait","Processing... ":cScript)
cRun=FileGet(cScript)
ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") 
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cRun,oScope)
objAsync = oPshell.BeginInvoke() 
oPShell.EndInvoke(objAsync)      

WinWaitExist("~chart", 60)
If WinExist("~chart")
   WinActivate("~chart")
   While  WinExist("~chart")
      TimeDelay(1)
   Endwhile
Endif

oPshell.Dispose()
oPshell=0

Exit


googlechart.ps1

## =====================================================================
## Title       : Get-GoogleChart
## Description : Example of creating a chart from data using Google Chart API
## Author      : Idera
## Date        : 10/15/2009
## Input       : 
## Output      : Chart
## Usage	   : PS> .\Get-GoogleChart
## Tag         : Google Chart
## Attributed  : Jabob Bindslet
##               http://mspowershell.blogspot.com/2008/01/google-chart-api-powershell.html
## Change log  : 
## ===================================================================== 

function DownloadAndShowImage ($url) {
    #$filename = c:\powershell\chart.png"
    $filename = "chart.png"
    $webClient = new-object System.Net.WebClient
    $webClient.Headers.Add("user-agent", "Idera PowerShellPlus")
    $Webclient.DownloadFile($url, $filename)
    Invoke-Item $filename
}

function simpleEncoding ($valueArray, $labelArray, $size, [switch] $chart3D) {
    $simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    if ($chart3D) {$chartType = "p3"} else {$chartType ="p"}
    $total = 0
    foreach ($value in $valueArray) {
        $total = $total + $value
    }
    for ($i = 0;$i -lt $valueArray.length;$i++) {
        $relativeValue = ($valueArray[$i] / $total)*62
        $relativeValue = [math]::round($relativeValue)
        $encodingValue = $simpleEncoding[$relativeValue]
        $chartData = $chartData + "" + $encodingValue
    }    
    $chartLabel = [string]::join("|",$labelArray)
    Write-Output "http://chart.apis.google.com/chart?chtt=Running Process CPU Usage&cht=$chartType&chd=s:$chartdata&chs=$size&chl=$chartLabel"  
    #Write-Output "http://chart.apis.google.com/chart?chtt=CPU_Usage&cht=$chartType&chd=s$chartdata&chs=$size&chl=$chartLabel"
}

function GetProcessArray() {
	$ListOfProcs = Get-Process | Sort-Object CPU -desc | Select-Object CPU, ProcessName -First 10
	$ListOfProcs | ForEach-Object {
		$ProcName = $ProcName + "," + $_.ProcessName
		$ProcUsage = $ProcUsage + "," + $_.CPU
	}
	Write-Output (($ProcName.trimStart(",")).split(","), ($ProcUsage.trimStart(",")).split(","))
}

$data = GetProcessArray
$url = simpleEncoding $data[1] $data[0] "700x350" -chart3D
DownloadAndShowImage $url



Article ID:   W17822
Filename:   Create a Chart from Data Using Google Chart API.txt
File Created: 2013:05:08:09:10:58
Last Updated: 2013:05:08:09:10:58