mirror of
https://github.com/tdulcet/Table-and-Graph-Libs.git
synced 2025-12-16 00:18:55 +08:00
Updated the style and color options to use Enums.
This commit is contained in:
382
python/graphs.py
382
python/graphs.py
@@ -7,13 +7,26 @@ from __future__ import division, print_function, unicode_literals
|
||||
import sys
|
||||
import math
|
||||
import shutil
|
||||
from fractions import Fraction
|
||||
import textwrap
|
||||
from enum import Enum, IntEnum, auto
|
||||
from wcwidth import wcswidth
|
||||
from typing import List, Tuple, Optional, Sequence, Callable
|
||||
import locale
|
||||
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
|
||||
|
||||
class style_types(IntEnum):
|
||||
ASCII = 0
|
||||
basic = auto()
|
||||
light = auto()
|
||||
heavy = auto()
|
||||
double = auto()
|
||||
light_dashed = auto()
|
||||
heavy_dashed = auto()
|
||||
|
||||
|
||||
styles = [
|
||||
["-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # ASCII
|
||||
["—", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # Basic
|
||||
@@ -25,33 +38,71 @@ styles = [
|
||||
]
|
||||
# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border
|
||||
|
||||
colors = ["\033[39m", "\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m", "\033[36m",
|
||||
"\033[37m", "\033[90m", "\033[91m", "\033[92m", "\033[93m", "\033[94m", "\033[95m", "\033[96m", "\033[97m"]
|
||||
|
||||
dots = ["⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾",
|
||||
"⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⣐", "⣑", "⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯", "⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"]
|
||||
class color_types(IntEnum):
|
||||
default = 0
|
||||
black = auto()
|
||||
red = auto()
|
||||
green = auto()
|
||||
yellow = auto()
|
||||
blue = auto()
|
||||
cyan = auto()
|
||||
light_gray = auto()
|
||||
dark_gray = auto()
|
||||
light_red = auto()
|
||||
light_green = auto()
|
||||
light_yellow = auto()
|
||||
light_blue = auto()
|
||||
light_cyan = auto()
|
||||
white = auto()
|
||||
|
||||
|
||||
colors = ["\033[39m", "\033[30m", "\033[31m", "\033[32m", "\033[33m",
|
||||
"\033[34m", "\033[35m", "\033[36m", "\033[37m", "\033[90m",
|
||||
"\033[91m", "\033[92m", "\033[93m", "\033[94m", "\033[95m",
|
||||
"\033[96m", "\033[97m"]
|
||||
|
||||
dots = [
|
||||
"⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎",
|
||||
"⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝",
|
||||
"⠞", "⠟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⠨", "⠩", "⠪", "⠫", "⠬",
|
||||
"⠭", "⠮", "⠯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⠸", "⠹", "⠺", "⠻",
|
||||
"⠼", "⠽", "⠾", "⠿", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⡈", "⡉", "⡊",
|
||||
"⡋", "⡌", "⡍", "⡎", "⡏", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⡘", "⡙",
|
||||
"⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⡨",
|
||||
"⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷",
|
||||
"⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆",
|
||||
"⢇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕",
|
||||
"⢖", "⢗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⢠", "⢡", "⢢", "⢣", "⢤",
|
||||
"⢥", "⢦", "⢧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⢰", "⢱", "⢲", "⢳",
|
||||
"⢴", "⢵", "⢶", "⢷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣀", "⣁", "⣂",
|
||||
"⣃", "⣄", "⣅", "⣆", "⣇", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⣐", "⣑",
|
||||
"⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⣠",
|
||||
"⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯",
|
||||
"⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾",
|
||||
"⣿"]
|
||||
|
||||
values = [[0x1, 0x2, 0x4, 0x40], [0x8, 0x10, 0x20, 0x80]]
|
||||
|
||||
fractions = {
|
||||
"¼": 1.0 / 4.0,
|
||||
"½": 1.0 / 2.0,
|
||||
"¾": 3.0 / 4.0,
|
||||
"⅐": 1.0 / 7.0,
|
||||
"⅑": 1.0 / 9.0,
|
||||
"⅒": 1.0 / 10.0,
|
||||
"⅓": 1.0 / 3.0,
|
||||
"⅔": 2.0 / 3.0,
|
||||
"⅕": 1.0 / 5.0,
|
||||
"⅖": 2.0 / 5.0,
|
||||
"⅗": 3.0 / 5.0,
|
||||
"⅘": 4.0 / 5.0,
|
||||
"⅙": 1.0 / 6.0,
|
||||
"⅚": 5.0 / 6.0,
|
||||
"⅛": 1.0 / 8.0,
|
||||
"⅜": 3.0 / 8.0,
|
||||
"⅝": 5.0 / 8.0,
|
||||
"⅞": 7.0 / 8.0
|
||||
"¼": Fraction(1, 4),
|
||||
"½": Fraction(1, 2),
|
||||
"¾": Fraction(3, 4),
|
||||
"⅐": Fraction(1, 7),
|
||||
"⅑": Fraction(1, 9),
|
||||
"⅒": Fraction(1, 10),
|
||||
"⅓": Fraction(1, 3),
|
||||
"⅔": Fraction(2, 3),
|
||||
"⅕": Fraction(1, 5),
|
||||
"⅖": Fraction(2, 5),
|
||||
"⅗": Fraction(3, 5),
|
||||
"⅘": Fraction(4, 5),
|
||||
"⅙": Fraction(1, 6),
|
||||
"⅚": Fraction(5, 6),
|
||||
"⅛": Fraction(1, 8),
|
||||
"⅜": Fraction(3, 8),
|
||||
"⅝": Fraction(5, 8),
|
||||
"⅞": Fraction(7, 8)
|
||||
}
|
||||
|
||||
constants = {
|
||||
@@ -59,68 +110,75 @@ constants = {
|
||||
"e": math.e
|
||||
}
|
||||
|
||||
MAX = sys.float_info.radix ** sys.float_info.mant_dig - 1
|
||||
|
||||
def strcol(str: str) -> int:
|
||||
|
||||
def strcol(astr: str) -> int:
|
||||
"""Returns the number of columns that the given string would take up if printed."""
|
||||
width = wcswidth(str)
|
||||
width = wcswidth(astr)
|
||||
if width == -1:
|
||||
print("\nError! wcswidth failed. Nonprintable wide character.", file=sys.stderr)
|
||||
print(
|
||||
"\nError! wcswidth failed. Nonprintable wide character.",
|
||||
file=sys.stderr)
|
||||
sys.exit(1)
|
||||
return width
|
||||
# return len(str)
|
||||
# return len(astr)
|
||||
|
||||
|
||||
def outputlabel(label: float) -> Tuple[int, str]:
|
||||
"""Outputs a label in a nice, human readable format."""
|
||||
def outputfraction(number: float) -> Tuple[int, str]:
|
||||
"""Convert fractions and constants to Unicode characters"""
|
||||
output = False
|
||||
|
||||
fractionpart, intpart = math.modf(label)
|
||||
fractionpart = abs(fractionpart)
|
||||
|
||||
strm = ""
|
||||
|
||||
for fraction in fractions:
|
||||
if abs(fractionpart - fractions[fraction]) < sys.float_info.epsilon:
|
||||
if intpart != 0:
|
||||
strm += str(intpart)
|
||||
n = abs(number)
|
||||
|
||||
strm += fraction
|
||||
if n <= MAX:
|
||||
# fractionpart, intpart = math.modf(number)
|
||||
# fractionpart = abs(fractionpart)
|
||||
intpart, fractionpart = divmod(Fraction(number).limit_denominator(), 1)
|
||||
|
||||
output = True
|
||||
break
|
||||
|
||||
if abs(label) >= sys.float_info.epsilon and not output:
|
||||
for constant in constants:
|
||||
if not output and label % constants[constant] == 0:
|
||||
intpart = label / constants[constant]
|
||||
|
||||
if intpart == -1:
|
||||
for fraction, value in fractions.items():
|
||||
if abs(fractionpart - value) <= sys.float_info.epsilon * n:
|
||||
if intpart == 0 and number < 0:
|
||||
strm += "-"
|
||||
elif intpart != 1:
|
||||
strm += str(intpart)
|
||||
elif intpart != 0:
|
||||
strm += "{0:n}".format(intpart)
|
||||
|
||||
strm += constant
|
||||
strm += fraction
|
||||
|
||||
output = True
|
||||
break
|
||||
|
||||
if n > sys.float_info.epsilon and not output:
|
||||
for constant, value in constants.items():
|
||||
if not output and number % value <= sys.float_info.epsilon * n:
|
||||
intpart = number / value
|
||||
|
||||
if intpart == -1:
|
||||
strm += "-"
|
||||
elif intpart != 1:
|
||||
strm += "{0:.{prec}n}".format(intpart,
|
||||
prec=sys.float_info.dig)
|
||||
|
||||
strm += constant
|
||||
|
||||
output = True
|
||||
break
|
||||
|
||||
if not output:
|
||||
strm += "{0:n}".format(label)
|
||||
strm += "{0:n}".format(number)
|
||||
|
||||
length = strcol(strm)
|
||||
|
||||
return length, strm
|
||||
|
||||
|
||||
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, title: Optional[str] = None) -> int:
|
||||
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Output graph"""
|
||||
if not array:
|
||||
return 1
|
||||
|
||||
if not (0 <= style < len(styles)):
|
||||
return 1
|
||||
|
||||
if height == 0:
|
||||
return 1
|
||||
|
||||
@@ -129,18 +187,19 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
|
||||
w = shutil.get_terminal_size()
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
@@ -150,46 +209,51 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
print("ymin must be less than ymax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
xscl = width / (xmax - xmin)
|
||||
yscl = height / (ymax - ymin)
|
||||
xaxis = width - (xmax * xscl)
|
||||
yaxis = ymax * yscl
|
||||
divisor = 2 * 4 * ((width // 160) + 1 if (width / 160.0) > 1 else 1)
|
||||
xstep = (xmax - xmin) / width
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = 0 if xmin > 0 else width if xmax < 0 else width - (xmax / xstep)
|
||||
yaxis = height if ymin > 0 else 0 if ymax < 0 else ymax / ystep
|
||||
xdivisor = 2 * 4 * ((width // 160) + 2)
|
||||
ydivisor = 2 * 4 * ((height // 160) + 2)
|
||||
|
||||
if title:
|
||||
print(textwrap.fill(title, width=w.columns))
|
||||
print(textwrap.fill(title, width=width // 2))
|
||||
|
||||
strm = ""
|
||||
|
||||
i = 0
|
||||
while i < height:
|
||||
ayaxis = i <= yaxis and (i + 4) > yaxis
|
||||
yaxislabel = i <= (yaxis + 4) and (i + 4) > (yaxis + 4)
|
||||
ayaxis = i <= yaxis and (
|
||||
i + 4) > yaxis if yaxis <= (height - 4) else i < yaxis and (i + 4) >= yaxis
|
||||
yaxislabel = i <= (yaxis + 4) and (i + 4) > (yaxis + 4) if yaxis <= (
|
||||
height - 4) else i < (yaxis - 4) and (i + 4) >= (yaxis - 4)
|
||||
|
||||
ylabelstrm = ""
|
||||
ylabellength = 0
|
||||
|
||||
if border and axislabel and axisunitslabel:
|
||||
if border and axislabel and axisunitslabel and yaxis >= 0 and yaxis <= height:
|
||||
output = False
|
||||
label = 0.0
|
||||
adivisor = -divisor if i < yaxis else divisor
|
||||
adivisor = -ydivisor if i < yaxis else ydivisor
|
||||
|
||||
k = yaxis + adivisor
|
||||
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and i >= 4 and not output:
|
||||
while (k >= i if i < yaxis else k < (i + 4)) and i >= 4 and not output:
|
||||
if (i <= k and (i + 4) > k):
|
||||
label = ymax - (k / yscl)
|
||||
label = ymax - ((height if k > height else k) * ystep)
|
||||
|
||||
output = True
|
||||
k += adivisor
|
||||
|
||||
if (output):
|
||||
ylabellength, ylabelstrm = outputlabel(label)
|
||||
ylabellength, ylabelstrm = outputfraction(label)
|
||||
ylabellength *= 2
|
||||
|
||||
j = 0
|
||||
while j < width:
|
||||
axaxis = j <= xaxis and (j + 2) > xaxis
|
||||
xaxislabel = j <= (xaxis - 2) and (j + 2) > (xaxis - 2)
|
||||
axaxis = j < xaxis and (
|
||||
j + 2) >= xaxis if xaxis >= 2 else j <= xaxis and (j + 2) > xaxis
|
||||
xaxislabel = j < (xaxis - 2) and (j + 2) >= (
|
||||
xaxis - 2) if xaxis >= 2 else j <= (xaxis + 2) and (j + 2) > (xaxis + 2)
|
||||
|
||||
output = False
|
||||
|
||||
@@ -198,57 +262,61 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
strm += styles[style][6]
|
||||
output = True
|
||||
elif axaxis:
|
||||
if axislabel and axisunitslabel:
|
||||
adivisor = -divisor if i < yaxis else divisor
|
||||
if i == 0:
|
||||
strm += styles[style][4]
|
||||
output = True
|
||||
elif i >= (height - 4):
|
||||
strm += styles[style][10]
|
||||
output = True
|
||||
elif axislabel and axisunitslabel:
|
||||
adivisor = -ydivisor if i < yaxis else ydivisor
|
||||
|
||||
k = yaxis + adivisor
|
||||
while ((i < yaxis and k >= i) or (i > yaxis and k < (i + 4))) and i >= 4 and not output:
|
||||
while (k >= i if i < yaxis else k < (i + 4)) and i >= 4 and not output:
|
||||
if i <= k and (i + 4) > k:
|
||||
strm += styles[style][7]
|
||||
strm += styles[style][7 if xaxis >= 2 else 5]
|
||||
output = True
|
||||
k += adivisor
|
||||
if not output:
|
||||
if i == 0:
|
||||
strm += styles[style][4]
|
||||
elif i >= (height - 4):
|
||||
strm += styles[style][10]
|
||||
else:
|
||||
strm += styles[style][1]
|
||||
strm += styles[style][1]
|
||||
output = True
|
||||
elif ayaxis:
|
||||
if axislabel and axisunitslabel:
|
||||
adivisor = -divisor if j < xaxis else divisor
|
||||
if j == 0:
|
||||
strm += styles[style][2]
|
||||
output = True
|
||||
elif j >= (width - 2):
|
||||
strm += styles[style][4]
|
||||
output = True
|
||||
elif axislabel and axisunitslabel:
|
||||
adivisor = -xdivisor if j < xaxis else xdivisor
|
||||
|
||||
k = xaxis + adivisor
|
||||
while ((j < xaxis and k >= j) or (j > xaxis and k < (j + 2))) and j < (width - 4) and not output:
|
||||
while (k >= j if j < xaxis else k < (j + 2)) and j < (width - 4) and not output:
|
||||
if j <= k and (j + 2) > k:
|
||||
strm += styles[style][3]
|
||||
strm += styles[style][3 if yaxis <=
|
||||
(height - 4) else 9]
|
||||
output = True
|
||||
k += adivisor
|
||||
if not output:
|
||||
if j == 0:
|
||||
strm += styles[style][2]
|
||||
elif j >= (width - 2):
|
||||
strm += styles[style][4]
|
||||
else:
|
||||
strm += styles[style][0]
|
||||
strm += styles[style][0]
|
||||
output = True
|
||||
elif yaxislabel and xaxislabel and axislabel and axisunitslabel:
|
||||
elif yaxislabel and xaxislabel and axislabel and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0:
|
||||
strm += "0"
|
||||
output = True
|
||||
elif j >= (width - 2) and yaxislabel and axislabel:
|
||||
elif (j >= (width - 2) if xaxis <= (width - 2) else j == 0) and yaxislabel and axislabel:
|
||||
strm += "x"
|
||||
output = True
|
||||
elif yaxislabel and axislabel and axisunitslabel:
|
||||
label = 0.0
|
||||
adivisor = -divisor if j < xaxis else divisor
|
||||
adivisor = -xdivisor if j < xaxis else xdivisor
|
||||
if j < xaxis:
|
||||
j += 2
|
||||
|
||||
k = xaxis + adivisor
|
||||
while ((j < xaxis and k >= j) or (j > xaxis and k < (j + 2))) and j < (width - 2) and not output:
|
||||
while (k >= j if j < xaxis else k < (j + 2)) and j < (width - 2) and not output:
|
||||
if j <= k and (j + 2) > k:
|
||||
label = (k / xscl) + xmin
|
||||
label = ((width if k > width else k)
|
||||
* xstep) + xmin
|
||||
|
||||
output = True
|
||||
k += adivisor
|
||||
@@ -259,7 +327,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if output:
|
||||
output = False
|
||||
|
||||
length, astrm = outputlabel(label)
|
||||
length, astrm = outputfraction(label)
|
||||
length *= 2
|
||||
if (j >= xaxis or (j + length) < (xaxis - 4)) and (j + length) < (width - 2):
|
||||
strm += astrm
|
||||
@@ -271,10 +339,10 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
output = True
|
||||
else:
|
||||
j += 2
|
||||
elif i == 0 and xaxislabel and axislabel:
|
||||
elif (i == 0 if yaxis >= 4 else i >= (height - 4)) and xaxislabel and axislabel:
|
||||
strm += "y"
|
||||
output = True
|
||||
elif (j <= (xaxis - ylabellength) and (j + 2) > (xaxis - ylabellength)) and axislabel and axisunitslabel:
|
||||
elif ylabellength and (xaxislabel if xaxis < 2 else j < (xaxis - ylabellength) and (j + 2) >= (xaxis - ylabellength)) and (yaxis >= 4 or i < (height - 4)) and axislabel and axisunitslabel:
|
||||
strm += ylabelstrm
|
||||
output = True
|
||||
if ylabellength > 2:
|
||||
@@ -299,14 +367,15 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if color:
|
||||
strm += colors[color]
|
||||
|
||||
strm += "\033[1m" + dots[dot] + "\033[22m"
|
||||
strm += dots[dot]
|
||||
|
||||
if color:
|
||||
strm += colors[0]
|
||||
|
||||
j += 2
|
||||
|
||||
strm += "\n"
|
||||
if i < (height - 4):
|
||||
strm += "\n"
|
||||
i += 4
|
||||
|
||||
print(strm)
|
||||
@@ -314,7 +383,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
return 0
|
||||
|
||||
|
||||
def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Convert one or more arrays to graph and output"""
|
||||
if not aarrays:
|
||||
return 1
|
||||
@@ -323,9 +392,6 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
print("Error: The arrays must have two columns.")
|
||||
return 1
|
||||
|
||||
if color < 0 or color >= len(colors):
|
||||
return 1
|
||||
|
||||
w = shutil.get_terminal_size()
|
||||
|
||||
if height == 0:
|
||||
@@ -334,18 +400,19 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
if width == 0:
|
||||
width = w.columns * 2
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin == 0 and xmax == 0:
|
||||
xmin = min(i[0] for aarray in aarrays for i in aarray)
|
||||
@@ -363,10 +430,10 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
print("ymin must be less than ymax.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
xscl = width / (xmax - xmin)
|
||||
yscl = height / (ymax - ymin)
|
||||
xaxis = width - (xmax * xscl)
|
||||
yaxis = ymax * yscl
|
||||
xstep = (xmax - xmin) / width
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = width - (xmax / xstep)
|
||||
yaxis = ymax / ystep
|
||||
|
||||
aaarray = [[0 for j in range(height)] for i in range(width)]
|
||||
|
||||
@@ -376,8 +443,8 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
|
||||
for i in aarray:
|
||||
if i[0] >= xmin and i[0] < xmax and i[1] >= ymin and i[1] < ymax:
|
||||
x = int((i[0] * xscl) + xaxis)
|
||||
y = int((yaxis - (i[1] * yscl)) - 1)
|
||||
x = int((i[0] / xstep) + xaxis)
|
||||
y = int((yaxis - (i[1] / ystep)) - 1)
|
||||
|
||||
if aaarray[x][y]:
|
||||
if aaarray[x][y] != acolor:
|
||||
@@ -385,19 +452,18 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
|
||||
else:
|
||||
aaarray[x][y] = acolor
|
||||
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border=border,
|
||||
axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
|
||||
|
||||
def array(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def array(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
"""Convert single array to graph and output"""
|
||||
return arrays(height, width, xmin, xmax, ymin, ymax, [aarray], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
return arrays(height, width, xmin, xmax, ymin, ymax, [
|
||||
aarray], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
|
||||
|
||||
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int:
|
||||
"""Convert one or more functions to graph and output"""
|
||||
if color < 0 or color >= len(colors):
|
||||
return 1
|
||||
|
||||
if not afunctions:
|
||||
return 1
|
||||
|
||||
@@ -409,18 +475,19 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
if width == 0:
|
||||
width = w.columns * 2
|
||||
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
if check:
|
||||
aheight = height // 4
|
||||
awidth = width // 2
|
||||
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
if aheight > w.lines:
|
||||
print("The height of the graph ({0}) is greater then the height of the terminal ({1}).".format(
|
||||
aheight, w.lines), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
if awidth > w.columns:
|
||||
print("The width of the graph ({0}) is greater then the width of the terminal ({1}).".format(
|
||||
awidth, w.columns), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if xmin >= xmax:
|
||||
print("xmin must be less than xmax.", file=sys.stderr)
|
||||
@@ -432,10 +499,11 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
|
||||
rows = width
|
||||
|
||||
xscl = width / (xmax - xmin)
|
||||
yscl = height / (ymax - ymin)
|
||||
xaxis = width - (xmax * xscl)
|
||||
yaxis = ymax * yscl
|
||||
xstep = (xmax - xmin) / width
|
||||
ystep = (ymax - ymin) / height
|
||||
xaxis = width - (xmax / xstep)
|
||||
yaxis = ymax / ystep
|
||||
xres = 2
|
||||
|
||||
array = [[0 for j in range(height)] for i in range(width)]
|
||||
|
||||
@@ -443,13 +511,13 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
acolor = color + 1 if len(afunctions) == 1 else (j %
|
||||
(len(colors) - 2)) + 3
|
||||
|
||||
for i in (x / 2 for x in range(rows * 2)):
|
||||
x = (i / xscl) + xmin
|
||||
for i in (x / xres for x in range(rows * xres)):
|
||||
x = (i * xstep) + xmin
|
||||
y = function(x)
|
||||
|
||||
if x >= xmin and x < xmax and y >= ymin and y < ymax:
|
||||
ax = int((x * xscl) + xaxis)
|
||||
ay = int((yaxis - (y * yscl)) - 1)
|
||||
ax = int((x / xstep) + xaxis)
|
||||
ay = int((yaxis - (y / ystep)) - 1)
|
||||
|
||||
if array[ax][ay]:
|
||||
if array[ax][ay] != acolor:
|
||||
@@ -457,9 +525,11 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
|
||||
else:
|
||||
array[ax][ay] = acolor
|
||||
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, array, border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
return graph(height, width, xmin, xmax, ymin, ymax, array, border=border,
|
||||
axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, title=title)
|
||||
|
||||
|
||||
def function(height, width, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: int = 2, color: int = 2, title: Optional[str] = None) -> int:
|
||||
def function(height, width, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = True, axislabel: bool = True, axisunitslabel: bool = True, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int:
|
||||
"""Convert single function to function array and output"""
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, [afunction], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
return functions(height, width, xmin, xmax, ymin, ymax, [
|
||||
afunction], border=border, axislabel=axislabel, axisunitslabel=axisunitslabel, style=style, color=color, title=title)
|
||||
|
||||
Reference in New Issue
Block a user