mkaz / termgraph

a python command-line tool which draws basic graphs in the terminal
MIT License
3.14k stars 165 forks source link

ZeroDivisionError on empty data #79

Closed ryan-25 closed 3 years ago

ryan-25 commented 3 years ago

Background

When passing in data containing all 0's termgraph will return a ZeroDivisionError. Ideally termgraph should accept this value and return an empty graph.

Example Code

termgraph.chart(
    colors=None,
    data=[0, 0]
    args={'width': 100, 'format': '{:,}', 'suffix': '', 'no_labels': False, 'no_values': False, 'vertical': False, 'stacked': False, 'histogram': False, 'different_scale': False},
    labels=['iOS', 'Android']
)

Error Message

ERROR: Problem running script: Traceback (most recent call last):
  File "/../main.py", line 523, in <module>
    cli_handler()
  File "/.../lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/.../lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/.../lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/.../lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/../main.py", line 194, in cli_handler
    main(
  File "/.../main.py", line 449, in main
    termgraph.chart(
  File "/.../lib/python3.8/site-packages/termgraph/termgraph.py", line 485, in chart
    normal_dat = normalize(data, args["width"])
  File "/.../lib/python3.8/site-packages/termgraph/termgraph.py", line 162, in normalize
    norm_factor = width / float(max_datum)
ZeroDivisionError: float division by zero
neil-greenwood commented 3 years ago

The case has min_datum == max_datum, so maybe this can be used as a guard to prevent the division by zero.