Created
December 30, 2021 22:26
-
-
Save igolden/a442bf59da59217d8f8768b8ccf0509b to your computer and use it in GitHub Desktop.
Generate a bar graph from a row of tabular data in the terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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