Skip to content

Instantly share code, notes, and snippets.

@arrayjam
Created November 7, 2015 11:17
Show Gist options
  • Save arrayjam/c129f3c04261c5bb8cf3 to your computer and use it in GitHub Desktop.
Save arrayjam/c129f3c04261c5bb8cf3 to your computer and use it in GitHub Desktop.
Brush resize direction
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis text {
font: 11px sans-serif;
}
.axis path {
display: none;
}
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.grid-background {
fill: #ddd;
}
.grid line,
.grid path {
fill: none;
stroke: #fff;
shape-rendering: crispEdges;
}
.grid .minor.tick line {
stroke-opacity: .5;
}
.brush .extent {
stroke: #000;
fill-opacity: .125;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 200, right: 40, bottom: 200, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date(2013, 7, 1), new Date(2013, 7, 15) - 1])
.range([0, width]);
var brush = d3.svg.brush()
.x(x)
.extent([new Date(2013, 7, 2), new Date(2013, 7, 3)])
.on("brushstart", onbrushstart)
.on("brush", onbrush);
var startExtent = [];
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 + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.hours, 12)
.tickSize(-height)
.tickFormat(""))
.selectAll(".tick")
.classed("minor", function(d) { return d.getHours(); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.days)
.tickPadding(0))
.selectAll("text")
.attr("x", 6)
.style("text-anchor", null);
var gBrush = svg.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.event);
gBrush.selectAll("rect")
.attr("height", height);
function onbrushstart() {
startExtent = brush.extent();
}
function onbrush() {
console.log(d3.event)
if (d3.event.type === "brush" && d3.event.mode === "resize") {
var currentExtent = brush.extent();
var side = (+currentExtent[0] !== +startExtent[0]) ? "left" : "right";
console.log(side);
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment