Easily draw function graphs in text mode.
# Using npm
npm install --save cli-graph
# Using yarn
yarn add cli-graph
Or you can check out the browser version, online.
// Dependencies
var CliGraph = require("cli-graph");
// Create a new function graph
var g1 = new CliGraph({
height: 20
, width: 20
, center: { y: 18 }
}).setFunctionX(function (x) {
return x * x / 5;
});
console.log(g1.toString());
// =>
// β²
// β
// β’ β β’
// β
// β
// β’ β β’
// β
// β
// β’ β β’
// β
// β
// β’ β β’
// β
// β’ β β’
// β
// β’ β β’
// β’ β β’
// β’ β β’
// βββββββββββββββββββ’ββ’ββ’βββββββββββββββββΆ
// β
// Another function
var g2 = new CliGraph({
height: 30
, width: 30
, marks: {
hAxis: '-'
, vAxis: '|'
, center: '+'
, point: '.'
}
});
g2.setFunctionX(function (x) {
return x;
});
console.log(g2.toString());
// =>
// β²
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// | .
// ------------------------------.----------------------------βΆ
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
// . |
var circle = new CliGraph({ height: 32, width: 32 }).setFunctionX(function (x) {
return Math.sqrt(200 - x * x);
}).setFunctionX(function (x) {
return -Math.sqrt(200 - x * x);
}).setFunctionY(function (y) {
return Math.sqrt(200 - y * y);
}).setFunctionY(function (y) {
return -Math.sqrt(200 - y * y);
});
console.log(circle.toString());
// =>
// β²
// β
// β’ β’ β’ β’ β’ β’ β’ β’ β’
// β’ β’ β β’ β’
// β’ β’ β β’ β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// βββββ’ββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ’βββΆ
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β β’
// β’ β’ β β’ β’
// β’ β’ β β’ β’
// β’ β’ β’ β’ β’ β’ β’ β’ β’
// β
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. π
- For direct and quick help, you can use Codementor. π
Creates a new CliGraph instance.
Example:
var g = new CliGraph();
- Object
options
: An object containing the following fields: height
(Number): The graph height (default:40
).width
(Number): The graph width (default:60
).aRatio
(Number): The horizontal aspect ratio (default:2
).center
(Object): An object containing:x
(Number): Thex
origin (default:width / 2
)y
(Number): They
origin (default:height / 2
)
marks
(Object): An object containing:hAxis
(String): The character for drawing horizontal axis (default"β"
).vAxis
(String): The character for drawing vertical axis (default "β").center
(String): The character for axis intersection (default"βΌ"
).point
(String): The character for drawing points (default"β’"
).rightArrow
(String): The character for drawing the right arrow (default"βΆ"
).topArrow
(String): The character for drawing the top arrow (default"β²"
).background
(String): The background character (default" "
).
- CliGraph The CliGraph instance.
Adds a point on the x
and y
coordinates.
- Number
x
: Thex
coordinate. - Number
y
: They
coordinate. - String
chr
: The point character (default: the one provided in defaults).
- CliGraph The CliGraph instance.
Checks if on given coordinates there is a point.
- Number
x
: Thex
coordinate. - Number
y
: They
coordinate. - String
chr
: The point character (default: the one provided in defaults).
Stringifies the graph.
- String The stringified graph.
Adds the function on the graph, iterating the x axis.
- Function
foo
: A function that receivesx
as the first parameter and returns they
value. - Number
min
: The minimumx
(default: the lowest possible value). - Number
max
: The maximumx
.(default: the highest possible value). - String
chr
: The point character (default: the one provided in defaults).
- CliGraph The CliGraph instance.
Adds the function on the graph, iterating the y axis.
- Function
foo
: A function that receivesy
as the first parameter and returns thex
value. - Number
min
: The minimumy
(default: the lowest possible value). - Number
max
: The maximumy
.(default: the highest possible value). - String
chr
: The point character (default: the one provided in defaults).
- CliGraph The CliGraph instance.
Have an idea? Found a bug? See how to contribute.
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
-
Starring and sharing the projects you like π
-
βI love books! I will remember you after years if you buy me one. π π
-
βYou can make one-time donations via PayPal. I'll probably buy a
coffeetea. π΅ -
βSet up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
-
BitcoinβYou can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! β€οΈ
If you are using this library in one of your projects, add it in this list. β¨
cli-circle
datanow
ascii-heart