d3.js具有动态更新的可重复使用的饼图

时间:2013-11-26 20:33:46

标签: d3.js

我正在尝试使用动态转换创建一个可重复使用的饼图作为学习任务。我正在使用Chris Viau的d3.js可恢复组件电子书。 我遇到的问题基本上不是更新,而是创建多个饼图。我想知道我是否不理解d3.dispatch是如何工作的,或者我是否已经以馅饼char的工作方式搞砸了。它会创建多个圆圈,而不是使用随机值动态更新单个饼图。

这是我的jsfiddle。

http://jsfiddle.net/seoulbrother/Upcr5/

谢谢!

以下js代码:

d3.edge = {};

d3.edge.donut = function module() {

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

    var color = d3.scale.category20();


   var dispatch = d3.dispatch("customHover");
   function graph(_selection) {
       _selection.each(function(_data) {    
            var pie = d3.layout.pie()
                .value(function(_data) { return _data; })
                .sort(null);

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

            if (!svg){
                var svg = d3.select(this).append("svg")
                    .attr("width", width)
                    .attr("height", height)
                    .append("g")
                    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
            }
            var path = svg.selectAll("path")
                .data(pie)
              .enter().append("path")
                .attr("fill", function(d, i) { return color(i); })
                .attr("d", arc)
                .each(function(d) {this._current = d;} );

            path.transition()
                  .ease("elastic")
                  .duration(750)
                  .attrTween("d", arcTween);              

            function arcTween(a) {
              var i = d3.interpolate(this._current, a);
              this._current = i(0);
              return function(t) {
                return arc(i(t));
              };
            }
        });

    }
    d3.rebind(graph, dispatch, "on");
    return graph;
}

donut = d3.edge.donut();
var data = [1, 2, 3, 4];
var container = d3.select("#viz").datum(data).call(donut);

function update(_data) {
    data = d3.range(~~(Math.random() * 20)).map(function(d, i) {
        return ~~(Math.random() * 100);
    });
    container.datum(data).transition().ease("linear").call(donut);
}

update();
setTimeout( update, 1000);

1 个答案:

答案 0 :(得分:6)

出现多个SVG的主要原因是您没有检查是否已有正确的SVG。您依赖于定义的变量svg,但只有在检查它是否已定义后才定义它。

更好的方法是选择您要查找的元素并检查该选择是否为空:

var svg = d3.select(this).select("svg > g");
if (svg.empty()){ // etc

此外,除了输入选择之外,您还需要处理代码中的更新和退出选择。完成jsfiddle here