Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to save graph to file #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/Show-Graph.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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{
Expand All @@ -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|"
Expand All @@ -231,20 +243,33 @@ 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)){
# Position the x-axis label at the center of the axis
$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
}

}

Expand Down
12 changes: 10 additions & 2 deletions src/UtilityFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}