Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix label padding for large numbers #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions asciichart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,49 @@

exports.plot = function (series, cfg = undefined) {

var toFixedTrailing = function(x, trailing = 2) {
if (Math.abs(x) > 1) {
var trailing = trailing || 0,
neg = x < 0,
power = Math.pow(10, trailing),
x = Math.round(x * power),
integral = String((neg ? Math.ceil : Math.floor)(x / power)),
fraction = String((neg ? -x : x) % power),
padding = new Array(Math.max(trailing - fraction.length, 0) + 1).join('0');

return trailing ? integral + '.' + padding + fraction : integral;
} else {
var numDigits = x !== 0 ? Math.ceil(Math.log10(Math.abs(x))) : 0;
var rounded = Math.round(x*Math.pow(10,trailing-numDigits))*Math.pow(10,numDigits-trailing);
return rounded.toFixed(Math.max(trailing-numDigits,0));
}
}


let min = series[0]
let max = series[0]

let paddingLength = 0
for (let i = 1; i < series.length; i++) {
min = Math.min (min, series[i])
max = Math.max (max, series[i])

var numChars = String(toFixedTrailing(series[i])).length
paddingLength = numChars > paddingLength ? numChars : paddingLength
}

let range = Math.abs (max - min)
cfg = (typeof cfg !== 'undefined') ? cfg : {}
let offset = (typeof cfg.offset !== 'undefined') ? cfg.offset : 3
let padding = (typeof cfg.padding !== 'undefined') ? cfg.padding : ' '
let padding = (typeof cfg.padding !== 'undefined') ? cfg.padding : Array(paddingLength).fill(" ")
let height = (typeof cfg.height !== 'undefined') ? cfg.height : range
let ratio = height / range
let min2 = Math.round (min * ratio)
let max2 = Math.round (max * ratio)
let rows = Math.abs (max2 - min2)
let width = series.length + offset
let format = (typeof cfg.format !== 'undefined') ? cfg.format : function (x) {
return (padding + x.toFixed (2)).slice (-padding.length)
return (padding + toFixedTrailing(x)).slice (-padding.length)
}

let result = new Array (rows + 1) // empty space
Expand Down
16 changes: 14 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ console.log (line)
console.log ("configuring / scale to desired height\n")

var config = {
padding: ' ', // padding string for label formatting (can be overrided)
offset: 3, // axis offset from the left (min 2)
height: 10, // any height you want
format: function (x, i) {
Expand All @@ -36,4 +35,17 @@ var s2 = new Array (width)
s2[0] = Math.round (Math.random () * 15)
for (i = 1; i < s2.length; i++)
s2[i] = s2[i - 1] + Math.round (Math.random () * (Math.random () > 0.5 ? 2 : -2))
console.log (asciichart.plot (s2) + "\n")
console.log (asciichart.plot (s2) + "\n")

console.log (line)
console.log ("\ncorrect padding for widely varying label lengths\n")

console.log ("\nrange 8000 - 12000\n")
var s3 = []
for (var i = 0; i < width; i++)
s3[i] = (2000 * Math.cos (i * ((Math.PI * 8) / width))) + 10000 // values range from 9000 to 11000
console.log (asciichart.plot (s3, { height: 10, width: 60 }))

console.log ("\nrange 0.000080 - 0.00012\n")
s3 = s3.map(x => { return x / 100000000 })
console.log (asciichart.plot (s3, { height: 10, width: 60 }))