Update a Donut Chart Arc Based on a Brush Extent

by Nivaldo

CSS

.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;
}

JavaScript

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", brushed);

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",'https://gist.githubusercontent.com/wboykinm/10054222/raw/f2e866ac838436ecb2fd5236ddcc82b1a7c8c54d/arrow_right.png');

function brushed() {
  //Returns the brush extent at the JS console:
  console.log(Math.round(brush.extent()[1]))
  
  var upPath = svg.selectAll('path')
    .data(pie([brush.extent()[1],100 - brush.extent()[1]]));
    
  upPath.exit().remove();  
  
  upPath
      .enter().append("path").attr("class","path");
    
  upPath
    .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()
   ...