(data: list)
| 28 | |
| 29 | |
| 30 | def bar_chart(data: list): |
| 31 | max_value = max(count for _, count in data.items()) |
| 32 | increment = max_value / 25 |
| 33 | |
| 34 | longest_label_length = max(len(label) for label, _ in data.items()) |
| 35 | |
| 36 | for label, count in data.items(): |
| 37 | |
| 38 | # The ASCII block elements come in chunks of 8, so we work out how |
| 39 | # many fractions of 8 we need. |
| 40 | # https://en.wikipedia.org/wiki/Block_Elements |
| 41 | bar_chunks, remainder = divmod(int(count * 8 / increment), 8) |
| 42 | |
| 43 | # First draw the full width chunks |
| 44 | bar = '█' * bar_chunks |
| 45 | |
| 46 | # Then add the fractional part. The Unicode code points for |
| 47 | # block elements are (8/8), (7/8), (6/8), ... , so we need to |
| 48 | # work backwards. |
| 49 | if remainder > 0: |
| 50 | bar += chr(ord('█') + (8 - remainder)) |
| 51 | |
| 52 | # If the bar is empty, add a left one-eighth block |
| 53 | bar = bar or '▏' |
| 54 | |
| 55 | print(f'{label.rjust(longest_label_length)} ▏ {count:#4f} {bar}') |
| 56 | |
| 57 | |
| 58 | class Report: |
no outgoing calls
no test coverage detected
searching dependent graphs…