D3将两张甜甜圈图叠加在一起



我想以某种方式将两个甜甜圈图叠在一起,或者至少只有弧形。我想隐藏一个特定的圆弧,并在单击时显示另一个,然后在再次单击时恢复。

我发现你可以通过选择切片并进行d3.select("the arc").attr("visibility", "hidden"); 来隐藏点击时的弧形

所以我想隐藏一个切片,然后显示另一个。我希望弧占据相同的位置,所以显示另一个似乎只会改变弧。

谢谢,Brian

据我所知,您希望在单击时更新特定的弧。因此,不要创建两个甜甜圈,一个在另一个上面,只需创建一个甜甜圈图表,并在单击弧形时更新它。

 $(document).ready(function() {
   var width = 400,
     height = 250,
     radius = Math.min(width, height) / 2;
   var color = d3.scale.category20();
   var pie = d3.layout.pie()
     .value(function(d) {
       return d.apples;
     })
     .sort(null);
   var arc = d3.svg.arc()
     .innerRadius(radius - 70)
     .outerRadius(radius - 20);
   var svg = d3.select("body").append("svg")
     .attr("width", width)
     .attr("height", height)
     .append("g")
     .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
   var data = [{
     "apples": 53245,
     "oranges": 200
   }, {
     "apples": 28479,
     "oranges": 200
   }, {
     "apples": 19697,
     "oranges": 200
   }, {
     "apples": 24037,
     "oranges": 200
   }];
   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
     .on("click", function(d) {
       var key = d.data.getKeyByValue(d.value);
       var oppKey = (key === "apples") ? "oranges" : "apples";
       change(oppKey);
     });
   function change(keyVal) {
     var value = keyVal;
     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 type(d) {
     d.apples = +d.apples;
     d.oranges = +d.oranges;
     return d;
   }
   // Store the displayed angles in _current.
   // Then, interpolate from _current to the new angles.
   // During the transition, _current is updated in-place by d3.interpolate.
   function arcTween(a) {
     var i = d3.interpolate(this._current, a);
     this._current = i(0);
     return function(t) {
       return arc(i(t));
     };
   }
   Object.prototype.getKeyByValue = function(value) {
     for (var prop in this) {
       if (this.hasOwnProperty(prop)) {
         if (this[prop] === value)
           return prop;
       }
     }
   }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

最新更新