2.6 KiB
2.6 KiB
id | title | required | challengeType | videoUrl | localeTitle | |||
---|---|---|---|---|---|---|---|---|
587d7fab367417b2b2512bda | Create a Linear Scale with D3 |
|
6 | 使用D3创建线性比例 |
Description
Scales
是告诉程序如何将一组原始数据点映射到SVG画布的像素上的函数。例如,假设您有一个100x500大小的SVG画布,并且您想绘制许多国家的国内生产总值(GDP)。这组数字将在数十亿或万亿美元的范围内。您提供D3一种比例来告诉它如何将大的GDP值放入100x500大小的区域。您不太可能按原样绘制原始数据。在绘制之前,您可以设置整个数据集的比例,以便x
和y
值适合您的画布宽度和高度。 D3有几种比例类型。对于线性标度(通常与定量数据一起使用),有D3方法scaleLinear()
: const scale = d3.scaleLinear()
默认情况下,标度使用标识关系。输入的值与输出的值相同。单独的挑战包括如何改变这一点。 Instructions
scale
变量以创建线性比例。然后将output
变量设置为使用输入参数50调用的比例。 Tests
tests:
- text: <code>h2</code>的文本应为50。
testString: 'assert($("h2").text() == "50", "The text in the <code>h2</code> should be 50.");'
- text: 您的代码应使用<code>scaleLinear()</code>方法。
testString: 'assert(code.match(/\.scaleLinear/g), "Your code should use the <code>scaleLinear()</code> method.");'
- text: <code>output</code>变量应该使用参数50调用<code>scale</code> 。
testString: 'assert(output == 50 && code.match(/scale\(\s*?50\s*?\)/g), "The <code>output</code> variable should call <code>scale</code> with an argument of 50.");'
Challenge Seed
<body>
<script>
// Add your code below this line
const scale = undefined; // Create the scale here
const output = scale(); // Call the scale with an argument here
// Add your code above this line
d3.select("body")
.append("h2")
.text(output);
</script>
</body>
Solution
// solution required