Skip to content

Instantly share code, notes, and snippets.

@stormpython
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormpython/d068802b05cd2eb8b1b7 to your computer and use it in GitHub Desktop.
Save stormpython/d068802b05cd2eb8b1b7 to your computer and use it in GitHub Desktop.
Brush code for histogram
brush = d3.svg.brush()
.x(xScale)
.on('brushend', brushend);
if (dispatch.on('brush')) {
svg.append('g')
.attr('class', 'brush')
.call(brush)
.selectAll('rect')
.attr('height', height);
}
function brushend() {
var selected, start, lastVal, end, selectedRange;
selected = xScale.domain().filter(function(d) {
return (brush.extent()[0] <= xScale(d)) && (xScale(d) <= brush.extent()[1]);
});
start = selected[0];
lastVal = selected.length - 1;
end = selected[lastVal];
selectedRange = [start, end];
return brush.extent()[0] instanceof Date ?
dispatch.brush({
range: brush.extent(),
config: args,
e: d3.event,
data: latestData,
}) :
dispatch.brush({
range: selectedRange,
config: args,
e: d3.event,
data: latestData,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment