【问题标题】:D3 Multiple Pie Chart UpdatesD3 多个饼图更新
【发布时间】:2016-12-15 15:49:42
【问题描述】:

我对 D3 很陌生,但一直在研究一些 mbostocks 示例,但在尝试更新多个饼图时遇到了问题。我可以从我的数据数组中生成这些罚款,但是当我想更新它们时,我遇到了问题。

这个问题很简单,但我对如何解决这个问题有点困惑。我已经在 js fiddle 中运行了我的代码,它可以是 found here。您会看到,在我的示例中,我构建了三个饼图,然后等待 3 秒并将它们更新为新数据。我遇到的问题是所有馅饼似乎总是使用相同的数据进行更新。

我相信这是由于我选择路径以更新饼图的方式。看起来我每次都用每个数据数组更新所有路径,所以它们最终都会用我数组中的最后一个数据集更新。

如果有人知道我如何更新它以正确构建馅饼,我将非常感谢任何帮助、指针或 cmets。

var data = [
  [3, 4, 5, 9],
  [1, 7, 3, 4],
  [4, 3, 2, 1],
];


function getData() {
    // Generate some random data to update the pie with
    tdata = []
    for(i in data) {
        rdata = []
        for(c in data[i]) {
            rdata.push(Math.floor((Math.random() * 10) + 1) )
        }
        tdata.push(rdata)
    }
    return tdata
}

// ------------

var m = 10,
    r = 100

var mycolors = ["red","#FF7F00","#F5CC11","#D61687","#1E93C1","#64B72D","#999999"]

var arc = d3.svg.arc()
            .innerRadius(r / 2)    
            .outerRadius(r)

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

var svg = d3.select("body").selectAll("svg")
    .data(data)
    .enter()
        .append("svg")
        .attr("width", (r + m) * 2)
        .attr("height", (r + m) * 2)
        .attr("id", function(d,i) {return 'pie'+i;})
        .append("svg:g")
            .attr("transform", "translate(" + (r + m) + "," + (r + m) + ")");

var path = svg.selectAll("path")
    .data(pie)
    .enter()
        .append("svg:path")
        .attr("d", arc)
        .style("fill", function(d, i) { return mycolors[i]; })
        .each(function(d) { this._current = d; }); // store the initial angles

var titles = svg.append("svg:text") 
    .attr("class", "title") 
    .text(function(d,i) {return i;}) 
    .attr("dy", "5px")  
    .attr("text-anchor", "middle") 


// -- Do the updates
//------------------------
setInterval(function() {
  change()
}, 3000);


function change() {
    // Update the Pie charts with random data
    piedata = getData()
    svg.each(function(d,i) {
        path = path.data(pie(piedata[i]))
        path.transition().duration(1000).attrTween("d", arcTween); 
        })
    // temp, print new array to screen
    tdata = ""
    for(x in piedata) {
        tdata += "<strong>"+x+":</strong> "+piedata[x]+"<br>"
    }
    $('#pieData').html(tdata)
}

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

【问题讨论】:

    标签: d3.js pie-chart


    【解决方案1】:

    好吧,我终于得到了这个工作,并发布了工作解决方案,以防其他人尝试做同样的事情。

    我希望这可能不是最好的也不是最有效的方法,但这对于我需要的东西来说是很好的(在这一点上)。但如果有人仍有更​​好的解决方案,很高兴收到您的来信。

    我最终根据我为创建的各个 SVG 元素提供的唯一 ID 选择了路径,然后只更新了这些路径。当我这样说时,现在听起来很简单,但确实让我难倒了一段时间。

    function change() {
        // Update the Pie charts with random data
        var newdata = getData()
        for(x in newdata) {
            var npath = d3.select("#pie"+x).selectAll("path").data(pie(newdata[x]))
            npath.transition().duration(1000).attrTween("d", arcTween); // redraw the arcs
        }
    }
    

    完整的工作副本可以在http://jsfiddle.net/THT75/nskwwbnf/找到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-08
      • 2018-06-14
      • 1970-01-01
      • 2014-08-17
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多