javascript - 在 D3 中对 SVG 元素进行分组

标签 javascript d3.js svg

我已经在我的 D3 中实现了一辆汽车。信息图形。然而;它在我的容器中相当大,并且没有动画。然后我想对这些元素进行分组,将其缩小,最后将其设置动画以循环或连续移动。基本上使汽车变得更小,它可以在页面上自由地移动动画。

d3.select('#dataVizContainer svg').append("rect")
    .attr('id', 'rect1')
    .attr("fill", "transparent")
    .attr("stroke", "#99C863")
    .style('stroke-width', '10px')
    .attr("width", 210)
    .attr("height", 130)
    .attr("opacity", 0)
    ;

d3.select('#rect1')
    .transition()
    .attr("x", 70)
    .attr("y", 10)
    .attr('rx', 150)
    .attr("fill", "transparent")

    .delay(2000)
    .duration(12000)
    .attr("opacity", 1);


d3.select('#dataVizContainer svg').append('line').attr('id', 'line')
    .attr('x1', 145).attr('y1', 10).attr('x2', 145).attr('y2', 80)
    .style('stroke', '#99C863').style('stroke-width', '10px').attr("opacity", 0);
//Animation
d3.select('#line')
    .transition()
    .delay(2000)
    .duration(12000)
    .attr("opacity", 1);

d3.select('#dataVizContainer svg').append('line').attr('id', 'line2')
    .attr('x1', 215).attr('y1', 10).attr('x2', 215).attr('y2', 80)
    .style('stroke', '#99C863').style('stroke-width', '10px').attr("opacity", 0);
//Animation
d3.select('#line2')
    .transition()
    .delay(2000)
    .duration(12000)
    .attr("opacity", 1);


d3.select('#dataVizContainer svg').append("rect")
    .attr('id', 'rect2')
    .attr("fill", "transparent")
    .attr("stroke", "#8C8C93")
    .style('stroke-width', '10px')
    .attr("width", 340)
    .attr("height", 80)
    .attr("opacity", 0)
    ;
d3.select('#rect2')
    .transition()
    .attr("x", 10)
    .attr("y", 70)
    .attr('rx', 30)
    .attr("fill", "#8C8C93")
    .delay(2000)
    .duration(8000)
    .attr("opacity", 1);

d3.select('#dataVizContainer svg').append("rect")
    .attr('id', 'rect3')
    .attr("fill", "transparent")
    .attr("stroke", "crimson")
    .style('stroke-width', '10px')
    .attr("width", 40)
    .attr("height", 20)
    .attr("opacity", 0)
    ;
d3.select('#rect3')
    .transition()
    .attr("x", 0)
    .attr("y", 110)
    .attr('rx', 10)
    .attr("fill", "#999")

    .delay(2000)
    .duration(12000)
    .attr("opacity", 1);

d3.select('#dataVizContainer svg').append("rect")
    .attr('id', 'rect4')
    .attr("fill", "transparent")
    .attr("stroke", "crimson")
    .style('stroke-width', '10px')
    .attr("width", 40)
    .attr("height", 20)
    .attr("opacity", 0)
    ;

//Draw the Rectangle
d3.select('#rect4')
    .transition()
    .attr("x", 325)
    .attr("y", 110)
    .attr('rx', 10)
    .attr("fill", "#999")

    .delay(2000)
    .duration(12000)
    .attr("opacity", 1);
// Left wheel 
d3.select('#dataVizContainer svg').append('circle')
    .attr('id', 'C14')
    .attr('cx', 90)
    .attr('cy', 140)
    .attr('r', 40)
    .style('stroke-width', '7px')
    .attr("opacity", 0)
    ;

d3.select('#C14')
    .transition()
    .attr("r", "40")
    .delay(4000)
    .ease(d3.easeBounce)
    .duration(12000)
    .attr("opacity", 1)
    .attr("fill", "#222")
    ;

// Rims of the Left Wheel 
d3.select('#dataVizContainer svg').append('circle')
    .attr('id', 'C15')
    .attr('cx', 90)
    .attr('cy', 140)
    .attr('r', 15)
    .attr("opacity", 0)
    ;

d3.select('#C15')
    .transition()
    .delay(7000)
    .ease(d3.easeBounce)
    .duration(12000)
    .attr("opacity", 1)
    .attr("fill", "#555")
    .attr("r", "15")

    ;

//Left Wheel Code Ended

//Right Wheel Started 
d3.select('#dataVizContainer svg').append('circle')
    .attr('id', 'C16')
    .attr('cx', 270)
    .attr('cy', 140)
    .attr('r', 40)
    .style('stroke-width', '7px')
    .attr("opacity", 0)
    ;

d3.select('#C16')
    .transition()
    .attr("r", "40")
    .delay(4000)
    .ease(d3.easeBounce)
    .duration(12000)
    .attr("opacity", 1)
    .attr("fill", "#222")
    ;

// Rims of the Right Wheel 
d3.select('#dataVizContainer svg').append('circle')
    .attr('id', 'C17')
    .attr('cx', 270)
    .attr('cy', 140)
    .attr('r', 15)
    .attr("opacity", 0)
    ;

d3.select('#C17')
    .transition()
    .delay(7000)
    .ease(d3.easeBounce)
    .duration(12000)
    .attr("opacity", 1)
    .attr("fill", "#555")
    .attr("r", "15")

    ;
//Right Wheel Code Ended

最佳答案

给你:我把你的车放在一个 svg 组中,并稍微改变了你的代码,最后我让你的车移动了,你还可以向汽车元素添加各种转换,如下所示:https://developer.mozilla.org/fr/docs/Web/CSS/transform

<!DOCTYPE html>
<meta charset="utf-8">

<body>

<!-- load the d3.js library -->
<script type="text/javascript" src="http://d3js.org/d3.v4.min.js">
</script>
<div id="dataVizContainer">
    <svg height="1000" width="1000" ></svg>
</div>
<script>
    var car = d3.select('svg')
        .append("g");

    car.append("rect")
        .attr('id', 'rect1')
        .attr("fill", "transparent")
        .attr("stroke", "#99C863")
        .style('stroke-width', '10px')
        .attr("width", 210)
        .attr("height", 130)
        .attr("opacity", 0)
    ;

    d3.select('#rect1')
        .transition()
        .attr("x", 70)
        .attr("y", 10)
        .attr('rx', 150)
        .attr("fill", "transparent")

        .delay(2000)
        .duration(12000)
        .attr("opacity", 1);


    car.append('line').attr('id', 'line')
        .attr('x1', 145).attr('y1', 10).attr('x2', 145).attr('y2', 80)
        .style('stroke', '#99C863').style('stroke-width', '10px').attr("opacity", 0);
    //Animation
    d3.select('#line')
        .transition()
        .delay(2000)
        .duration(12000)
        .attr("opacity", 1);

    car.append('line').attr('id', 'line2')
        .attr('x1', 215).attr('y1', 10).attr('x2', 215).attr('y2', 80)
        .style('stroke', '#99C863').style('stroke-width', '10px').attr("opacity", 0);
    //Animation
    d3.select('#line2')
        .transition()
        .delay(2000)
        .duration(12000)
        .attr("opacity", 1);


    car.append("rect")
        .attr('id', 'rect2')
        .attr("fill", "transparent")
        .attr("stroke", "#8C8C93")
        .style('stroke-width', '10px')
        .attr("width", 340)
        .attr("height", 80)
        .attr("opacity", 0)
    ;
    d3.select('#rect2')
        .transition()
        .attr("x", 10)
        .attr("y", 70)
        .attr('rx', 30)
        .attr("fill", "#8C8C93")
        .delay(2000)
        .duration(8000)
        .attr("opacity", 1);

    car.append("rect")
        .attr('id', 'rect3')
        .attr("fill", "transparent")
        .attr("stroke", "crimson")
        .style('stroke-width', '10px')
        .attr("width", 40)
        .attr("height", 20)
        .attr("opacity", 0)
    ;
    d3.select('#rect3')
        .transition()
        .attr("x", 0)
        .attr("y", 110)
        .attr('rx', 10)
        .attr("fill", "#999")

        .delay(2000)
        .duration(12000)
        .attr("opacity", 1);

    car.append("rect")
        .attr('id', 'rect4')
        .attr("fill", "transparent")
        .attr("stroke", "crimson")
        .style('stroke-width', '10px')
        .attr("width", 40)
        .attr("height", 20)
        .attr("opacity", 0)
    ;

    //Draw the Rectangle
    d3.select('#rect4')
        .transition()
        .attr("x", 325)
        .attr("y", 110)
        .attr('rx', 10)
        .attr("fill", "#999")

        .delay(2000)
        .duration(12000)
        .attr("opacity", 1);
    // Left wheel
    car.append('circle')
        .attr('id', 'C14')
        .attr('cx', 90)
        .attr('cy', 140)
        .attr('r', 40)
        .style('stroke-width', '7px')
        .attr("opacity", 0)
    ;

    d3.select('#C14')
        .transition()
        .attr("r", "40")
        .delay(4000)
        .ease(d3.easeBounce)
        .duration(12000)
        .attr("opacity", 1)
        .attr("fill", "#222")
    ;

    // Rims of the Left Wheel
    car.append('circle')
        .attr('id', 'C15')
        .attr('cx', 90)
        .attr('cy', 140)
        .attr('r', 15)
        .attr("opacity", 0)
    ;

    d3.select('#C15')
        .transition()
        .delay(7000)
        .ease(d3.easeBounce)
        .duration(12000)
        .attr("opacity", 1)
        .attr("fill", "#555")
        .attr("r", "15")

    ;

    //Left Wheel Code Ended

    //Right Wheel Started
    car.append('circle')
        .attr('id', 'C16')
        .attr('cx', 270)
        .attr('cy', 140)
        .attr('r', 40)
        .style('stroke-width', '7px')
        .attr("opacity", 0)
    ;

    d3.select('#C16')
        .transition()
        .attr("r", "40")
        .delay(4000)
        .ease(d3.easeBounce)
        .duration(12000)
        .attr("opacity", 1)
        .attr("fill", "#222")
    ;

    // Rims of the Right Wheel
    car.append('circle')
        .attr('id', 'C17')
        .attr('cx', 270)
        .attr('cy', 140)
        .attr('r', 15)
        .attr("opacity", 0)
    ;

    d3.select('#C17')
        .transition()
        .delay(7000)
        .ease(d3.easeBounce)
        .duration(12000)
        .attr("opacity", 1)
        .attr("fill", "#555")
        .attr("r", "15")

    ;
   //here i scale down the car
    car.attr("transform", "scale(0.1)")
   //here i mak eit move (after your fade in animation)
    car.transition().delay(12000).duration(5000).attr("transform", "scale(0.1) translate(8000,0)")

    //Right Wheel Code Ended
</script>
</body>

关于javascript - 在 D3 中对 SVG 元素进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534487/

相关文章:

javascript - 用 d3 将测地线钳制到最小/最大纬度

javascript - 使用 HTML Unicode 表达式的 D3 数字格式

html - Web 浏览器不显示 svg 文件

javascript - 指定 URL 或 URL 根属性 - backbone.js

javascript - 有关闭问题。似乎无法解决。请指教

javascript - mup 部署的应用程序中的 .meteor 目录在哪里?

javascript - d3.js 和谷歌地图 API 的多条路径

javascript - 附加元素不保留在父元素中

javascript - 根据数据动态设置SVG的宽高

jquery - 尝试让 JQVMap 在 iphone 上正确缩放