Created
December 4, 2021 01:33
-
-
Save kstrauss/93bc8b0e0d5f2ce1ef088d465f92cc6d to your computer and use it in GitHub Desktop.
powershell bar chart - thought/idea of doing an ascii bar chart from a group by data (count)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GraphMe { | |
[CmdletBinding] | |
param( | |
[Parameter()] | |
[Hashtable] $something | |
) | |
<# | |
max_value = max(count for _, count in data) | |
increment = max_value / 25 | |
longest_label_length = max(len(label) for label, _ in data) | |
for label, count in data: | |
# The ASCII block elements come in chunks of 8, so we work out how | |
# many fractions of 8 we need. | |
# https://en.wikipedia.org/wiki/Block_Elements | |
bar_chunks, remainder = divmod(int(count * 8 / increment), 8) | |
# First draw the full width chunks | |
bar = '█' * bar_chunks | |
# Then add the fractional part. The Unicode code points for | |
# block elements are (8/8), (7/8), (6/8), ... , so we need to | |
# work backwards. | |
if remainder > 0: | |
bar += chr(ord('█') + (8 - remainder)) | |
# If the bar is empty, add a left one-eighth block | |
bar = bar or '▏' | |
print(f'{label.rjust(longest_label_length)} ▏ {count:#4d} {bar}') | |
#> | |
} | |
$dataToGraph = dir -recurse | group -Property {$_.LastWriteTime.Date} -NoElement |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment