Donut Chart with Transition

by Rishabh Sharma

HTML

<script src="https://d3js.org/d3.v3.min.js"></script>
<div id="chart"></div>

JavaScript

var newData = {
	"data-1": 20,
  "data-2": 40,
  "data-3": 40
}

var chart = donut()
              .$el(d3.select("#chart"))
              .data(newData)
              .render();

// CHART CODE BELOW

function donut(){  
  // Default settings
  var $el = d3.select("body")
  var data = {};
  // var showTitle = true;
  var width = 480,
      height = 200,
      radius = Math.min(width, height) / 2;

  var currentVal;
  var color = d3.scale.category20();
  var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.value; });

  var svg, g, arc; 


  var object = {};

  // Method for render/refresh graph
  object.render = function(){
    if(!svg){
      arc = d3.svg.arc()
      .outerRadius(radius)
      .innerRadius(radius - (radius/2.5));

      svg = $el.append("svg")
        .attr("width", width)
        .attr("height", height)
      .append("g")
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

      g = svg.selectAll(".arc")
        .data(pie(d3.entries(data)))
      .enter().append("g")
      .attr("class", "arc");

      g.append("path")
        // Attach current value to g so that we can use it for animation
        .each(function(d) { this._current = d; })
        .attr("d", arc)
        .style("fill", function(d) { return color(d.data.key); });
      g.append("text")
          .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
          .attr("dy", ".35em")
          .style("text-anchor", "middle");
      g.select("text").text(function(d) { return d.data.key; });

      svg.append("text")
          .datum(data)
          .attr("x", 0 )
          .attr("y", 0 + radius/10 )
          .attr("class", "text-tooltip")        
          .style("text-anchor", "middle")
          .attr("font-weight", "bold")
          .style("font-size", radius/2.5+"px");

      g.on("mouseover", function(obj){
        console.log(obj)
        svg.select("text.text-tooltip")
        .attr("fill",...