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

Add a title option in the Python version. #27

Open
wants to merge 4 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
17 changes: 14 additions & 3 deletions asciichartpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

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

def plot(series, cfg=None):
def plot(series, cfg=None, title=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)
Expand All @@ -22,6 +22,8 @@ def plot(series, cfg=None):
interval = abs(float(maximum) - float(minimum))
offset = cfg['offset'] if 'offset' in cfg else 3
height = cfg['height'] if 'height' in cfg else interval
if title:
height -= 1
ratio = height / interval
min2 = floor(float(minimum) * ratio)
max2 = ceil(float(maximum) * ratio)
Expand Down Expand Up @@ -57,4 +59,13 @@ def plot(series, cfg=None):
for y in range(start, end):
result[rows - y][x + offset] = '│'

return '\n'.join([''.join(row) for row in result])
plot_string = '\n'.join([''.join(row) for row in result])
if title:
# Center the title over the non-axis portion of the plot.
# If the title is too long, start just after axis ends.
axis_idx = min(plot_string.index('┤'), plot_string.index('┼'))
plot_length = plot_string.index('\n') - axis_idx
n_padding = axis_idx + max((plot_length - len(title)) // 2, 1)
title = ' ' * n_padding + title
plot_string = '\n'.join((title, plot_string))
return plot_string
4 changes: 3 additions & 1 deletion pplot
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def parse_args():
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.")
parser.add_argument("--title")
return parser.parse_args()

if __name__ == "__main__":
args = vars(parse_args())
series = args.pop("series")
title = args.pop("title")
cfg = {k: v for k, v in args.items() if v is not None}
print(plot(series, cfg))
print(plot(series, cfg, title))