diff --git a/README.md b/README.md index ad7847c..fbe207d 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,8 @@ __Parameters__ * `values` - An array of values to go into the sparkline * `suffix` - A suffix to use when drawing the current and max values at the end of sparkline +* `opts` - Allows various options to be passed trough: + * `highlight` - Either "max" (default), "min" or none. Which hilights the biggest, least or none of the values. __Example__ @@ -180,7 +182,7 @@ __Example__ var Sparkline = require('clui').Sparkline; var reqsPerSec = [10,12,3,7,12,9,23,10,9,19,16,18,12,12]; -console.log(Sparkline(reqsPerSec, 'reqs/sec')); +console.log(Sparkline(reqsPerSec, 'reqs/sec', {"highlight": "min"})); ``` diff --git a/lib/clui.js b/lib/clui.js index 1435553..85f031d 100644 --- a/lib/clui.js +++ b/lib/clui.js @@ -118,11 +118,18 @@ var helpers = { }, // Make a unicode sparkline chart - Sparkline: function (points, suffix) { + Sparkline: function (points, suffix, opts) { if(typeof suffix == 'undefined') suffix = ''; + if(typeof opts == 'undefined') + opts = {'highlight': 'max'} + + + + var max = Math.max.apply(Math, points); + var min = Math.min.apply(Math, points); var scaledSequence = points.map(function (thisPoint) { if(max === 0) @@ -139,16 +146,29 @@ var helpers = { var sparklineGraph = ''; var alreadyDrawnMax = false; scaledSequence.forEach(function (symbolNumber) { - if(symbolNumber[1] == max & !alreadyDrawnMax) + if(symbolNumber[1] == max & !alreadyDrawnMax & opts.highlight == 'max') { sparklineGraph += clc.green(sparklineSymbols[symbolNumber[0]]); alreadyDrawnMax = true; } + else if(symbolNumber[1] == min & !alreadyDrawnMax & opts.highlight == 'min') + { + sparklineGraph += clc.red(sparklineSymbols[symbolNumber[0]]); + alreadyDrawnMax = true; + } else sparklineGraph += sparklineSymbols[symbolNumber[0]]; }); - return sparklineGraph + ' ' + clc.blackBright(points[points.length-1] + suffix + ' (') + clc.green(max + suffix) + clc.blackBright(')'); + var statistic = ""; + + if(opts.highlight == "max") + statistic = clc.blackBright(' (') + clc.green(max + suffix) + clc.blackBright(')'); + + if(opts.highlight == "min") + statistic = clc.blackBright(' (') + clc.red(min + suffix) + clc.blackBright(')'); + + return sparklineGraph + ' ' + clc.blackBright(points[points.length-1] + suffix) + statistic; }, // Interface for storing multiple lines and then outputting them all at once.