Rebuilding nv.d3.js. Updating nearestValueIndex function to use <= function when doing comparison

master
Robin Hu 11 years ago
parent 02ca4d7238
commit 1488afcc41

@ -353,6 +353,24 @@ 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;
};/* Tooltip rendering model for nvd3 charts. };/* Tooltip rendering model for nvd3 charts.
window.nv.models.tooltip is the updated,new way to render tooltips. window.nv.models.tooltip is the updated,new way to render tooltips.
@ -435,7 +453,7 @@ window.nv.tooltip.* also has various helper methods.
.attr("colspan",3) .attr("colspan",3)
.append("strong") .append("strong")
.classed("x-value",true) .classed("x-value",true)
.text(headerFormatter(d.value)); .html(headerFormatter(d.value));
var tbodyEnter = table.selectAll("tbody") var tbodyEnter = table.selectAll("tbody")
.data([d]) .data([d])
@ -453,16 +471,16 @@ window.nv.tooltip.* also has various helper methods.
.style("background-color", function(p) { return p.color}); .style("background-color", function(p) { return p.color});
trowEnter.append("td") trowEnter.append("td")
.classed("key",true) .classed("key",true)
.text(function(p) {return p.key}); .html(function(p) {return p.key});
trowEnter.append("td") trowEnter.append("td")
.classed("value",true) .classed("value",true)
.text(function(p,i) { return valueFormatter(p.value,i) }); .html(function(p,i) { return valueFormatter(p.value,i) });
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)
; ;
}); });
@ -2640,15 +2658,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);
@ -2784,8 +2798,8 @@ nv.models.cumulativeLineChart = function() {
chart.rescaleY = function(_) { chart.rescaleY = function(_) {
if (!arguments.length) return rescaleY; if (!arguments.length) return rescaleY;
rescaleY = _ rescaleY = _;
return rescaleY; return chart;
}; };
chart.showControls = function(_) { chart.showControls = function(_) {
@ -5648,15 +5662,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));
@ -7533,6 +7543,7 @@ nv.models.multiBar = function() {
, forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
, clipEdge = true , clipEdge = true
, stacked = false , stacked = false
, stackOffset = 'zero' // options include 'silhouette', 'wiggle', 'expand', 'zero', or a custom function
, color = nv.utils.defaultColor() , color = nv.utils.defaultColor()
, hideable = false , hideable = false
, barColor = null // adding the ability to set the color for each rather than the whole group , barColor = null // adding the ability to set the color for each rather than the whole group
@ -7577,7 +7588,7 @@ nv.models.multiBar = function() {
if (stacked) if (stacked)
data = d3.layout.stack() data = d3.layout.stack()
.offset('zero') .offset(stackOffset)
.values(function(d){ return d.values }) .values(function(d){ return d.values })
.y(getY) .y(getY)
(!data.length && hideable ? hideable : data); (!data.length && hideable ? hideable : data);
@ -7918,6 +7929,12 @@ nv.models.multiBar = function() {
return chart; return chart;
}; };
chart.stackOffset = function(_) {
if (!arguments.length) return stackOffset;
stackOffset = _;
return chart;
};
chart.clipEdge = function(_) { chart.clipEdge = function(_) {
if (!arguments.length) return clipEdge; if (!arguments.length) return clipEdge;
clipEdge = _; clipEdge = _;
@ -8367,7 +8384,7 @@ nv.models.multiBarChart = function() {
chart.yAxis = yAxis; chart.yAxis = yAxis;
d3.rebind(chart, multibar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'clipEdge', d3.rebind(chart, multibar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'clipEdge',
'id', 'stacked', 'delay', 'barColor','groupSpacing'); 'id', 'stacked', 'stackOffset', 'delay', 'barColor','groupSpacing');
chart.options = nv.utils.optionsFunc.bind(chart); chart.options = nv.utils.optionsFunc.bind(chart);
@ -9873,7 +9890,6 @@ nv.models.ohlcBar = function() {
.range(yRange || [availableHeight, 0]); .range(yRange || [availableHeight, 0]);
// If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
if (x.domain()[0] === x.domain()[1] || y.domain()[0] === y.domain()[1]) singlePoint = true;
if (x.domain()[0] === x.domain()[1]) if (x.domain()[0] === x.domain()[1])
x.domain()[0] ? x.domain()[0] ?
x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01]) x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
@ -10942,12 +10958,10 @@ nv.models.scatter = function() {
container = d3.select(this); container = d3.select(this);
//add series index to each data point for reference //add series index to each data point for reference
data = data.map(function(series, i) { data.forEach(function(series, i) {
series.values = series.values.map(function(point) { series.values.forEach(function(point) {
point.series = i; point.series = i;
return point;
}); });
return series;
}); });
//------------------------------------------------------------ //------------------------------------------------------------
@ -11049,9 +11063,9 @@ nv.models.scatter = function() {
var pX = getX(point,pointIndex); var pX = getX(point,pointIndex);
var pY = getY(point,pointIndex); var pY = getY(point,pointIndex);
return [x(pX)+ Math.random() * 1e-7, return [x(pX)+ Math.random() * 1e-7,
y(pY)+ Math.random() * 1e-7, y(pY)+ Math.random() * 1e-7,
groupIndex, groupIndex,
pointIndex, point]; //temp hack to add noise untill I think of a better way so there are no duplicates pointIndex, point]; //temp hack to add noise untill I think of a better way so there are no duplicates
}) })
.filter(function(pointArray, pointIndex) { .filter(function(pointArray, pointIndex) {
@ -11119,17 +11133,17 @@ nv.models.scatter = function() {
pointPaths.exit().remove(); pointPaths.exit().remove();
pointPaths pointPaths
.attr('d', function(d) { .attr('d', function(d) {
if (d.data.length === 0) if (d.data.length === 0)
return 'M 0 0' return 'M 0 0'
else else
return 'M' + d.data.join('L') + 'Z'; return 'M' + d.data.join('L') + 'Z';
}); });
var mouseEventCallback = function(d,mDispatch) { var mouseEventCallback = function(d,mDispatch) {
if (needsUpdate) return 0; if (needsUpdate) return 0;
var series = data[d.series]; var series = data[d.series];
if (typeof series === 'undefined') return; if (typeof series === 'undefined') return;
var point = series.values[d.point]; var point = series.values[d.point];
mDispatch({ mDispatch({
@ -11170,7 +11184,7 @@ nv.models.scatter = function() {
.selectAll('.nv-point') .selectAll('.nv-point')
//.data(dataWithPoints) //.data(dataWithPoints)
//.style('pointer-events', 'auto') // recativate events, disabled by css //.style('pointer-events', 'auto') // recativate events, disabled by css
.on('click', function(d,i) { .on('click', function(d,i) {
//nv.log('test', d, i); //nv.log('test', d, i);
if (needsUpdate || !data[d.series]) return 0; //check if this is a dummy point if (needsUpdate || !data[d.series]) return 0; //check if this is a dummy point
var series = data[d.series], var series = data[d.series],
@ -11329,7 +11343,7 @@ nv.models.scatter = function() {
chart.highlightPoint = function(seriesIndex,pointIndex,isHoverOver) { chart.highlightPoint = function(seriesIndex,pointIndex,isHoverOver) {
d3.select(".nv-chart-" + id + " .nv-series-" + seriesIndex + " .nv-point-" + pointIndex) d3.select(".nv-chart-" + id + " .nv-series-" + seriesIndex + " .nv-point-" + pointIndex)
.classed("hover",isHoverOver); .classed("hover",isHoverOver);
}; };
@ -11350,7 +11364,7 @@ nv.models.scatter = function() {
chart.dispatch = dispatch; chart.dispatch = dispatch;
chart.options = nv.utils.optionsFunc.bind(chart); chart.options = nv.utils.optionsFunc.bind(chart);
chart.x = function(_) { chart.x = function(_) {
if (!arguments.length) return getX; if (!arguments.length) return getX;
getX = d3.functor(_); getX = d3.functor(_);

12
nv.d3.min.js vendored

File diff suppressed because one or more lines are too long

@ -242,7 +242,7 @@ nv.nearestValueIndex = function (values, searchVal, threshold) {
var yDistMax = Infinity, indexToHighlight = null; var yDistMax = Infinity, indexToHighlight = null;
values.forEach(function(d,i) { values.forEach(function(d,i) {
var delta = Math.abs(searchVal - d); var delta = Math.abs(searchVal - d);
if ( delta < yDistMax && delta < threshold) { if ( delta <= yDistMax && delta < threshold) {
yDistMax = delta; yDistMax = delta;
indexToHighlight = i; indexToHighlight = i;
} }

@ -80,7 +80,7 @@ window.nv.tooltip.* also has various helper methods.
.attr("colspan",3) .attr("colspan",3)
.append("strong") .append("strong")
.classed("x-value",true) .classed("x-value",true)
.text(headerFormatter(d.value)); .html(headerFormatter(d.value));
var tbodyEnter = table.selectAll("tbody") var tbodyEnter = table.selectAll("tbody")
.data([d]) .data([d])
@ -98,10 +98,10 @@ window.nv.tooltip.* also has various helper methods.
.style("background-color", function(p) { return p.color}); .style("background-color", function(p) { return p.color});
trowEnter.append("td") trowEnter.append("td")
.classed("key",true) .classed("key",true)
.text(function(p) {return p.key}); .html(function(p) {return p.key});
trowEnter.append("td") trowEnter.append("td")
.classed("value",true) .classed("value",true)
.text(function(p,i) { return valueFormatter(p.value,i) }); .html(function(p,i) { return valueFormatter(p.value,i) });
trowEnter.selectAll("td").each(function(p) { trowEnter.selectAll("td").each(function(p) {
if (p.highlight) if (p.highlight)

Loading…
Cancel
Save