Repository: jstrace/chart Branch: master Commit: 27bef25d26a3 Files: 7 Total size: 5.0 KB Directory structure: gitextract_qm5nh8ee/ ├── .gitignore ├── History.md ├── Makefile ├── Readme.md ├── examples/ │ └── simple.js ├── index.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules/ ================================================ FILE: History.md ================================================ 1.3.3 / 2016-10-06 ================== * Fix Incorrect dates in history 1.3.2 / 2016-10-06 ================== * Fix size calculation 1.3.1 / 2016-09-09 ================== * allow 0 padding 1.3.0 / 2016-09-09 ================== * add option to set axis character * change default axis character to '.' 1.2.0 / 2016-09-05 ================== * add options to set point characters * update docs * update repo link 1.1.1 / 2014-03-25 ================== * rename 1.1.0 / 2014-02-27 ================== * add excess head clipping for rolling data 1.0.1 / 2014-02-27 ================== * bump array-matrix to try and get npm to work 1.2.0 / 2014-02-26 ================== * add value mapping support 1.1.0 / 2014-02-26 ================== * change to make .sort an option * docs ================================================ FILE: Makefile ================================================ test: @./node_modules/.bin/mocha \ --require should \ --reporter dot \ --bail .PHONY: test ================================================ FILE: Readme.md ================================================ # Chart Ascii bar chart for nodejs. ![](https://dl.dropboxusercontent.com/u/6396913/misc/Screen%20Shot%202014-02-27%20at%208.56.42%20AM.png) ## Installation ``` $ npm install jstrace/chart ``` ## Example When `data` exceeds the available width the data will "roll" to the tail-end of the array. This may become an option in the future, but that's the default behaviour for now ;) ```js var chart = require('chart'); var clear = require('clear'); var data = [1, 2, ...]; clear(); console.log(chart(data, { width: 130, height: 30, pointChar: '█', negativePointChar: '░' })); ``` # License MIT ================================================ FILE: examples/simple.js ================================================ var chart = require('..'); var clear = require('clear'); var data = [ 1, 5, 5, 13, 3, 2, 0, 2, 34, 22, 15, 12, 8, 4, 3, 6, 18, -5, -15, -11, -23, -3, 10, 18, 23, 17, 4, 5, 6, 3, 12, 10, 7, -4, 17, 30, 27, 25, 23, 16, 14, 12, 8, 6, 4, 2 ]; clear(); console.log(chart(data)); ================================================ FILE: index.js ================================================ /** * Module dependencies. */ var matrix = require('array-matrix'); /** * Expose `chart()`. */ module.exports = chart; /** * Return ascii chart of `data`. * * - `width` total chart width [130] * - `height` total chart height [30] * - `padding` edge padding [3] * - `pointChar` character used to plot a point [█] * - `negativePointChar` character used to plot a negative point [░] * - `axisChar` character used to draw axis [.] * * @param {Array} data * @param {Object} [opts] * @return {String} * @api public */ function chart(data, opts) { opts = opts || {}; // options var w = opts.width || 130; var h = opts.height || 30; var pc = opts.pointChar || '█'; var nc = opts.negativePointChar || '░'; var ac = opts.axisChar || '.'; // padding var pad = typeof opts.padding === 'number' ? opts.padding : 3; w -= pad * 2; h -= pad * 2; // setup var out = matrix(w, h); var m = max(data) || 0; var label = Math.abs(m).toString(); var labelw = label.length; var labelp = 1; // chart sizes void of padding etc var ch = h; var cw = w - labelw - labelp; // fill for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { out[y][x] = ' '; } } // y-axis labels for (var i = 0; i < labelw; i++) { out[0][i] = label[i]; } out[h - 1][labelw - labelp] = '0'; // y-axis for (var y = 0; y < h; y++) { out[y][labelw + labelp] = ac; } // x-axis var x = labelw + labelp; while (x < w) { out[h - 1][x++] = ac; out[h - 1][x++] = ' '; } // strip excess from head // so that data may "roll" var space = Math.floor(w / 2) - 1; var excess = Math.max(0, data.length - space); if (excess) data = data.slice(excess); // plot data var x = labelw + labelp + 2; for (var i = 0; i < data.length; i++) { var d = data[i]; var p = d / m; var y = Math.round((h - 2) * p); var c = y < 0 ? nc : pc; if (y < 0) y = -y; while (y--) { out[Math.abs(y - h) - 2][x] = c; } x += 2; } // Return string var str = string(out, h); return pad ? padding(str, pad) : str; } /** * Apply padding. */ function padding(str, n) { var linew = str.split('\n')[0].length; var line = Array(linew).join(' ') + '\n'; // y str = Array(n).join(line) + str; str = str + Array(n).join(line); // x str = str.replace(/^/gm, Array(n).join(' ')); return str; } /** * Convert matrix to a string. */ function string(out) { var buf = []; for (var i = 0; i < out.length; i++) { buf.push(out[i].join('')); } return buf.join('\n'); } /** * Return max in array. */ function max(data) { var n = Math.abs(data[0]); for (var i = 1; i < data.length; i++) { n = Math.abs(data[i]) > n ? Math.abs(data[i]) : n; } return n; } ================================================ FILE: package.json ================================================ { "name": "chart", "version": "1.3.3", "repository": "jstrace/chart", "description": "Ascii bar chart", "keywords": [ "ascii", "chart", "bar", "stats", "data", "cli", "terminal" ], "dependencies": { "array-matrix": "1.0.0" }, "devDependencies": { "mocha": "*", "should": "*", "bytes": "~0.2.1", "clear": "0.0.1" }, "license": "MIT" }