freeCodeCamp/curriculum/challenges/chinese/04-data-visualization/data-visualization-with-d3/dynamically-set-the-coordin...

3.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7fa9367417b2b2512bce Dynamically Set the Coordinates for Each Bar 6 动态设置每个条形的坐标

Description

最后一个挑战是为dataset每个点创建一个矩形并将其附加到svg元素以表示一个条形。不幸的是,它们都堆叠在一起。矩形的放置由xy属性处理。他们告诉D3从哪里开始在svg区域中绘制形状。最后一个挑战将它们设置为0因此每个栏都放在左上角。对于条形图所有条形应位于相同的垂直水平这意味着所有条形的y值保持不变为0。但是在添加新柱时 x值需要更改。请记住,较大的x值会将项目推向更远的位置。当您浏览dataset的数组元素时x值应该会增加。 D3中的attr()方法接受回调函数来动态设置该属性。回调函数有两个参数,一个用于数据点本身(通常为d ),另一个用于数组中数据点的索引。索引的第二个参数是可选的。这是格式:
selection.attr“property”di=> {
/ *
* d是数据点值
* i是数组中数据点的索引
* /
}
重要的是要注意,您不需要编写for循环或使用forEach()来迭代数据集中的项。回想一下, data()方法解析数据集,并且data()之后链接的任何方法对数据集中的每个项目运行一次。

Instructions

更改x属性回调函数使其返回索引时间30. 注意
每个条的宽度为25因此将每个x值增加30会在条之间增加一些空间。任何大于25的值都可以在此示例中使用。

Tests

tests:
  - text: 第一个<code>rect</code>的<code>x</code>值应为0。
    testString: assert($('rect').eq(0).attr('x') == '0');
  - text: 第二个<code>rect</code>的<code>x</code>值应为30。
    testString: assert($('rect').eq(1).attr('x') == '30');
  - text: 第三个<code>rect</code>的<code>x</code>值应为60。
    testString: assert($('rect').eq(2).attr('x') == '60');
  - text: 第四个<code>rect</code>的<code>x</code>值应为90。
    testString: assert($('rect').eq(3).attr('x') == '90');
  - text: 第五个<code>rect</code>的<code>x</code>值应为120。
    testString: assert($('rect').eq(4).attr('x') == '120');
  - text: 第六个<code>rect</code>的<code>x</code>值应为150。
    testString: assert($('rect').eq(5).attr('x') == '150');
  - text: 第七个<code>rect</code>的<code>x</code>值应为180。
    testString: assert($('rect').eq(6).attr('x') == '180');
  - text: 第八个<code>rect</code>的<code>x</code>值应为210。
    testString: assert($('rect').eq(7).attr('x') == '210');
  - text: 第九个<code>rect</code>的<code>x</code>值应为240。
    testString: assert($('rect').eq(8).attr('x') == '240');

Challenge Seed

<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];

    const w = 500;
    const h = 100;

    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);

    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => {
         // Add your code below this line



         // Add your code above this line
       })
       .attr("y", 0)
       .attr("width", 25)
       .attr("height", 100);
  </script>
</body>

Solution

// solution required