Skip to content

Instantly share code, notes, and snippets.

@igolden
Created December 30, 2021 22:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Generate a bar graph from a row of tabular data in the terminal
const Canvas = require("./");
const canvas = new Canvas();
c = canvas.getContext("2d");
const ROWDATA = [213, 398, 12, 334, 102, 499, 60, 230, 222];
function draw() {
let cols = 6;
let rows = 10;
c.clearRect(0, 0, canvas.width, canvas.height);
c.save();
for (var i = 0; i < ROWDATA.length; i++) {
let colWidth = canvas.width / ROWDATA.length;
let colPadding = (canvas.width / ROWDATA.length) * parseFloat(0.2);
let xScale = 500;
c.beginPath();
c.fillRect(
colWidth * i + colPadding,
canvas.height,
colWidth - colPadding * 2,
-((xScale - ROWDATA[i]) / xScale) * (canvas.height - 20)
);
c.stroke();
}
c.strokeRect(0, 0, canvas.width - 1, canvas.height);
process.stdout.write(c.toString());
}
draw();
@igolden
Copy link
Author

igolden commented Dec 30, 2021

Messing around with node-drawille and node-drawille-canvas API.

Assuming an array of integers is passed, along with the scale, a bar graph will be generated.

Ref: https://github.com/madbence/node-drawille-canvas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment