From: Dan Vanderkam Date: Wed, 10 Aug 2011 16:34:00 +0000 (-0400) Subject: separate numberFormatter into two functions X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=052a20923a3404d92baa92499c20ebc00fc05c0b;p=dygraphs.git separate numberFormatter into two functions --- diff --git a/dygraph-tickers.js b/dygraph-tickers.js index a0c8187..87c5ce5 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -165,7 +165,8 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { if (ticks[i].label !== undefined) continue; // Use current label. var tickV = ticks[i].v; var absTickV = Math.abs(tickV); - var label = formatter(tickV, opts, dygraph); + // TODO(danvk): set granularity to something appropriate here. + var label = formatter(tickV, 0, opts, dygraph); if (k_labels.length > 0) { // TODO(danvk): should this be integrated into the axisLabelFormatter? // Round up to an appropriate unit. @@ -197,7 +198,7 @@ Dygraph.dateTicker = function(a, b, pixels, opts, dygraph, vals) { } if (chosen >= 0) { - return Dygraph.getDateAxis(a, b, chosen, opts); + return Dygraph.getDateAxis(a, b, chosen, opts, dygraph); } else { // this can happen if self.width_ is zero. return []; @@ -284,7 +285,7 @@ Dygraph.numDateTicks = function(start_time, end_time, granularity) { } }; -Dygraph.getDateAxis = function(start_time, end_time, granularity, opts) { +Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { var formatter = opts("axisLabelFormatter"); var ticks = []; if (granularity < Dygraph.MONTHLY) { @@ -322,7 +323,9 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts) { start_time = d.getTime(); for (var t = start_time; t <= end_time; t += spacing) { - ticks.push({ v:t, label: formatter(new Date(t), granularity) }); + ticks.push({ v:t, + label: formatter(new Date(t), granularity, opts, dg) + }); } } else { // Display a tick mark on the first of a set of months of each year. @@ -358,7 +361,9 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts) { var date_str = i + "/" + zeropad(1 + months[j]) + "/01"; var t = Dygraph.dateStrToMillis(date_str); if (t < start_time || t > end_time) continue; - ticks.push({ v:t, label: formatter(new Date(t), granularity) }); + ticks.push({ v:t, + label: formatter(new Date(t), granularity, opts, dg) + }); } } } diff --git a/dygraph.js b/dygraph.js index dd857d6..944e5f8 100644 --- a/dygraph.js +++ b/dygraph.js @@ -95,7 +95,7 @@ Dygraph.DEFAULT_HEIGHT = 320; * @param {Dygraph} opts An options view * @param {Dygraph} g The dygraph object */ -Dygraph.numberFormatter = function(x, opts, g) { +Dygraph.numberValueFormatter = function(x, opts, g) { var sigFigs = opts('sigFigs'); if (sigFigs !== null) { @@ -117,6 +117,14 @@ Dygraph.numberFormatter = function(x, opts, g) { }; /** + * variant for use as an axisLabelFormatter. + * @private + */ +Dygraph.numberAxisLabelFormatter = function(x, granularity, opts, g) { + return Dygraph.numberValueFormatter(x, opts, g); +}; + +/** * Convert a JS date (millis since epoch) to YYYY/MM/DD * @param {Number} date The JavaScript date (ms since epoch) * @return {String} A date of the form "YYYY/MM/DD" @@ -242,14 +250,14 @@ Dygraph.DEFAULT_ATTRS = { }, y: { pixelsPerLabel: 30, - valueFormatter: Dygraph.numberFormatter, - axisLabelFormatter: Dygraph.numberFormatter, + valueFormatter: Dygraph.numberValueFormatter, + axisLabelFormatter: Dygraph.numberAxisLabelFormatter, ticker: null // will be set in dygraph-tickers.js }, y2: { pixelsPerLabel: 30, - valueFormatter: Dygraph.numberFormatter, - axisLabelFormatter: Dygraph.numberFormatter, + valueFormatter: Dygraph.numberValueFormatter, + axisLabelFormatter: Dygraph.numberAxisLabelFormatter, ticker: null // will be set in dygraph-tickers.js } } @@ -1357,8 +1365,9 @@ Dygraph.prototype.generateLegendHTML_ = function(x, sel_points) { return html; } - var xvf = this.optionsViewForAxis_('x')('valueFormatter'); - var html = xvf(x) + ":"; + var xOptView = this.optionsViewForAxis_('x'); + var xvf = xOptView('valueFormatter'); + var html = xvf(x, xOptView, this) + ":"; var yOptView = this.optionsViewForAxis_('y'); var fmtFunc = yOptView('valueFormatter'); @@ -2252,9 +2261,9 @@ Dygraph.prototype.detectTypeFromString_ = function(str) { this.attrs_.axes.x.ticker = Dygraph.dateTicker; this.attrs_.axes.x.axisLabelFormatter = Dygraph.dateAxisFormatter; } else { - // TODO(danvk): use Dygraph.numberFormatter here? /** @private (shut up, jsdoc!) */ this.attrs_.xValueParser = function(x) { return parseFloat(x); }; + // TODO(danvk): use Dygraph.numberValueFormatter here? /** @private (shut up, jsdoc!) */ this.attrs_.axes.x.valueFormatter = function(x) { return x; }; this.attrs_.axes.x.ticker = Dygraph.numericTicks; @@ -2493,7 +2502,7 @@ Dygraph.prototype.parseArray_ = function(data) { // Some intelligent defaults for a numeric x-axis. /** @private (shut up, jsdoc!) */ this.attrs_.axes.x.valueFormatter = function(x) { return x; }; - this.attrs_.axes.x.axisLabelFormatter = Dygraph.numberFormatter; + this.attrs_.axes.x.axisLabelFormatter = Dygraph.numberAxisLabelFormatter; this.attrs_.axes.x.ticker = Dygraph.numericTicks; return data; }