Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Last active August 29, 2015 13:58
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 wboykinm/10054222 to your computer and use it in GitHub Desktop.
Save wboykinm/10054222 to your computer and use it in GitHub Desktop.
Coordinating a brush and a donut

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;
}
path.update {
shape-rendering: crispEdges;
}
.brush .extent {
fill:#3300ff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
</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", brushend);
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');
// 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 - 60);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
function brushend() {
var path = svg.selectAll("path")
.data(pie([brush.extent()[1],100 - brush.extent()[1]]))
path.attr("class","update");
path.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc);
path.exit().remove("path");
};
brushend();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment