Added method for printing a graph of Unicode characters.
This commit is contained in:
parent
6a9ef909b0
commit
a77b06a25e
@ -364,6 +364,31 @@ def internet():
|
|||||||
except OSError:
|
except OSError:
|
||||||
return False
|
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):
|
def make_bar(percentage):
|
||||||
"""
|
"""
|
||||||
Draws a bar made of unicode box characters.
|
Draws a bar made of unicode box characters.
|
||||||
|
Loading…
Reference in New Issue
Block a user