Skip to content

Commit

Permalink
Add options for dense display and truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashton Eby committed May 16, 2022
1 parent 27bef25 commit 7eaf789
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function chart(data, opts) {
var pc = opts.pointChar || '█';
var nc = opts.negativePointChar || '░';
var ac = opts.axisChar || '.';
var dense = opts.dense || false;
var truncate = opts.truncate || true;

// padding
var pad = typeof opts.padding === 'number' ? opts.padding : 3;
Expand Down Expand Up @@ -81,9 +83,15 @@ function chart(data, opts) {

// strip excess from head
// so that data may "roll"
var space = Math.floor(w / 2) - 1;
var space = dense ? Math.floor(w) - 1 : Math.floor(w / 2) - 1;
var excess = Math.max(0, data.length - space);
if (excess) data = data.slice(excess);
if (excess) {
if (truncate) {
data = data.slice(excess);
} else {
throw new Error(`Could not fit last ${excess} data points.`);
}
}

// plot data
var x = labelw + labelp + 2;
Expand All @@ -98,7 +106,7 @@ function chart(data, opts) {
out[Math.abs(y - h) - 2][x] = c;
}

x += 2;
x += dense ? 1 : 2;
}

// Return string
Expand Down

0 comments on commit 7eaf789

Please sign in to comment.