Darksergio

Untitled

Sep 20th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function drawDonutChart(element, percent, width, height,c1, c2,name)
  2. {
  3.              
  4.     var duration   = 2000;
  5.  
  6.     var  color = d3.scale.ordinal()
  7.            .range([c1,c2]);
  8.  
  9.     var dataset = {
  10.           lower: calcPercent(0),
  11.           upper: calcPercent(percent)
  12.         },
  13.         radius = Math.min(width, height) / 2,
  14.         pie = d3.layout.pie().sort(null),
  15.         format = d3.format(".0%");
  16.  
  17.     var arc = d3.svg.arc()
  18.           .innerRadius(radius - radius/4)
  19.           .outerRadius(radius);
  20.  
  21.     var svg = d3.select(element).append("svg")
  22.           .attr("id",name)
  23.           .attr("width", width+5)
  24.           .attr("height", height+5)
  25.           .append("g")
  26.           .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
  27.  
  28.  
  29.     var path = svg.selectAll("path")
  30.           .data(pie(dataset.lower))
  31.           .enter().append("path")
  32.           .attr("fill", function(d,i) { return color(d+i); })
  33.           .attr("d", arc)
  34.           .each(function(d) { this._current = d; }); // store the initial values
  35.  
  36.     var text = svg.append("text")
  37.           .attr("text-anchor", "middle")
  38.           .attr("dy", ".35em")                  
  39.          .attr("fill", function(d) { return color(d); })
  40.           .attr("font-size", 40)
  41.           .attr("font-weight",900);
  42.  
  43.  
  44.       var progress = 0;
  45.       var timeout = setTimeout(function () {
  46.         clearTimeout(timeout);
  47.         path = path.data(pie(dataset.upper)); // update the data
  48.         path.transition().duration(duration).attrTween("d", function (a) {
  49.  
  50.           var i  = d3.interpolate(this._current, a);
  51.           var i2 = d3.interpolate(progress, percent);
  52.           this._current = i(0);
  53.           return function(t) {
  54.             text.text( format(i2(t) / 100) );
  55.             return arc(i(t));
  56.           };
  57.         }); // redraw the arcs
  58.       }, 200);
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment