title: D3: How to refresh a chart with new data?
body: <p>I have created a d3 donut chart. Here is my code:</p>
  
  <pre><code>var width = 480;
  var height = 480;
  var radius = Math.min(width, height) / 2;
  var doughnutWidth = 30;
  
  var color = d3.scale.category10();
  
  var arc = d3.svg.arc()
  .outerRadius(radius - 10)
  .innerRadius(radius - 70);
  
  var pie = d3.layout.pie()
  .sort(null)
  .value(function(d) { return d[1]; });
  
  var dataset = settings.dataset;
  console.log(dataset);
  
  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')
  .data(pie(dataset))
  .enter()
  .append('path')
  .attr('d', arc)
  .attr('fill', function(d, i) { 
    return color(d.data[0]);
  })
  </code></pre>
  
  <p>I have a simple form on my web page which displays a dropdown menu with several options. Every time a user changes a value on the form a new dataset is sent to my script (settings.dataset) and the donut is redrawn on the page.</p>
  
  <p>Problem is, some of the values from the previous dataset remain in the DOM. In the console output below, you can see that the second dataset only has two elements. The third one is from the previous dataset. This is messing up the chart, as it is displaying a value that shouldn't be there.</p>
  
  <p><a href="https://i.stack.imgur.com/7gCOd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7gCOd.png" alt="enter image description here"></a></p>
  
  <p>My question: how do I clear the old values? I've read up on <code>.exit()</code> and <code>.remove()</code>, but I can't get my head around these methods.</p>