diff --git a/src/Show-Graph.ps1 b/src/Show-Graph.ps1 index 5948584..382660f 100644 --- a/src/Show-Graph.ps1 +++ b/src/Show-Graph.ps1 @@ -32,6 +32,9 @@ Hash table that defines the range of color codes .PARAMETER HorizontalLines Add horizontal lines to the graph area +.PARAMETER OutFile +Prints the graph to the specified file + .EXAMPLE $data = 1..100 | Get-Random -Count 50 Show-Graph -Datapoints $Data -GraphTitle 'CPU' @@ -48,6 +51,10 @@ Show-Graph -Datapoints $Data -Type Scatter $data = 1..100 | Get-Random -Count 50 Show-Graph -Datapoints $Data -YAxisTitle "Percentage" -XAxistitle "Time" +.EXAMPLE +$data = 1..100 | Get-Random -Count 50 +Show-Graph -Datapoints $Data -YAxisTitle "Percentage" -XAxistitle "Time" -OutFile C:\temp\graph.txt + .NOTES Blog: https://RidiCurious.com/ Github: https://github.com/PrateekKumarSingh/Graphical @@ -82,7 +89,8 @@ Function Show-Graph { [Int] $YAxisStep = 10, [ValidateSet("Bar","Scatter","Line")] [String] $Type = 'Bar', [Hashtable] $ColorMap, - [Switch] $HorizontalLines + [Switch] $HorizontalLines, + [String] $OutFile ) # graph boundary marks @@ -141,9 +149,13 @@ Function Show-Graph { $BottomBoundaryLength = $XAxis.Length + 2 # draw top boundary - [string]::Concat($TopLeft," ",$GraphTitle," ",$([string]$TopEdge * $TopBoundaryLength),$TopRight) - [String]::Concat($VerticalEdge,$(" "*$($XAxis.length+2)),$VerticalEdge) # extra line to add space between top-boundary and the graph - + ($Title = [string]::Concat($TopLeft," ",$GraphTitle," ",$([string]$TopEdge * $TopBoundaryLength),$TopRight)) + ($EmptyLine = [String]::Concat($VerticalEdge,$(" "*$($XAxis.length+2)),$VerticalEdge)) # extra line to add space between top-boundary and the graph + if($OutFile){ + Out-File -InputObject $Title -FilePath $OutFile -Encoding unicode -Append + Out-File -InputObject $EmptyLine -FilePath $OutFile -Encoding unicode -Append + } + # draw the graph For($i=$NumOfRows;$i -gt 0;$i--){ $Row = '' @@ -199,7 +211,7 @@ Function Show-Graph { if ([String]::IsNullOrEmpty($Color)) {$Color = "White"} - Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row $Color 'DarkYellow' + Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row $Color 'DarkYellow' $OutFile } else{ @@ -213,13 +225,13 @@ Function Show-Graph { $RangePercent = $i/$NumOfRows * 100 # To color the graph depending upon the datapoint value If ($RangePercent -gt 80) { - Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row 'Red' 'DarkYellow' + Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row 'Red' 'DarkYellow' $OutFile } elseif($RangePercent-le 80 -and $RangePercent -gt 40) { - Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row 'Yellow' 'DarkYellow' + Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row 'Yellow' 'DarkYellow' $OutFile } elseif($RangePercent -le 40 -and $RangePercent -ge 1) { - Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row 'Green' 'DarkYellow' + Write-Graph $YAxisLabelAlphabet $YAxisLabel $Row 'Green' 'DarkYellow' $OutFile } else { #Write-Host "$YAxisLabel|" @@ -231,8 +243,12 @@ Function Show-Graph { # draw bottom boundary $XAxisLabel +=" "*($XAxis.Length-$XAxisLabel.Length) # to match x-axis label length with x-axis length - [String]::Concat($VerticalEdge,$XAxis," ",$VerticalEdge) # Prints X-Axis horizontal line - [string]::Concat($VerticalEdge,$XAxisLabel," ",$VerticalEdge) # Prints X-Axis step labels + ($BottomLine =[String]::Concat($VerticalEdge,$XAxis," ",$VerticalEdge)) # Prints X-Axis horizontal line + ($XStepLabel = [string]::Concat($VerticalEdge,$XAxisLabel," ",$VerticalEdge)) # Prints X-Axis step labels + if($OutFile){ + Out-File -InputObject $BottomLine -FilePath $OutFile -Encoding unicode -Append + Out-File -InputObject $XStepLabel -FilePath $OutFile -Encoding unicode -Append + } if(![String]::IsNullOrWhiteSpace($XAxisTitle)){ @@ -240,11 +256,20 @@ Function Show-Graph { $XAxisTitle = " "*$LengthOfMaxYAxisLabel + (CenterAlignString $XAxisTitle $XAxis.Length) Write-Host -Object $VerticalEdge -NoNewline Write-Host -Object $XAxisTitle -ForegroundColor DarkYellow -NoNewline # Prints XAxisTitle - Write-Host -Object $(" "*$(($LengthOfMaxYAxisLabel + $XAxis.length) - $XAxisTitle.Length - 2)) $VerticalEdge + Write-Host -Object "$(" "*$(($LengthOfMaxYAxisLabel + $XAxis.length) - $XAxisTitle.Length - 1)) $VerticalEdge" + + if($OutFile){ + Out-File -InputObject $VerticalEdge -NoNewline -Append -FilePath $OutFile -Encoding unicode + Out-File -InputObject $XAxisTitle -NoNewline -Append -FilePath $OutFile -Encoding unicode # Prints XAxisTitle + Out-File -InputObject "$(" "*$(($LengthOfMaxYAxisLabel + $XAxis.length) - $XAxisTitle.Length - 1)) $VerticalEdge" -Append -FilePath $OutFile -Encoding unicode + } } # bottom boundary - [string]::Concat($BottomLeft,$([string]$BottomEdge * $BottomBoundaryLength),$BottomRight) + ($BottomBoundary =[string]::Concat($BottomLeft,$([string]$BottomEdge * $BottomBoundaryLength),$BottomRight)) + if($OutFile){ + Out-File -InputObject $BottomBoundary -FilePath $OutFile -Encoding unicode -Append + } } diff --git a/src/UtilityFunctions.ps1 b/src/UtilityFunctions.ps1 index 31f4b50..c412006 100644 --- a/src/UtilityFunctions.ps1 +++ b/src/UtilityFunctions.ps1 @@ -8,12 +8,20 @@ Function CenterAlignStringReturnIndices ($String, $Length) { return $StartIdx, $EndIdx } -Function Write-Graph($YAxisLabelAlphabet, $YAxisLabel, $Row, $RowColor, $LabelColor) +Function Write-Graph($YAxisLabelAlphabet, $YAxisLabel, $Row, $RowColor, $LabelColor, $OutFile) { Write-Host -Object $([char]9474) -NoNewline Write-Host -Object $YAxisLabelAlphabet -ForegroundColor $LabelColor -NoNewline Write-Host -Object "$($YAxisLabel.tostring().PadLeft($LengthOfMaxYAxisLabel+2) + [Char]9508)" -NoNewline ##Write-Host "$YAxisLabel|" -NoNewline Write-Host -Object $Row -ForegroundColor $RowColor -NoNewline - Write-Host -Object " " $([char]9474) + Write-Host -Object " $([char]9474)" + + if($OutFile){ + Out-File -InputObject "$([char]9474)" -NoNewline -FilePath $OutFile -Append -Encoding unicode + Out-File -InputObject $YAxisLabelAlphabet -NoNewline -FilePath $OutFile -Append -Encoding unicode + Out-File -InputObject "$($YAxisLabel.tostring().PadLeft($LengthOfMaxYAxisLabel+2) + [Char]9508)" -NoNewline -FilePath $OutFile -Append -Encoding unicode + Out-File -InputObject $Row -NoNewline -FilePath $OutFile -Append -Encoding unicode + Out-File -InputObject " $([char]9474)" -FilePath $OutFile -Append -Encoding unicode + } }