From a25459d2e7bee9a3b60728a398f2fc09404e75fb Mon Sep 17 00:00:00 2001 From: Jyry Suvilehto Date: Thu, 26 Jul 2012 12:26:31 +0000 Subject: [PATCH] crude fix to broken y axis handling in sparklinesPlys --- src/models/sparklinePlus.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/models/sparklinePlus.js b/src/models/sparklinePlus.js index 1fcbab0..ce4df1d 100644 --- a/src/models/sparklinePlus.js +++ b/src/models/sparklinePlus.js @@ -86,11 +86,22 @@ nv.models.sparklinePlus = function() { .attr('transform', function(d) { return 'translate(' + (pos - 6) + ',' + (-margin.top) + ')' }) //.text(xTickFormat(pos)); .text(xTickFormat(Math.round(x.invert(pos)))); //TODO: refactor this line + var f = function(data, x){ + var distance = Math.abs(getX(data[0]) - x) ; + var closestIndex = 0; + for (var i = 0; i < data.length; i++){ + if (Math.abs(getX(data[i]) - x) < distance) { + distance = Math.abs(getX(data[i]) -x); + closestIndex = i; + } + } + return closestIndex; + } hoverY .attr('transform', function(d) { return 'translate(' + (pos + 6) + ',' + (-margin.top) + ')' }) //.text(data[pos] && yTickFormat(data[pos].y)); - .text(yTickFormat(getY(data[Math.round(x.invert(pos))]))); //TODO: refactor this line + .text(yTickFormat(getY(data[f(data, Math.round(x.invert(pos)))]))); //TODO: refactor this line } });