npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-graph.js

v1.1.2

Published

A versatile JavaScript library for creating ASCII charts in the terminal and browser console

Downloads

20

Readme

text-graph.js

text-graph.js is a compact and easy-to-use JavaScript/TypeScript library. It lets you create charts in the terminal and browser console. You can make line charts and build a dashboard with multiple charts, all without any extarnal dependencies. It has features for different axis and data scaling methods. Plus, it supports adding colors to your charts.

npm version

Features

  • Create line charts
  • Drawing multiple series on one plot
  • Creating dashboards with multiple charts
  • Colors for chart elements
  • Built-in axis scale functions (i.e. log)
  • Built-in different data overflow handling functions (i.e. linear scale, clapm, etc)
  • Built-in different data compression functions (i.e. mean, max, etc)

Installation

npm install text-graph.js

Run from source

# clone repo
git clone https://github.com/DrA1ex/text-graph.js.git
cd ./text-graph.js

# install dependencies
npm install

# Run example
npx tsx ./examples/dashboard.ts

Get Started

// Importing the Plot class
import {Plot} from 'text-graph.js';

// Creating a new instance of the Plot class with a width of 80 characters and height of 20 characters
const plot = new Plot(80, 20);

// Adding a new series to the plot and storing its ID
const id = plot.addSeries();

// Defining a function that takes a number "x" as input and calculates a mathematical expression
const fn = (x) => Math.pow(Math.sin(x), 3) + Math.pow(Math.cos(x), 3) - 1.5 * Math.sin(x) * Math.cos(x);

// Iterating over a range of values from -2 to just below 2, incrementing by 0.05 at each step
for (let x = -2; x < 2; x += 0.05) {
    // Adding an entry to the series with the given ID, calculated using the function "fn"
    plot.addSeriesEntry(id, fn(x));
}

// Printing the chart output to the console
console.log(plot.paint());

Source code: link

Usage

To use text-graph.js, follow these steps:

  1. Import the necessary classes and enums:
import {
    Plot, LabelPositionFlags, PlotAxisScale, PlotSeriesAggregationFn, PlotSeriesOverflow, Color, BackgroundColor 
} from 'text-graph.js';
  1. Define the plot options:
const plotOptions = {
    showAxis: true,
    title: 'Chart Title',
    horizontalBoundary: 1,
    verticalBoundary: 2,
    titlePosition: LabelPositionFlags.top,
    titleForeground: Color.blue,
    titleBackground: BackgroundColor.black,
    axisLabelsFraction: 4,
    axisScale: PlotAxisScale.linear,
    aggregation: PlotSeriesAggregationFn.mean,
    zoom: false,
}
  1. Create a plot instance:
const width = 60;
const height = 20;
const plot = new Plot(width, height, plotOptions);
  1. Define the series configurations:
const plotSeriesConfig1 = {
    color: Color.cyan,
    overflow: PlotSeriesOverflow.logScale
};

const plotSeriesConfig2 = {
    color: Color.magenta,
    overflow: PlotSeriesOverflow.clamp
};
  1. Add plot series to the plot:
const seriesId1 = plot.addSeries(plotSeriesConfig1);
const seriesId2 = plot.addSeries(plotSeriesConfig2);
  1. Option 1: Iterate over your data and update the plot:
const data1 = [1, 2, 3];
for (const value of data1) {
    plot.addSeriesEntry(seriesId1, value);
}

const data2 = [0, -1, -2];
for (const value of data2) {
    plot.addSeriesEntry(seriesId2, value);
}
  1. Option 2: Populate all data to the plot:
plot.addSeriesRange(seriesId1, [1, 2, 3]);
plot.addSeriesRange(seriesId2, [0, -1, -2]);
  1. Display the plot:
const chartData = plot.paint();
console.log(chartData);

Examples

Single Series Chart

Source code: link

Multi-line Series Chart

Source code: link

Dashboard

Source code: link

Neural network training dashboard

Project: link

Snippets

Fast plot drawing

const data = new Array(200);
for (let i = 0; i < data.length; i++) {
    data[i] = Math.sin(0.3 * i) * Math.cos(0.6 * i) - Math.sin(i * Math.PI / 50) * Math.abs(i - 100) / 30
}

console.log(Plot.plot(data));

Axis scale

const data = new Array(300);
for (let i = 0; i < data.length; i++) {
    data[i] = Math.sin(0.3 * i) * Math.cos(0.6 * i) - Math.sin(i * Math.PI / 50) * Math.abs(i - 100) / 30
}

console.log(Plot.plot(data, {axisScale: PlotAxisScale.log}));

Series data zoom

const data = [1, 2, 3]
console.log(Plot.plot(data, {zoom: true}));

Series scale on overflow

const data = new Array(300);
for (let i = 0; i < data.length; i++) {
    data[i] = Math.sin(0.3 * i) * Math.cos(0.6 * i) - Math.sin(i * Math.PI / 50) * Math.abs(i - 100) / 30
}

console.log(Plot.plot(data, {}, {overflow: PlotSeriesOverflow.logScale}));

Series color

const data = new Array(300);
for (let i = 0; i < data.length; i++) {
    data[i] = Math.sin(0.3 * i) * Math.cos(0.6 * i) - Math.sin(i * Math.PI / 50) * Math.abs(i - 100) / 30
}

console.log(Plot.plot(data, {}, {color: Color.green}));

Different height

const data = new Array(200);
for (let i = 0; i < data.length; i++) {
    data[i] = Math.sin(0.3 * i) * Math.cos(0.6 * i) - Math.sin(i * Math.PI / 50) * Math.abs(i - 100) / 30
}

console.log(Plot.plot(data, {height: 5}));

License

text-graph.js is released under the BSD-3-Clause License.