X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-tickers.js;h=dada4cdc788aa8d4bd67b9cbdcae186335f9187d;hb=16f00742bcc437c9acb34507d41600d976fd79d9;hp=46fe765d409324b2d2200556ae46f8754b314944;hpb=758a629f806fa73483f730fb343013acd0ace078;p=dygraphs.git diff --git a/dygraph-tickers.js b/dygraph-tickers.js index 46fe765..dada4cd 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -62,7 +62,24 @@ /*global Dygraph:false */ "use strict"; +Dygraph.numericLinearTicks = function(a, b, pixels, opts, dygraph, vals) { + var nonLogscaleOpts = function(opt) { + if (opt === 'logscale') return false; + return opts(opt); + }; + return Dygraph.numericTicks(a, b, pixels, nonLogscaleOpts, dygraph, vals); +}; + Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { + // This masks some numeric issues in older versions of Firefox, + // where 1.0/Math.pow(10,2) != Math.pow(10,-2). + var pow = function(base, exp) { + if (exp < 0) { + return 1.0 / Math.pow(base, -exp); + } + return Math.pow(base, exp); + }; + var pixels_per_tick = opts('pixelsPerLabel'); var ticks = []; var i, j, tickV, nTicks; @@ -130,9 +147,9 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { for (i = -10; i < 50; i++) { var base_scale; if (kmg2) { - base_scale = Math.pow(16, i); + base_scale = pow(16, i); } else { - base_scale = Math.pow(10, i); + base_scale = pow(10, i); } var spacing = 0; for (j = 0; j < mults.length; j++) { @@ -160,14 +177,16 @@ 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" ]; + k_labels = [ "K", "M", "B", "T", "Q" ]; } if (opts("labelsKMG2")) { if (k) Dygraph.warn("Setting both labelsKMB and labelsKMG2. Pick one!"); k = 1024; - k_labels = [ "k", "M", "G", "T" ]; + k_labels = [ "k", "M", "G", "T", "P", "E", "Z", "Y" ]; + m_labels = [ "m", "u", "n", "p", "f", "a", "z", "y" ]; } var formatter = opts('axisLabelFormatter'); @@ -182,8 +201,8 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { if (k_labels.length > 0) { // TODO(danvk): should this be integrated into the axisLabelFormatter? // Round up to an appropriate unit. - var n = k*k*k*k; - for (j = 3; j >= 0; j--, n /= k) { + var n = pow(k, k_labels.length); + for (j = k_labels.length - 1; j >= 0; j--, n /= k) { if (absTickV >= n) { label = Dygraph.round_(tickV / n, opts('digitsAfterDecimal')) + k_labels[j]; @@ -191,6 +210,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 && tickV.split('e-')[1] <= 24){ + if(tickV.split('e-')[1] % 3 > 0) { + label = Dygraph.round_(tickV.split('e-')[0] / + 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; } @@ -199,15 +231,7 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { Dygraph.dateTicker = function(a, b, pixels, opts, dygraph, vals) { - var pixels_per_tick = opts('pixelsPerLabel'); - var chosen = -1; - for (var i = 0; i < Dygraph.NUM_GRANULARITIES; i++) { - var num_ticks = Dygraph.numDateTicks(a, b, i); - if (pixels / num_ticks >= pixels_per_tick) { - chosen = i; - break; - } - } + var chosen = Dygraph.pickDateTickGranularity(a, b, pixels, opts); if (chosen >= 0) { return Dygraph.getDateAxis(a, b, chosen, opts, dygraph); @@ -277,6 +301,27 @@ Dygraph.PREFERRED_LOG_TICK_VALUES = function() { return vals; }(); +/** + * Determine the correct granularity of ticks on a date axis. + * + * @param {Number} a Left edge of the chart (ms) + * @param {Number} b Right edge of the chart (ms) + * @param {Number} pixels Size of the chart in the relevant dimension (width). + * @param {Function} opts Function mapping from option name -> value. + * @return {Number} The appropriate axis granularity for this chart. See the + * enumeration of possible values in dygraph-tickers.js. + */ +Dygraph.pickDateTickGranularity = function(a, b, pixels, opts) { + var pixels_per_tick = opts('pixelsPerLabel'); + for (var i = 0; i < Dygraph.NUM_GRANULARITIES; i++) { + var num_ticks = Dygraph.numDateTicks(a, b, i); + if (pixels / num_ticks >= pixels_per_tick) { + return i; + } + } + return -1; +}; + Dygraph.numDateTicks = function(start_time, end_time, granularity) { if (granularity < Dygraph.MONTHLY) { // Generate one tick mark for every fixed interval of time. @@ -310,6 +355,7 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { // for this granularity. var g = spacing / 1000; var d = new Date(start_time); + d.setMilliseconds(0); var x; if (g <= 60) { // seconds x = d.getSeconds(); d.setSeconds(x - x % g);