freeCodeCamp/curriculum/challenges/chinese/04-data-visualization/data-visualization-with-d3/use-the-d3.max-and-d3.min-f...

2.7 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7fac367417b2b2512bdc Use the d3.max and d3.min Functions to Find Minimum and Maximum Values in a Dataset 6 使用d3.max和d3.min函数查找数据集中的最小值和最大值

Description

D3方法domain()range()根据数据为您的比例设置该信息。有几种方法可以使这更容易。通常在设置域时,您需要使用数据集中的最小值和最大值。尝试手动查找这些值,尤其是在大型数据集中,可能会导致错误。 D3有两种方法min()max()来返回这些信息。这是一个例子:
const exampleData = [34,234,73,90,6,52];
d3.minexampleData//返回6
d3.maxexampleData//返回234
数据集可能具有嵌套数组,例如散点图示例中的[xy]坐标对。在这种情况下您需要告诉D3如何计算最大值和最小值。幸运的是 min()max()方法都采用了回调函数。在此示例中,回调函数的参数d用于当前内部数组。回调需要从内部数组x或y值返回要计算最大值或最小值的元素。这是一个如何使用数组数组查找最小值和最大值的示例
const locationData = 1,7][6,3][8,3;
//返回第一个元素中的最小数字
const minX = d3.minlocationDatad=> d [0];
// minX比较1,6和8并设置为1

Instructions

positionData变量包含一个三维3D数组。使用D3方法从数组中查找z坐标第三个值的最大值并将其保存在output变量中。 注意
有趣的事实 - D3可以绘制3D阵列。

Tests

tests:
  - text: <code>h2</code>的文本应为8。
    testString: 'assert(output == 8 && $("h2").text() == "8", "The text in the <code>h2</code> should be 8.");'
  - text: 您的代码应使用<code>max()</code>方法。
    testString: 'assert(code.match(/\.max/g), "Your code should use the <code>max()</code> method.")'

Challenge Seed

<body>
  <script>
    const positionData = [[1, 7, -4],[6, 3, 8],[2, 8, 3]]
    // Add your code below this line

    const output = undefined; // Change this line

    // Add your code above this line

    d3.select("body")
      .append("h2")
      .text(output)
  </script>
</body>

Solution

// solution required