X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-tickers.js;h=457d23c25b13e9673c5c8fa880e74ccd8723ea0f;hb=fa0d7ad80802e2b31d9889936eb6ffd55dab1f43;hp=c80bdf52320f9b7dcb24c22d4819d55ef9299a5c;hpb=eed32275fbd3d81b84467853b1781fa4293f3b85;p=dygraphs.git diff --git a/dygraph-tickers.js b/dygraph-tickers.js index c80bdf5..457d23c 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -62,6 +62,22 @@ /*global Dygraph:false */ "use strict"; +/** @typedef {Array.<{v:number, label:string, label_v:(string|undefined)}>} */ +Dygraph.TickList; + +/** @typedef {function( + * number, + * number, + * number, + * function(string):*, + * Dygraph=, + * Array.= + * ): Dygraph.TickList} + */ +Dygraph.Ticker; + + +/** @type {Dygraph.Ticker} */ Dygraph.numericLinearTicks = function(a, b, pixels, opts, dygraph, vals) { var nonLogscaleOpts = function(opt) { if (opt === 'logscale') return false; @@ -70,8 +86,19 @@ Dygraph.numericLinearTicks = function(a, b, pixels, opts, dygraph, vals) { return Dygraph.numericTicks(a, b, pixels, nonLogscaleOpts, dygraph, vals); }; +/** @type {Dygraph.Ticker} */ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { - var pixels_per_tick = opts('pixelsPerLabel'); + // This masks some numeric issues in older versions of Firefox, + // where 1.0/Math.pow(10,2) != Math.pow(10,-2). + /** @type {function(number,number):number} */ + var pow = function(base, exp) { + if (exp < 0) { + return 1.0 / Math.pow(base, -exp); + } + return Math.pow(base, exp); + }; + + var pixels_per_tick = /** @type{number} */(opts('pixelsPerLabel')); var ticks = []; var i, j, tickV, nTicks; if (vals) { @@ -128,30 +155,26 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { // The first spacing greater than pixelsPerYLabel is what we use. // TODO(danvk): version that works on a log scale. var kmg2 = opts("labelsKMG2"); - var mults; + var mults, base; if (kmg2) { - mults = [1, 2, 4, 8]; + mults = [1, 2, 4, 8, 16]; + base = 16; } else { - mults = [1, 2, 5]; + mults = [1, 2, 5, 10]; + base = 10; } - var scale, low_val, high_val; - for (i = -10; i < 50; i++) { - var base_scale; - if (kmg2) { - base_scale = Math.pow(16, i); - } else { - base_scale = Math.pow(10, i); - } - var spacing = 0; - for (j = 0; j < mults.length; j++) { - scale = base_scale * mults[j]; - low_val = Math.floor(a / scale) * scale; - high_val = Math.ceil(b / scale) * scale; - nTicks = Math.abs(high_val - low_val) / scale; - spacing = pixels / nTicks; - // wish I could break out of both loops at once... - if (spacing > pixels_per_tick) break; - } + + var wanted_ticks = pixels / pixels_per_tick; + var units_per_tick = Math.abs(b - a) / wanted_ticks; + var base_power = Math.floor(Math.log(units_per_tick) / Math.log(base)); + var base_scale = Math.pow(base, base_power); + var scale, low_val, high_val, nTicks, spacing; + for (j = 0; j < mults.length; j++) { + scale = base_scale * mults[j]; + low_val = Math.floor(a / scale) * scale; + high_val = Math.ceil(b / scale) * scale; + nTicks = Math.abs(high_val - low_val) / scale; + spacing = pixels / nTicks; if (spacing > pixels_per_tick) break; } @@ -180,9 +203,12 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { m_labels = [ "m", "u", "n", "p", "f", "a", "z", "y" ]; } - var formatter = opts('axisLabelFormatter'); + k = k || 1; // If neither option is specified. + + var formatter = /**@type{AxisLabelFormatter}*/(opts('axisLabelFormatter')); // Add labels to the ticks. + var digitsAfterDecimal = /** @type{number} */(opts('digitsAfterDecimal')); for (i = 0; i < ticks.length; i++) { if (ticks[i].label !== undefined) continue; // Use current label. tickV = ticks[i].v; @@ -192,11 +218,10 @@ 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 = Math.pow(k, k_labels.length); + 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]; + label = Dygraph.round_(tickV / n, digitsAfterDecimal) + k_labels[j]; break; } } @@ -206,8 +231,8 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { 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] / - Math.pow(10,(tickV.split('e-')[1] % 3)), - opts('digitsAfterDecimal')); + pow(10,(tickV.split('e-')[1] % 3)), + digitsAfterDecimal); } else { label = Number(tickV.split('e-')[0]).toFixed(2); } @@ -221,6 +246,7 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { }; +/** @type {Dygraph.Ticker} */ Dygraph.dateTicker = function(a, b, pixels, opts, dygraph, vals) { var chosen = Dygraph.pickDateTickGranularity(a, b, pixels, opts); @@ -233,6 +259,7 @@ Dygraph.dateTicker = function(a, b, pixels, opts, dygraph, vals) { }; // Time granularity enumeration +// TODO(danvk): make this an @enum Dygraph.SECONDLY = 0; Dygraph.TWO_SECONDLY = 1; Dygraph.FIVE_SECONDLY = 2; @@ -256,6 +283,7 @@ Dygraph.DECADAL = 19; Dygraph.CENTENNIAL = 20; Dygraph.NUM_GRANULARITIES = 21; +/** @type {Array.} */ Dygraph.SHORT_SPACINGS = []; Dygraph.SHORT_SPACINGS[Dygraph.SECONDLY] = 1000 * 1; Dygraph.SHORT_SPACINGS[Dygraph.TWO_SECONDLY] = 1000 * 2; @@ -274,11 +302,11 @@ Dygraph.SHORT_SPACINGS[Dygraph.DAILY] = 1000 * 86400; Dygraph.SHORT_SPACINGS[Dygraph.WEEKLY] = 1000 * 604800; /** - * @private * This is a list of human-friendly values at which to show tick marks on a log * scale. It is k * 10^n, where k=1..9 and n=-39..+39, so: * ..., 1, 2, 3, 4, 5, ..., 9, 10, 20, 30, ..., 90, 100, 200, 300, ... * NOTE: this assumes that Dygraph.LOG_SCALE = 10. + * @type {Array.} */ Dygraph.PREFERRED_LOG_TICK_VALUES = function() { var vals = []; @@ -295,15 +323,16 @@ Dygraph.PREFERRED_LOG_TICK_VALUES = function() { /** * 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. + * @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(string):*} 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'); + var pixels_per_tick = /** @type{number} */(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) { @@ -313,6 +342,12 @@ Dygraph.pickDateTickGranularity = function(a, b, pixels, opts) { return -1; }; +/** + * @param {number} start_time + * @param {number} end_time + * @param {number} granularity (one of the granularities enumerated above) + * @return {number} Number of ticks that would result. + */ Dygraph.numDateTicks = function(start_time, end_time, granularity) { if (granularity < Dygraph.MONTHLY) { // Generate one tick mark for every fixed interval of time. @@ -333,8 +368,17 @@ Dygraph.numDateTicks = function(start_time, end_time, granularity) { } }; +/** + * @param {number} start_time + * @param {number} end_time + * @param {number} granularity (one of the granularities enumerated above) + * @param {function(string):*} opts Function mapping from option name -> value. + * @param {Dygraph=} dg + * @return {!Dygraph.TickList} + */ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { - var formatter = opts("axisLabelFormatter"); + var formatter = /** @type{AxisLabelFormatter} */( + opts("axisLabelFormatter")); var ticks = []; var t; @@ -423,6 +467,6 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { }; // These are set here so that this file can be included after dygraph.js. -Dygraph.DEFAULT_ATTRS.axes.x.ticker = Dygraph.dateTicker; -Dygraph.DEFAULT_ATTRS.axes.y.ticker = Dygraph.numericTicks; -Dygraph.DEFAULT_ATTRS.axes.y2.ticker = Dygraph.numericTicks; +Dygraph.DEFAULT_ATTRS['axes']['x']['ticker'] = Dygraph.dateTicker; +Dygraph.DEFAULT_ATTRS['axes']['y']['ticker'] = Dygraph.numericTicks; +Dygraph.DEFAULT_ATTRS['axes']['y2']['ticker'] = Dygraph.numericTicks;