Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Forked from makoto/index.html
Last active August 29, 2015 14:02
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 bollwyvl/4cecc09cf1047aa27e46 to your computer and use it in GitHub Desktop.
Save bollwyvl/4cecc09cf1047aa27e46 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.border {
color: yellow;
}
.resize path {
fill: #666;
fill-opacity: .8;
stroke: #000;
stroke-width: 1.5px;
}
.e{
fill: red;
}
.w{
fill: green;
}
.axis path, .axis line {
fill: none;
stroke: #000;
/*shape-rendering: crispEdges;*/
}
.brush .extent {
fill-opacity: .125;
/*shape-rendering: crispEdges;*/
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 194, right: 50, bottom: 214, left: 50},
width = 800 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0,60])
.range([0, width]);
var brush = d3.svg.brush()
.x(x)
.extent([20,26]);
brush.on("brush", boundedBrushmove(brush, 1, 10));
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("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
var brushg = svg.append("g")
.attr("class", "brush")
.call(brush)
brushg.selectAll(".resize").append("rect")
.attr("width", '20px')
.classed("border")
brushg.selectAll("rect")
.attr("height", height);
brushg.select(".background")
.on("mousedown.brush", nobrush)
.on("touchstart.brush", nobrush);
function boundedBrushmove(brush, min, max) {
return function(){
var extent = brush.extent(),
diff = extent[1] - extent[0];
if(min && (diff < min)) {
extent[1] = extent[0] + min;
}else if(max && (diff > max)){
extent[1] = extent[0] + max;
}else{
return;
}
brush.extent(extent)(d3.select(this));
}
}
function nobrush(a, b, c) {
console.log('brushcenter')
d3.event.stopPropagation()
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment