简体   繁体   English

d3对多条SVG线进行动画处理

[英]d3 Animate Multiple SVG Lines

I'm trying to create multiple lines on a line graph one at a time. 我试图一次在一个折线图上创建多条线。 I've created an object array of about 100 lines in the below format: 我以以下格式创建了约100行的对象数组:

var allLines = [{type: "linear", values: [1000, 2000, 3000]}, {}, ... {}];

var line = d3.svg.line()
    .defined(function (d) {
        return d != null;
    })
    .x(function (d, i) {
        return x(new Date(minYear + i, 1, 1));
    })
    .y(function (d) {
        return y(d);
    });

Now I want to draw each line, one at a time with a delay of about 250 milliseconds between each line. 现在,我想绘制每条线,一次绘制一条线,每条线之间的延迟大约为250毫秒。 I've tried the below approach which I thought would work, but I must be missing something because it just waits 250ms and then draws all the lines. 我尝试了以下我认为可行的方法,但是我必须缺少一些东西,因为它仅等待250ms,然后绘制所有线条。

svg.append('g')
    .attr('class', 'lineGroup')
    .selectAll('path')
    .data(allLines)
    .enter()
    .append('path')
    .attr('class', function (d) {
        return d.type;
    })
    .style('visibility', 'hidden')
    .attr('d', function (d) {
        return line(d.values);
    });

    function foo(transition) {
        transition
            .style('visibility', 'visible');
    }

    d3.select('.lineGroup').selectAll('path').transition().delay(250).call(foo);

Your basic approach is right, you just need to adjust the delay dynamically such that the later lines are drawn later. 您的基本方法是正确的,您只需要动态调整延迟,以便稍后绘制以后的线条。 At the moment, the delay applies to all lines. 目前,延迟适用于所有线路。 To make it dynamic, you can use something like 为了使其动态,您可以使用类似

svg.append("g")
   // etc
   .transition()
   .delay(function(d, i) { return i * 250; })
   .style('visibility', 'visible');

You can also do everything in one call, no need for a separate one after creating the lines. 您还可以在一个呼叫中完成所有操作,无需在创建线路后再进行单独的呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM