Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created April 7, 2014 21:35
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 morganherlocker/10062447 to your computer and use it in GitHub Desktop.
Save morganherlocker/10062447 to your computer and use it in GitHub Desktop.
brush pie

Update a donut chart extent based on a dragged brush extent

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis path {
display: none;
}
.axis line {
display: none;
}
.grid-background {
fill: #e2e3e3;
}
.grid line,
.grid path {
display: none;
}
.brush .extent {
fill:#3300ff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
.brush .resize path {
fill: #e2e3e3;
stroke: #3c4745;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 40, bottom: 20, left: 40},
width = 500 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
radius = Math.min(width, height) / 2;
//brush stuff:
var x = d3.scale.linear()
.domain([0,100])
.range([0, width]);
var brush = d3.svg.brush()
.x(x)
.extent([0,40])
.on("brush", brushended);
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 + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")");
var gBrush = svg.append("g")
.attr("class", "brush")
.call(brush);
gBrush.selectAll("rect")
.attr("height", height);
gBrush.selectAll("rect")
.style("pointer-events","none")
.attr("y", -1)
.attr("height", height);
gBrush.selectAll(".resize.e")
.append("image")
.attr("width", 35)
.attr("height",35)
.attr("y",height/2.1)
.attr("x",-17)
.attr("xlink:href",'arrow_right.png');
function brushended() {
//Returns the brush extent at the JS console:
console.log(Math.round(brush.extent()[1]))
path.select('.donutPath')
.data(pie([brush.extent()[1],100 - brush.extent()[1]]))
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc);
}
// end brush stuff
// donut stuff:
var color = d3.scale.category20();
var pie = d3.layout.pie()
.sort(null);
var arc = d3.svg.arc()
.innerRadius(radius - 100)
.outerRadius(radius - 70);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var path = svg.selectAll("path")
.attr("class","donutPath")
.data(pie([brush.extent()[1],100 - brush.extent()[1]]))
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment