0.0.3 • Published 4 years ago

string-graph v0.0.3

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

String Graph

Create simple graphs in strings for use in the command line or text documents!

Bar Graph

String graph exports a barGraph function for generating bar graphs from an array of numbers.

Example

const { barGraph } = require("string-graph");

const graph = barGraph([5, 10, 15], {
  yLabels: true,
  yLabelCount: 3
});

/* Output
15     ■
       ■
       ■
       ■
       ■
10   ■ ■
     ■ ■
     ■ ■
     ■ ■
     ■ ■
 5 ■ ■ ■
   ■ ■ ■
   ■ ■ ■
   ■ ■ ■
   ■ ■ ■
*/

Usage

The barGraph accepts two arguments.

The first argument is an array of data to be displayed in the graph. Items of this array can either be numbers or objects. The object form is required to support a data field with the number value of the data point, it can also accept a label field to add a label to the x-axis under the data bar.

The second argument is the options object that is used to cusomize the appearance of the graph. The following fields are supported for this graph:

NameTypeDescriptionDefault
heightnumberDescribes the max character height of the graphThe maximum value in your data or 50 whichever is lower
tokenstringThe character to be used to show data in the graph
lineEndingstringThe string to place at each line break to support multiple systems"\n"
columnWidthnumberThe number of characters wide each bar in the graph should be1
columnSpacingnumberThe number of characters used between each bar in the graph1
yLabelsbooleanWhether or not to show y-axis labelsfalse
yLabelCountnumberHow many y-axis labels to show on the graph. If this number and the height are not divisible it will round and may not be exact.5
unitstringA unit to display next to the y-axis labels''
xLabelWidthnumberThe character width allowed for the x-axis labels, labels longer than this width will wrap to the next line. This will add spacing to the bars in addition to the existing columnSpacing5

Scatter Plot (beta)

String graph exports a scatterPlot function for generating scatter plots from an array of points (x/y number tuples).

Example

const { barGraph } = require("string-graph");

const graph = scatterPlot([
    [200, 12],
    [201, 15],
    [201, 14],
    [202, 14],
    [208, 13]
  ], {
    yLabels: true,
    yLabelCount: 4,
    xLabels: true,
    xLabelCount: 5,
    guides: false
  })

/* Output
15 ┤    +
14 ┤    +  +
13 ┤                         +
12 ┤ +
    -┬--┬--┬--┬--┬--┬--┬--┬--┬-
     200   202   204   206   208
*/

Usage

The scatterPlot function accepts two arguments.

The first argument is an array of data to be displayed in the graph. Items of this array can either be arrays or objects. The object form requires an explicit x and y parameter, whereas the array form assumes the first element is the x value and the second is the y value.

The second argument is the options object that is used to cusomize the appearance of the graph. The following fields are supported for this graph:

NameTypeDescriptionDefault
tokenstringThe character to be used to show data in the graph+
lineEndingstringThe string to place at each line break to support multiple systems"\n"
yLabelsbooleanWhether or not to show y-axis labelsfalse
xLabelsbooleanWhether or not to show x-axis labelsfalse
yLabelCountnumberHow many y-axis labels to show on the graph. If this number and the height are not divisible it will round and may not be exact.5
xLabelCountnumberHow many x-axis labels to show on the graph. If this number and the height are not divisible it will round and may not be exact.5
0.0.4-beta.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago