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

Fix for exact specification of plot height. #29

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
48 changes: 22 additions & 26 deletions asciichartpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# -----------------------------------------------------------------------------

from __future__ import division
from math import ceil, floor

# -----------------------------------------------------------------------------
Expand All @@ -13,48 +14,43 @@
def plot(series, cfg=None):
""" Possible cfg parameters are 'minimum', 'maximum', 'offset', 'height' and 'format'.
cfg is a dictionary, thus dictionary syntax has to be used.
Example: print(plot(series, { 'height' :10 }))
Example: print(plot(series, { 'height' :10 }))
"""
cfg = cfg or {}
minimum = cfg['minimum'] if 'minimum' in cfg else min(series)
maximum = cfg['maximum'] if 'maximum' in cfg else max(series)
if minimum > maximum:
raise ValueError('The minimum value cannot exceed the maximum value.')

interval = abs(float(maximum) - float(minimum))
interval = maximum - minimum
offset = cfg['offset'] if 'offset' in cfg else 3
height = cfg['height'] if 'height' in cfg else interval
ratio = height / interval
min2 = floor(float(minimum) * ratio)
max2 = ceil(float(maximum) * ratio)

intmin2 = int(min2)
intmax2 = int(max2)

rows = abs(intmax2 - intmin2)
height = cfg['height'] if 'height' in cfg else ceil(interval)
ratio = interval / (height - 1)
width = len(series) + offset
placeholder = cfg['format'] if 'format' in cfg else '{:8.2f} '

result = [[' '] * width for i in range(rows + 1)]
result = [[' '] * width for i in range(height)]

# axis and labels
for y in range(intmin2, intmax2 + 1):
label = placeholder.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 '┤'
for i in range(height):
label = placeholder.format(minimum + i * ratio)
result[i][max(offset - len(label), 0)] = label
result[i][offset - 1] = '┼' if i == 0 else '┤'

y0 = int(series[0] * ratio - min2)
result[rows - y0][offset - 1] = '┼' # first value
y0 = int((series[0] - minimum) / ratio)
result[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)
for i in range(len(series) - 1): # plot the line
y0 = round((series[i] - minimum) / ratio)
y1 = round((series[i + 1] - minimum) / ratio)
if y0 == y1:
result[rows - y0][x + offset] = '─'
result[y0][i + offset] = '─'
else:
result[rows - y1][x + offset] = '╰' if y0 > y1 else '╭'
result[rows - y0][x + offset] = '╮' if y0 > y1 else '╯'
result[y1][i + offset] = '╰' if y0 > y1 else '╭'
result[y0][i + 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] = '│'
result[y][i + offset] = '│'

return '\n'.join([''.join(row) for row in result])
return '\n'.join([''.join(row) for row in result[::-1]])
2 changes: 1 addition & 1 deletion pplot
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def parse_args():
parser = ArgumentParser()
parser.add_argument("series", type=float, nargs="+")
parser.add_argument("--offset", type=int)
parser.add_argument("--height", type=float)
parser.add_argument("--height", type=int)
parser.add_argument("--format", help="Format for tick numbers.")
parser.add_argument("--minimum", type=float, help="Min y value.")
parser.add_argument("--maximum", type=float, help="Max y value.")
Expand Down