freeCodeCamp/challenges/04-data-visualization/data-visualization-with-d3....

2547 lines
138 KiB
JSON

{
"name": "Data Visualization with D3",
"order": 1,
"time": "5 hours",
"helpRoom": "Help",
"challenges": [
{
"id": "587d7fa6367417b2b2512bc2",
"title": "Add Document Elements with D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"D3 has several methods that let you add and change elements in your document.",
"The <code>select()</code> method selects one element from the document. It takes an argument for the name of the element you want and returns an HTML node for the first element in the document that matches the name. Here's an example:",
"<code>const anchor = d3.select(\"a\");</code>",
"The above example finds the first anchor tag on the page and saves an HTML node for it in the variable <code>anchor</code>. You can use the selection with other methods. The \"d3\" part of the example is a reference to the D3 object, which is how you access D3 methods.",
"Two other useful methods are <code>append()</code> and <code>text()</code>.",
"The <code>append()</code> method takes an argument for the element you want to add to the document. It appends an HTML node to a selected item, and returns a handle to that node.",
"The <code>text()</code> method either sets the text of the selected node, or gets the current text. To set the value, you pass a string as an argument inside the parentheses of the method.",
"Here's an example that selects an unordered list, appends a list item, and adds text:",
"<blockquote>d3.select(\"ul\")<br> .append(\"li\")<br> .text(\"Very important item\");</blockquote>",
"D3 allows you to chain several methods together with periods to perform a number of actions in a row.",
"<hr>",
"Use the <code>select</code> method to select the <code>body</code> tag in the document. Then <code>append</code> an <code>h1</code> tag to it, and add the text \"Learning D3\" into the <code>h1</code> element."
],
"tests": [
{
"text": "The <code>body</code> should have one <code>h1</code> element.",
"testString": "assert($('body').children('h1').length == 1, 'The <code>body</code> should have one <code>h1</code> element.');"
},
{
"text": "The <code>h1</code> element should have the text \"Learning D3\" in it.",
"testString": "assert($('h1').text() == \"Learning D3\", 'The <code>h1</code> element should have the text \"Learning D3\" in it.');"
},
{
"text": "Your code should access the <code>d3</code> object.",
"testString": "assert(code.match(/d3/g), 'Your code should access the <code>d3</code> object.');"
},
{
"text": "Your code should use the <code>select</code> method.",
"testString": "assert(code.match(/\\.select/g), 'Your code should use the <code>select</code> method.');"
},
{
"text": "Your code should use the <code>append</code> method.",
"testString": "assert(code.match(/\\.append/g), 'Your code should use the <code>append</code> method.');"
},
{
"text": "Your code should use the <code>text</code> method.",
"testString": "assert(code.match(/\\.text/g), 'Your code should use the <code>text</code> method.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa6367417b2b2512bc3",
"title": "Select a Group of Elements with D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"D3 also has the <code>selectAll()</code> method to select a group of elements. It returns an array of HTML nodes for all the items in the document that match the input string. Here's an example to select all the anchor tags in a document:",
"<code>const anchors = d3.selectAll(\"a\");</code>",
"Like the <code>select()</code> method, <code>selectAll()</code> supports method chaining, and you can use it with other methods.",
"<hr>",
"Select all of the <code>li</code> tags in the document, and change their text to \"list item\" by chaining the <code>.text()</code> method."
],
"tests": [
{
"text": "There should be 3 <code>li</code> elements on the page, and the text in each one should say \"list item\". Capitalization and spacing should match exactly.",
"testString": "assert($('li').text().match(/list item/g).length == 3, 'There should be 3 <code>li</code> elements on the page, and the text in each one should say \"list item\". Capitalization and spacing should match exactly.');"
},
{
"text": "Your code should access the <code>d3</code> object.",
"testString": "assert(code.match(/d3/g), 'Your code should access the <code>d3</code> object.');"
},
{
"text": "Your code should use the <code>selectAll</code> method.",
"testString": "assert(code.match(/\\.selectAll/g), 'Your code should use the <code>selectAll</code> method.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <ul>",
" <li>Example</li>",
" <li>Example</li>",
" <li>Example</li>",
" </ul>",
" <script>",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa7367417b2b2512bc4",
"title": "Work with Data in D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The D3 library focuses on a data-driven approach. When you have a set of data, you can apply D3 methods to display it on the page. Data comes in many formats, but this challenge uses a simple array of numbers.",
"The first step is to make D3 aware of the data. The <code>data()</code> method is used on a selection of DOM elements to attach the data to those elements. The data set is passed as an argument to the method.",
"A common workflow pattern is to create a new element in the document for each piece of data in the set. D3 has the <code>enter()</code> method for this purpose.",
"When <code>enter()</code> is combined with the <code>data()</code> method, it looks at the selected elements from the page and compares them to the number of data items in the set. If there are fewer elements than data items, it creates the missing elements.",
"Here is an example that selects a <code>ul</code> element and creates a new list item based on the number of entries in the array:",
"<blockquote>&lt;body&gt;<br> &lt;ul&gt;&lt;/ul&gt;<br> &lt;script&gt;<br> const dataset = [\"a\", \"b\", \"c\"];<br> d3.select(\"ul\").selectAll(\"li\")<br> .data(dataset)<br> .enter()<br> .append(\"li\")<br> .text(\"New item\");<br> &lt;/script&gt;<br>&lt;/body&gt;</blockquote>",
"It may seem confusing to select elements that don't exist yet. This code is telling D3 to first select the <code>ul</code> on the page. Next, select all list items, which returns an empty selection. Then the <code>data()</code> method reviews the dataset and runs the following code three times, once for each item in the array. The <code>enter()</code> method sees there are no <code>li</code> elements on the page, but it needs 3 (one for each piece of data in <code>dataset</code>). New <code>li</code> elements are appended to the <code>ul</code> and have the text \"New item\".",
"<hr>",
"Select the <code>body</code> node, then select all <code>h2</code> elements. Have D3 create and append an <code>h2</code> tag for each item in the <code>dataset</code> array. The text in the <code>h2</code> should say \"New Title\". Your code should use the <code>data()</code> and <code>enter()</code> methods."
],
"tests": [
{
"text": "Your document should have 9 <code>h2</code> elements.",
"testString": "assert($('h2').length == 9, 'Your document should have 9 <code>h2</code> elements.');"
},
{
"text": "The text in the <code>h2</code> elements should say \"New Title\". The capitalization and spacing should match exactly.",
"testString": "assert($('h2').text().match(/New Title/g).length == 9, 'The text in the <code>h2</code> elements should say \"New Title\". The capitalization and spacing should match exactly.');"
},
{
"text": "Your code should use the <code>data()</code> method.",
"testString": "assert(code.match(/\\.data/g), 'Your code should use the <code>data()</code> method.');"
},
{
"text": "Your code should use the <code>enter()</code> method.",
"testString": "assert(code.match(/\\.enter/g), 'Your code should use the <code>enter()</code> method.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa7367417b2b2512bc5",
"title": "Work with Dynamic Data in D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The last two challenges cover the basics of displaying data dynamically with D3 using the <code>data()</code> and <code>enter()</code> methods. These methods take a data set and, together with the <code>append()</code> method, create a new DOM element for each entry in the data set.",
"In the previous challenge, you created a new <code>h2</code> element for each item in the <code>dataset</code> array, but they all contained the same text, \"New Title\". This is because you have not made use of the data that is bound to each of the <code>h2</code> elements.",
"The D3 <code>text()</code> method can take a string or a callback function as an argument:",
"<code>selection.text((d) => d)</code>",
"In the example above, the parameter <code>d</code> refers to a single entry in the dataset that a selection is bound to.",
"Using the current example as context, the first <code>h2</code> element is bound to 12, the second <code>h2</code> element is bound to 31, the third <code>h2</code> element is bound to 22, and so on.",
"<hr>",
"Change the <code>text()</code> method so that each <code>h2</code> element displays the corresponding value from the <code>dataset</code> array with a single space and \"USD\". For example, the first heading should be \"12 USD\"."
],
"tests": [
{
"text": "The first <code>h2</code> should have the text \"12 USD\".",
"testString": "assert($('h2').eq(0).text() == \"12 USD\", 'The first <code>h2</code> should have the text \"12 USD\".');"
},
{
"text": "The second <code>h2</code> should have the text \"31 USD\".",
"testString": "assert($('h2').eq(1).text() == \"31 USD\", 'The second <code>h2</code> should have the text \"31 USD\".');"
},
{
"text": "The third <code>h2</code> should have the text \"22 USD\".",
"testString": "assert($('h2').eq(2).text() == \"22 USD\", 'The third <code>h2</code> should have the text \"22 USD\".');"
},
{
"text": "The fourth <code>h2</code> should have the text \"17 USD\".",
"testString": "assert($('h2').eq(3).text() == \"17 USD\", 'The fourth <code>h2</code> should have the text \"17 USD\".');"
},
{
"text": "The fifth <code>h2</code> should have the text \"25 USD\".",
"testString": "assert($('h2').eq(4).text() == \"25 USD\", 'The fifth <code>h2</code> should have the text \"25 USD\".');"
},
{
"text": "The sixth <code>h2</code> should have the text \"18 USD\".",
"testString": "assert($('h2').eq(5).text() == \"18 USD\", 'The sixth <code>h2</code> should have the text \"18 USD\".');"
},
{
"text": "The seventh <code>h2</code> should have the text \"29 USD\".",
"testString": "assert($('h2').eq(6).text() == \"29 USD\", 'The seventh <code>h2</code> should have the text \"29 USD\".');"
},
{
"text": "The eighth <code>h2</code> should have the text \"14 USD\".",
"testString": "assert($('h2').eq(7).text() == \"14 USD\", 'The eighth <code>h2</code> should have the text \"14 USD\".');"
},
{
"text": "The ninth <code>h2</code> should have the text \"9 USD\".",
"testString": "assert($('h2').eq(8).text() == \"9 USD\", 'The ninth <code>h2</code> should have the text \"9 USD\".');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" d3.select(\"body\").selectAll(\"h2\")",
" .data(dataset)",
" .enter()",
" .append(\"h2\")",
" // Add your code below this line",
" ",
" .text(\"New Title\");",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa7367417b2b2512bc6",
"title": "Add Inline Styling to Elements",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"D3 lets you add inline CSS styles on dynamic elements with the <code>style()</code> method.",
"The <code>style()</code> method takes a comma-separated key-value pair as an argument. Here's an example to set the selection's text color to blue:",
"<code>selection.style(\"color\",\"blue\");</code>",
"<hr>",
"Add the <code>style()</code> method to the code in the editor to make all the displayed text have a <code>font-family</code> of <code>verdana</code>."
],
"tests": [
{
"text": "Your <code>h2</code> elements should have a <code>font-family</code> of verdana.",
"testString": "assert($('h2').css('font-family') == 'verdana', 'Your <code>h2</code> elements should have a <code>font-family</code> of verdana.');"
},
{
"text": "Your code should use the <code>style()</code> method.",
"testString": "assert(code.match(/\\.style/g), 'Your code should use the <code>style()</code> method.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" d3.select(\"body\").selectAll(\"h2\")",
" .data(dataset)",
" .enter()",
" .append(\"h2\")",
" .text((d) => (d + \" USD\"))",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa7367417b2b2512bc7",
"title": "Change Styles Based on Data",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"D3 is about visualization and presentation of data. It's likely you'll want to change the styling of elements based on the data. You can use a callback function in the <code>style()</code> method to change the styling for different elements.",
"For example, you may want to color a data point blue if has a value less than 20, and red otherwise. You can use a callback function in the <code>style()</code> method and include the conditional logic. The callback function uses the <code>d</code> parameter to represent the data point:",
"<blockquote>selection.style(\"color\", (d) => {<br> /* Logic that returns the color based on a condition */<br>});</blockquote>",
"The <code>style()</code> method is not limited to setting the <code>color</code> - it can be used with other CSS properties as well.",
"<hr>",
"Add the <code>style()</code> method to the code in the editor to set the <code>color</code> of the <code>h2</code> elements conditionally. Write the callback function so if the data value is less than 20, it returns \"red\", otherwise it returns \"green\".",
"<strong>Note</strong><br>You can use if-else logic, or the ternary operator."
],
"tests": [
{
"text": "The first <code>h2</code> should have a <code>color</code> of red.",
"testString": "assert($('h2').eq(0).css('color') == \"rgb(255, 0, 0)\", 'The first <code>h2</code> should have a <code>color</code> of red.');"
},
{
"text": "The second <code>h2</code> should have a <code>color</code> of green.",
"testString": "assert($('h2').eq(1).css('color') == \"rgb(0, 128, 0)\", 'The second <code>h2</code> should have a <code>color</code> of green.');"
},
{
"text": "The third <code>h2</code> should have a <code>color</code> of green.",
"testString": "assert($('h2').eq(2).css('color') == \"rgb(0, 128, 0)\", 'The third <code>h2</code> should have a <code>color</code> of green.');"
},
{
"text": "The fourth <code>h2</code> should have a <code>color</code> of red.",
"testString": "assert($('h2').eq(3).css('color') == \"rgb(255, 0, 0)\", 'The fourth <code>h2</code> should have a <code>color</code> of red.');"
},
{
"text": "The fifth <code>h2</code> should have a <code>color</code> of green.",
"testString": "assert($('h2').eq(4).css('color') == \"rgb(0, 128, 0)\", 'The fifth <code>h2</code> should have a <code>color</code> of green.');"
},
{
"text": "The sixth <code>h2</code> should have a <code>color</code> of red.",
"testString": "assert($('h2').eq(5).css('color') == \"rgb(255, 0, 0)\", 'The sixth <code>h2</code> should have a <code>color</code> of red.');"
},
{
"text": "The seventh <code>h2</code> should have a <code>color</code> of green.",
"testString": "assert($('h2').eq(6).css('color') == \"rgb(0, 128, 0)\", 'The seventh <code>h2</code> should have a <code>color</code> of green.');"
},
{
"text": "The eighth <code>h2</code> should have a <code>color</code> of red.",
"testString": "assert($('h2').eq(7).css('color') == \"rgb(255, 0, 0)\", 'The eighth <code>h2</code> should have a <code>color</code> of red.');"
},
{
"text": "The ninth <code>h2</code> should have a <code>color</code> of red.",
"testString": "assert($('h2').eq(8).css('color') == \"rgb(255, 0, 0)\", 'The ninth <code>h2</code> should have a <code>color</code> of red.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" d3.select(\"body\").selectAll(\"h2\")",
" .data(dataset)",
" .enter()",
" .append(\"h2\")",
" .text((d) => (d + \" USD\"))",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa7367417b2b2512bc8",
"title": "Add Classes with D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"Using a lot of inline styles on HTML elements gets hard to manage, even for smaller apps. It's easier to add a class to elements and style that class one time using CSS rules. D3 has the <code>attr()</code> method to add any HTML attribute to an element, including a class name.",
"The <code>attr()</code> method works the same way that <code>style()</code> does. It takes comma-separated values, and can use a callback function. Here's an example to add a class of \"container\" to a selection:",
"<code>selection.attr(\"class\", \"container\");</code>",
"<hr>",
"Add the <code>attr()</code> method to the code in the editor and put a class of <code>bar</code> on the <code>div</code> elements."
],
"tests": [
{
"text": "Your <code>div</code> elements should have a class of <code>bar</code>.",
"testString": "assert($('div').attr('class') == \"bar\", 'Your <code>div</code> elements should have a class of <code>bar</code>.');"
},
{
"text": "Your code should use the <code>attr()</code> method.",
"testString": "assert(code.match(/\\.attr/g), 'Your code should use the <code>attr()</code> method.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<style>",
" .bar {",
" width: 25px;",
" height: 100px;",
" display: inline-block;",
" background-color: blue;",
" }",
"</style>",
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" d3.select(\"body\").selectAll(\"div\")",
" .data(dataset)",
" .enter()",
" .append(\"div\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa8367417b2b2512bc9",
"title": "Update the Height of an Element Dynamically",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The previous challenges covered how to display data from an array and how to add CSS classes. You can combine these lessons to create a simple bar chart. There are two steps to this:",
"1) Create a <code>div</code> for each data point in the array",
"2) Give each <code>div</code> a dynamic height, using a callback function in the <code>style()</code> method that sets height equal to the data value",
"Recall the format to set a style using a callback function:",
"<code>selection.style(\"cssProperty\", (d) => d)</code>",
"<hr>",
"Add the <code>style()</code> method to the code in the editor to set the <code>height</code> property for each element. Use a callback function to return the value of the data point with the string \"px\" added to it."
],
"tests": [
{
"text": "The first <code>div</code> should have a <code>height</code> of 12 pixels.",
"testString": "assert($('div').eq(0).css('height') == '12px', 'The first <code>div</code> should have a <code>height</code> of 12 pixels.');"
},
{
"text": "The second <code>div</code> should have a <code>height</code> of 31 pixels.",
"testString": "assert($('div').eq(1).css('height') == '31px', 'The second <code>div</code> should have a <code>height</code> of 31 pixels.');"
},
{
"text": "The third <code>div</code> should have a <code>height</code> of 22 pixels.",
"testString": "assert($('div').eq(2).css('height') == '22px', 'The third <code>div</code> should have a <code>height</code> of 22 pixels.');"
},
{
"text": "The fourth <code>div</code> should have a <code>height</code> of 17 pixels.",
"testString": "assert($('div').eq(3).css('height') == '17px', 'The fourth <code>div</code> should have a <code>height</code> of 17 pixels.');"
},
{
"text": "The fifth <code>div</code> should have a <code>height</code> of 25 pixels.",
"testString": "assert($('div').eq(4).css('height') == '25px', 'The fifth <code>div</code> should have a <code>height</code> of 25 pixels.');"
},
{
"text": "The sixth <code>div</code> should have a <code>height</code> of 18 pixels.",
"testString": "assert($('div').eq(5).css('height') == '18px', 'The sixth <code>div</code> should have a <code>height</code> of 18 pixels.');"
},
{
"text": "The seventh <code>div</code> should have a <code>height</code> of 29 pixels.",
"testString": "assert($('div').eq(6).css('height') == '29px', 'The seventh <code>div</code> should have a <code>height</code> of 29 pixels.');"
},
{
"text": "The eighth <code>div</code> should have a <code>height</code> of 14 pixels.",
"testString": "assert($('div').eq(7).css('height') == '14px', 'The eighth <code>div</code> should have a <code>height</code> of 14 pixels.');"
},
{
"text": "The ninth <code>div</code> should have a <code>height</code> of 9 pixels.",
"testString": "assert($('div').eq(8).css('height') == '9px', 'The ninth <code>div</code> should have a <code>height</code> of 9 pixels.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<style>",
" .bar {",
" width: 25px;",
" height: 100px;",
" display: inline-block;",
" background-color: blue;",
" }",
"</style>",
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" d3.select(\"body\").selectAll(\"div\")",
" .data(dataset)",
" .enter()",
" .append(\"div\")",
" .attr(\"class\", \"bar\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa8367417b2b2512bca",
"title": "Change the Presentation of a Bar Chart",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The last challenge created a bar chart, but there are a couple of formatting changes that could improve it:",
"1) Add space between each bar to visually separate them, which is done by adding a margin to the CSS for the <code>bar</code> class",
"2) Increase the height of the bars to better show the difference in values, which is done by multiplying the value by a number to scale the height",
"<hr>",
"First, add a <code>margin</code> of 2px to the <code>bar</code> class in the <code>style</code> tag. Next, change the callback function in the <code>style()</code> method so it returns a value 10 times the original data value (plus the \"px\").",
"<strong>Note</strong><br>Multiplying each data point by the <em>same</em> constant only alters the scale. It's like zooming in, and it doesn't change the meaning of the underlying data."
],
"tests": [
{
"text": "The first <code>div</code> should have a <code>height</code> of 120 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(0).css('height') == '120px' && $('div').eq(0).css('margin-right') == '2px', 'The first <code>div</code> should have a <code>height</code> of 120 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The second <code>div</code> should have a <code>height</code> of 310 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(1).css('height') == '310px' && $('div').eq(1).css('margin-right') == '2px', 'The second <code>div</code> should have a <code>height</code> of 310 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The third <code>div</code> should have a <code>height</code> of 220 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(2).css('height') == '220px' && $('div').eq(2).css('margin-right') == '2px', 'The third <code>div</code> should have a <code>height</code> of 220 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The fourth <code>div</code> should have a <code>height</code> of 170 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(3).css('height') == '170px' && $('div').eq(3).css('margin-right') == '2px', 'The fourth <code>div</code> should have a <code>height</code> of 170 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The fifth <code>div</code> should have a <code>height</code> of 250 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(4).css('height') == '250px' && $('div').eq(4).css('margin-right') == '2px', 'The fifth <code>div</code> should have a <code>height</code> of 250 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The sixth <code>div</code> should have a <code>height</code> of 180 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(5).css('height') == '180px' && $('div').eq(5).css('margin-right') == '2px', 'The sixth <code>div</code> should have a <code>height</code> of 180 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The seventh <code>div</code> should have a <code>height</code> of 290 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(6).css('height') == '290px' && $('div').eq(6).css('margin-right') == '2px', 'The seventh <code>div</code> should have a <code>height</code> of 290 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The eighth <code>div</code> should have a <code>height</code> of 140 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(7).css('height') == '140px' && $('div').eq(7).css('margin-right') == '2px', 'The eighth <code>div</code> should have a <code>height</code> of 140 pixels and a <code>margin</code> of 2 pixels.');"
},
{
"text": "The ninth <code>div</code> should have a <code>height</code> of 90 pixels and a <code>margin</code> of 2 pixels.",
"testString": "assert($('div').eq(8).css('height') == '90px' && $('div').eq(8).css('margin-right') == '2px', 'The ninth <code>div</code> should have a <code>height</code> of 90 pixels and a <code>margin</code> of 2 pixels.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<style>",
" .bar {",
" width: 25px;",
" height: 100px;",
" /* Add your code below this line */",
" ",
" /* Add your code above this line */",
" display: inline-block;",
" background-color: blue;",
" }",
"</style>",
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" d3.select(\"body\").selectAll(\"div\")",
" .data(dataset)",
" .enter()",
" .append(\"div\")",
" .attr(\"class\", \"bar\")",
" // Add your code below this line",
" .style(\"height\", (d) => (d + \"px\"))",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa8367417b2b2512bcb",
"title": "Learn About SVG in D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"SVG stands for <code>Scalable Vector Graphics</code>.",
"Here \"scalable\" means that, if you zoom in or out on an object, it would not appear pixelated. It scales with the display system, whether it's on a small mobile screen or a large TV monitor.",
"SVG is used to make common geometric shapes. Since D3 maps data into a visual representation, it uses SVG to create the shapes for the visualization. SVG shapes for a web page must go within an HTML <code>svg</code> tag.",
"CSS can be scalable when styles use relative units (such as <code>vh</code>, <code>vw</code>, or percentages), but using SVG is more flexible to build data visualizations.",
"<hr>",
"Add an <code>svg</code> node to the <code>body</code> using <code>append()</code>. Give it a <code>width</code> attribute set to the provided <code>w</code> constant and a <code>height</code> attribute set to the provided <code>h</code> constant using the <code>attr()</code> method for each. You'll see it in the output because there's a <code>background-color</code> of pink applied to it in the <code>style</code> tag.",
"<strong>Note</strong><br>Width and height attributes do not have units. This is the building block of scaling - the element will always have a 5:1 width to height ratio, no matter what the zoom level is."
],
"tests": [
{
"text": "Your document should have 1 <code>svg</code> element.",
"testString": "assert($('svg').length == 1, 'Your document should have 1 <code>svg</code> element.');"
},
{
"text": "The <code>svg</code> element should have a <code>width</code> attribute set to 500.",
"testString": "assert($('svg').attr('width') == '500', 'The <code>svg</code> element should have a <code>width</code> attribute set to 500.');"
},
{
"text": "The <code>svg</code> element should have a <code>height</code> attribute set to 100.",
"testString": "assert($('svg').attr('height') == '100', 'The <code>svg</code> element should have a <code>height</code> attribute set to 100.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<style>",
" svg {",
" background-color: pink;",
" }",
"</style>",
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa8367417b2b2512bcc",
"title": "Display Shapes with SVG",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The last challenge created an <code>svg</code> element with a given width and height, which was visible because it had a <code>background-color</code> applied to it in the <code>style</code> tag. The code made space for the given width and height.",
"The next step is to create a shape to put in the <code>svg</code> area. There are a number of supported shapes in SVG, such as rectangles and circles. They are used to display data. For example, a rectangle (<code>&lt;rect&gt;</code>) SVG shape could create a bar in a bar chart.",
"When you place a shape into the <code>svg</code> area, you can specify where it goes with <code>x</code> and <code>y</code> coordinates. The origin point of (0, 0) is in the upper-left corner. Positive vales for <code>x</code> push the shape to the right, and positive values for <code>y</code> push the shape down from the origin point.",
"To place a shape in the middle of the 500 (width) x 100 (height) <code>svg</code> from last challenge, the <code>x</code> coordinate would be 250 and the <code>y</code> coordinate would be 50.",
"An SVG <code>rect</code> has four attributes. There are the <code>x</code> and <code>y</code> coordinates for where it is placed in the <code>svg</code> area. It also has a <code>height</code> and <code>width</code> to specify the size.",
"<hr>",
"Add a <code>rect</code> shape to the <code>svg</code> using <code>append()</code>, and give it a <code>width</code> attribute of 25 and <code>height</code> attribute of 100. Also, give the <code>rect</code> <code>x</code> and <code>y</code> attributes each set to 0."
],
"tests": [
{
"text": "Your document should have 1 <code>rect</code> element.",
"testString": "assert($('rect').length == 1, 'Your document should have 1 <code>rect</code> element.');"
},
{
"text": "The <code>rect</code> element should have a <code>width</code> attribute set to 25.",
"testString": "assert($('rect').attr('width') == '25', 'The <code>rect</code> element should have a <code>width</code> attribute set to 25.');"
},
{
"text": "The <code>rect</code> element should have a <code>height</code> attribute set to 100.",
"testString": "assert($('rect').attr('height') == '100', 'The <code>rect</code> element should have a <code>height</code> attribute set to 100.');"
},
{
"text": "The <code>rect</code> element should have an <code>x</code> attribute set to 0.",
"testString": "assert($('rect').attr('x') == '0', 'The <code>rect</code> element should have an <code>x</code> attribute set to 0.');"
},
{
"text": "The <code>rect</code> element should have a <code>y</code> attribute set to 0.",
"testString": "assert($('rect').attr('y') == '0', 'The <code>rect</code> element should have a <code>y</code> attribute set to 0.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h)",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa8367417b2b2512bcd",
"title": "Create a Bar for Each Data Point in the Set",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The last challenge added only one rectangle to the <code>svg</code> element to represent a bar. Here, you'll combine what you've learned so far about <code>data()</code>, <code>enter()</code>, and SVG shapes to create and append a rectangle for each data point in <code>dataset</code>.",
"A previous challenge showed the format for how to create and append a <code>div</code> for each item in <code>dataset</code>:",
"<blockquote>d3.select(\"body\").selectAll(\"div\")<br> .data(dataset)<br> .enter()<br> .append(\"div\")</blockquote>",
"There are a few differences working with <code>rect</code> elements instead of <code>divs</code>. The <code>rects</code> must be appended to an <code>svg</code> element, not directly to the <code>body</code>. Also, you need to tell D3 where to place each <code>rect</code> within the <code>svg</code> area. The bar placement will be covered in the next challenge.",
"<hr>",
"Use the <code>data()</code>, <code>enter()</code>, and <code>append()</code> methods to create and append a <code>rect</code> for each item in <code>dataset</code>. The bars should display all on top of each other, this will be fixed in the next challenge."
],
"tests": [
{
"text": "Your document should have 9 <code>rect</code> elements.",
"testString": "assert($('rect').length == 9, 'Your document should have 9 <code>rect</code> elements.');"
},
{
"text": "Your code should use the <code>data()</code> method.",
"testString": "assert(code.match(/\\.data/g), 'Your code should use the <code>data()</code> method.');"
},
{
"text": "Your code should use the <code>enter()</code> method.",
"testString": "assert(code.match(/\\.enter/g), 'Your code should use the <code>enter()</code> method.');"
},
{
"text": "Your code should use the <code>append()</code> method.",
"testString": "assert(code.match(/\\.append/g), 'Your code should use the <code>append()</code> method.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" .attr(\"x\", 0)",
" .attr(\"y\", 0)",
" .attr(\"width\", 25)",
" .attr(\"height\", 100);",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa9367417b2b2512bce",
"title": "Dynamically Set the Coordinates for Each Bar",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The last challenge created and appended a rectangle to the <code>svg</code> element for each point in <code>dataset</code> to represent a bar. Unfortunately, they were all stacked on top of each other.",
"The placement of a rectangle is handled by the <code>x</code> and <code>y</code> attributes. They tell D3 where to start drawing the shape in the <code>svg</code> area. The last challenge set them each to 0, so every bar was placed in the upper-left corner.",
"For a bar chart, all of the bars should sit on the same vertical level, which means the <code>y</code> value stays the same (at 0) for all bars. The <code>x</code> value, however, needs to change as you add new bars. Remember that larger <code>x</code> values push items farther to the right. As you go through the array elements in <code>dataset</code>, the x value should increase.",
"The <code>attr()</code> method in D3 accepts a callback function to dynamically set that attribute. The callback function takes two arguments, one for the data point itself (usually <code>d</code>) and one for the index of the data point in the array. The second argument for the index is optional. Here's the format:",
"<blockquote>selection.attr(\"property\", (d, i) => {<br> /* <br> * d is the data point value<br> * i is the index of the data point in the array<br> */<br>})</blockquote>",
"It's important to note that you do NOT need to write a <code>for</code> loop or use <code>forEach()</code> to iterate over the items in the data set. Recall that the <code>data()</code> method parses the data set, and any method that's chained after <code>data()</code> is run once for each item in the data set.",
"<hr>",
"Change the <code>x</code> attribute callback function so it returns the index times 30.",
"<strong>Note</strong><br>Each bar has a width of 25, so increasing each <code>x</code> value by 30 adds some space between the bars. Any value greater than 25 would work in this example."
],
"tests": [
{
"text": "The first <code>rect</code> should have an <code>x</code> value of 0.",
"testString": "assert($('rect').eq(0).attr('x') == '0', 'The first <code>rect</code> should have an <code>x</code> value of 0.');"
},
{
"text": "The second <code>rect</code> should have an <code>x</code> value of 30.",
"testString": "assert($('rect').eq(1).attr('x') == '30', 'The second <code>rect</code> should have an <code>x</code> value of 30.');"
},
{
"text": "The third <code>rect</code> should have an <code>x</code> value of 60.",
"testString": "assert($('rect').eq(2).attr('x') == '60', 'The third <code>rect</code> should have an <code>x</code> value of 60.');"
},
{
"text": "The fourth <code>rect</code> should have an <code>x</code> value of 90.",
"testString": "assert($('rect').eq(3).attr('x') == '90', 'The fourth <code>rect</code> should have an <code>x</code> value of 90.');"
},
{
"text": "The fifth <code>rect</code> should have an <code>x</code> value of 120.",
"testString": "assert($('rect').eq(4).attr('x') == '120', 'The fifth <code>rect</code> should have an <code>x</code> value of 120.');"
},
{
"text": "The sixth <code>rect</code> should have an <code>x</code> value of 150.",
"testString": "assert($('rect').eq(5).attr('x') == '150', 'The sixth <code>rect</code> should have an <code>x</code> value of 150.');"
},
{
"text": "The seventh <code>rect</code> should have an <code>x</code> value of 180.",
"testString": "assert($('rect').eq(6).attr('x') == '180', 'The seventh <code>rect</code> should have an <code>x</code> value of 180.');"
},
{
"text": "The eighth <code>rect</code> should have an <code>x</code> value of 210.",
"testString": "assert($('rect').eq(7).attr('x') == '210', 'The eighth <code>rect</code> should have an <code>x</code> value of 210.');"
},
{
"text": "The ninth <code>rect</code> should have an <code>x</code> value of 240.",
"testString": "assert($('rect').eq(8).attr('x') == '240', 'The ninth <code>rect</code> should have an <code>x</code> value of 240.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => {",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" })",
" .attr(\"y\", 0)",
" .attr(\"width\", 25)",
" .attr(\"height\", 100);",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa9367417b2b2512bcf",
"title": "Dynamically Change the Height of Each Bar",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The height of each bar can be set to the value of the data point in the array, similar to how the <code>x</code> value was set dynamically.",
"<blockquote>selection.attr(\"property\", (d, i) => {<br> /* <br> * d is the data point value<br> * i is the index of the data point in the array<br> */<br>})</blockquote>",
"<hr>",
"Change the callback function for the <code>height</code> attribute to return the data value times 3.",
"<strong>Note</strong><br>Remember that multiplying all data points by the same constant scales the data (like zooming in). It helps to see the differences between bar values in this example."
],
"tests": [
{
"text": "The first <code>rect</code> should have a <code>height</code> of 36.",
"testString": "assert($('rect').eq(0).attr('height') == '36', 'The first <code>rect</code> should have a <code>height</code> of 36.');"
},
{
"text": "The second <code>rect</code> should have a <code>height</code> of 93.",
"testString": "assert($('rect').eq(1).attr('height') == '93', 'The second <code>rect</code> should have a <code>height</code> of 93.');"
},
{
"text": "The third <code>rect</code> should have a <code>height</code> of 66.",
"testString": "assert($('rect').eq(2).attr('height') == '66', 'The third <code>rect</code> should have a <code>height</code> of 66.');"
},
{
"text": "The fourth <code>rect</code> should have a <code>height</code> of 51.",
"testString": "assert($('rect').eq(3).attr('height') == '51', 'The fourth <code>rect</code> should have a <code>height</code> of 51.');"
},
{
"text": "The fifth <code>rect</code> should have a <code>height</code> of 75.",
"testString": "assert($('rect').eq(4).attr('height') == '75', 'The fifth <code>rect</code> should have a <code>height</code> of 75.');"
},
{
"text": "The sixth <code>rect</code> should have a <code>height</code> of 54.",
"testString": "assert($('rect').eq(5).attr('height') == '54', 'The sixth <code>rect</code> should have a <code>height</code> of 54.');"
},
{
"text": "The seventh <code>rect</code> should have a <code>height</code> of 87.",
"testString": "assert($('rect').eq(6).attr('height') == '87', 'The seventh <code>rect</code> should have a <code>height</code> of 87.');"
},
{
"text": "The eighth <code>rect</code> should have a <code>height</code> of 42.",
"testString": "assert($('rect').eq(7).attr('height') == '42', 'The eighth <code>rect</code> should have a <code>height</code> of 42.');"
},
{
"text": "The ninth <code>rect</code> should have a <code>height</code> of 27.",
"testString": "assert($('rect').eq(8).attr('height') == '27', 'The ninth <code>rect</code> should have a <code>height</code> of 27.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", 0)",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => {",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" });",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa9367417b2b2512bd0",
"title": "Invert SVG Elements",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"You may have noticed the bar chart looked like it's upside-down, or inverted. This is because of how SVG uses (x, y) coordinates.",
"In SVG, the origin point for the coordinates is in the upper-left corner. An <code>x</code> coordinate of 0 places a shape on the left edge of the SVG area. A <code>y</code> coordinate of 0 places a shape on the top edge of the SVG area. Higher <code>x</code> values push the rectangle to the right. Higher <code>y</code> values push the rectangle down.",
"To make the bars right-side-up, you need to change the way the <code>y</code> coordinate is calculated. It needs to account for both the height of the bar and the total height of the SVG area.",
"The height of the SVG area is 100. If you have a data point of 0 in the set, you would want the bar to start at the bottom of the SVG area (not the top). To do this, the <code>y</code> coordinate needs a value of 100. If the data point value were 1, you would start with a <code>y</code> coordinate of 100 to set the bar at the bottom. Then you need to account for the height of the bar of 1, so the final <code>y</code> coordinate would be 99.",
"The <code>y</code> coordinate that is <code>y = heightOfSVG - heightOfBar</code> would place the bars right-side-up.",
"<hr>",
"Change the callback function for the <code>y</code> attribute to set the bars right-side-up. Remember that the <code>height</code> of the bar is 3 times the data value <code>d</code>.",
"<strong>Note</strong><br>In general, the relationship is <code>y = h - m * d</code>, where <code>m</code> is the constant that scales the data points."
],
"tests": [
{
"text": "The first <code>rect</code> should have a <code>y</code> value of 64.",
"testString": "assert($('rect').eq(0).attr('y') == h - (dataset[0] * 3), 'The first <code>rect</code> should have a <code>y</code> value of 64.');"
},
{
"text": "The second <code>rect</code> should have a <code>y</code> value of 7.",
"testString": "assert($('rect').eq(1).attr('y') == h - (dataset[1] * 3), 'The second <code>rect</code> should have a <code>y</code> value of 7.');"
},
{
"text": "The third <code>rect</code> should have a <code>y</code> value of 34.",
"testString": "assert($('rect').eq(2).attr('y') == h - (dataset[2] * 3), 'The third <code>rect</code> should have a <code>y</code> value of 34.');"
},
{
"text": "The fourth <code>rect</code> should have a <code>y</code> value of 49.",
"testString": "assert($('rect').eq(3).attr('y') == h - (dataset[3] * 3), 'The fourth <code>rect</code> should have a <code>y</code> value of 49.');"
},
{
"text": "The fifth <code>rect</code> should have a <code>y</code> value of 25.",
"testString": "assert($('rect').eq(4).attr('y') == h - (dataset[4] * 3), 'The fifth <code>rect</code> should have a <code>y</code> value of 25.');"
},
{
"text": "The sixth <code>rect</code> should have a <code>y</code> value of 46.",
"testString": "assert($('rect').eq(5).attr('y') == h - (dataset[5] * 3), 'The sixth <code>rect</code> should have a <code>y</code> value of 46.');"
},
{
"text": "The seventh <code>rect</code> should have a <code>y</code> value of 13.",
"testString": "assert($('rect').eq(6).attr('y') == h - (dataset[6] * 3), 'The seventh <code>rect</code> should have a <code>y</code> value of 13.');"
},
{
"text": "The eighth <code>rect</code> should have a <code>y</code> value of 58.",
"testString": "assert($('rect').eq(7).attr('y') == h - (dataset[7] * 3), 'The eighth <code>rect</code> should have a <code>y</code> value of 58.');"
},
{
"text": "The ninth <code>rect</code> should have a <code>y</code> value of 73.",
"testString": "assert($('rect').eq(8).attr('y') == h - (dataset[8] * 3), 'The ninth <code>rect</code> should have a <code>y</code> value of 73.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => {",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" })",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => 3 * d);",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fa9367417b2b2512bd1",
"title": "Change the Color of an SVG Element",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The bars are in the right position, but they are all the same black color. SVG has a way to change the color of the bars.",
"In SVG, a <code>rect</code> shape is colored with the <code>fill</code> attribute. It supports hex codes, color names, and rgb values, as well as more complex options like gradients and transparency.",
"<hr>",
"Add an <code>attr()</code> method to set the \"fill\" of all the bars to the color \"navy\"."
],
"tests": [
{
"text": "The bars should all have a <code>fill</code> color of navy.",
"testString": "assert($('rect').css('fill') == \"rgb(0, 0, 128)\", 'The bars should all have a <code>fill</code> color of navy.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - 3 * d)",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => 3 * d)",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7faa367417b2b2512bd2",
"title": "Add Labels to D3 Elements",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"D3 lets you label a graph element, such as a bar, using the SVG <code>text</code> element.",
"Like the <code>rect</code> element, a <code>text</code> element needs to have <code>x</code> and <code>y</code> attributes, to place it on the SVG canvas. It also needs to access the data to display those values.",
"D3 gives you a high level of control over how you label your bars.",
"<hr>",
"The code in the editor already binds the data to each new <code>text</code> element. First, append <code>text</code> nodes to the <code>svg</code>. Next, add attributes for the <code>x</code> and <code>y</code> coordinates. They should be calculated the same way as the <code>rect</code> ones, except the <code>y</code> value for the <code>text</code> should make the label sit 3 units higher than the bar. Finally, use the D3 <code>text()</code> method to set the label equal to the data point value.",
"<strong>Note</strong><br>For the label to sit higher than the bar, decide if the <code>y</code> value for the <code>text</code> should be 3 greater or 3 less than the <code>y</code> value for the bar."
],
"tests": [
{
"text": "The first <code>text</code> element should have a label of 12 and a <code>y</code> value of 61.",
"testString": "assert($('text').eq(0).text() == '12' && $('text').eq(0).attr('y') == '61', 'The first <code>text</code> element should have a label of 12 and a <code>y</code> value of 61.');"
},
{
"text": "The second <code>text</code> element should have a label of 31 and a <code>y</code> value of 4.",
"testString": "assert($('text').eq(1).text() == '31' && $('text').eq(1).attr('y') == '4', 'The second <code>text</code> element should have a label of 31 and a <code>y</code> value of 4.');"
},
{
"text": "The third <code>text</code> element should have a label of 22 and a <code>y</code> value of 31.",
"testString": "assert($('text').eq(2).text() == '22' && $('text').eq(2).attr('y') == '31', 'The third <code>text</code> element should have a label of 22 and a <code>y</code> value of 31.');"
},
{
"text": "The fourth <code>text</code> element should have a label of 17 and a <code>y</code> value of 46.",
"testString": "assert($('text').eq(3).text() == '17' && $('text').eq(3).attr('y') == '46', 'The fourth <code>text</code> element should have a label of 17 and a <code>y</code> value of 46.');"
},
{
"text": "The fifth <code>text</code> element should have a label of 25 and a <code>y</code> value of 22.",
"testString": "assert($('text').eq(4).text() == '25' && $('text').eq(4).attr('y') == '22', 'The fifth <code>text</code> element should have a label of 25 and a <code>y</code> value of 22.');"
},
{
"text": "The sixth <code>text</code> element should have a label of 18 and a <code>y</code> value of 43.",
"testString": "assert($('text').eq(5).text() == '18' && $('text').eq(5).attr('y') == '43', 'The sixth <code>text</code> element should have a label of 18 and a <code>y</code> value of 43.');"
},
{
"text": "The seventh <code>text</code> element should have a label of 29 and a <code>y</code> value of 10.",
"testString": "assert($('text').eq(6).text() == '29' && $('text').eq(6).attr('y') == '10', 'The seventh <code>text</code> element should have a label of 29 and a <code>y</code> value of 10.');"
},
{
"text": "The eighth <code>text</code> element should have a label of 14 and a <code>y</code> value of 55.",
"testString": "assert($('text').eq(7).text() == '14' && $('text').eq(7).attr('y') == '55', 'The eighth <code>text</code> element should have a label of 14 and a <code>y</code> value of 55.');"
},
{
"text": "The ninth <code>text</code> element should have a label of 9 and a <code>y</code> value of 70.",
"testString": "assert($('text').eq(8).text() == '9' && $('text').eq(8).attr('y') == '70', 'The ninth <code>text</code> element should have a label of 9 and a <code>y</code> value of 70.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - 3 * d)",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => 3 * d)",
" .attr(\"fill\", \"navy\");",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" // Add your code below this line",
" ",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"<body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7faa367417b2b2512bd3",
"title": "Style D3 Labels",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"D3 methods can add styles to the bar labels. The <code>fill</code> attribute sets the color of the text for a <code>text</code> node. The <code>style()</code> method sets CSS rules for other styles, such as \"font-family\" or \"font-size\".",
"<hr>",
"Set the <code>font-size</code> of the <code>text</code> elements to 25px, and the color of the text to red."
],
"tests": [
{
"text": "The labels should all have a <code>fill</code> color of red.",
"testString": "assert($('text').css('fill') == 'rgb(255, 0, 0)', 'The labels should all have a <code>fill</code> color of red.');"
},
{
"text": "The labels should all have a <code>font-size</code> of 25 pixels.",
"testString": "assert($('text').css('font-size') == '25px', 'The labels should all have a <code>font-size</code> of 25 pixels.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - 3 * d)",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => d * 3)",
" .attr(\"fill\", \"navy\");",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" .append(\"text\")",
" .text((d) => d)",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - (3 * d) - 3)",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7faa367417b2b2512bd4",
"title": "Add a Hover Effect to a D3 Element",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"It's possible to add effects that highlight a bar when the user hovers over it with the mouse. So far, the styling for the rectangles is applied with the built-in D3 and SVG methods, but you can use CSS as well.",
"You set the CSS class on the SVG elements with the <code>attr()</code> method. Then the <code>:hover</code> pseudo-class for your new class holds the style rules for any hover effects.",
"<hr>",
"Use the <code>attr()</code> method to add a class of <code>bar</code> to all the <code>rect</code> elements. This changes the <code>fill</code> color of the bar to brown when you mouse over it."
],
"tests": [
{
"text": "Your <code>rect</code> elements should have a class of <code>bar</code>.",
"testString": "assert($('rect').attr('class') == \"bar\", 'Your <code>rect</code> elements should have a class of <code>bar</code>.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<style>",
" .bar:hover {",
" fill: brown;",
" }",
"</style>",
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - 3 * d)",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => 3 * d)",
" .attr(\"fill\", \"navy\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" .append(\"text\")",
" .text((d) => d)",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - (3 * d) - 3);",
" ",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7faa367417b2b2512bd6",
"title": "Add a Tooltip to a D3 Element",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"A tooltip shows more information about an item on a page when the user hovers over that item. There are several ways to add a tooltip to a visualization, this challenge uses the SVG <code>title</code> element.",
"<code>title</code> pairs with the <code>text()</code> method to dynamically add data to the bars.",
"<hr>",
"Append a <code>title</code> element under each <code>rect</code> node. Then call the <code>text()</code> method with a callback function so the text displays the data value."
],
"tests": [
{
"text": "Your code should have 9 <code>title</code> elements.",
"testString": "assert($('title').length == 9, 'Your code should have 9 <code>title</code> elements.');"
},
{
"text": "The first <code>title</code> element should have tooltip text of 12.",
"testString": "assert($('title').eq(0).text() == '12', 'The first <code>title</code> element should have tooltip text of 12.');"
},
{
"text": "The second <code>title</code> element should have tooltip text of 31.",
"testString": "assert($('title').eq(1).text() == '31', 'The second <code>title</code> element should have tooltip text of 31.');"
},
{
"text": "The third <code>title</code> element should have tooltip text of 22.",
"testString": "assert($('title').eq(2).text() == '22', 'The third <code>title</code> element should have tooltip text of 22.');"
},
{
"text": "The fourth <code>title</code> element should have tooltip text of 17.",
"testString": "assert($('title').eq(3).text() == '17', 'The fourth <code>title</code> element should have tooltip text of 17.');"
},
{
"text": "The fifth <code>title</code> element should have tooltip text of 25.",
"testString": "assert($('title').eq(4).text() == '25', 'The fifth <code>title</code> element should have tooltip text of 25.');"
},
{
"text": "The sixth <code>title</code> element should have tooltip text of 18.",
"testString": "assert($('title').eq(5).text() == '18', 'The sixth <code>title</code> element should have tooltip text of 18.');"
},
{
"text": "The seventh <code>title</code> element should have tooltip text of 29.",
"testString": "assert($('title').eq(6).text() == '29', 'The seventh <code>title</code> element should have tooltip text of 29.');"
},
{
"text": "The eighth <code>title</code> element should have tooltip text of 14.",
"testString": "assert($('title').eq(7).text() == '14', 'The eighth <code>title</code> element should have tooltip text of 14.');"
},
{
"text": "The ninth <code>title</code> element should have tooltip text of 9.",
"testString": "assert($('title').eq(8).text() == '9', 'The ninth <code>title</code> element should have tooltip text of 9.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<style>",
" .bar:hover {",
" fill: brown;",
" }",
"</style>",
"<body>",
" <script>",
" const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];",
" ",
" const w = 500;",
" const h = 100;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"rect\")",
" .data(dataset)",
" .enter()",
" .append(\"rect\")",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - 3 * d)",
" .attr(\"width\", 25)",
" .attr(\"height\", (d, i) => d * 3)",
" .attr(\"fill\", \"navy\")",
" .attr(\"class\", \"bar\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" .append(\"text\")",
" .text((d) => d)",
" .attr(\"x\", (d, i) => i * 30)",
" .attr(\"y\", (d, i) => h - (d * 3 + 3)) ",
" ",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fab367417b2b2512bd7",
"title": "Create a Scatterplot with SVG Circles",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"A scatter plot is another type of visualization. It usually uses circles to map data points, which have two values each. These values tie to the <code>x</code> and <code>y</code> axes, and are used to position the circle in the visualization.",
"SVG has a <code>circle</code> tag to create the circle shape. It works a lot like the <code>rect</code> elements you used for the bar chart.",
"<hr>",
"Use the <code>data()</code>, <code>enter()</code>, and <code>append()</code> methods to bind <code>dataset</code> to new <code>circle</code> elements that are appended to the SVG canvas."
],
"tests": [
{
"text": "Your code should have 10 <code>circle</code> elements.",
"testString": "assert($('circle').length == 10, 'Your code should have 10 <code>circle</code> elements.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [",
" [ 34, 78 ],",
" [ 109, 280 ],",
" [ 310, 120 ],",
" [ 79, 411 ],",
" [ 420, 220 ],",
" [ 233, 145 ],",
" [ 333, 96 ],",
" [ 222, 333 ],",
" [ 78, 320 ],",
" [ 21, 123 ]",
" ];",
" ",
" ",
" const w = 500;",
" const h = 500;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"circle\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" ",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fab367417b2b2512bd8",
"title": "Add Attributes to the Circle Elements",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The last challenge created the <code>circle</code> elements for each point in the <code>dataset</code>, and appended them to the SVG canvas. But D3 needs more information about the position and size of each <code>circle</code> to display them correctly.",
"A <code>circle</code> in SVG has three main attributes. The <code>cx</code> and <code>cy</code> attributes are the coordinates. They tell D3 where to position the <em>center</em> of the shape on the SVG canvas. The radius (<code>r</code> attribute) gives the size of the <code>circle</code>.",
"Just like the <code>rect</code> <code>y</code> coordinate, the <code>cy</code> attribute for a <code>circle</code> is measured from the top of the SVG canvas, not from the bottom.",
"All three attributes can use a callback function to set their values dynamically. Remember that all methods chained after <code>data(dataset)</code> run once per item in <code>dataset</code>. The <code>d</code> parameter in the callback function refers to the current item in <code>dataset</code>, which is an array for each point. You use bracket notation, like <code>d[0]</code>, to access the values in that array.",
"<hr>",
"Add <code>cx</code>, <code>cy</code>, and <code>r</code> attributes to the <code>circle</code> elements. The <code>cx</code> value should be the first number in the array for each item in <code>dataset</code>. The <code>cy</code> value should be based off the second number in the array, but make sure to show the chart right-side-up and not inverted. The <code>r</code> value should be 5 for all circles."
],
"tests": [
{
"text": "Your code should have 10 <code>circle</code> elements.",
"testString": "assert($('circle').length == 10, 'Your code should have 10 <code>circle</code> elements.');"
},
{
"text": "The first <code>circle</code> element should have a <code>cx</code> value of 34, a <code>cy</code> value of 422, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(0).attr('cx') == '34' && $('circle').eq(0).attr('cy') == '422' && $('circle').eq(0).attr('r') == '5', 'The first <code>circle</code> element should have a <code>cx</code> value of 34, a <code>cy</code> value of 422, and an <code>r</code> value of 5.');"
},
{
"text": "The second <code>circle</code> element should have a <code>cx</code> value of 109, a <code>cy</code> value of 220, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(1).attr('cx') == '109' && $('circle').eq(1).attr('cy') == '220' && $('circle').eq(1).attr('r') == '5', 'The second <code>circle</code> element should have a <code>cx</code> value of 109, a <code>cy</code> value of 220, and an <code>r</code> value of 5.');"
},
{
"text": "The third <code>circle</code> element should have a <code>cx</code> value of 310, a <code>cy</code> value of 380, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(2).attr('cx') == '310' && $('circle').eq(2).attr('cy') == '380' && $('circle').eq(2).attr('r') == '5', 'The third <code>circle</code> element should have a <code>cx</code> value of 310, a <code>cy</code> value of 380, and an <code>r</code> value of 5.');"
},
{
"text": "The fourth <code>circle</code> element should have a <code>cx</code> value of 79, a <code>cy</code> value of 89, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(3).attr('cx') == '79' && $('circle').eq(3).attr('cy') == '89' && $('circle').eq(3).attr('r') == '5', 'The fourth <code>circle</code> element should have a <code>cx</code> value of 79, a <code>cy</code> value of 89, and an <code>r</code> value of 5.');"
},
{
"text": "The fifth <code>circle</code> element should have a <code>cx</code> value of 420, a <code>cy</code> value of 280, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(4).attr('cx') == '420' && $('circle').eq(4).attr('cy') == '280' && $('circle').eq(4).attr('r') == '5', 'The fifth <code>circle</code> element should have a <code>cx</code> value of 420, a <code>cy</code> value of 280, and an <code>r</code> value of 5.');"
},
{
"text": "The sixth <code>circle</code> element should have a <code>cx</code> value of 233, a <code>cy</code> value of 355, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(5).attr('cx') == '233' && $('circle').eq(5).attr('cy') == '355' && $('circle').eq(5).attr('r') == '5', 'The sixth <code>circle</code> element should have a <code>cx</code> value of 233, a <code>cy</code> value of 355, and an <code>r</code> value of 5.');"
},
{
"text": "The seventh <code>circle</code> element should have a <code>cx</code> value of 333, a <code>cy</code> value of 404, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(6).attr('cx') == '333' && $('circle').eq(6).attr('cy') == '404' && $('circle').eq(6).attr('r') == '5', 'The seventh <code>circle</code> element should have a <code>cx</code> value of 333, a <code>cy</code> value of 404, and an <code>r</code> value of 5.');"
},
{
"text": "The eighth <code>circle</code> element should have a <code>cx</code> value of 222, a <code>cy</code> value of 167, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(7).attr('cx') == '222' && $('circle').eq(7).attr('cy') == '167' && $('circle').eq(7).attr('r') == '5', 'The eighth <code>circle</code> element should have a <code>cx</code> value of 222, a <code>cy</code> value of 167, and an <code>r</code> value of 5.');"
},
{
"text": "The ninth <code>circle</code> element should have a <code>cx</code> value of 78, a <code>cy</code> value of 180, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(8).attr('cx') == '78' && $('circle').eq(8).attr('cy') == '180' && $('circle').eq(8).attr('r') == '5', 'The ninth <code>circle</code> element should have a <code>cx</code> value of 78, a <code>cy</code> value of 180, and an <code>r</code> value of 5.');"
},
{
"text": "The tenth <code>circle</code> element should have a <code>cx</code> value of 21, a <code>cy</code> value of 377, and an <code>r</code> value of 5.",
"testString": "assert($('circle').eq(9).attr('cx') == '21' && $('circle').eq(9).attr('cy') == '377' && $('circle').eq(9).attr('r') == '5', 'The tenth <code>circle</code> element should have a <code>cx</code> value of 21, a <code>cy</code> value of 377, and an <code>r</code> value of 5.');"
}
],
"solutions": [],
"hints": [
"The cy attribute should be the second number of the data point array subtracted from the height of the SVG."
],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [",
" [ 34, 78 ],",
" [ 109, 280 ],",
" [ 310, 120 ],",
" [ 79, 411 ],",
" [ 420, 220 ],",
" [ 233, 145 ],",
" [ 333, 96 ],",
" [ 222, 333 ],",
" [ 78, 320 ],",
" [ 21, 123 ]",
" ];",
" ",
" ",
" const w = 500;",
" const h = 500;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"circle\")",
" .data(dataset)",
" .enter()",
" .append(\"circle\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" ",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fab367417b2b2512bd9",
"title": "Add Labels to Scatter Plot Circles",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"You can add text to create labels for the points in a scatter plot.",
"The goal is to display the comma-separated values for the first (<code>x</code>) and second (<code>y</code>) fields of each item in <code>dataset</code>.",
"The <code>text</code> nodes need <code>x</code> and <code>y</code> attributes to position it on the SVG canvas. In this challenge, the <code>y</code> value (which determines height) can use the same value that the <code>circle</code> uses for its <code>cy</code> attribute. The <code>x</code> value can be slightly larger than the <code>cx</code> value of the <code>circle</code>, so the label is visible. This will push the label to the right of the plotted point.",
"<hr>",
"Label each point on the scatter plot using the <code>text</code> elements. The text of the label should be the two values separated by a comma and a space. For example, the label for the first point is \"34, 78\". Set the <code>x</code> attribute so it's 5 units more than the value you used for the <code>cx</code> attribute on the <code>circle</code>. Set the <code>y</code> attribute the same way that's used for the <code>cy</code> value on the <code>circle</code>."
],
"tests": [
{
"text": "Your code should have 10 <code>text</code> elements.",
"testString": "assert($('text').length == 10, 'Your code should have 10 <code>text</code> elements.');"
},
{
"text": "The first label should have text of \"34, 78\", an <code>x</code> value of 39, and a <code>y</code> value of 422.",
"testString": "assert($('text').eq(0).text() == '34, 78' && $('text').eq(0).attr('x') == '39' && $('text').eq(0).attr('y') == '422', 'The first label should have text of \"34, 78\", an <code>x</code> value of 39, and a <code>y</code> value of 422.');"
},
{
"text": "The second label should have text of \"109, 280\", an <code>x</code> value of 114, and a <code>y</code> value of 220.",
"testString": "assert($('text').eq(1).text() == '109, 280' && $('text').eq(1).attr('x') == '114' && $('text').eq(1).attr('y') == '220', 'The second label should have text of \"109, 280\", an <code>x</code> value of 114, and a <code>y</code> value of 220.');"
},
{
"text": "The third label should have text of \"310, 120\", an <code>x</code> value of 315, and a <code>y</code> value of 380.",
"testString": "assert($('text').eq(2).text() == '310, 120' && $('text').eq(2).attr('x') == '315' && $('text').eq(2).attr('y') == '380', 'The third label should have text of \"310, 120\", an <code>x</code> value of 315, and a <code>y</code> value of 380.');"
},
{
"text": "The fourth label should have text of \"79, 411\", an <code>x</code> value of 84, and a <code>y</code> value of 89.",
"testString": "assert($('text').eq(3).text() == '79, 411' && $('text').eq(3).attr('x') == '84' && $('text').eq(3).attr('y') == '89', 'The fourth label should have text of \"79, 411\", an <code>x</code> value of 84, and a <code>y</code> value of 89.');"
},
{
"text": "The fifth label should have text of \"420, 220\", an <code>x</code> value of 425, and a <code>y</code> value of 280.",
"testString": "assert($('text').eq(4).text() == '420, 220' && $('text').eq(4).attr('x') == '425' && $('text').eq(4).attr('y') == '280', 'The fifth label should have text of \"420, 220\", an <code>x</code> value of 425, and a <code>y</code> value of 280.');"
},
{
"text": "The sixth label should have text of \"233, 145\", an <code>x</code> value of 238, and a <code>y</code> value of 355.",
"testString": "assert($('text').eq(5).text() == '233, 145' && $('text').eq(5).attr('x') == '238' && $('text').eq(5).attr('y') == '355', 'The sixth label should have text of \"233, 145\", an <code>x</code> value of 238, and a <code>y</code> value of 355.');"
},
{
"text": "The seventh label should have text of \"333, 96\", an <code>x</code> value of 338, and a <code>y</code> value of 404.",
"testString": "assert($('text').eq(6).text() == '333, 96' && $('text').eq(6).attr('x') == '338' && $('text').eq(6).attr('y') == '404', 'The seventh label should have text of \"333, 96\", an <code>x</code> value of 338, and a <code>y</code> value of 404.');"
},
{
"text": "The eighth label should have text of \"222, 333\", an <code>x</code> value of 227, and a <code>y</code> value of 167.",
"testString": "assert($('text').eq(7).text() == '222, 333' && $('text').eq(7).attr('x') == '227' && $('text').eq(7).attr('y') == '167', 'The eighth label should have text of \"222, 333\", an <code>x</code> value of 227, and a <code>y</code> value of 167.');"
},
{
"text": "The ninth label should have text of \"78, 320\", an <code>x</code> value of 83, and a <code>y</code> value of 180.",
"testString": "assert($('text').eq(8).text() == '78, 320' && $('text').eq(8).attr('x') == '83' && $('text').eq(8).attr('y') == '180', 'The ninth label should have text of \"78, 320\", an <code>x</code> value of 83, and a <code>y</code> value of 180.');"
},
{
"text": "The tenth label should have text of \"21, 123\", an <code>x</code> value of 26, and a <code>y</code> value of 377.",
"testString": "assert($('text').eq(9).text() == '21, 123' && $('text').eq(9).attr('x') == '26' && $('text').eq(9).attr('y') == '377', 'The tenth label should have text of \"21, 123\", an <code>x</code> value of 26, and a <code>y</code> value of 377.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [",
" [ 34, 78 ],",
" [ 109, 280 ],",
" [ 310, 120 ],",
" [ 79, 411 ],",
" [ 420, 220 ],",
" [ 233, 145 ],",
" [ 333, 96 ],",
" [ 222, 333 ],",
" [ 78, 320 ],",
" [ 21, 123 ]",
" ];",
" ",
" ",
" const w = 500;",
" const h = 500;",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"circle\")",
" .data(dataset)",
" .enter()",
" .append(\"circle\")",
" .attr(\"cx\", (d, i) => d[0])",
" .attr(\"cy\", (d, i) => h - d[1])",
" .attr(\"r\", 5);",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" .append(\"text\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fab367417b2b2512bda",
"title": "Create a Linear Scale with D3",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The bar and scatter plot charts both plotted data directly onto the SVG canvas. However, if the height of a bar or one of the data points were larger than the SVG height or width values, it would go outside the SVG area.",
"In D3, there are scales to help plot data. <code>Scales</code> are functions that tell the program how to map a set of raw data points onto the pixels of the SVG canvas.",
"For example, say you have a 100x500-sized SVG canvas and you want to plot Gross Domestic Product (GDP) for a number of countries. The set of numbers would be in the billion or trillion-dollar range. You provide D3 a type of scale to tell it how to place the large GDP values into that 100x500-sized area.",
"It's unlikely you would plot raw data as-is. Before plotting it, you set the scale for your entire data set, so that the <code>x</code> and <code>y</code> values fit your canvas width and height.",
"D3 has several scale types. For a linear scale (usually used with quantitative data), there is the D3 method <code>scaleLinear()</code>:",
"<code> const scale = d3.scaleLinear()</code>",
"By default, a scale uses the identity relationship. The value of the input is the same as the value of the output. A separate challenge covers how to change this.",
"<hr>",
"Change the <code>scale</code> variable to create a linear scale. Then set the <code>output</code> variable to the scale called with an input argument of 50."
],
"tests": [
{
"text": "The text in the <code>h2</code> should be 50.",
"testString": "assert($('h2').text() == '50', 'The text in the <code>h2</code> should be 50.');"
},
{
"text": "Your code should use the <code>scaleLinear()</code> method.",
"testString": "assert(code.match(/\\.scaleLinear/g), 'Your code should use the <code>scaleLinear()</code> method.');"
},
{
"text": "The <code>output</code> variable should call <code>scale</code> with an argument of 50.",
"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.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<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>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fac367417b2b2512bdb",
"title": "Set a Domain and a Range on a Scale",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"By default, scales use the identity relationship - the input value maps to the output value. But scales can be much more flexible and interesting.",
"Say a data set has values ranging from 50 to 480. This is the input information for a scale, and is also known as the domain.",
"You want to map those points along the <code>x</code> axis on the SVG canvas, between 10 units and 500 units. This is the output information, which is also known as the range.",
"The <code>domain()</code> and <code>range()</code> methods set these values for the scale. Both methods take an array of at least two elements as an argument. Here's an example:",
"<blockquote>// Set a domain<br>// The domain covers the set of input values<br>scale.domain([50, 480]);<br>// Set a range<br>// The range covers the set of output values<br>scale.range([10, 500]);<br>scale(50) // Returns 10<br>scale(480) // Returns 500<br>scale(325) // Returns 323.37<br>scale(750) // Returns 807.67<br>d3.scaleLinear()</blockquote>",
"Notice that the scale uses the linear relationship between the domain and range values to figure out what the output should be for a given number. The minimum value in the domain (50) maps to the minimum value (10) in the range.",
"<hr>",
"Create a scale and set its domain to <code>[250, 500]</code> and range to <code>[10, 150]</code>.",
"<strong>Note</strong><br>You can chain the <code>domain()</code> and <code>range()</code> methods onto the <code>scale</code> variable."
],
"tests": [
{
"text": "Your code should use the <code>domain()</code> method.",
"testString": "assert(code.match(/\\.domain/g), 'Your code should use the <code>domain()</code> method.');"
},
{
"text": "The <code>domain()</code> of the scale should be set to <code>[250, 500]</code>.",
"testString": "assert(JSON.stringify(scale.domain()) == JSON.stringify([250, 500]), 'The <code>domain()</code> of the scale should be set to <code>[250, 500]</code>.');"
},
{
"text": "Your code should use the <code>range()</code> method.",
"testString": "assert(code.match(/\\.range/g), 'Your code should use the <code>range()</code> method.');"
},
{
"text": "The <code>range()</code> of the scale should be set to <code>[10, 150]</code>.",
"testString": "assert(JSON.stringify(scale.range()) == JSON.stringify([10, 150]), 'The <code>range()</code> of the scale should be set to <code>[10, 150]</code>.');"
},
{
"text": "The text in the <code>h2</code> should be -102.",
"testString": "assert($('h2').text() == '-102', 'The text in the <code>h2</code> should be -102.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" // Add your code below this line",
" const scale = d3.scaleLinear();",
" ",
" ",
" ",
" // Add your code above this line",
" const output = scale(50);",
" d3.select(\"body\")",
" .append(\"h2\")",
" .text(output);",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fac367417b2b2512bdc",
"title": "Use the d3.max and d3.min Functions to Find Minimum and Maximum Values in a Dataset",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The D3 methods <code>domain()</code> and <code>range()</code> set that information for your scale based on the data. There are a couple methods to make that easier.",
"Often when you set the domain, you'll want to use the minimum and maximum values within the data set. Trying to find these values manually, especially in a large data set, may cause errors.",
"D3 has two methods - <code>min()</code> and <code>max()</code> to return this information. Here's an example:",
"<blockquote>const exampleData = [34, 234, 73, 90, 6, 52];<br>d3.min(exampleData) // Returns 6<br>d3.max(exampleData) // Returns 234</blockquote>",
"A dataset may have nested arrays, like the [x, y] coordinate pairs that were in the scatter plot example. In that case, you need to tell D3 how to calculate the maximum and minimum.",
"Fortunately, both the <code>min()</code> and <code>max()</code> methods take a callback function.",
"In this example, the callback function's argument <code>d</code> is for the current inner array. The callback needs to return the element from the inner array (the x or y value) over which you want to compute the maximum or minimum. Here's an example for how to find the min and max values with an array of arrays:",
"<blockquote>const locationData = [[1, 7],[6, 3],[8, 3]];<br>// Returns the smallest number out of the first elements<br>const minX = d3.min(locationData, (d) => d[0]);<br>// minX compared 1, 6, and 8 and is set to 1</blockquote>",
"<hr>",
"The <code>positionData</code> variable holds a 3-dimensional (3D) array. Use a D3 method to find the maximum value of the z coordinate (the third value) from the arrays and save it in the <code>output</code> variable.",
"<strong>Note</strong><br>Fun fact - D3 can plot 3D arrays."
],
"tests": [
{
"text": "The text in the <code>h2</code> should be 8.",
"testString": "assert(output == 8 && $('h2').text() == '8', 'The text in the <code>h2</code> should be 8.');"
},
{
"text": "Your code should use the <code>max()</code> method.",
"testString": "assert(code.match(/\\.max/g), 'Your code should use the <code>max()</code> method.')"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<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>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fac367417b2b2512bdd",
"title": "Use Dynamic Scales",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The D3 <code>min()</code> and <code>max()</code> methods are useful to help set the scale.",
"Given a complex data set, one priority is to set the scale so the visualization fits the SVG container's width and height. You want all the data plotted inside the SVG canvas so it's visible on the web page.",
"The example below sets the x-axis scale for scatter plot data. The <code>domain()</code> method passes information to the scale about the raw data values for the plot. The <code>range()</code> method gives it information about the actual space on the web page for the visualization.",
"In the example, the domain goes from 0 to the maximum in the set. It uses the <code>max()</code> method with a callback function based on the x values in the arrays. The range uses the SVG canvas' width (<code>w</code>), but it includes some padding, too. This puts space between the scatter plot dots and the edge of the SVG canvas.",
"<blockquote>const dataset = [<br> [ 34, 78 ],<br> [ 109, 280 ],<br> [ 310, 120 ],<br> [ 79, 411 ],<br> [ 420, 220 ],<br> [ 233, 145 ],<br> [ 333, 96 ],<br> [ 222, 333 ],<br> [ 78, 320 ],<br> [ 21, 123 ]<br> ];<br>const w = 500;<br>const h = 500;<br><br>// Padding between the SVG canvas boundary and the plot<br>const padding = 30;<br>const xScale = d3.scaleLinear()<br> .domain([0, d3.max(dataset, (d) => d[0])])<br> .range([padding, w - padding]);</blockquote>",
"The padding may be confusing at first. Picture the x-axis as a horizontal line from 0 to 500 (the width value for the SVG canvas). Including the padding in the <code>range()</code> method forces the plot to start at 30 along that line (instead of 0), and end at 470 (instead of 500).",
"<hr>",
"Use the <code>yScale</code> variable to create a linear y-axis scale. The domain should start at zero and go to the maximum y value in the set. The range should use the SVG height (<code>h</code>) and include padding.",
"<strong>Note</strong><br>Remember to keep the plot right-side-up. When you set the range for the y coordinates, the higher value (height minus padding) is the first argument, and the lower value is the second argument."
],
"tests": [
{
"text": "The text in the <code>h2</code> should be 30.",
"testString": "assert(output == 30 && $('h2').text() == '30', 'The text in the <code>h2</code> should be 30.');"
},
{
"text": "The <code>domain()</code> of yScale should be equivalent to <code>[0, 411]</code>.",
"testString": "assert(JSON.stringify(yScale.domain()) == JSON.stringify([0, 411]), 'The <code>domain()</code> of yScale should be equivalent to <code>[0, 411]</code>.');"
},
{
"text": "The <code>range()</code> of yScale should be equivalent to <code>[470, 30]</code>.",
"testString": "assert(JSON.stringify(yScale.range()) == JSON.stringify([470, 30]), 'The <code>range()</code> of yScale should be equivalent to <code>[470, 30]</code>.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [",
" [ 34, 78 ],",
" [ 109, 280 ],",
" [ 310, 120 ],",
" [ 79, 411 ],",
" [ 420, 220 ],",
" [ 233, 145 ],",
" [ 333, 96 ],",
" [ 222, 333 ],",
" [ 78, 320 ],",
" [ 21, 123 ]",
" ];",
" ",
" const w = 500;",
" const h = 500;",
" ",
" // Padding between the SVG canvas boundary and the plot",
" const padding = 30;",
" ",
" // Create an x and y scale",
" ",
" const xScale = d3.scaleLinear()",
" .domain([0, d3.max(dataset, (d) => d[0])])",
" .range([padding, w - padding]);",
" ",
" // Add your code below this line",
" ",
" const yScale = undefined;",
" ",
" ",
" // Add your code above this line",
" ",
" const output = yScale(411); // Returns 30",
" d3.select(\"body\")",
" .append(\"h2\")",
" .text(output)",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"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"
}
],
"description": [
"With the scales set up, it's time to map the scatter plot again. The scales are like processing functions that turn the x and y raw data into values that fit and render correctly on the SVG canvas. They keep the data within the screen's plotting area.",
"You set the coordinate attribute values for an SVG shape with the scaling function. This includes <code>x</code> and <code>y</code> attributes for <code>rect</code> or <code>text</code> elements, or <code>cx</code> and <code>cy</code> for <code>circles</code>. Here's an example:",
"<blockquote>shape<br> .attr(\"x\", (d) => xScale(d[0]))</blockquote>",
"Scales set shape coordinate attributes to place the data points onto the SVG canvas. You don't need to apply scales when you display the actual data value, for example, in the <code>text()</code> method for a tooltip or label.",
"<hr>",
"Use <code>xScale</code> and <code>yScale</code> to position both the <code>circle</code> and <code>text</code> shapes onto the SVG canvas. For the <code>circles</code>, apply the scales to set the <code>cx</code> and <code>cy</code> attributes. Give them a radius of 5 units, too.",
"For the <code>text</code> elements, apply the scales to set the <code>x</code> and <code>y</code> attributes. The labels should be offset to the right of the dots. To do this, add 10 units to the x data value before passing it to the <code>xScale</code>."
],
"tests": [
{
"text": "Your code should have 10 <code>circle</code> elements.",
"testString": "assert($('circle').length == 10, 'Your code should have 10 <code>circle</code> elements.');"
},
{
"text": "The first <code>circle</code> element should have a <code>cx</code> value of approximately 91 and a <code>cy</code> value of approximately 368 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 91 and a <code>cy</code> value of approximately 368 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The second <code>circle</code> element should have a <code>cx</code> value of approximately 159 and a <code>cy</code> value of approximately 181 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 159 and a <code>cy</code> value of approximately 181 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The third <code>circle</code> element should have a <code>cx</code> value of approximately 340 and a <code>cy</code> value of approximately 329 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 340 and a <code>cy</code> value of approximately 329 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The fourth <code>circle</code> element should have a <code>cx</code> value of approximately 131 and a <code>cy</code> value of approximately 60 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 131 and a <code>cy</code> value of approximately 60 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The fifth <code>circle</code> element should have a <code>cx</code> value of approximately 440 and a <code>cy</code> value of approximately 237 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 440 and a <code>cy</code> value of approximately 237 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The sixth <code>circle</code> element should have a <code>cx</code> value of approximately 271 and a <code>cy</code> value of approximately 306 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 271 and a <code>cy</code> value of approximately 306 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The seventh <code>circle</code> element should have a <code>cx</code> value of approximately 361 and a <code>cy</code> value of approximately 351 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 361 and a <code>cy</code> value of approximately 351 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The eighth <code>circle</code> element should have a <code>cx</code> value of approximately 261 and a <code>cy</code> value of approximately 132 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 261 and a <code>cy</code> value of approximately 132 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The ninth <code>circle</code> element should have a <code>cx</code> value of approximately 131 and a <code>cy</code> value of approximately 144 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 131 and a <code>cy</code> value of approximately 144 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "The tenth <code>circle</code> element should have a <code>cx</code> value of approximately 79 and a <code>cy</code> value of approximately 326 after applying the scales. It should also have an <code>r</code> value of 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 <code>circle</code> element should have a <code>cx</code> value of approximately 79 and a <code>cy</code> value of approximately 326 after applying the scales. It should also have an <code>r</code> value of 5.');"
},
{
"text": "Your code should have 10 <code>text</code> elements.",
"testString": "assert($('text').length == 10, 'Your code should have 10 <code>text</code> elements.');"
},
{
"text": "The first label should have an <code>x</code> value of approximately 100 and a <code>y</code> value of approximately 368 after applying the scales.",
"testString": "assert(Math.round($('text').eq(0).attr('x')) == '100' && Math.round($('text').eq(0).attr('y')) == '368', 'The first label should have an <code>x</code> value of approximately 100 and a <code>y</code> value of approximately 368 after applying the scales.');"
},
{
"text": "The second label should have an <code>x</code> value of approximately 168 and a <code>y</code> value of approximately 181 after applying the scales.",
"testString": "assert(Math.round($('text').eq(1).attr('x')) == '168' && Math.round($('text').eq(1).attr('y')) == '181', 'The second label should have an <code>x</code> value of approximately 168 and a <code>y</code> value of approximately 181 after applying the scales.');"
},
{
"text": "The third label should have an <code>x</code> value of approximately 350 and a <code>y</code> value of approximately 329 after applying the scales.",
"testString": "assert(Math.round($('text').eq(2).attr('x')) == '350' && Math.round($('text').eq(2).attr('y')) == '329', 'The third label should have an <code>x</code> value of approximately 350 and a <code>y</code> value of approximately 329 after applying the scales.');"
},
{
"text": "The fourth label should have an <code>x</code> value of approximately 141 and a <code>y</code> value of approximately 60 after applying the scales.",
"testString": "assert(Math.round($('text').eq(3).attr('x')) == '141' && Math.round($('text').eq(3).attr('y')) == '60', 'The fourth label should have an <code>x</code> value of approximately 141 and a <code>y</code> value of approximately 60 after applying the scales.');"
},
{
"text": "The fifth label should have an <code>x</code> value of approximately 449 and a <code>y</code> value of approximately 237 after applying the scales.",
"testString": "assert(Math.round($('text').eq(4).attr('x')) == '449' && Math.round($('text').eq(4).attr('y')) == '237', 'The fifth label should have an <code>x</code> value of approximately 449 and a <code>y</code> value of approximately 237 after applying the scales.');"
},
{
"text": "The sixth label should have an <code>x</code> value of approximately 280 and a <code>y</code> value of approximately 306 after applying the scales.",
"testString": "assert(Math.round($('text').eq(5).attr('x')) == '280' && Math.round($('text').eq(5).attr('y')) == '306', 'The sixth label should have an <code>x</code> value of approximately 280 and a <code>y</code> value of approximately 306 after applying the scales.');"
},
{
"text": "The seventh label should have an <code>x</code> value of approximately 370 and a <code>y</code> value of approximately 351 after applying the scales.",
"testString": "assert(Math.round($('text').eq(6).attr('x')) == '370' && Math.round($('text').eq(6).attr('y')) == '351', 'The seventh label should have an <code>x</code> value of approximately 370 and a <code>y</code> value of approximately 351 after applying the scales.');"
},
{
"text": "The eighth label should have an <code>x</code> value of approximately 270 and a <code>y</code> value of approximately 132 after applying the scales.",
"testString": "assert(Math.round($('text').eq(7).attr('x')) == '270' && Math.round($('text').eq(7).attr('y')) == '132', 'The eighth label should have an <code>x</code> value of approximately 270 and a <code>y</code> value of approximately 132 after applying the scales.');"
},
{
"text": "The ninth label should have an <code>x</code> value of approximately 140 and a <code>y</code> value of approximately 144 after applying the scales.",
"testString": "assert(Math.round($('text').eq(8).attr('x')) == '140' && Math.round($('text').eq(8).attr('y')) == '144', 'The ninth label should have an <code>x</code> value of approximately 140 and a <code>y</code> value of approximately 144 after applying the scales.');"
},
{
"text": "The tenth label should have an <code>x</code> value of approximately 88 and a <code>y</code> value of approximately 326 after applying the scales.",
"testString": "assert(Math.round($('text').eq(9).attr('x')) == '88' && Math.round($('text').eq(9).attr('y')) == '326', 'The tenth label should have an <code>x</code> value of approximately 88 and a <code>y</code> value of approximately 326 after applying the scales.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [",
" [ 34, 78 ],",
" [ 109, 280 ],",
" [ 310, 120 ],",
" [ 79, 411 ],",
" [ 420, 220 ],",
" [ 233, 145 ],",
" [ 333, 96 ],",
" [ 222, 333 ],",
" [ 78, 320 ],",
" [ 21, 123 ]",
" ];",
" ",
" const w = 500;",
" const h = 500;",
" const padding = 60;",
" ",
" const xScale = d3.scaleLinear()",
" .domain([0, d3.max(dataset, (d) => d[0])])",
" .range([padding, w - padding]);",
" ",
" const yScale = d3.scaleLinear()",
" .domain([0, d3.max(dataset, (d) => d[1])])",
" .range([h - padding, padding]);",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"circle\")",
" .data(dataset)",
" .enter()",
" .append(\"circle\")",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" .append(\"text\")",
" .text((d) => (d[0] + \", \"",
" + d[1]))",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7fad367417b2b2512bdf",
"title": "Add Axes to a Visualization",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"Another way to improve the scatter plot is to add an x-axis and a y-axis.",
"D3 has two methods <code>axisLeft()</code> and <code>axisBottom()</code> to render the y and x axes, respectively. (Axes is the plural form of axis). Here's an example to create the x-axis based on the <code>xScale</code> in the previous challenges:",
"<code>const xAxis = d3.axisBottom(xScale);</code>",
"The next step is to render the axis on the SVG canvas. To do so, you can use a general SVG component, the <code>g</code> element. The <code>g</code> stands for group.",
"Unlike <code>rect</code>, <code>circle</code>, and <code>text</code>, an axis is just a straight line when it's rendered. Because it is a simple shape, using <code>g</code> works.",
"The last step is to apply a <code>transform</code> attribute to position the axis on the SVG canvas in the right place. Otherwise, the line would render along the border of SVG canvas and wouldn't be visible.",
"SVG supports different types of <code>transforms</code>, but positioning an axis needs <code>translate</code>. When it's applied to the <code>g</code> element, it moves the whole group over and down by the given amounts. Here's an example:",
"<blockquote>const xAxis = d3.axisBottom(xScale);<br><br>svg.append(\"g\")<br> .attr(\"transform\", \"translate(0, \" + (h - padding) + \")\")<br> .call(xAxis);</blockquote>",
"The above code places the x-axis at the bottom of the SVG canvas. Then it's passed as an argument to the <code>call()</code> method.",
"The y-axis works is the same way, except the <code>translate</code> argument is in the form (x, 0). Because <code>translate</code> is a string in the <code>attr()</code> method above, you can use concatenation to include variable values for its arguments.",
"<hr>",
"The scatter plot now has an x-axis. Create a y-axis in a variable named <code>yAxis</code> using the <code>axisLeft()</code> method. Then render the axis using a <code>g</code> element. Make sure to use a <code>transform</code> attribute to translate the axis by the amount of padding units right, and 0 units down. Remember to <code>call()</code> the axis."
],
"tests": [
{
"text": "Your code should use the <code>axisLeft()</code> method with <code>yScale</code> passed as the argument.",
"testString": "assert(code.match(/\\.axisLeft\\(yScale\\)/g), 'Your code should use the <code>axisLeft()</code> method with <code>yScale</code> passed as the argument.');"
},
{
"text": "The y-axis <code>g</code> element should have a <code>transform</code> attribute to translate the axis by (60, 0).",
"testString": "assert($('g').eq(1).attr('transform').match(/translate\\(60\\s*?,\\s*?0\\)/g), 'The y-axis <code>g</code> element should have a <code>transform</code> attribute to translate the axis by (60, 0).');"
},
{
"text": "Your code should call the <code>yAxis</code>.",
"testString": "assert(code.match(/\\.call\\(yAxis\\)/g), 'Your code should call the <code>yAxis</code>.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 0,
"translations": {},
"files": {
"indexhtml": {
"key": "indexhtml",
"ext": "html",
"name": "index",
"contents": [
"<body>",
" <script>",
" const dataset = [",
" [ 34, 78 ],",
" [ 109, 280 ],",
" [ 310, 120 ],",
" [ 79, 411 ],",
" [ 420, 220 ],",
" [ 233, 145 ],",
" [ 333, 96 ],",
" [ 222, 333 ],",
" [ 78, 320 ],",
" [ 21, 123 ]",
" ];",
" ",
" const w = 500;",
" const h = 500;",
" const padding = 60;",
" ",
" const xScale = d3.scaleLinear()",
" .domain([0, d3.max(dataset, (d) => d[0])])",
" .range([padding, w - padding]);",
" ",
" const yScale = d3.scaleLinear()",
" .domain([0, d3.max(dataset, (d) => d[1])])",
" .range([h - padding, padding]);",
" ",
" const svg = d3.select(\"body\")",
" .append(\"svg\")",
" .attr(\"width\", w)",
" .attr(\"height\", h);",
" ",
" svg.selectAll(\"circle\")",
" .data(dataset)",
" .enter()",
" .append(\"circle\")",
" .attr(\"cx\", (d) => xScale(d[0]))",
" .attr(\"cy\",(d) => yScale(d[1]))",
" .attr(\"r\", (d) => 5);",
" ",
" svg.selectAll(\"text\")",
" .data(dataset)",
" .enter()",
" .append(\"text\")",
" .text((d) => (d[0] + \",\" + d[1]))",
" .attr(\"x\", (d) => xScale(d[0] + 10))",
" .attr(\"y\", (d) => yScale(d[1]))",
" ",
" const xAxis = d3.axisBottom(xScale);",
" ",
" svg.append(\"g\")",
" .attr(\"transform\", \"translate(0,\" + (h - padding) + \")\")",
" .call(xAxis);",
" ",
" // Add your code below this line",
" ",
" ",
" ",
" // Add your code above this line",
" ",
" </script>",
"</body>"
],
"head": [],
"tail": []
}
}
}
]
}