【问题标题】:d3.js arc segment animation issued3.js弧段动画问题
【发布时间】:2013-05-23 13:47:21
【问题描述】:

我正在尝试使用 d3.js 创建动画弧段。我得到了弧线和过渡工作,但是在动画运行时,弧线被扭曲了,我不知道为什么。

这是我目前所拥有的:

jsfiddle

var dataset = {
    apples: [532, 284]
};

var degree = Math.PI/180;

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

var color = d3.scale.category20();

var pie = d3.layout.pie().startAngle(-90*degree).endAngle(90*degree)
    .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);   


window.setInterval(dummyData, 2000);

function dummyData(){
    var num = Math.round(Math.random() * 100);
    var key = Math.floor(Math.random() * dataset.apples.length);

    dataset.apples[key] = num;

    draw();
};

function draw(){     
    svg.selectAll("path")
        .data(pie(dataset.apples))
    .transition()
    .duration(2500)
        .attr("fill", function(d, i) { return color(i); })
        .attr("d", arc);   
}

【问题讨论】:

标签: javascript svg d3.js


【解决方案1】:

正如 Richard 解释的那样,您是在先前计算的 SVG 路径字符串和新计算的字符串之间进行插值 - 这会做一些奇怪的事情 - 而不是在前一个角度和新角度之间进行插值,这就是你想要的。

您需要使用 arc 函数对输入进行插值,并将每个插值映射映射到 SVG 路径字符串。为此,您需要将每个先前的数据存储在某处并使用自定义补间函数,您可以在我之前评论的示例中找到该函数。

1。记住以前的数据(最初):

.each(function(d) { this._current = d; });

2。定义一个自定义补间函数:

function arcTween(a) {
  var i = d3.interpolate(this._current, a);
  this._current = i(0); // Remember previous datum for next time
  return function(t) {
    return arc(i(t));
  };
}

3。使用它:

.attrTween("d", arcTween)

如下所示:http://jsfiddle.net/Qh9X5/18/

【讨论】:

  • 另一个问题是将间隔设置为 2 秒。动画需要 2,5 秒。因此,应该将转换时间修改为低于新数据的到来时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2012-10-30
  • 2020-11-03
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多