From 1c7121562108425c230f3bd169920c8b953f6c3d Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 10 Dec 2014 14:11:16 -0500 Subject: [PATCH] Squashed version of PR #522 --- auto_tests/tests/axis_labels.js | 4 ++-- dygraph-options-reference.js | 12 ++++++++++-- dygraph.js | 1 + plugins/legend.js | 20 +++++++++++--------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/auto_tests/tests/axis_labels.js b/auto_tests/tests/axis_labels.js index 0c1690d..fd29235 100644 --- a/auto_tests/tests/axis_labels.js +++ b/auto_tests/tests/axis_labels.js @@ -295,7 +295,7 @@ AxisLabelsTestCase.prototype.testDateValueFormatter = function () { axes : { x : { pixelsPerLabel: 60, - valueFormatter: function(x, opts, series_name, dg) { + valueFormatter: function(x, opts, series_name, dg, row, col) { assertEquals('number', typeof(x)); assertEquals('function', typeof(opts)); assertEquals('string', typeof(series_name)); @@ -307,7 +307,7 @@ AxisLabelsTestCase.prototype.testDateValueFormatter = function () { } }, y : { - valueFormatter: function(y, opts, series_name, dg) { + valueFormatter: function(y, opts, series_name, dg, row, col) { assertEquals('number', typeof(y)); assertEquals('function', typeof(opts)); assertEquals('string', typeof(series_name)); diff --git a/dygraph-options-reference.js b/dygraph-options-reference.js index cfcdb79..4849134 100644 --- a/dygraph-options-reference.js +++ b/dygraph-options-reference.js @@ -199,8 +199,16 @@ Dygraph.OPTIONS_REFERENCE = // "valueFormatter": { "default": "Depends on the type of your data.", "labels": ["Legend", "Value display/formatting"], - "type": "function(num or millis, opts, dygraph)", - "description": "Function to provide a custom display format for the values displayed on mouseover. This does not affect the values that appear on tick marks next to the axes. To format those, see axisLabelFormatter. This is usually set on a per-axis basis. For date axes, you can call new Date(millis) to get a Date object. opts is a function you can call to access various options (e.g. opts('labelsKMB'))." + "type": "function(num or millis, opts, seriesName, dygraph, row, col)", + "description": "Function to provide a custom display format for the values displayed on mouseover. This does not affect the values that appear on tick marks next to the axes. To format those, see axisLabelFormatter. This is usually set on a per-axis basis. .", + "parameters": [ + ["num_or_millis", "The value to be formatted. This is always a number. For date axes, it's millis since epoch. You can call new Date(millis) to get a Date object."], + ["opts", "This is a function you can call to access various options (e.g. opts('labelsKMB')). It returns per-axis values for the option when available."], + ["seriesName", "The name of the series from which the point came, e.g. 'X', 'Y', 'A', etc."], + ["dygraph", "The dygraph object for which the formatting is being done"], + ["row", "The row of the data from which this point comes. g.getValue(row, 0) will return the x-value for this point."], + ["col", "The column of the data from which this point comes. g.getValue(row, col) will return the original y-value for this point. This can be used to get the full confidence interval for the point, or access un-rolled values for the point."] + ] }, "pixelsPerYLabel": { "default": "", diff --git a/dygraph.js b/dygraph.js index f337385..ea04d3e 100644 --- a/dygraph.js +++ b/dygraph.js @@ -2074,6 +2074,7 @@ Dygraph.prototype.animateSelection_ = function(direction) { Dygraph.prototype.updateSelection_ = function(opt_animFraction) { /*var defaultPrevented = */ this.cascadeEvents_('select', { + selectedRow: this.lastRow_, selectedX: this.lastx_, selectedPoints: this.selPoints_ }); diff --git a/plugins/legend.js b/plugins/legend.js index c9f9d72..f8f768b 100644 --- a/plugins/legend.js +++ b/plugins/legend.js @@ -124,6 +124,7 @@ var escapeHTML = function(str) { legend.prototype.select = function(e) { var xValue = e.selectedX; var points = e.selectedPoints; + var row = e.selectedRow; var legendMode = e.dygraph.getOption('legend'); if (legendMode === 'never') { @@ -154,7 +155,7 @@ legend.prototype.select = function(e) { this.legend_div_.style.top = topLegend + "px"; } - var html = legend.generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_); + var html = legend.generateLegendHTML(e.dygraph, xValue, points, this.one_em_width_, row); this.legend_div_.innerHTML = html; this.legend_div_.style.display = ''; }; @@ -169,7 +170,7 @@ legend.prototype.deselect = function(e) { var oneEmWidth = calculateEmWidthInDiv(this.legend_div_); this.one_em_width_ = oneEmWidth; - var html = legend.generateLegendHTML(e.dygraph, undefined, undefined, oneEmWidth); + var html = legend.generateLegendHTML(e.dygraph, undefined, undefined, oneEmWidth, null); this.legend_div_.innerHTML = html; }; @@ -212,14 +213,15 @@ legend.prototype.destroy = function() { * Generates HTML for the legend which is displayed when hovering over the * chart. If no selected points are specified, a default legend is returned * (this may just be the empty string). - * @param { Number } [x] The x-value of the selected points. - * @param { [Object] } [sel_points] List of selected points for the given - * x-value. Should have properties like 'name', 'yval' and 'canvasy'. - * @param { Number } [oneEmWidth] The pixel width for 1em in the legend. Only - * relevant when displaying a legend with no selection (i.e. {legend: - * 'always'}) and with dashed lines. + * @param {number} x The x-value of the selected points. + * @param {Object} sel_points List of selected points for the given + * x-value. Should have properties like 'name', 'yval' and 'canvasy'. + * @param {number} oneEmWidth The pixel width for 1em in the legend. Only + * relevant when displaying a legend with no selection (i.e. {legend: + * 'always'}) and with dashed lines. + * @param {number} row The selected row index. */ -legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth) { +legend.generateLegendHTML = function(g, x, sel_points, oneEmWidth, row) { // TODO(danvk): deprecate this option in place of {legend: 'never'} if (g.getOption('showLabelsOnHighlight') !== true) return ''; -- 2.7.4