Updating tooltip highlight so that the item will only highlight if the

mouse is really close to the chart point. Created a "nearestValueIndex"
function.
master
Robin Hu 11 years ago
parent 96840219ee
commit ef96592694

@ -230,4 +230,22 @@ nv.interactiveBisect = function (values, searchVal, xAccessor) {
return index; return index;
else else
return nextIndex return nextIndex
};
/*
Returns the index in the array "values" that is closest to searchVal.
Only returns an index if searchVal is within some "threshold".
Otherwise, returns null.
*/
nv.nearestValueIndex = function (values, searchVal, threshold) {
"use strict";
var yDistMax = Infinity, indexToHighlight = null;
values.forEach(function(d,i) {
var delta = Math.abs(searchVal - d);
if ( delta < yDistMax && delta < threshold) {
yDistMax = delta;
indexToHighlight = i;
}
});
return indexToHighlight;
}; };

@ -515,15 +515,11 @@ nv.models.cumulativeLineChart = function() {
//Highlight the tooltip entry based on which point the mouse is closest to. //Highlight the tooltip entry based on which point the mouse is closest to.
if (allData.length > 2) { if (allData.length > 2) {
var yValue = chart.yScale().invert(e.mouseY); var yValue = chart.yScale().invert(e.mouseY);
var yDistMax = Infinity, indexToHighlight = null; var domainExtent = Math.abs(chart.yScale().domain()[0] - chart.yScale().domain()[1]);
allData.forEach(function(series,i) { var threshold = 0.03 * domainExtent;
var delta = Math.abs(yValue - series.value); var indexToHighlight = nv.nearestValueIndex(allData.map(function(d){return d.value}),yValue,threshold);
if ( delta < yDistMax) { if (indexToHighlight !== null)
yDistMax = delta; allData[indexToHighlight].highlight = true;
indexToHighlight = i;
}
});
allData[indexToHighlight].highlight = true;
} }
var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex), pointIndex); var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex), pointIndex);

@ -265,15 +265,11 @@ nv.models.lineChart = function() {
//Highlight the tooltip entry based on which point the mouse is closest to. //Highlight the tooltip entry based on which point the mouse is closest to.
if (allData.length > 2) { if (allData.length > 2) {
var yValue = chart.yScale().invert(e.mouseY); var yValue = chart.yScale().invert(e.mouseY);
var yDistMax = Infinity, indexToHighlight = null; var domainExtent = Math.abs(chart.yScale().domain()[0] - chart.yScale().domain()[1]);
allData.forEach(function(series,i) { var threshold = 0.03 * domainExtent;
var delta = Math.abs(yValue - series.value); var indexToHighlight = nv.nearestValueIndex(allData.map(function(d){return d.value}),yValue,threshold);
if ( delta < yDistMax) { if (indexToHighlight !== null)
yDistMax = delta; allData[indexToHighlight].highlight = true;
indexToHighlight = i;
}
});
allData[indexToHighlight].highlight = true;
} }
var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex)); var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex));

@ -98,6 +98,7 @@
border-spacing:0; border-spacing:0;
} }
.nvtooltip table td { .nvtooltip table td {
padding: 2px 9px 2px 0; padding: 2px 9px 2px 0;
vertical-align: middle; vertical-align: middle;
@ -111,6 +112,14 @@
font-weight: bold; font-weight: bold;
} }
.nvtooltip table tr.highlight td {
padding: 1px 9px 1px 0;
border-bottom-style: solid;
border-bottom-width: 1px;
border-top-style: solid;
border-top-width: 1px;
}
.nvtooltip table td.legend-color-guide div { .nvtooltip table td.legend-color-guide div {
width: 8px; width: 8px;
height: 8px; height: 8px;

@ -106,8 +106,8 @@ window.nv.tooltip.* also has various helper methods.
trowEnter.selectAll("td").each(function(p) { trowEnter.selectAll("td").each(function(p) {
if (p.highlight) if (p.highlight)
d3.select(this) d3.select(this)
.style("border-bottom","solid 1px " + p.color) .style("border-bottom-color", p.color)
.style("border-top","solid 1px " + p.color) .style("border-top-color", p.color)
; ;
}); });

Loading…
Cancel
Save