freeCodeCamp/curriculum/challenges/chinese/04-data-visualization/data-visualization-with-d3/display-shapes-with-svg.md

1.7 KiB
Raw Blame History

id title challengeType forumTopicId
587d7fa8367417b2b2512bcc 用 SVG 显示形状 6 301485

--description--

上个挑战用给定的宽和高创建了一个 svg 元素,因为在它的 style 标签中有 background-color,所以它是可见的。这一段代码为给定的宽和高腾出空间。

下一步是在 svg 区域中创建图形。SVG 支持多种图形,比如矩形和圆形,并用它们来显示数据。例如,在条形图中一个矩形(<rect>SVG 图形可以创建一个组。

当把图形放入 svg 区域中时,你可以用 xy 坐标来指定它的位置。起始点 (0,0) 是在左上角。x 正值将图形右移,y 正值将图形从原点下移

若要把一个图形放在上个挑战的 500x 100svg 中心,可将 x 坐标设置为 250y 坐标设置为 50。

SVG 的 rect 有四个属性。xy 坐标指定图形放在 svg 区域的位置,heightwidth 指定图形大小。

--instructions--

append() 方法给 svg 添加一个 rect 图形。将它的 width 属性设置为 25height 属性设置为 100xy 属性都设置为 0。

--hints--

你的文档应该有 1 个 rect 元素。

assert($('rect').length == 1);

rect 元素的 width 属性应该为 25。

assert($('rect').attr('width') == '25');

rect 元素的 height 属性应该为 100。

assert($('rect').attr('height') == '100');

rect 元素的 x 属性应该为 0。

assert($('rect').attr('x') == '0');

rect 元素的 y 属性应该为 0。

assert($('rect').attr('y') == '0');

--solutions--