-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciichart.py
64 lines (49 loc) · 2.09 KB
/
asciichart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
# This file is a copied implementation from my asciichart repository on GitHub
# https://github.com/kroitor/asciichart
from math import cos
# from math import sin
from math import pi
from math import floor
from math import ceil
def plot(series, cfg={}):
minimum = min(series)
maximum = max(series)
interval = abs(float(maximum) - float(minimum))
offset = cfg['offset'] if 'offset' in cfg else 3
# padding = cfg['padding'] if 'padding' in cfg else ' '
height = cfg['height'] if 'height' in cfg else interval
ratio = height / interval
# print(minimum,ratio,type(minimum))
min2 = floor(float(minimum) * ratio)
max2 = ceil(float(maximum) * ratio)
intmin2 = int(min2)
intmax2 = int(max2)
rows = abs(intmax2 - intmin2)
width = len(series) + offset
# format = cfg['format'] if 'format' in cfg else lambda x: (padding + '{:.2f}'.format(x))[:-len(padding)]
result = [[' '] * width for i in range(rows + 1)]
# axis and labels
for y in range(intmin2, intmax2 + 1):
label = '{:8.2f}'.format(float(maximum) - ((y - intmin2) * interval / rows))
result[y - intmin2][max(offset - len(label), 0)] = label
result[y - intmin2][offset - 1] = '┼' if y == 0 else '┤'
y0 = int(series[0] * ratio - min2)
result[rows - y0][offset - 1] = '┼' # first value
for x in range(0, len(series) - 1): # plot the line
y0 = int(round(series[x + 0] * ratio) - intmin2)
y1 = int(round(series[x + 1] * ratio) - intmin2)
if y0 == y1:
result[rows - y0][x + offset] = '─'
else:
result[rows - y1][x + offset] = '╰' if y0 > y1 else '╭'
result[rows - y0][x + offset] = '╮' if y0 > y1 else '╯'
start = min(y0, y1) + 1
end = max(y0, y1)
for y in range(start, end):
result[rows - y][x + offset] = '│'
return '\n'.join([''.join(row) for row in result])
if __name__ == '__main__':
width = 180
series = [15 * cos(i * ((pi * 4) / width)) for i in range(width)]
print(plot(series))