Merge pull request #123 from facetoe/master

Added method for printing a graph of Unicode characters.
This commit is contained in:
enkore 2014-10-06 15:51:42 +02:00
commit a6fb92936d

View File

@ -364,6 +364,31 @@ def internet():
except OSError:
return False
def make_graph(values, upper_limit=100.0):
"""
Draws a graph made of unicode characters.
:param values: An array of values to graph.
:param upper_limit: Maximum value for the y axis.
:returns: Bar as a string
"""
values = [float(n) for n in values]
# Add the upper limit to the end of the array so the graph doesn't distort
# as high values drop off the end.
values.append(float(upper_limit))
bar = u'_▁▂▃▄▅▆▇█'
bar_count = len(bar) - 1
mn, mx = min(values), max(values)
extent = mx - mn
if extent == 0:
graph = '_' * len(values)
else:
graph = ''.join(bar[int( (n - mn) / extent * bar_count)]
for n in values[:len(values)-1]) # Don't show the upper limit value.
return graph
def make_bar(percentage):
"""
Draws a bar made of unicode box characters.