Please don't use this version and refer to mbostock's original block: Brush Handles (now in d3v4).
See issue #14.
license: gpl-3.0 |
Please don't use this version and refer to mbostock's original block: Brush Handles (now in d3v4).
See issue #14.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
svg { | |
font: 10px sans-serif; | |
} | |
circle { | |
-webkit-transition: fill-opacity 250ms linear; | |
} | |
.selecting circle { | |
fill-opacity: .2; | |
} | |
.selecting circle.selected { | |
stroke: #f00; | |
} | |
.resize { | |
fill: #666; | |
fill-opacity: .8; | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.brush .extent { | |
fill-opacity: .125; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v4.js"></script> | |
<script> | |
var data = d3.range(800).map(Math.random); | |
var margin = {top: 194, right: 50, bottom: 214, left: 50}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scaleLinear() | |
.range([0, width]); | |
var y = d3.randomNormal(height / 2, height / 8); | |
var brush = d3.brushX() | |
.extent([[0, 0], [width, height]]) | |
.on("start", brushstart) | |
.on("brush", brushmove) | |
.on("end", brushend) | |
.handleSize(height); | |
var arc = d3.arc() | |
.outerRadius(height / 2) | |
.innerRadius(0) | |
.startAngle(0) | |
.endAngle(function(d) { return d.type=="w" ? -Math.PI : Math.PI; }); | |
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.axisBottom().scale(x)); | |
var circle = svg.append("g").selectAll("circle") | |
.data(data) | |
.enter().append("circle") | |
.attr("transform", function(d) { return "translate(" + x(d) + "," + y() + ")"; }) | |
.attr("r", 3.5); | |
var brushg = svg.append("g") | |
.attr("class", "brush") | |
.call(brush); | |
brushg.call(brush.move, [x(0.3), x(0.5)]); | |
brushg.selectAll(".resize") | |
.data([{type:"w"}, {type:"e"}]) | |
.enter() | |
.append("path") | |
.attr("class","resize"); | |
brushstart(); | |
brushmove(); | |
function brushstart() { | |
svg.classed("selecting", true); | |
brushg.selectAll('.resize') | |
.attr('d', arc) | |
} | |
function brushmove() { | |
var p = d3.brushSelection(brushg.node()), | |
s = p.map(x.invert); | |
brushg.selectAll('.resize') | |
.attr('transform', function(d,i){ return 'translate(' + [p[i], height/2] + ')';}); | |
circle.classed("selected", function(d) { return s[0] <= d && d <= s[1]; }); | |
} | |
function brushend() { | |
svg.classed("selecting", d3.event.selection); | |
if (!d3.event.selection) brushg.selectAll('.resize') | |
.attr('d', null) | |
} | |
</script> |