#include #include #include #cs ;~ Pink Floyd - Dec 2016 Create Graph with powershell help Need .NET framework 3.5 mini, and powershell Use : _create_graph($conf,$data1,$data2,$data3) $data2 and data3 optional If your write your own ini file, ( please see provided example ), you can use : _create_ps1($ini_file) Each function return path of generated graph Function _create_graph requiert at least 2 array parameter : First one contain general configuration Second one contain data to display Third ( optionnal ) contain data to display Fourth ( optionnal ) contain data to display Format for conf array : $conf[10] = [Width,Height,BackgroundColor,Graph Title,X data Title,Y Data Title, Axis X Interval , Axis Y Interval, file path to export graph] Example : Local $conf[10] = [600,600,"white","Example 1","Xtitle","YTitle",1,10,@ScriptDir & "\example1.png"] Format for Data array : Please note : 2 lines, xx column : if two or more graph in same image, array need identical column Size is [2][number of value + 2] $data1[2][6] = [[Serie Name,,key 1,key 2,key 3,key4],[hex graph color,graph type,value 1,value 2, value3, value 4]] Example : Local $data1[2][6] = [["TEST","","A","B","C","D"],["#00FF00","Column",25,40,25,10]] Possible graph type : column, spline , bar , baxplot, bubble,Candlestick, Doughnut , FastLine, FastPoint , More info here : https://msdn.microsoft.com/en-us/library/dd489233.aspx #ce ;Example 1 // 2 graph Local $conf[10] = [600,600,"white","Example 1","Xtitle","YTitle",1,10,@ScriptDir & "\example1.png"] Local $data1[2][6] = [["TEST" ,"","A","B","C","D"],["#00FF00","Column",25,40,25,10]] Local $data2[2][6] = [["OTHER","","A","B","C","D"],["#FF0000","Column",10,50,25,40]] $img = _create_graph($conf,$data1,$data2) ;Display the image ShellExecuteWait($img) ; Example 2 // one graph Local $conf[10] = [800,400,"green","Example 2","My X Values","My Y Values",0,1,@ScriptDir & "\example2.png"] Local $data1[2][10] = [["TEST","",@OSArch,"B","2","*","Extended","//","G","@"],["#00FF00","spline",5,12,25,10,33,2,10,5]] $img = _create_graph($conf,$data1) ;Display the image ShellExecuteWait($img) ; Example 3 / wtih date ; Get last 4 dates Local $day[5] For $boucle = 1 To 4 $day[$boucle] = _DateAdd('d', $boucle * -1 , _NowCalcDate()) Next Local $conf[10] = [500,500,"white","Example 3","Date","Values",0,1,@ScriptDir & "\example3.png"] Local $data1[2][6] = [["TEST","",$day[4],$day[3],$day[2],$day[1]],["#FF0000","SplineArea",5,12,25,10]] $img = _create_graph($conf,$data1) ;Display the image ShellExecuteWait($img) ; Example 4 // Using an existing ini file // possible use for command line, or directly modified existant ini file with autoit $ini_file = @ScriptDir & "\Example_conf_chart.ini" $img = _create_ps1($ini_file,"no delete") ;Display the image ShellExecute($img) Exit(0) ; ================================================================ Functions ===================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _create_ini ; Description ...: ; Syntax ........: _create_ini($conf[, $data1 = ""[, $data2 = ""[, $data3 = ""]]]) ; Parameters ....: $conf - an 10 size array , $conf[10] = [Width,Height,BackgroundColor,Graph Title,X data Title,Y Data Title, Axis X Interval , Axis Y Interval, file path to export graph] ; $data1 - [optional] data to graph $data1[2][6] = [[Serie Name,,key 1,key 2,key 3,key4],[hex graph color,graph type,value 1,value 2, value3, value 4]] ; $data2 - [optional] data to graph $data2[2][6] = [[Serie Name,,key 1,key 2,key 3,key4],[hex graph color,graph type,value 1,value 2, value3, value 4]] ; $data3 - [optional] data to graph $data3[2][6] = [[Serie Name,,key 1,key 2,key 3,key4],[hex graph color,graph type,value 1,value 2, value3, value 4]] ; Return values .: ini file path created ; =============================================================================================================================== Func _create_graph($conf,$data1="",$data2="",$data3="") ; Create temp file For ps1, delete it if previously exist $ini_file = _TempFile("","",".ini", Default) If FileExists($ini_file) Then FileDelete($ini_file) #cs _ArrayDisplay($conf) Row|Col 0 [0]|600 [1]|600 [2]|white [3]|Title [4]|Xtitle [5]|YTitle [6]|1 [7]|1 [8]|H:\Temp\_A_Tester\Chart\myimage.png #ce If FileExists($ini_file) Then FileDelete($ini_file) $img_file = $conf[8] IniWrite($ini_file,"GENERAL","Width" ,$conf[0]) IniWrite($ini_file,"GENERAL","Height" ,$conf[1]) IniWrite($ini_file,"GENERAL","BackColor" ,$conf[2]) IniWrite($ini_file,"GENERAL","Title" ,$conf[3]) IniWrite($ini_file,"GENERAL","AxisX.Title" ,$conf[4]) IniWrite($ini_file,"GENERAL","AxisY.Title" ,$conf[5]) IniWrite($ini_file,"GENERAL","AxisX.Interval" ,$conf[6]) IniWrite($ini_file,"GENERAL","AxisY.Interval" ,$conf[7]) IniWrite($ini_file,"GENERAL","ImageName" ,$conf[8]) If IsArray($data1) Then #cs _ArrayDisplay($data1) Row|Col 0 | Col 1 |Col 2 |Col 3 |Col 4 |Col 5 [0]|CPU | |A | B | C |D [1]|FF0000 |Column |5 |10 |15 |20 #ce $column = UBound($data1,2) - 1 $name = $data1[0][0] $color = $data1[1][0] $type = $data1[1][1] IniWrite($ini_file,$name,"CONF_TYPE" ,$type) IniWrite($ini_file,$name,"CONF_COLOR",$color) For $boucle = 2 To $Column IniWrite($ini_file,$name,$data1[0][$boucle],$data1[1][$boucle]) Next EndIf If IsArray($data2) Then $column = UBound($data2,2) - 1 $name = $data2[0][0] $color = $data2[1][0] $type = $data2[1][1] IniWrite($ini_file,$name,"CONF_TYPE" ,$type) IniWrite($ini_file,$name,"CONF_COLOR",$color) For $boucle = 2 To $Column IniWrite($ini_file,$name,$data2[0][$boucle],$data2[1][$boucle]) Next EndIf If IsArray($data3) Then $column = UBound($data3,2) - 1 $name = $data3[0][0] $color = $data3[1][0] $type = $data3[1][1] IniWrite($ini_file,$name,"CONF_TYPE" ,$type) IniWrite($ini_file,$name,"CONF_COLOR",$color) For $boucle = 2 To $Column IniWrite($ini_file,$name,$data3[0][$boucle],$data3[1][$boucle]) Next EndIf $img = _create_ps1($ini_file) Return($img) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _create_ps1 ; Description ...: ( used by _create_graph function ) , only useable if you make manually your own ini file ; Syntax ........: _create_ps1($ini_file) ; Parameters ....: $ini_file - an ini file path ; Return values .: img path generated ; =============================================================================================================================== Func _create_ps1($ini_file,$delete = "") Local $powershell = "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" If Not FileExists($powershell) Then ConsoleWriteError("Powerwshell not found ! : " & $powershell & @CRLF ) Exit EndIf If Not FileExists($ini_file) Then ConsoleWriteError("Ini File not found ! : " & $ini_file & @CRLF ) Exit EndIf ; Create temp file For ps1, delete it if previously exist $ps1 = _TempFile("","",".ps1", Default) If FileExists($ps1) Then FileDelete($ps1) ; Starting powershell script FileWriteLine($ps1,"[void][Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms.DataVisualization')") FileWriteLine($ps1,"$scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition") ; chart object FileWriteLine($ps1,"$chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart") FileWriteLine($ps1,"$chart.Width = " & IniRead($ini_file,"GENERAL","Width" , 600) ) FileWriteLine($ps1,"$chart.Height = " & IniRead($ini_file,"GENERAL","Height" , 600) ) FileWriteLine($ps1,"$chart.BackColor = [System.Drawing.Color]::" & IniRead($ini_file,"GENERAL","BackColor" , "White") ) ; title FileWriteLine($ps1,"[void]$chart.Titles.Add('" & IniRead($ini_file,"GENERAL","Title","") & "')" ) FileWriteLine($ps1,"$chart.Titles[0].Font = 'Arial,13pt'" ) FileWriteLine($ps1,"$chart.Titles[0].Alignment = 'topLeft'" ) ; chart area FileWriteLine($ps1,"$chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea") FileWriteLine($ps1,"$chartarea.Name = 'ChartArea1'" ) FileWriteLine($ps1,"$chartarea.AxisY.Title = '" & IniRead($ini_file,"GENERAL","AxisY.Title","") & "'" ) FileWriteLine($ps1,"$chartarea.AxisX.Title = '" & IniRead($ini_file,"GENERAL","AxisX.Title","") & "'" ) FileWriteLine($ps1,"$chartarea.AxisY.Interval = " & IniRead($ini_file,"GENERAL","AxisY.Interval", 0 ) ) FileWriteLine($ps1,"$chartarea.AxisX.Interval = " & IniRead($ini_file,"GENERAL","AxisX.Interval", 1 ) ) FileWriteLine($ps1,"$chart.ChartAreas.Add($chartarea)" ) ; legend FileWriteLine($ps1,"$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend") FileWriteLine($ps1,"$legend.name = 'Legend'") FileWriteLine($ps1,"$chart.Legends.Add($legend)") ; data series ; retrieve name/number of series $temp = IniReadSectionNames($ini_file) #cs _ArrayDisplay($temp) Row|Col 0 [0]|3 [1]|GENERAL [2]|DATA01 [3]|DATA02 #ce ;check if data found in ini file If UBound($temp) <=2 Then ConsoleWriteError("Error ! No data series find in : " & $ini_file & @CRLF ) Exit EndIf ; Check If each serie have equal value $old_value = 0 For $boucle = 2 To $temp[0] $count = IniReadSection($ini_file,$temp[$boucle]) If $old_value = 0 Then $old_value = UBound($count) If UBound($count) <> $old_value Then ConsoleWriteError("Error ! Number of value in serie : " & UBound($count) & "/" & $old_value & @CRLF ) Exit EndIf Next ; for each series For $boucle = 2 To $temp[0] $name = $temp[$boucle] $temp_section = IniReadSection($ini_file,$name) #cs _ArrayDisplay($temp_section) Row|Col 0|Col 1 [1]|CONF_TYPE|Column [2]|CONF_COLOR|#62B5CC [3]|a|20 [4]|z|30 [5]|b|60 [6]|y|289 [7]|5|50 #ce FileWriteLine($ps1,"[void]$chart.Series.Add('" & $name & "')" ) FileWriteLine($ps1,"$chart.Series['" & $name & "'].ChartType = '" & $temp_section[1][1] & "'" ) FileWriteLine($ps1,"$chart.Series['" & $name & "'].BorderWidth = 3" ) FileWriteLine($ps1,"$chart.Series['" & $name & "'].IsVisibleInLegend = $true " ) FileWriteLine($ps1,"$chart.Series['" & $name & "'].chartarea = 'ChartArea1' " ) FileWriteLine($ps1,"$chart.Series['" & $name & "'].Legend = 'Legend' " ) FileWriteLine($ps1,"$chart.Series['" & $name & "'].Color = '" & $temp_section[2][1] & "'" ) For $boucle_data = 3 To UBound($temp_section) - 1 FileWriteLine($ps1,"$chart.Series['" & $name & "'].Points.addxy( '" & $temp_section[$boucle_data][0] & "','" & $temp_section[$boucle_data][1] & "')" ) Next Next ; save chart $image_path = IniRead($ini_file,"GENERAL","ImageName",@ScriptDir & "\chart.png") FileWriteLine($ps1,"$chart.SaveImage('"& $image_path & "','png')") ; Execute powershell file ShellExecuteWait($powershell,$ps1,"","",@SW_HIDE ) ; Delete ps1 and ini temp file FileDelete($ps1) If $delete = "" then FileDelete($ini_file) ; return path of generated chart for easiest use Return($image_path) EndFunc