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

65 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
id: 587d7fac367417b2b2512bdc
title: Use the d3.max and d3.min Functions to Find Minimum and Maximum Values in a Dataset
challengeType: 6
videoUrl: ''
localeTitle: 使用d3.max和d3.min函数查找数据集中的最小值和最大值
---
## Description
<section id="description"> D3方法<code>domain()</code><code>range()</code>根据数据为您的比例设置该信息。有几种方法可以使这更容易。通常在设置域时,您需要使用数据集中的最小值和最大值。尝试手动查找这些值,尤其是在大型数据集中,可能会导致错误。 D3有两种方法<code>min()</code><code>max()</code>来返回这些信息。这是一个例子: <blockquote> const exampleData = [34,234,73,90,6,52]; <br> d3.minexampleData//返回6 <br> d3.maxexampleData//返回234 </blockquote>数据集可能具有嵌套数组,例如散点图示例中的[xy]坐标对。在这种情况下您需要告诉D3如何计算最大值和最小值。幸运的是 <code>min()</code><code>max()</code>方法都采用了回调函数。在此示例中,回调函数的参数<code>d</code>用于当前内部数组。回调需要从内部数组x或y值返回要计算最大值或最小值的元素。这是一个如何使用数组数组查找最小值和最大值的示例 <blockquote> const locationData = [[1,7][6,3][8,3]]; <br> //返回第一个元素中的最小数字<br> const minX = d3.minlocationDatad=&gt; d [0]; <br> // minX比较1,6和8并设置为1 </blockquote></section>
## Instructions
<section id="instructions"> <code>positionData</code>变量包含一个三维3D数组。使用D3方法从数组中查找z坐标第三个值的最大值并将其保存在<code>output</code>变量中。 <strong>注意</strong> <br>有趣的事实 - D3可以绘制3D阵列。 </section>
## Tests
<section id='tests'>
```yml
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.")'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='html-seed'>
```html
<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>
```
</div>
</section>
## Solution
<section id='solution'>
```js
// solution required
```
</section>