Skip to content

Instantly share code, notes, and snippets.

@joshcarr
Forked from mbostock/.block
Last active August 29, 2015 14:23
Show Gist options
  • Save joshcarr/ce19f19d2f04918431ff to your computer and use it in GitHub Desktop.
Save joshcarr/ce19f19d2f04918431ff to your computer and use it in GitHub Desktop.

This brush snaps to day boundaries. As the user brushes, the brush fires brush events, allowing a listener to adjust the brush extent. Compare this approach to using brush transitions on brushend.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis text {
font: 11px sans-serif;
fill: #999;
}
.axis path {
display: none;
}
.axis line {
fill: none;
stroke: #DDD;
shape-rendering: crispEdges;
}
.grid-background {
fill: none;
}
.grid line,
.grid path {
fill: none;
stroke: #DDD;
shape-rendering: crispEdges;
}
.grid .minor.tick line {
stroke-opacity: .5;
}
.brush .extent {
stroke: #DDD;
fill: #DDD;
shape-rendering: crispEdges;
}
g.brush>.resize {
display: none;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 1, right: 1, bottom: 20, left: 1},
width = 960 - margin.left - margin.right,
height = 35 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date('July 31, 2014 06:00:00'), new Date('July 31, 2014 23:59:59')])
.range([0, width]);
var brush = d3.svg.brush()
.x(x)
.extent([new Date('July 31, 2014 06:00:00'), new Date('July 31, 2014 07:00:00')])
.on("brush", brushed);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("rect")
.attr("class", "grid-background")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x grid")
.attr("transform", "translate(0," + height/2 + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.minutes, 60)
.tickSize(-height*2)
.tickFormat(""))
.selectAll(".tick")
.classed("minor", function(d) { console.log(d);return d.getMinutes(); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height/2 + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.hours, 3)
.tickPadding(height/2)
.tickFormat(d3.time.format("%-I %p"))
)
.selectAll("text")
.attr("x", 6)
.style("text-anchor", null);
var gBrush = svg.append("g")
.attr("class", "brush")
.call(brush);
gBrush.selectAll("rect")
.attr("height", height);
function brushed() {
var extent0 = brush.extent(),
extent1;
// console.log(extent0);
// if dragging, preserve the width of the extent
// if (d3.event.mode === "move") {
var d0 = d3.time.hour.floor(extent0[0]),
d1 = d3.time.hour.offset(d0, 1);
extent1 = [d0, d1];
// }
// // otherwise, if resizing, round both dates
// else {
// extent1 = extent0.map(d3.time.hour.round);
// // if empty when rounded, use floor & ceil instead
// if (extent1[0] >= extent1[1]) {
// extent1[0] = d3.time.hour.floor(extent0[0]);
// extent1[1] = d3.time.hour.ceil(extent0[1]);
// }
// }
console.log(extent1[0]);
d3.select(this).call(brush.extent(extent1));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment