【问题标题】:Staggered transition in multi line graph多线图中的交错过渡
【发布时间】:2014-01-15 10:45:25
【问题描述】:

我是新来的,一般来说是 d3.js/JavaScript。我想向多线图添加过渡,以便每条线(和相关标签)一个接一个地“绘制”。我已经设法让它适用于第一行(在较小程度上同时适用于所有行),但我正在努力了解如何错开过渡。我已经尝试使用for 循环和.each() 通过函数调用转换,但是这两种方法都没有真正实现。任何帮助将不胜感激。下面代码的相关部分。谢谢。

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

var path = country.append("path")
    .attr("class", "line")
    .attr("d", function(d) { return line(d.values); })
    .style("stroke", function(d) { return color(d.country); })

var totalLength = path.node().getTotalLength();

d3.select(".line")
    .attr("stroke-dasharray", totalLength + " " + totalLength)
    .attr("stroke-dashoffset", totalLength)
  .transition()
    .duration(1000)
    .ease("linear")
    .attr("stroke-dashoffset", 0)
    .each("end", function() {
        d3.select(".label")
          .transition()
            .style("opacity", 1);
        });

var labels = country.append("text")
    .datum(function(d) { return {country: d.country, value: d.values[d.values.length - 1]}; })
    .attr("class", "label")
    .attr("transform", function(d) { return "translate(" + x(d.value.month) + "," + y(d.value.rainfall) + ")"; })
    .attr("x", 3)
    .attr("dy", ".35em")
    .style("opacity", 0)
    .text(function(d) { return d.country; });

【问题讨论】:

    标签: d3.js transition linechart


    【解决方案1】:

    您可以将.delay() 与函数一起使用:

    d3.select(".line")
      .attr("stroke-dasharray", totalLength + " " + totalLength)
      .attr("stroke-dashoffset", totalLength)
    .transition()
      .delay(function(d, i) { return i * 1000; })
      .duration(1000)
      .ease("linear")
      .attr("stroke-dashoffset", 0)
      .each("end", function() {
        d3.select(".label")
          .transition()
            .style("opacity", 1);
        });
    

    【讨论】:

    • 这正是我所需要的!我知道这将是一件非常简单的事情。感谢您这么快回复!现在我只需要弄清楚为什么除了第一行之外的所有内容都在错误的地方开始......
    • 您是获取所有行的总长度还是仅获取第一行?
    • 只是第一次看到它。我需要 totalLength 来计算并为每一行返回一个新值,有什么想法吗?再次感谢。
    • 查看完整代码会有所帮助。您可能还想把它放在一个单独的问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多