From: David Moena Date: Thu, 14 Jun 2012 21:53:20 +0000 (-0400) Subject: added support for labelsKMG2 to handle very small numbers (mili, micro, nano...) X-Git-Tag: v1.0.0~261^2^2~2 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=e11c15fd04a8848a1fe695bfb1892571ff06deb5;p=dygraphs.git added support for labelsKMG2 to handle very small numbers (mili, micro, nano...) --- diff --git a/dygraph-tickers.js b/dygraph-tickers.js index b9815fe..20a7519 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -168,6 +168,7 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { // Add formatted labels to the ticks. var k; var k_labels = []; + var m_labels = []; if (opts("labelsKMB")) { k = 1000; k_labels = [ "K", "M", "B", "T", "Q" ]; @@ -176,6 +177,7 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { if (k) Dygraph.warn("Setting both labelsKMB and labelsKMG2. Pick one!"); k = 1024; k_labels = [ "k", "M", "G", "T", "P", "E" ]; + m_labels = [ "m", "u", "n", "p", "f", "a", "z", "y" ]; } var formatter = opts('axisLabelFormatter'); @@ -199,6 +201,19 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { } } } + if(opts("labelsKMG2")){ + tickV = String(tickV.toExponential()); + if(tickV.split('e-').length === 2 && tickV.split('e-')[1] >= 3){ + if(tickV.split('e-')[1] % 3 > 0) { + label = Dygraph.round_(tickV.split('e-')[0] / + Math.pow(10,(tickV.split('e-')[1] % 3)), + opts('digitsAfterDecimal')); + } else { + label = Number(tickV.split('e-')[0]).toFixed(2); + } + label += m_labels[Math.floor(tickV.split('e-')[1] / 3) - 1]; + } + } ticks[i].label = label; } diff --git a/tests/labelsKMB.html b/tests/labelsKMB.html index 0093b06..1a4d5c9 100644 --- a/tests/labelsKMB.html +++ b/tests/labelsKMB.html @@ -23,6 +23,9 @@

labelsKMG2 with yValueFormatter:

+

labelsKMG2 with very small numbers:

+
+

The curves are exponentials. Zooming in should reveal each of the 'K', 'M', 'B', etc. labels.

@@ -32,9 +35,10 @@ return Math.round(num * shift)/shift; }; - var data = []; + var data = [], smalldata = []; for (var i = 0, n = 1; i < 63; i++, n *= 2) { data.push([i, n]); + smalldata.push([i, Math.pow(10,-n)]); } var suffixes = ['', 'k', 'M', 'G', 'T']; @@ -61,6 +65,11 @@ yValueFormatter: formatValue, labels: ['Base', 'Power'] }); + + var g4 = new Dygraph(document.getElementById("labelsKMG2SmallNumbers"), smalldata, { + labelsKMG2: true, + labels: ['Base', 'Power'] + });