Skip to content

Instantly share code, notes, and snippets.

@mauroSolis
Created May 27, 2013 07:28
Show Gist options
  • Save mauroSolis/5655640 to your computer and use it in GitHub Desktop.
Save mauroSolis/5655640 to your computer and use it in GitHub Desktop.
practica zoom & Brush con D3
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Brush</title>
<script type="text/javascript"
src="http://mbostock.github.com/d3/d3.js"></script>
<style type="text/css">
svg { font: 10px sans-serif;shape-rendering: crispEdges; }
rect { fill: #ddd; }
circle { -webkit-transition: fill-opacity 250ms linear; }
.selecting circle { fill-opacity: .2; }
.selecting circle.selected { stroke: #f00; }
.axis path, .axis line {
fill: none; stroke: #fff;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff; fill-opacity: .125;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<script type="text/javascript">
var color = d3.scale.category20();
var xMax = 400,
yMax = 200;
var random = d3.random.normal(.5, .1),
data = d3.range(800).map(function() {
return [random()*xMax, random()*yMax]; });
var margin = {top: 10, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0, xMax])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, yMax])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(-height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width);
var zm = d3.behavior.zoom().x(x).y(y)
.scaleExtent([1,8]).on("zoom", zoom);
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 + ")")
.call(zm);
svg.append("rect")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
draw();
svg.append("g")
.attr("class", "brush")
.call(d3.svg.brush().x(x).y(y)
.on("brushstart", brushstart)
.on("brush", brush)
.on("brushend", brushend));
function brushstart() {
svg.classed("selecting", true);
}
function draw() {
vis=svg.append("g")
.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("transform", function(d) {
return "translate(" + x(d[0]) + "," + y(d[1]) + ")"; })
.attr("r", 3.5);
}
function update() {
svg.selectAll("circle")
.attr("transform", function(d) {
return "translate(" + x(d[0]) + "," + y(d[1]) + ")"; })
.attr("r", zm.scale()*3)
.attr("fill",function(d,i){return color(i);});
}
function brush() {
var e = d3.event.target.extent();
svg.selectAll("circle").classed("selected", function(d) {
return e[0][0] <= d[0] && d[0] <= e[1][0]
&& e[0][1] <= d[1] && d[1] <= e[1][1];
});
}
function brushend() {
svg.classed("selecting", !d3.event.target.empty());
}
function zoom() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
update();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment