---
id: 587d7fac367417b2b2512bde
title: Use a Pre-Defined Scale to Place Elements
required:
- src: 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js'
challengeType: 6
videoUrl: ''
localeTitle: 使用预定义比例放置元素
---
## Description
设置了比例后,就可以再次绘制散点图。比例就像处理函数一样,可以将x和y原始数据转换为适合SVG画布上正确渲染的值。他们将数据保存在屏幕的绘图区域内。使用缩放功能设置SVG形状的坐标属性值。这包括rect
或text
元素的x
和y
属性,或者circles
cx
和cy
。这是一个例子: 形状
.attr(“x”,(d)=> xScale(d [0]))
Scales设置形状坐标属性以将数据点放置到SVG画布上。显示实际数据值时不需要应用比例,例如,在工具提示或标签的text()
方法中。
## Instructions
使用xScale
和yScale
将circle
和text
形状定位到SVG画布上。对于circles
,应用比例来设置cx
和cy
属性。给它们半径5个单位。对于text
元素,应用比例来设置x
和y
属性。标签应偏移到点的右侧。要执行此操作,请在将x数据传递给xScale
之前将其添加10个单位。
## Tests
```yml
tests:
- text: 您的代码应该有10个circle
元素。
testString: 'assert($("circle").length == 10, "Your code should have 10 circle
elements.");'
- text: 应用刻度后,第一个circle
元素应具有大约91的cx
值和大约368的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(0).attr("cx")) == "91" && Math.round($("circle").eq(0).attr("cy")) == "368" && $("circle").eq(0).attr("r") == "5", "The first circle
element should have a cx
value of approximately 91 and a cy
value of approximately 368 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第二个circle
元素的cx
值应约为159, cy
值应约为181。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(1).attr("cx")) == "159" && Math.round($("circle").eq(1).attr("cy")) == "181" && $("circle").eq(1).attr("r") == "5", "The second circle
element should have a cx
value of approximately 159 and a cy
value of approximately 181 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第三个circle
元素应具有约340的cx
值和约329的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(2).attr("cx")) == "340" && Math.round($("circle").eq(2).attr("cy")) == "329" && $("circle").eq(2).attr("r") == "5", "The third circle
element should have a cx
value of approximately 340 and a cy
value of approximately 329 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第四个circle
元素应具有大约131的cx
值和大约60的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(3).attr("cx")) == "131" && Math.round($("circle").eq(3).attr("cy")) == "60" && $("circle").eq(3).attr("r") == "5", "The fourth circle
element should have a cx
value of approximately 131 and a cy
value of approximately 60 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第五个circle
元素应具有大约440的cx
值和大约237的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(4).attr("cx")) == "440" && Math.round($("circle").eq(4).attr("cy")) == "237" && $("circle").eq(4).attr("r") == "5", "The fifth circle
element should have a cx
value of approximately 440 and a cy
value of approximately 237 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第六个circle
元素应具有约271的cx
值和约306的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(5).attr("cx")) == "271" && Math.round($("circle").eq(5).attr("cy")) == "306" && $("circle").eq(5).attr("r") == "5", "The sixth circle
element should have a cx
value of approximately 271 and a cy
value of approximately 306 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第七个circle
元素的cx
值约为361, cy
值约为351。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(6).attr("cx")) == "361" && Math.round($("circle").eq(6).attr("cy")) == "351" && $("circle").eq(6).attr("r") == "5", "The seventh circle
element should have a cx
value of approximately 361 and a cy
value of approximately 351 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第八个circle
元素应具有约261的cx
值和约132的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(7).attr("cx")) == "261" && Math.round($("circle").eq(7).attr("cy")) == "132" && $("circle").eq(7).attr("r") == "5", "The eighth circle
element should have a cx
value of approximately 261 and a cy
value of approximately 132 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第九个circle
元素应具有大约131的cx
值和大约144的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(8).attr("cx")) == "131" && Math.round($("circle").eq(8).attr("cy")) == "144" && $("circle").eq(8).attr("r") == "5", "The ninth circle
element should have a cx
value of approximately 131 and a cy
value of approximately 144 after applying the scales. It should also have an r
value of 5.");'
- text: 应用刻度后,第十个circle
元素应具有大约79的cx
值和大约326的cy
值。它的r
值也应为5。
testString: 'assert(Math.round($("circle").eq(9).attr("cx")) == "79" && Math.round($("circle").eq(9).attr("cy")) == "326" && $("circle").eq(9).attr("r") == "5", "The tenth circle
element should have a cx
value of approximately 79 and a cy
value of approximately 326 after applying the scales. It should also have an r
value of 5.");'
- text: 您的代码应该有10个text
元素。
testString: 'assert($("text").length == 10, "Your code should have 10 text
elements.");'
- text: 应用刻度后,第一个标签的x
值约为100, y
值约为368。
testString: 'assert(Math.round($("text").eq(0).attr("x")) == "100" && Math.round($("text").eq(0).attr("y")) == "368", "The first label should have an x
value of approximately 100 and a y
value of approximately 368 after applying the scales.");'
- text: 第二标签应该有一个x
的大约168值和y
施加鳞后的约181的值。
testString: 'assert(Math.round($("text").eq(1).attr("x")) == "168" && Math.round($("text").eq(1).attr("y")) == "181", "The second label should have an x
value of approximately 168 and a y
value of approximately 181 after applying the scales.");'
- text: 应用刻度后,第三个标签的x
值约为350, y
值约为329。
testString: 'assert(Math.round($("text").eq(2).attr("x")) == "350" && Math.round($("text").eq(2).attr("y")) == "329", "The third label should have an x
value of approximately 350 and a y
value of approximately 329 after applying the scales.");'
- text: 第四标签应该有一个x
的大约141值和y
施加鳞后的约60的值。
testString: 'assert(Math.round($("text").eq(3).attr("x")) == "141" && Math.round($("text").eq(3).attr("y")) == "60", "The fourth label should have an x
value of approximately 141 and a y
value of approximately 60 after applying the scales.");'
- text: 应用刻度后,第五个标签的x
值约为449, y
值约为237。
testString: 'assert(Math.round($("text").eq(4).attr("x")) == "449" && Math.round($("text").eq(4).attr("y")) == "237", "The fifth label should have an x
value of approximately 449 and a y
value of approximately 237 after applying the scales.");'
- text: 应用刻度后,第六个标签的x
值约为280, y
值约为306。
testString: 'assert(Math.round($("text").eq(5).attr("x")) == "280" && Math.round($("text").eq(5).attr("y")) == "306", "The sixth label should have an x
value of approximately 280 and a y
value of approximately 306 after applying the scales.");'
- text: 应用刻度后,第七个标签的x
值约为370, y
值约为351。
testString: 'assert(Math.round($("text").eq(6).attr("x")) == "370" && Math.round($("text").eq(6).attr("y")) == "351", "The seventh label should have an x
value of approximately 370 and a y
value of approximately 351 after applying the scales.");'
- text: 应用刻度后,第八个标签的x
值约为270, y
值约为132。
testString: 'assert(Math.round($("text").eq(7).attr("x")) == "270" && Math.round($("text").eq(7).attr("y")) == "132", "The eighth label should have an x
value of approximately 270 and a y
value of approximately 132 after applying the scales.");'
- text: 应用刻度后,第九个标签的x
值约为140, y
值约为144。
testString: 'assert(Math.round($("text").eq(8).attr("x")) == "140" && Math.round($("text").eq(8).attr("y")) == "144", "The ninth label should have an x
value of approximately 140 and a y
value of approximately 144 after applying the scales.");'
- text: 应用刻度后,第十个标签的x
值约为88, y
值约为326。
testString: 'assert(Math.round($("text").eq(9).attr("x")) == "88" && Math.round($("text").eq(9).attr("y")) == "326", "The tenth label should have an x
value of approximately 88 and a y
value of approximately 326 after applying the scales.");'
```
## Challenge Seed
## Solution
```js
// solution required
```