From 6addd5b79f747a869d3689046b9ce450dfac633a Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Sat, 13 Dec 2014 11:30:54 -0500 Subject: [PATCH] Set `this` to the Dygraph for formatting callbacks. --- auto_tests/tests/axis_labels.js | 30 ++++++++++++++++++++---------- dygraph-tickers.js | 6 +++--- dygraph.js | 2 +- extras/super-annotations.js | 6 +++--- plugins/legend.js | 4 ++-- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/auto_tests/tests/axis_labels.js b/auto_tests/tests/axis_labels.js index a7e6b78..fd29235 100644 --- a/auto_tests/tests/axis_labels.js +++ b/auto_tests/tests/axis_labels.js @@ -243,8 +243,8 @@ AxisLabelsTestCase.prototype.testValueFormatter = function () { var opts = { width: 480, height: 320, - axes : { - x : { + axes: { + x: { valueFormatter: function(x, opts, series_name, dg, row, col) { assertEquals('number', typeof(x)); assertEquals('function', typeof(opts)); @@ -252,10 +252,11 @@ AxisLabelsTestCase.prototype.testValueFormatter = function () { assertEquals('[Dygraph graph]', dg.toString()); assertEquals('number', typeof(row)); assertEquals('number', typeof(col)); + assertEquals(dg, this); return 'x' + x; } }, - y : { + y: { valueFormatter: function(y, opts, series_name, dg, row, col) { assertEquals('number', typeof(y)); assertEquals('function', typeof(opts)); @@ -263,6 +264,7 @@ AxisLabelsTestCase.prototype.testValueFormatter = function () { assertEquals('[Dygraph graph]', dg.toString()); assertEquals('number', typeof(row)); assertEquals('number', typeof(col)); + assertEquals(dg, this); return 'y' + y; } } @@ -300,6 +302,7 @@ AxisLabelsTestCase.prototype.testDateValueFormatter = function () { assertEquals('[Dygraph graph]', dg.toString()); assertEquals('number', typeof(row)); assertEquals('number', typeof(col)); + assertEquals(dg, this); return 'x' + Util.formatDate(x); } }, @@ -311,6 +314,7 @@ AxisLabelsTestCase.prototype.testDateValueFormatter = function () { assertEquals('[Dygraph graph]', dg.toString()); assertEquals('number', typeof(row)); assertEquals('number', typeof(col)); + assertEquals(dg, this); return 'y' + y; } } @@ -343,17 +347,21 @@ AxisLabelsTestCase.prototype.testAxisLabelFormatterPrecedence = function () { axes : { x : { valueFormatter: function(x) { + assertEquals('[Dygraph graph]', this.toString()); return 'xvf' + x; }, axisLabelFormatter: function(x, granularity) { + assertEquals('[Dygraph graph]', this.toString()); return 'x' + x; } }, y : { valueFormatter: function(y) { + assertEquals('[Dygraph graph]', this.toString()); return 'yvf' + y; }, axisLabelFormatter: function(y) { + assertEquals('[Dygraph graph]', this.toString()); return 'y' + y; } } @@ -438,9 +446,11 @@ AxisLabelsTestCase.prototype.testGlobalFormatters = function() { height: 320, labels: ['x', 'y'], valueFormatter: function(x) { + assertEquals('[Dygraph graph]', this); return 'vf' + x; }, axisLabelFormatter: function(x) { + assertEquals('[Dygraph graph]', this); return 'alf' + x; } }; @@ -474,7 +484,7 @@ AxisLabelsTestCase.prototype.testValueFormatterParameters = function() { }; var taggedRecorder = function(tag) { return function() { - calls.push([tag].concat(killFunctions(arguments))); + calls.push([tag].concat([this], killFunctions(arguments))); return ''; } }; @@ -501,17 +511,17 @@ AxisLabelsTestCase.prototype.testValueFormatterParameters = function() { g.setSelection(0); assertEquals([ // num or millis, opts, series, dygraph, row, col - [ 'x', 0, 'fn', 'x', g, 0, 0], - [ 'y', 1, 'fn', 'y1', g, 0, 1], - ['y2', 2, 'fn', 'y2', g, 0, 2] + [ 'x', g, 0, 'fn', 'x', g, 0, 0], + [ 'y', g, 1, 'fn', 'y1', g, 0, 1], + ['y2', g, 2, 'fn', 'y2', g, 0, 2] ], calls); calls = []; g.setSelection(1); assertEquals([ - [ 'x', 1, 'fn', 'x', g, 1, 0], - [ 'y', 3, 'fn', 'y1', g, 1, 1], - ['y2', 4, 'fn', 'y2', g, 1, 2] + [ 'x', g, 1, 'fn', 'x', g, 1, 0], + [ 'y', g, 3, 'fn', 'y1', g, 1, 1], + ['y2', g, 4, 'fn', 'y2', g, 1, 2] ], calls); }; diff --git a/dygraph-tickers.js b/dygraph-tickers.js index 35cd75a..0f6b1ab 100644 --- a/dygraph-tickers.js +++ b/dygraph-tickers.js @@ -198,7 +198,7 @@ Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) { for (i = 0; i < ticks.length; i++) { if (ticks[i].label !== undefined) continue; // Use current label. // TODO(danvk): set granularity to something appropriate here. - ticks[i].label = formatter(ticks[i].v, 0, opts, dygraph); + ticks[i].label = formatter.call(dygraph, ticks[i].v, 0, opts, dygraph); } return ticks; @@ -414,7 +414,7 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { } while (tick_time <= end_time) { ticks.push({ v: tick_time, - label: formatter(tick_date, granularity, opts, dg) + label: formatter.call(dg, tick_date, granularity, opts, dg) }); tick_time += spacing; tick_date = new Date(tick_time); @@ -429,7 +429,7 @@ Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) { if (granularity >= Dygraph.DAILY || accessors.getHours(tick_date) % step === 0) { ticks.push({ v: tick_time, - label: formatter(tick_date, granularity, opts, dg) + label: formatter.call(dg, tick_date, granularity, opts, dg) }); } date_array[datefield] += step; diff --git a/dygraph.js b/dygraph.js index 6ae12c3..5f6e1e1 100644 --- a/dygraph.js +++ b/dygraph.js @@ -190,7 +190,7 @@ Dygraph.numberValueFormatter = function(x, opts) { * @private */ Dygraph.numberAxisLabelFormatter = function(x, granularity, opts) { - return Dygraph.numberValueFormatter(x, opts); + return Dygraph.numberValueFormatter.call(this, x, opts); }; /** diff --git a/extras/super-annotations.js b/extras/super-annotations.js index 7697beb..b8b10c6 100644 --- a/extras/super-annotations.js +++ b/extras/super-annotations.js @@ -317,9 +317,9 @@ annotations.prototype.getTemplateHTML = function(div, a) { var yOptView = g.optionsViewForAxis_('y1'); // TODO: support secondary, too var xvf = g.getOptionForAxis('valueFormatter', 'x'); - var x = xvf(a.xval); - var y = g.getOption('valueFormatter', a.series)( - g.getValue(row, col), yOptView); + var x = xvf.call(g, a.xval); + var y = g.getOption('valueFormatter', a.series).call( + g, g.getValue(row, col), yOptView); var displayAnnotation = this.createPublicAnnotation_(a, {x:x, y:y}); var html = div.innerHTML; diff --git a/plugins/legend.js b/plugins/legend.js index 5c46aee..b54770f 100644 --- a/plugins/legend.js +++ b/plugins/legend.js @@ -254,7 +254,7 @@ legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth, row) { // TODO(danvk): remove this use of a private API var xOptView = g.optionsViewForAxis_('x'); var xvf = xOptView('valueFormatter'); - html = xvf(x, xOptView, labels[0], g, row, 0); + html = xvf.call(g, x, xOptView, labels[0], g, row, 0); if (html !== '') { html += ':'; } @@ -277,7 +277,7 @@ legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth, row) { var series = g.getPropertiesForSeries(pt.name); var yOptView = yOptViews[series.axis - 1]; var fmtFunc = yOptView('valueFormatter'); - var yval = fmtFunc(pt.yval, yOptView, pt.name, g, row, labels.indexOf(pt.name)); + var yval = fmtFunc.call(g, pt.yval, yOptView, pt.name, g, row, labels.indexOf(pt.name)); var cls = (pt.name == highlightSeries) ? " class='highlight'" : ""; -- 2.7.4