freeCodeCamp/curriculum/challenges/chinese/04-data-visualization/data-visualization-with-d3/create-a-linear-scale-with-...

2.6 KiB
Raw Blame History

id title required challengeType videoUrl localeTitle
587d7fab367417b2b2512bda Create a Linear Scale with D3
src
https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js
6 使用D3创建线性比例

Description

条形图和散点图图表都将数据直接绘制到SVG画布上。但是如果条形或其中一个数据点的高度大于SVG高度或宽度值则它将超出SVG区域。在D3中有一些比例可以帮助绘制数据。 Scales是告诉程序如何将一组原始数据点映射到SVG画布的像素上的函数。例如假设您有一个100x500大小的SVG画布并且您想绘制许多国家的国内生产总值GDP。这组数字将在数十亿或万亿美元的范围内。您提供D3一种比例来告诉它如何将大的GDP值放入100x500大小的区域。您不太可能按原样绘制原始数据。在绘制之前您可以设置整个数据集的比例以便xy值适合您的画布宽度和高度。 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