From 949e46b2974619737d6442f257543e28a50a45f3 Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Mon, 13 Mar 2023 07:29:43 -0700 Subject: [PATCH] Updated graph library and added options. --- README.md | 59 +++++++++++------ graphs.hpp | 68 ++++++++++++++------ python/README.md | 58 +++++++++++------ python/graphs.py | 160 +++++++++++++++++++++++++++-------------------- python/tables.py | 57 +++++++++-------- python/test.py | 13 +++- tables.hpp | 9 +-- 7 files changed, 269 insertions(+), 155 deletions(-) diff --git a/README.md b/README.md index 657ada3..1306b8d 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,15 @@ See the [python](python) directory for Python ports of the libraries. ❤️ Please visit [tealdulcet.com](https://www.tealdulcet.com/) to support these libraries and my other software development. +## Table of Contents +* [Tables](#tables) + * [Usage](#usage) + * [Options](#options) +* [Graphs/Plots](#graphsplots) + * [Usage](#usage-1) + * [Options](#options-1) +* [Contributing](#contributing) + ## Tables ### Usage @@ -474,7 +483,7 @@ Default value: `false` Option: `title`\ Default value: `nullptr` -The title is word wrapped based on the current width of the terminal, using [this](https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0) solution. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). +The title is output at the top of the table. It is word wrapped based on the current width of the terminal, using [this](https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0) solution. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). #### Border style @@ -733,9 +742,14 @@ int main() ### Options -#### Border/Axis +#### Border Option: `border`\ +Default value: `false` + +#### Axis + +Option: `axis`\ Default value: `true` #### Axis labels @@ -743,21 +757,28 @@ Default value: `true` Option: `axislabel`\ Default value: `true` -Requires `border` to be `true`. +Requires `axis` to be `true`. + +#### Axis ticks + +Option: `axistick`\ +Default value: `true` + +Requires `axis` and `axislabel` to be `true`. #### Axis units labels Option: `axisunitslabel`\ Default value: `true` -Requires `border` and `axislabel` to be `true`. +Requires `axis`, `axislabel` and `axistick` to be `true`. #### Title Option: `title`\ Default value: `nullptr` -The title is word wrapped based on the current width of the terminal, using [this](https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0) solution. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). +The title is output at the top of the graph. It is word wrapped based on the current width of the terminal, using [this](https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0) solution. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). #### Axis/Border style @@ -797,19 +818,21 @@ Values: 3. `color_green`: Green 4. `color_yellow`: Yellow 5. `color_blue`: Blue -6. `color_cyan`: Cyan -7. `color_light_gray`: Light gray -8. `color_dark_gray`: Dark gray -9. `color_light_red`: Light red -10. `color_light_green`: Light green -11. `color_light_yellow`: Light yellow -12. `color_light_blue`: Light blue -13. `color_light_cyan`: Light cyan -14. `color_white`: White +6. `color_magenta`: Magenta +7. `color_cyan`: Cyan +8. `color_light_gray`: Light Gray +9. `color_dark_gray`: Dark Gray +10. `color_light_red`: Light Red +11. `color_light_green`: Light Green +12. `color_light_yellow`: Light Yellow +13. `color_light_blue`: Light Blue +14. `color_light_magenta`: Light Magenta +15. `color_light_cyan`: Light Cyan +16. `color_white`: White See [here](https://misc.flogisoft.com/bash/tip_colors_and_formatting#foreground_text) for examples of the colors. -Only used when plotting a single array and when graphing a single function. When plotting multiple arrays or graphing multiple functions, colors 2 - 14 are used inorder. The system default color is used where the plots cross. +Only used when plotting a single array and when graphing a single function. When plotting multiple arrays or graphing multiple functions, colors 2 - 16 are used inorder. The system default color is used where the plots cross. ##### Plot @@ -836,16 +859,16 @@ Pull requests welcome! Ideas for contributions: Both: * Add more options - * Add an option to print a border around graphs/plots * Add options to word wrap and truncate long text in table cells * Add option to center text in table cells * Add more examples +* Update screenshots of existing examples * Improve the performance * Handle newlines and tabs in the tables * Handle formatted text in the table and graph/plot titles * Support more graph/plot colors - * Support 24-bit color - * Support combining colors when functions cross + * Support 256 and 24-bit color + * Support combining/blending colors when functions cross * Update the `-t, --table` options of column command from [util-linux](https://en.wikipedia.org/wiki/Util-linux) to use the Table library * Create a new CLI tool that uses the Graph library * Port to other languages (C, Java, Rust, etc.) diff --git a/graphs.hpp b/graphs.hpp index f7aa95e..453b99b 100644 --- a/graphs.hpp +++ b/graphs.hpp @@ -41,10 +41,10 @@ namespace graphs {"─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light {"━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"}, // Heavy {"═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"}, // Double - {"╌", "╎", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light Dashed - {"╍", "╏", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"} // Heavy Dashed + {"╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light Dashed + {"╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"} // Heavy Dashed + // {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} // No border }; - // {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};//No border enum color_type { @@ -54,6 +54,7 @@ namespace graphs color_green, color_yellow, color_blue, + color_magenta, color_cyan, color_light_gray, color_dark_gray, @@ -61,11 +62,12 @@ namespace graphs color_light_green, color_light_yellow, color_light_blue, + color_light_magenta, color_light_cyan, color_white }; - enum color_type const color_types[] = {color_default, color_black, color_red, color_green, color_yellow, color_blue, color_cyan, color_light_gray, color_dark_gray, color_light_red, color_light_green, color_light_yellow, color_light_blue, color_light_cyan, color_white}; + enum color_type const color_types[] = {color_default, color_black, color_red, color_green, color_yellow, color_blue, color_magenta, color_cyan, color_light_gray, color_dark_gray, color_light_red, color_light_green, color_light_yellow, color_light_blue, color_light_magenta, color_light_cyan, color_white}; const char *const colors[] = {"\e[39m", "\e[30m", "\e[31m", "\e[32m", "\e[33m", "\e[34m", "\e[35m", "\e[36m", "\e[37m", "\e[90m", "\e[91m", "\e[92m", "\e[93m", "\e[94m", "\e[95m", "\e[96m", "\e[97m"}; @@ -84,8 +86,10 @@ namespace graphs struct options { - bool border = true; + bool border = false; + bool axis = true; bool axislabel = true; + bool axistick = true; bool axisunitslabel = true; const char *title = nullptr; style_type style = style_light; @@ -258,7 +262,9 @@ namespace graphs return 1; const bool border = aoptions.border; + const bool axis = aoptions.axis; const bool axislabel = aoptions.axislabel; + const bool axistick = aoptions.axistick; const bool axisunitslabel = aoptions.axisunitslabel; const char *const title = aoptions.title; const style_type style = aoptions.style; @@ -272,11 +278,11 @@ namespace graphs struct winsize w; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + const size_t aheight = height / 4; + const size_t awidth = width / 2; + if (aoptions.check) { - const size_t aheight = height / 4; - const size_t awidth = width / 2; - if (aheight > w.ws_row) { cerr << "The height of the graph (" << aheight << ") is greater then the height of the terminal (" << w.ws_row << ").\n"; @@ -314,7 +320,17 @@ namespace graphs setlocale(LC_ALL, ""); if (title and title[0] != '\0') - cout << wrap(title, width / 2) << "\n"; + cout << wrap(title, awidth) << "\n"; + + if (border) + { + cout << styles[style][2]; + + for (size_t k = 0; k < awidth; ++k) + cout << styles[style][0]; + + cout << styles[style][4] << "\n"; + } for (size_t i = 0; i < height; i += 4) { @@ -324,7 +340,7 @@ namespace graphs ostringstream ylabelstrm; size_t ylabellength = 0; - if (border and axislabel and axisunitslabel and yaxis >= 0 and yaxis <= height) + if (axis and axislabel and axistick and axisunitslabel and yaxis >= 0 and yaxis <= height) { bool output = false; long double label = 0; @@ -347,6 +363,9 @@ namespace graphs } } + if (border) + cout << styles[style][1]; + for (size_t j = 0; j < width; j += 2) { const bool axaxis = xaxis >= 2 ? j < xaxis and (j + 2) >= xaxis : j <= xaxis and (j + 2) > xaxis; @@ -354,7 +373,7 @@ namespace graphs bool output = false; - if (border) + if (axis) { if (axaxis and ayaxis) { @@ -373,7 +392,7 @@ namespace graphs cout << styles[style][10]; output = true; } - else if (axislabel and axisunitslabel) + else if (axislabel and axistick) { int adivisor = i < yaxis ? -ydivisor : ydivisor; @@ -404,7 +423,7 @@ namespace graphs cout << styles[style][4]; output = true; } - else if (axislabel and axisunitslabel) + else if (axislabel and axistick) { int adivisor = j < xaxis ? -xdivisor : xdivisor; @@ -423,7 +442,7 @@ namespace graphs output = true; } } - else if (yaxislabel and xaxislabel and axislabel and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0) + else if (yaxislabel and xaxislabel and axislabel and axistick and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0) { cout << "0"; output = true; @@ -433,7 +452,7 @@ namespace graphs cout << "x"; output = true; } - else if (yaxislabel and axislabel and axisunitslabel) + else if (yaxislabel and axislabel and axistick and axisunitslabel) { long double label = 0; int adivisor = j < xaxis ? -xdivisor : xdivisor; @@ -479,7 +498,7 @@ namespace graphs cout << "y"; output = true; } - else if (ylabellength and (xaxis < 2 ? xaxislabel : j < (xaxis - ylabellength) and (j + 2) >= (xaxis - ylabellength)) and (yaxis >= 4 or i < (height - 4)) and axislabel and axisunitslabel) + else if (ylabellength and (xaxis < 2 ? xaxislabel : j < (xaxis - ylabellength) and (j + 2) >= (xaxis - ylabellength)) and (yaxis >= 4 or i < (height - 4)) and axislabel and axistick and axisunitslabel) { cout << ylabelstrm.str(); output = true; @@ -523,10 +542,23 @@ namespace graphs } } - if (i < (height - 4)) + if (border) + cout << styles[style][1]; + + if (i < (height - 4) or border) cout << "\n"; } + if (border) + { + cout << styles[style][8]; + + for (size_t k = 0; k < awidth; ++k) + cout << styles[style][0]; + + cout << styles[style][10]; + } + cout << endl; return 0; @@ -623,7 +655,7 @@ namespace graphs for (size_t i = 0; i < graphs::size(array); ++i) { const auto &x = array[i][0], &y = array[i][1]; - + if (x >= xmin and x < xmax and y >= ymin and y < ymax) { const size_t ax = (x / xstep) + xaxis; diff --git a/python/README.md b/python/README.md index c7981c1..7092479 100644 --- a/python/README.md +++ b/python/README.md @@ -1,3 +1,11 @@ +## Table of Contents +* [Tables](#tables) + * [Usage](#usage) + * [Options](#options) +* [Graphs/Plots](#graphsplots) + * [Usage](#usage-1) + * [Options](#options-1) + ## Tables ### Usage @@ -157,7 +165,7 @@ Header columns are bolded, centered and have a border. #### Table border Option: `tableborder`\ -Default value: `False` +Default value: `True` #### Cell border @@ -183,7 +191,7 @@ Values: Option: `title`\ Default value: `None` -The title is word wrapped based on the current width of the terminal. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). +The title is output at the top of the table. It is word wrapped based on the current width of the terminal. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). #### Border style @@ -346,31 +354,43 @@ Output same as example above. ### Options -#### Border/Axis +#### Border Option: `border`\ Default value: `False` +#### Axis + +Option: `axis`\ +Default value: `True` + #### Axis labels Option: `axislabel`\ -Default value: `False` +Default value: `True` -Requires `border` to be `False`. +Requires `axis` to be `True`. + +#### Axis ticks + +Option: `axistick`\ +Default value: `True` + +Requires `axis` and `axislabel` to be `True`. #### Axis units labels Option: `axisunitslabel`\ -Default value: `False` +Default value: `True` -Requires `border` and `axislabel` to be `False`. +Requires `axis`, `axislabel` and `axistick` to be `True`. #### Title Option: `title`\ Default value: `None` -The title is word wrapped based on the current width of the terminal. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). +The title is output at the top of the graph. It is word wrapped based on the current width of the terminal. Handles newlines, tabs and [Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters). #### Axis/Border style @@ -410,19 +430,21 @@ Values: 3. `color_types.green`: Green 4. `color_types.yellow`: Yellow 5. `color_types.blue`: Blue -6. `color_types.cyan`: Cyan -7. `color_types.dark_gray`: Light gray -8. `color_types.dark_gray`: Dark gray -9. `color_types.light_red`: Light red -10. `color_types.light_green`: Light green -11. `color_types.light_yellow`: Light yellow -12. `color_types.light_blue`: Light blue -13. `color_types.light_cyan`: Light cyan -14. `color_types.white`: White +6. `color_types.magenta`: Magenta +7. `color_types.cyan`: Cyan +8. `color_types.dark_gray`: Light Gray +9. `color_types.dark_gray`: Dark Gray +10. `color_types.light_red`: Light Red +11. `color_types.light_green`: Light Green +12. `color_types.light_yellow`: Light Yellow +13. `color_types.light_blue`: Light Blue +14. `color_types.light_magenta`: Light Magenta +15. `color_types.light_cyan`: Light Cyan +16. `color_types.white`: White See [here](https://misc.flogisoft.com/bash/tip_colors_and_formatting#foreground_text) for examples of the colors. -Only used when plotting a single array and when graphing a single function. When plotting multiple arrays or graphing multiple functions, colors 2 - 14 are used inorder. The system default color is used where the plots cross. +Only used when plotting a single array and when graphing a single function. When plotting multiple arrays or graphing multiple functions, colors 2 - 16 are used inorder. The system default color is used where the plots cross. ##### Plot diff --git a/python/graphs.py b/python/graphs.py index 9e74ebe..fe1a8bd 100644 --- a/python/graphs.py +++ b/python/graphs.py @@ -33,10 +33,10 @@ styles = [ ["─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light ["━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"], # Heavy ["═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"], # Double - ["╌", "╎", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed - ["╍", "╏", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed + ["╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed + ["╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed + # [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border ] -# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border class color_types(IntEnum): @@ -46,6 +46,7 @@ class color_types(IntEnum): green = auto() yellow = auto() blue = auto() + magenta = auto() cyan = auto() light_gray = auto() dark_gray = auto() @@ -53,6 +54,7 @@ class color_types(IntEnum): light_green = auto() light_yellow = auto() light_blue = auto() + light_magenta = auto() light_cyan = auto() white = auto() @@ -174,7 +176,8 @@ def outputfraction(number: float) -> Tuple[int, str]: 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: style_types = style_types.light, title: Optional[str] = None, check: bool = True) -> int: +def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: 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 @@ -187,10 +190,10 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: w = shutil.get_terminal_size() - if check: - aheight = height // 4 - awidth = width // 2 + aheight = height // 4 + awidth = width // 2 + if check: 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) @@ -211,53 +214,62 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: xstep = (xmax - xmin) / width ystep = (ymax - ymin) / height - xaxis = 0 if xmin > 0 else width if xmax < 0 else width - (xmax / xstep) + 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) + xdivisor = 2 * 4 * (width // 160 + 2) + ydivisor = 2 * 4 * (height // 160 + 2) if title: - print(textwrap.fill(title, width=width // 2)) + print(textwrap.fill(title, width=awidth)) strm = "" + if border: + strm += styles[style][2] + + strm += styles[style][0] * awidth + + strm += styles[style][4] + "\n" + i = 0 while i < height: - 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) + 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 ylabelstrm = "" ylabellength = 0 - if border and axislabel and axisunitslabel and yaxis >= 0 and yaxis <= height: + if axis and axislabel and axistick and axisunitslabel and yaxis >= 0 and yaxis <= height: output = False label = 0.0 adivisor = -ydivisor if i < yaxis else ydivisor k = yaxis + adivisor - 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 - ((height if k > height else k) * ystep) + 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 - (height if k > height else k) * ystep output = True k += adivisor - if (output): + if output: ylabellength, ylabelstrm = outputfraction(label) ylabellength *= 2 + if border: + strm += styles[style][1] + j = 0 while j < width: - 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) + 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 - if border: + if axis: if axaxis and ayaxis: strm += styles[style][6] output = True @@ -265,15 +277,15 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if i == 0: strm += styles[style][4] output = True - elif i >= (height - 4): + elif i >= height - 4: strm += styles[style][10] output = True - elif axislabel and axisunitslabel: + elif axislabel and axistick: adivisor = -ydivisor if i < yaxis else ydivisor k = yaxis + adivisor - while (k >= i if i < yaxis else k < (i + 4)) and i >= 4 and not output: - if i <= k and (i + 4) > k: + 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 if xaxis >= 2 else 5] output = True k += adivisor @@ -284,39 +296,38 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: if j == 0: strm += styles[style][2] output = True - elif j >= (width - 2): + elif j >= width - 2: strm += styles[style][4] output = True - elif axislabel and axisunitslabel: + elif axislabel and axistick: adivisor = -xdivisor if j < xaxis else xdivisor k = xaxis + adivisor - while (k >= j if j < xaxis else k < (j + 2)) and j < (width - 4) and not output: - if j <= k and (j + 2) > k: + 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 if yaxis <= - (height - 4) else 9] + height - 4 else 9] output = True k += adivisor if not output: strm += styles[style][0] output = True - elif yaxislabel and xaxislabel and axislabel and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0: + elif yaxislabel and xaxislabel and axislabel and axistick and axisunitslabel and ymin <= 0 and ymax >= 0 and xmin <= 0 and xmax >= 0: strm += "0" output = True - elif (j >= (width - 2) if xaxis <= (width - 2) else j == 0) 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: + elif yaxislabel and axislabel and axistick and axisunitslabel: label = 0.0 adivisor = -xdivisor if j < xaxis else xdivisor if j < xaxis: j += 2 k = xaxis + adivisor - 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 = ((width if k > width else k) - * xstep) + xmin + 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 = (width if k > width else k) * xstep + xmin output = True k += adivisor @@ -329,7 +340,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: length, astrm = outputfraction(label) length *= 2 - if (j >= xaxis or (j + length) < (xaxis - 4)) and (j + length) < (width - 2): + if (j >= xaxis or j + length < xaxis - 4) and j + length < width - 2: strm += astrm if length > 2: @@ -339,10 +350,10 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: output = True else: j += 2 - elif (i == 0 if yaxis >= 4 else i >= (height - 4)) and xaxislabel and axislabel: + elif (i == 0 if yaxis >= 4 else i >= height - 4) and xaxislabel and axislabel: strm += "y" output = True - 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: + 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 axistick and axisunitslabel: strm += ylabelstrm output = True if ylabellength > 2: @@ -374,16 +385,27 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: j += 2 - if i < (height - 4): + if border: + strm += styles[style][1] + + if i < height - 4 or border: strm += "\n" i += 4 + if border: + strm += styles[style][8] + + strm += styles[style][0] * awidth + + strm += styles[style][10] + print(strm) 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: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int: +def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: 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 @@ -432,19 +454,18 @@ def arrays(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: xstep = (xmax - xmin) / width ystep = (ymax - ymin) / height - xaxis = width - (xmax / xstep) + xaxis = width - xmax / xstep yaxis = ymax / ystep aaarray = [[0 for j in range(height)] for i in range(width)] for j, aarray in enumerate(aarrays): - acolor = color + 1 if len(aarrays) == 1 else (j % - (len(colors) - 2)) + 3 + acolor = color + 1 if len(aarrays) == 1 else j % (len(colors) - 2) + 3 for i in aarray: if i[0] >= xmin and i[0] < xmax and i[1] >= ymin and i[1] < ymax: - x = int((i[0] / xstep) + xaxis) - y = int((yaxis - (i[1] / ystep)) - 1) + x = int(i[0] / xstep + xaxis) + y = int(yaxis - i[1] / ystep - 1) if aaarray[x][y]: if aaarray[x][y] != acolor: @@ -452,17 +473,19 @@ 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, axis=axis, axislabel=axislabel, + axistick=axistick, 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: style_types = style_types.light, color: color_types = color_types.red, 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 = False, axis: bool = True, axislabel: bool = True, axistick: 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, axis=axis, axislabel=axislabel, + axistick=axistick, 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: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, check: bool = True) -> int: +def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: 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 not afunctions: return 1 @@ -501,23 +524,23 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym xstep = (xmax - xmin) / width ystep = (ymax - ymin) / height - xaxis = width - (xmax / xstep) + xaxis = width - xmax / xstep yaxis = ymax / ystep xres = 2 array = [[0 for j in range(height)] for i in range(width)] for j, function in enumerate(afunctions): - acolor = color + 1 if len(afunctions) == 1 else (j % - (len(colors) - 2)) + 3 + acolor = color + \ + 1 if len(afunctions) == 1 else j % (len(colors) - 2) + 3 for i in (x / xres for x in range(rows * xres)): - x = (i * xstep) + xmin + x = i * xstep + xmin y = function(x) if x >= xmin and x < xmax and y >= ymin and y < ymax: - ax = int((x / xstep) + xaxis) - ay = int((yaxis - (y / ystep)) - 1) + ax = int(x / xstep + xaxis) + ay = int(yaxis - y / ystep - 1) if array[ax][ay]: if array[ax][ay] != acolor: @@ -525,11 +548,12 @@ 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, axis=axis, axislabel=axislabel, + axistick=axistick, 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: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None) -> int: +def function(height, width, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: 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, axis=axis, axislabel=axislabel, + axistick=axistick, axisunitslabel=axisunitslabel, style=style, color=color, title=title) diff --git a/python/tables.py b/python/tables.py index f984030..1ab3d00 100644 --- a/python/tables.py +++ b/python/tables.py @@ -32,10 +32,10 @@ styles = [ ["─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light ["━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"], # Heavy ["═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"], # Double - ["╌", "╎", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed - ["╍", "╏", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed + ["╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed + ["╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed + # [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border ] -# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border ansi = re.compile(r'\x1B\[(?:[0-9]+(?:;[0-9]+)*)?m') @@ -53,7 +53,8 @@ def strcol(astr: str) -> int: # return len(astr) -def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light, check: bool = True) -> int: +def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, + padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light, check: bool = True) -> int: """Output char array as table""" if not array: return 1 @@ -68,9 +69,9 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = w = shutil.get_terminal_size() if tableborder or cellborder or headerrow or headercolumn: - width += (((2 * padding) + 1) * columns) + (1 if tableborder else -1) + width += (2 * padding + 1) * columns + (1 if tableborder else -1) else: - width += (2 * padding) * columns + width += 2 * padding * columns if check: if width > w.columns: @@ -87,9 +88,9 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = strm += styles[style][2] for j in range(columns): - strm += styles[style][0] * ((2 * padding) + columnwidth[j]) + strm += styles[style][0] * (2 * padding + columnwidth[j]) - if j < (columns - 1): + if j < columns - 1: if cellborder or headerrow or (j == 0 and headercolumn): strm += styles[style][3] else: @@ -130,46 +131,47 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = if tableborder: strm += styles[style][1] - strm += "\n" + if i < rows - 1 or tableborder: + strm += "\n" if tableborder: - if i == (rows - 1): + if i == rows - 1: strm += styles[style][8] elif cellborder or (i == 0 and headerrow) or headercolumn: strm += styles[style][5] - if (i == (rows - 1) and tableborder) or (i < (rows - 1) and cellborder) or (i == 0 and headerrow) or (i < (rows - 1) and headercolumn): + if (i == rows - 1 and tableborder) or (i < rows - 1 and cellborder) or (i == 0 and headerrow) or (i < rows - 1 and headercolumn): for j in range(columns): - if (i == (rows - 1) and tableborder) or (i < (rows - 1) and cellborder) or (i == 0 and headerrow) or (i < (rows - 1) and j == 0 and headercolumn): - strm += styles[style][0] * ((2 * padding) + columnwidth[j]) - elif i < (rows - 1) and headercolumn: - strm += " " * ((2 * padding) + columnwidth[j]) + if (i == rows - 1 and tableborder) or (i < rows - 1 and cellborder) or (i == 0 and headerrow) or (i < rows - 1 and j == 0 and headercolumn): + strm += styles[style][0] * (2 * padding + columnwidth[j]) + elif i < rows - 1 and headercolumn: + strm += " " * (2 * padding + columnwidth[j]) - if j < (columns - 1): - if i == (rows - 1) and tableborder: + if j < columns - 1: + if i == rows - 1 and tableborder: if cellborder or (j == 0 and headercolumn): strm += styles[style][9] else: strm += styles[style][0] - elif (i < (rows - 1) and cellborder) or ((i == 0 and headerrow) and (j == 0 and headercolumn)): + elif (i < rows - 1 and cellborder) or ((i == 0 and headerrow) and (j == 0 and headercolumn)): strm += styles[style][6] elif i == 0 and headerrow: strm += styles[style][9] - elif i < (rows - 1) and headercolumn: + elif i < rows - 1 and headercolumn: if j == 0: strm += styles[style][7] else: strm += " " if tableborder: - if i == (rows - 1): + if i == rows - 1: strm += styles[style][10] elif cellborder or (i == 0 and headerrow): strm += styles[style][7] elif headercolumn: strm += styles[style][1] - if i < (rows - 1): + if i < rows - 1: strm += "\n" print(strm) @@ -177,7 +179,8 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = return 0 -def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = None, aheadercolumn: Optional[Sequence[Any]] = None, headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: +def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = None, aheadercolumn: Optional[Sequence[Any]] = None, headerrow: bool = False, headercolumn: bool = False, + tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: """Convert array to char array and output as table""" if not aarray: return 1 @@ -227,7 +230,8 @@ def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style) -def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: +def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, + cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: """Convert one or more functions to array and output as table""" if not afunctions: return 1 @@ -240,7 +244,7 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla print("xstep must be greater than zero.", file=sys.stderr) return 1 - rows = int(((xmax - xmin) / xstep)) + 1 + rows = int((xmax - xmin) / xstep) + 1 columns = len(afunctions) + 1 aaheaderrow = ["x", "y"] @@ -257,7 +261,7 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla aarray = [[0 for j in range(columns)] for i in range(rows)] for i in range(rows): - aarray[i][0] = (i * xstep) + xmin + aarray[i][0] = i * xstep + xmin aarray[i][1:] = [function(aarray[i][0]) for function in afunctions] @@ -265,7 +269,8 @@ def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Calla cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style) -def function(xmin: float, xmax: float, xstep: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: +def function(xmin: float, xmax: float, xstep: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, + cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int: """Convert single function to array and output as table""" return functions(xmin, xmax, xstep, [afunction], headerrow=headerrow, headercolumn=headercolumn, tableborder=tableborder, cellborder=cellborder, padding=padding, alignment=alignment, title=title, style=style) diff --git a/python/test.py b/python/test.py index 399bc82..ad9a6b5 100644 --- a/python/test.py +++ b/python/test.py @@ -11,9 +11,16 @@ import graphs import tables -def afunction(x): return x + 1 -def function1(x): return 2 * x -def function2(x): return x ** 2 +def afunction(x): + return x + 1 + + +def function1(x): + return 2 * x + + +def function2(x): + return x ** 2 rows = 5 diff --git a/tables.hpp b/tables.hpp index 6c7c0f6..353dfce 100644 --- a/tables.hpp +++ b/tables.hpp @@ -37,10 +37,10 @@ namespace tables {"─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light {"━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"}, // Heavy {"═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"}, // Double - {"╌", "╎", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light Dashed - {"╍", "╏", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"} // Heavy Dashed + {"╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"}, // Light Dashed + {"╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"} // Heavy Dashed + // {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "} // No border }; - // {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}};//No border const regex ansi(R"(\x1B\[(?:[0-9]+(?:;[0-9]+)*)?m)"); @@ -281,7 +281,8 @@ namespace tables if (tableborder) cout << styles[style][1]; - cout << "\n"; + if (i < (rows - 1) or tableborder) + cout << "\n"; if (tableborder) {