From 83c8a93dc080bc82fc25a664374ba457ff1dcf45 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Fri, 11 Jan 2019 11:32:24 +0100 Subject: [PATCH 1/3] Round Human friendly numbers less than 1000 When human friendly flag is true numbers less than 1k (1000) are not rounded. Ex: n = 123.456789 d = 2 Output: 123.456789 <--- Error Expected: 123.45 --- justgage.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/justgage.js b/justgage.js index 3b6d514..655b0a7 100644 --- a/justgage.js +++ b/justgage.js @@ -1083,20 +1083,19 @@ function cutHex(str) { return (str.charAt(0) == "#") ? str.substring(1, 7) : str; } -/** Human friendly number suffix - From: http://stackoverflow.com/questions/2692323/code-golf-friendly-number-abbreviator */ +/** Human friendly number suffix - @robertsLando */ function humanFriendlyNumber(n, d) { - var p, d2, i, s; - - p = Math.pow; - d2 = p(10, d); - i = 7; - while (i) { - s = p(10, i-- * 3); - if (s <= n) { - n = Math.round(n * d2 / s) / d2 + "KMGTPE" [i]; - } - } - return n; + var d2, i, s; + + d2 = Math.pow(10, d); + s = " KMGTPE"; + i = 0; + + while(n >= 1000 && ++i < s.length) n = n / 1000; + + i = i >= s.length ? s.length - 1 : i; + + return Math.round(n * d2) / d2 + s[i]; } /** Format numbers with commas - From: http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript */ From 66c48c5b1e058bee73dd82095a9caa8b41935e9b Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Sat, 12 Jan 2019 16:38:45 +0100 Subject: [PATCH 2/3] Fix make function work with negative numbers too --- justgage.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/justgage.js b/justgage.js index 655b0a7..7c5ae9e 100644 --- a/justgage.js +++ b/justgage.js @@ -1085,13 +1085,14 @@ function cutHex(str) { /** Human friendly number suffix - @robertsLando */ function humanFriendlyNumber(n, d) { - var d2, i, s; + var d2, i, s, c; d2 = Math.pow(10, d); s = " KMGTPE"; i = 0; + c = 1000; - while(n >= 1000 && ++i < s.length) n = n / 1000; + while((n >= c || n <= -c) && ++i < s.length) n = n / c; i = i >= s.length ? s.length - 1 : i; From 10dc1f93883e0024f8f6d495b53886f3fb4f517b Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Wed, 19 Jun 2019 13:43:49 +0200 Subject: [PATCH 3/3] Fix custom sectors colors --- justgage.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/justgage.js b/justgage.js index 7c5ae9e..a9e2bda 100644 --- a/justgage.js +++ b/justgage.js @@ -214,9 +214,11 @@ JustGage = function(config) { // number of digits after floating point decimals: kvLookup('decimals', config, dataset, 0), - // customSectors : [] of objects - // number of digits after floating point - customSectors: kvLookup('customSectors', config, dataset, []), + // customSectors : object + // custom sectors colors. Expects an object with props + // percents : bool hi/lo are percents values + // ranges : array of objects : {hi, lo, color} + customSectors: kvLookup('customSectors', config, dataset, {}), // formatNumber: boolean // formats numbers with commas where appropriate @@ -1009,9 +1011,10 @@ function kvLookup(key, tablea, tableb, defval, datatype, delimiter) { function getColor(val, pct, col, noGradient, custSec) { var no, inc, colors, percentage, rval, gval, bval, lower, upper, range, rangePct, pctLower, pctUpper, color; - var noGradient = noGradient || custSec.length > 0; + var cust = custSec && custSec.ranges && custSec.ranges.length > 0; + var noGradient = noGradient || cust; - if (custSec.length > 0) { + if (cust) { if (custSec.percents === true) val = pct * 100; for (var i = 0; i < custSec.ranges.length; i++) { if (val >= custSec.ranges[i].lo && val <= custSec.ranges[i].hi) {