├── termchart ├── __init__.py └── termchart.py ├── termchart_screenshot.png ├── setup.py ├── LICENSE └── README.md /termchart/__init__.py: -------------------------------------------------------------------------------- 1 | from .termchart import * 2 | -------------------------------------------------------------------------------- /termchart_screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tazeg/termchart/master/termchart_screenshot.png -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md", "r") as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name="termchart", 8 | version="0.0.4", 9 | author="@JeffProd", 10 | author_email="termchart@jeffprod.com", 11 | description="A tool to draw ascii line chart in terminal", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | keywords='terminal line chart graph', 15 | url="https://github.com/Tazeg/termchart", 16 | packages=setuptools.find_packages(), 17 | classifiers=[ 18 | "Programming Language :: Python :: 3", 19 | "License :: OSI Approved :: MIT License", 20 | "Operating System :: OS Independent", 21 | ], 22 | project_urls={ 23 | 'Funding': 'https://en.jeffprod.com/donate/', 24 | 'Twitter': 'https://twitter.com/jeffprod', 25 | 'Web': 'https://jeffprod.com' 26 | }, 27 | ) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 JeffProd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### TermChart 2 | 3 | Draw ascii line charts in terminal. 4 | 5 | ![termchart_screenshot](https://raw.githubusercontent.com/Tazeg/termchart/master/termchart_screenshot.png) 6 | 7 | ### Install 8 | 9 | ```bash 10 | pip3 install termchart 11 | ``` 12 | 13 | ### Usage 14 | 15 | Create a Python file : 16 | 17 | ```python 18 | import termchart 19 | 20 | graph = termchart.Graph([1,2,3,2,5,1,-1,-5,-3]) 21 | graph.draw() 22 | ``` 23 | 24 | You can change the plot (default is `+`): 25 | 26 | ```python 27 | graph.setDot('|') 28 | ``` 29 | 30 | Change the width and height (default cols is 160x50) 31 | 32 | ```python 33 | graph.setCols(200) 34 | graph.setRows(40) 35 | ``` 36 | 37 | Add values whenever you need it with `addData()`. Here is a full example for a live graph with random values : 38 | 39 | ```python 40 | import termchart 41 | import time 42 | import os 43 | from random import randint 44 | 45 | graph = termchart.Graph([]) 46 | while True: 47 | rand = randint(0, 9) 48 | graph.addData(rand) 49 | graph.draw() 50 | time.sleep(1) 51 | os.system('cls' if os.name == 'nt' else 'clear') 52 | ``` 53 | 54 | ### Donate 55 | 56 | 57 | -------------------------------------------------------------------------------- /termchart/termchart.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # coding=utf-8 3 | 4 | """ 5 | A python module to draw ascii line chart in terminal 6 | By : https://jeffprod.com - https://twitter.com/jeffprod 7 | """ 8 | 9 | class Graph: 10 | 11 | def __init__(self, data): 12 | if isinstance(data, list): 13 | self.data = data 14 | else: 15 | self.data = [] 16 | self.nb_rows = 50 17 | self.nb_cols = 160 18 | self.dot = '+' 19 | 20 | def addData(self, val): 21 | self.data.append(val) 22 | 23 | def setRows(self, nb): 24 | self.nb_rows = nb 25 | 26 | def setCols(self, nb): 27 | self.nb_cols = nb 28 | 29 | def setDot(self, char): 30 | self.dot = char 31 | 32 | def draw(self): 33 | maxY = max(self.data) 34 | minY = min(self.data) 35 | scaleY = (maxY - minY) / self.nb_rows 36 | xdata = self.data[-self.nb_cols:] 37 | self.data = xdata 38 | yval = maxY 39 | cpt = self.nb_rows 40 | while cpt>0: 41 | print("%.2f" % yval, end= ' ') 42 | for xval in xdata: 43 | if xval>=yval: 44 | print('\033[01;33m' + self.dot + '\033[00m', end='') 45 | else: 46 | print(' ', end='') 47 | print() 48 | yval = yval - scaleY 49 | cpt = cpt - 1 50 | 51 | 52 | if __name__ == "__main__": 53 | 54 | graph = Graph([0.8251, 0.8254, 0.8253, 0.8254, 0.8253, 0.8253, 0.8253, 0.8254, 0.8254, 0.8254, 0.8254, 0.8254, 0.8251, 0.826, 0.826, 0.826, 0.826, 0.824, 0.824, 0.8245, 0.8258, 0.8258, 0.826, 0.825, 0.8253, 0.8255, 0.827, 0.8272, 0.829, 0.83, 0.8301, 0.829, 0.8291, 0.8291, 0.8272, 0.8272, 0.8272, 0.8266, 0.8269, 0.8269, 0.8285, 0.828, 0.8269, 0.8285, 0.83, 0.8317, 0.8321, 0.8345, 0.8353, 0.8345, 0.8348, 0.8321, 0.8324, 0.8323, 0.8325, 0.8331, 0.8336, 0.8336, 0.8342, 0.8345, 0.835, 0.835, 0.836, 0.8343, 0.834, 0.8341, 0.8341, 0.8341, 0.8342, 0.835, 0.835, 0.836, 0.8362, 0.8358, 0.839, 0.8353, 0.836, 0.8362, 0.84, 0.838, 0.838, 0.838, 0.838, 0.8383, 0.8381, 0.837, 0.837, 0.837, 0.8371, 0.8371, 0.837, 0.837, 0.8372, 0.8311, 0.8325, 0.832, 0.832, 0.832, 0.832, 0.832, 0.83, 0.83, 0.8312, 0.8317, 0.8317, 0.83, 0.8301, 0.83, 0.83, 0.83, 0.83, 0.8302, 0.8301, 0.8302, 0.83, 0.83, 0.83, 0.83, 0.829, 0.8258, 0.826, 0.8252, 0.825, 0.8232, 0.8239, 0.8232, 0.8241, 0.8243, 0.825, 0.8253, 0.8255, 0.824, 0.8228, 0.8222, 0.8221, 0.822, 0.822, 0.8205, 0.82, 0.8186, 0.8129, 0.813, 0.8136, 0.814, 0.8111, 0.8113, 0.8113, 0.8101, 0.81, 0.812, 0.811, 0.8108, 0.8111, 0.812, 0.8087, 0.8102, 0.81, 0.8095, 0.8103, 0.812, 0.812, 0.8102, 0.8077, 0.8077, 0.8064, 0.8055, 0.8061, 0.806, 0.8065, 0.805, 0.8067, 0.8058, 0.805, 0.805, 0.806, 0.8063, 0.812, 0.8112, 0.8112, 0.811, 0.81, 0.8085, 0.8086, 0.8086, 0.806, 0.806, 0.806, 0.805, 0.8037, 0.804, 0.8015, 0.8, 0.8, 0.7954, 0.7934, 0.7901, 0.7905, 0.791, 0.7905, 0.7906, 0.7906, 0.791, 0.7905, 0.79, 0.7911]) 55 | graph.setCols(205) 56 | graph.setRows(50) 57 | graph.setDot('|') 58 | graph.draw() 59 | --------------------------------------------------------------------------------