d3重构饼图动画失败 [英] d3 refactored pie chart animations failing

查看:273
本文介绍了d3重构饼图动画失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个早期的饼图,在动画方面非常好。
https://jsfiddle.net/tk5xog0g/29/

I have an earlier pie chart that worked very well in terms of animations. https://jsfiddle.net/tk5xog0g/29/

我已经重建了图表,并增强了它接近我需要 - 但我尝试和添加回来的动画不工作 - 我不知道如果它是因为弧和切片路径现在在不同的g元素等。

I have rebuilt the chart and enhanced it close to what I need - but the animations I try and add back in do not work -- I am not sure if its because the arcs and slice paths are now in different g elements etc..

http:/ /jsfiddle.net/NYEaX/1507/

这是旧派动画,效果非常好

this is the old pie animations and worked very well

                /* ------- ANIMATE PIE SLICES -------*/
                var slice = doughpie.select(".slices").selectAll("path.slice")
                  .data(pie(data), key);

                slice.enter()
                  .insert("path")
                  .style("fill", function(d) {
                    return color(d.data.label);
                  })
                  .style("transform", function(d, i){
                    //return "translate(0, 0)";
                  })
                  .attr("class", "slice");

                slice
                  .transition().duration(1000)
                  .attrTween("d", function(d) {
                    this._current = this._current || d;
                    var interpolate = d3.interpolate(this._current, d);
                    this._current = interpolate(0);
                    return function(t) {
                      return arc(interpolate(t));
                    };
                  })

                slice.exit()
                  .remove();
                /* ------- ANIMATE PIE SLICES -------*/

//这是当前的饼圆 - 但是当我尝试以相同的方式对饼进行动画时,它会失败。

//this is the current pie arcs - but when I try and animate the pie in the same manner - it fails.

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

g.append("path")
  .attr("d", arc)
  .style("fill", function(d) {
    return color(d.data.label);
  });

arc
  .outerRadius(radius - 10)
  .innerRadius(0);


推荐答案

randomize但动画存在。

randomize is not working. But animations are present.

var width = 800,
  height = 600,
  radius = Math.min(width, height) / 2;

var color = d3.scale.ordinal()
  .range(["#46a2de", "#7b3cc9", "#31d89c", "#de5942", "#ffa618"]);

var arc = d3.svg.arc()
  .outerRadius(radius - 66)
  .innerRadius(radius - 70);

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

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



function randomData() {
  var data1 = [{
    "label": "AA",
    "value": 0.911035425558026,
    "children": [{
      name: "some text aa",
      group: "AA",
      size: 120
    }]
  }, {
    "label": "BB",
    "value": 0.08175111844879179,
    "children": [{
      name: "some text bb",
      group: "BB",
      size: 123
    }]
  }, {
    "label": "CC",
    "value": 0.25262439557273275,
    "children": [{
      name: "some text cc",
      group: "CC",
      size: 193
    }]
  }, {
    "label": "DD",
    "value": 0.8301366989535612,
    "children": [{
      name: "some text dd",
      group: "DD",
      size: 29
    }, {
      name: "some text dd",
      group: "DD",
      size: 289
    }]
  }, {
    "label": "EE",
    "value": 0.0517762265780517,
    "children": [{
      name: "some text ee",
      group: "EE",
      size: 389
    }, {
      name: "some text ee",
      group: "EE",
      size: 89
    }]
  }];

  var data2 = [{
    "label": "AA",
    "value": 0.243879179,
    "children": [{
      name: "some text aa",
      group: "AA",
      size: 12320
    }]
  }, {
    "label": "BB",
    "value": 0.243879179,
    "children": [{
      name: "some text bb",
      group: "BB",
      size: 1123
    }]
  }, {
    "label": "CC",
    "value": 0.2342439557273275,
    "children": [{
      name: "some text cc",
      group: "CC",
      size: 1923
    }]
  }, {
    "label": "DD",
    "value": 0.2349535612,
    "children": [{
      name: "some text dd",
      group: "DD",
      size: 29
    }, {
      name: "some text dd",
      group: "DD",
      size: 289
    }]
  }, {
    "label": "EE",
    "value": 0.2345780517,
    "children": [{
      name: "some text ee",
      group: "EE",
      size: 389
    }, {
      name: "some text ee",
      group: "EE",
      size: 89
    }]
  }];

  var j = Math.floor((Math.random() * 2) + 1);

  if (j == 1) {
    return data1;
  } else {
    return data2;
  }
}




