mkaz / termgraph

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

Made Termgraph an API/Module and some new features #75

Closed AdityaTaggar05 closed 3 years ago

AdityaTaggar05 commented 3 years ago

I have made termgraph an API/module to use in other scripts and would like it to be merged with the base repository.

I have kept the structure very simple and anyone can easily extend upon it and add other types of charts to the module. Some other features have also been added.

I have made some changes in termgraph.py itself and the API/module code can be found in module.py (I know I am really bad at naming things)

Issues Handled: #27, #39, #47

Features Added:

image

API/Module:

Colors

It houses all the colours that can be used in termgraph

Args

The Args class is basically the same thing as passing args through the command line. You can pass args as keyword arguments or use update_args on an Args instance. Args are directly passed into Chart.

Data

The Data class merges the data, labels and categories all in one. I wasn't able to add normal_data to it as well cause the normalize function required 'width' parameter for which we needed the access to the args but they are only accessible by Chart. So the only resolution was passing the args to data as well which didn't seem like a good idea. So took the normalize function and put it in Chart class. You can also visualize the data by just printing it or by using the str() function on it. It also has a property called dims (I was inspired by NumPy for this) which may or may not be helpful but I added it for future use.

Chart

The Chart class takes in the data and the args. It is drawn by using the draw() method. It is an abstract-ish class and has nothing special in it except for the _normalize() method which is the same as before and _print_header() method which prints the title and category names.

Horizontal Chart

It is derived off of the Chart class and the main reason for deriving the new chart off of this is that it contains the print_row function. I did so because no other chart required access to the function except for the ones which are horizontal.

I was only able to implement Bar Chart due to time limitations and my exams approaching and some other reasons. Below is the example code for usage (it can also be found in example.py)

from module import Data, BarChart, Args, Colors

data = Data(data=[[765, 787], [781, 769]], labels=["6th G", "7th G"], categories=["Boys", "Girls"])
chart = BarChart(data, Args(title="Total Marks Per Class", colors=[Colors.Red, Colors.Magenta], space_between=True))

chart.draw()

image

alexge233 commented 3 years ago

Has this been merged? I'm very keen to use it as an imort module!

mkaz commented 3 years ago

Thanks @AdityaTaggar05 for the great contributions! :+1:

vergelli commented 2 years ago

Any way to change the numbers to integers instead of float, it seems to be the default type.