JSFiddle - React, Tailwind, and code Playground

by HemalathaThanigaivel

HTML

<div id="pie">

</div>
<!--   <label><input type="radio" name="dataset" value="apples" checked> Apples</label>
  <label><input type="radio" name="dataset" value="oranges"> Oranges</label>
   -->

CSS

svg{
  background: #ddd;
  
}

JavaScript

let data = [{
apples: 53245,
oranges: 200},
{
apples: 28479,
oranges: 200},
{
apples: 19697,
oranges: 200},
{
apples: 24037,
oranges: 200},
{
apples: 40245,
oranges: 200}]

var width = 360,
    height = 250,
    radius = Math.min(width, height) / 2;

var color = d3.scaleOrdinal(d3.schemeCategory10);

var pie = d3.pie()
    .value(function(d) { return d.apples; })
    .sort(null);

var arc = d3.arc()
    .innerRadius(radius - 60)
    .outerRadius(radius - 20);

var svg = d3.select("#pie").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

  var path = svg.datum(data).selectAll("path")
      .data(pie)
    .enter().append("path")
      .attr("fill", function(d, i) { return color(i); })
      .attr("d", arc)
      .each(function(d) { this._current = d; }); // store the initial angles




  function change() {
    var value = this.value;
    clearTimeout(timeout);
    pie.value(function(d) { return d[value]; }); // change the value function
    path = path.data(pie); // compute the new angles
    path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
  }
//});


function arcTween(a) {
  var i = d3.interpolate(this._current, a);
  this._current = i(0);
  return function(t) {
    return arc(i(t));
  };
}