//change(randomData());
d3.select(".randomize")
  .on("click", function() {
    var data = randomData()
    console.log("data", data); change(randomData());
  });


change(randomData());

function change(data){


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

g.append("path")
  .attr("d", arc)
  .transition()
        .duration(1000)
  .style("fill", function(d) {
    return color(d.data.label);
  });

arc
  .outerRadius(radius - 10)
  .innerRadius(0);


//create zone regions
var zones = [];
g.append("circle")
  .attr("transform", function(d) {
    zones[d.data.label] = arc.centroid(d);
    return "translate(" + arc.centroid(d) + ")";
  })
  .attr("r", "1px")
  .style("fill", function(d) {
    return "black" //color(d.data.group); 
  });

g.append("g")
  .attr("class", function(d, i) {
    return "bubble" + i;
  }).transition()
        .duration(1500)
  .attr("transform", function(d) {
    zones[d.data.label] = arc.centroid(d);
    return "translate(" + arc.centroid(d) + ")";
  });
//create zone regions

//loop through data and for EACH children array paint dots.
$.each(data, function(index, value) {
  setBubbleChart(160, index, value.children);
});
//custom bubble chart    




}


//custom bubble chart
function bubbledata(data) {
  return $.extend(true, {}, {
    "children": data
  }); // return deep clone
}

function setBubbleChart(width, index, data) {
  //_create bubble
  var diameter = width / 2; //take half/width

  var bubs = svg.select(".bubble" + index).append("g")
    .attr("class", "bubs");

  bubs.attr("transform", "translate(" + -diameter / 2 + "," + -diameter / 2 + ")");

  var bubble = d3.layout.pack()
    .size([diameter, diameter])
    .value(function(d) {
      return d.size;
    })
    .padding(3);

  //_create bubble
  var data = bubbledata(data);

  var nodes = bubble.nodes(data)
    .filter(function(d) {
      return !d.children;
    }); // filter out the outer bubble

  var bubbles = bubs.selectAll('circle')
    .data(nodes);

  bubbles.enter()
    .insert("circle")
    .attr('transform', function(d) {
      return 'translate(' + d.x + ',' + d.y + ')';
    })
    .attr('r', function(d) {
      return d.r;
    })
    .style("fill", function(d) {
      return color(d.group);
    });


  bubbles.enter()
    .insert("text")
    .attr('transform', function(d) {
      return 'translate(' + d.x + ',' + d.y + ')';
    })
    .attr("class", "bubbletext")
    .attr("dy", ".3em")
    .style("text-anchor", "middle")
    .text(function(d) {
      if (d.r > 15) {
        return (Math.floor(d.value / d.parent.value * 100)) + "%";
      }
    });

  bubbles = bubbles.transition()
    .transition()
    .duration(250)
    .attr('transform', function(d) {
      return 'translate(' + d.x + ',' + d.y + ')';
    })
    .attr('r', function(d) {
      return d.r;
    })
    .ease('sine');
}


function type(d) {
  d.value = +d.value;
  return d;
}

body {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  width: 560px;
  height: 500px;
  position: relative;
  background: #292527;
}

path.slice {
  stroke-width: 2px;
}

.bubbletext {
  fill: #ffffff;
  font-size: 14px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<button class="randomize">randomize</button>

这篇关于d3重构饼图动画失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