2016-08-15 24 views
1

タイトルには次のように書かれています。アニメーションパスを円で反転するにはどうすればいいですか(D3.js)。デフォルトでは、パイはアニメーションで時計回りにレンダリングされます。それをどうやって逆転させるの?D3.js円グラフのアニメーション反対方向

画像の例を参照してください。ここ

enter image description here

enter image description here

JS:

var pie = d3.layout.pie() 
     .sort(null) 
     .startAngle(1 * Math.PI) 
     .endAngle(3 * Math.PI) 
     .value(function (d) { return d.percentage; }); 

     g.append("path") 
      .attr("d", arc) 
      .style("fill", function (d) { return d.data.color; }) 
      .attr({ 
       "fill": function (d) { 
        return d.data.color; 
       } 
      }) 
      .transition() 
      .duration(1000) 
      .attrTween("d", function (d) { 
       var i = d3.interpolate(d.startAngle, d.endAngle); 
       return function (t) { 
        d.endAngle = i(t); 
        return arc(d); 
       } 
      }); 

答えて

1

ただstartAngleendAngleを入れ替える:

.transition() 
.duration(1000) 
.attrTween("d", function (d) { 
    var i = d3.interpolate(d.endAngle, d.startAngle); 
    return function (t) { 
     d.startAngle = i(t); 
     return arc(d); 
    } 
}); 

はスニペットを確認します。

var dataset = { 
 
    apples: [5345, 2879, 1997, 2437, 4045], 
 
}; 
 

 
var width = 460, 
 
    height = 300, 
 
    radius = Math.min(width, height)/2; 
 

 
var color = d3.scale.category20(); 
 

 
var pie = d3.layout.pie() 
 
    .sort(null); 
 

 
var arc = d3.svg.arc() 
 
    .innerRadius(radius - 100) 
 
    .outerRadius(radius - 50); 
 

 
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.apples)) 
 
    .enter().append("path") 
 
    .attr("fill", function(d, i) { return color(i); }) 
 
    .attr("d", arc) 
 
    .transition() 
 
      .duration(1000) 
 
      .attrTween("d", function (d) { 
 
       var i = d3.interpolate(d.endAngle, d.startAngle); 
 
       return function (t) { 
 
        d.startAngle = i(t); 
 
        return arc(d); 
 
       } 
 
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

+1

うわー!私の答えにそのような簡単な解決策。ありがとうございました!決してそれを考えなかった... – maverick

関連する問題