{
"name": "Data Visualization with D3",
"order": 1,
"time": "5 hours",
"helpRoom": "Help",
"challenges": [
{
"id": "587d7fa6367417b2b2512bc1",
"title": "Introduction to the Data Visualization with D3 Challenges",
"description": [
[
"http://imgs.xkcd.com/comics/movie_narrative_charts.png",
"XKCD web comic showing very detailed charts of movie character interactions for Lord of the Rings, Star Wars (original trilogy), Jurassic Park, 12 Angry Men, and Primer. The charts show grouped lines, representing when characters appear together, over time.",
"D3.js, or D3, stands for Data Driven Documents. D3 is a JavaScript library to create dynamic and interactive data visualizations in the browser. It's built to work with common web standards, namely HTML, CSS, and Scalable Vector Graphics (SVG).
D3 takes input data and maps it into a visual representation of that data. It supports many different data formats. D3 lets you bind (or attach) the data to the Document Object Model (DOM). You use HTML or SVG elements with D3's built-in methods to transform the data into a visualization.
D3 gives you a lot of control over the presentation of data. This section covers the basic functionality and how to create visualizations with the D3 library.",
""
]
],
"releasedOn": "",
"challengeSeed": [],
"tests": [],
"type": "Waypoint",
"challengeType": 7,
"isRequired": false,
"titleEs": "",
"descriptionEs": [
[]
],
"titleFr": "",
"descriptionFr": [
[]
],
"titleDe": "",
"descriptionDe": [
[]
]
},
{
"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 select()
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:",
"const anchor = d3.select(\"a\");
",
"The above example finds the first anchor tag on the page and saves an HTML node for it in the variable anchor
. 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 append()
and text()
.",
"The append()
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 text()
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:",
"
d3.select(\"ul\")", "D3 allows you to chain several methods together with periods to perform a number of actions in a row.", "
.append(\"li\")
.text(\"Very important item\");
select
method to select the body
tag in the document. Then append
an h1
tag to it, and add the text \"Learning D3\" into the h1
element."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('body').children('h1').length == 1, 'message: The body
should have one h1
element.');",
"assert($('h1').text() == \"Learning D3\", 'message: The h1
element should have the text \"Learning D3\" in it.');",
"assert(code.match(/d3/g), 'message: Your code should access the d3
object.');",
"assert(code.match(/\\.select/g), 'message: Your code should use the select
method.');",
"assert(code.match(/\\.append/g), 'message: Your code should use the append
method.');",
"assert(code.match(/\\.text/g), 'message: Your code should use the text
method.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 selectAll()
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:",
"const anchors = d3.selectAll(\"a\");
",
"Like the select()
method, selectAll()
supports method chaining, and you can use it with other methods.",
"li
tags in the document, and change their text to \"list item\"."
],
"challengeSeed": [
"",
" li
elements on the page, and the text in each one should say \"list item\". Capitalization and spacing should match exactly.');",
"assert(code.match(/d3/g), 'message: Your code should access the d3
object.');",
"assert(code.match(/\\.selectAll/g), 'message: Your code should use the selectAll
method.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 data()
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 enter()
method for this purpose.",
"When enter()
is combined with the data()
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 ul
element and creates a new list item based on the number of entries in the array:",
"<body>", "It may seem confusing to select elements that don't exist yet. This code is telling D3 to first select the
<ul></ul>
<script>
const dataset = [\"a\", \"b\", \"c\"];
d3.select(\"ul\").selectAll(\"li\")
.data(dataset)
.enter()
.append(\"li\")
.text(\"New item\");
</script>
</body>
ul
on the page. Next, select all list items, which returns an empty selection. Then the data()
method reviews the dataset and runs the following code three times, once for each item in the array. The enter()
method sees there are no li
elements on the page, but it needs 3 (one for each piece of data in dataset
). New li
elements are appended to the ul
and have the text \"New item\".",
"body
node, then select all h2
elements. Have D3 create and append an h2
tag for each item in the dataset
array. The text in the h2
should say \"New Title\". Your code should use the data()
and enter()
methods."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('h2').length == 9, 'message: Your document should have 9 h2
elements.');",
"assert($('h2').text().match(/New Title/g).length == 9, 'message: The text in the h2
elements should say \"New Title\". The capitalization and spacing should match exactly.');",
"assert(code.match(/\\.data/g), 'message: Your code should use the data()
method.');",
"assert(code.match(/\\.enter/g), 'message: Your code should use the enter()
method.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 couple challenges covered the tools used to display dynamic data. They introduced the data()
and enter()
methods. These functions take a data set, then for each item in the set, create a new element with that piece of data attached to it.",
"The last challenge created a new h2
for each item in the data array, but it displayed the same text (\"New Title\") for each heading. Fortunately, there is a way to access and display the actual data with callback functions.",
"The text()
method can take a string or a callback function as an argument. Since the data from the dataset
array is attached to each element, the callback function has access to it. The parameter used in the callback function (d
in the example below) is for the individual data-point itself. This callback function would set the text in the selection to the data value:",
"selection.text((d) => d)
",
"text()
method so it does not place \"New Title\" in each heading. Instead, it displays the data from the array with a space and \"USD\". For example, the first heading should say \"12 USD\"."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('h2').eq(0).text() == \"12 USD\", 'message: The first h2
should have the text \"12 USD\".');",
"assert($('h2').eq(1).text() == \"31 USD\", 'message: The second h2
should have the text \"31 USD\".');",
"assert($('h2').eq(2).text() == \"22 USD\", 'message: The third h2
should have the text \"22 USD\".');",
"assert($('h2').eq(3).text() == \"17 USD\", 'message: The fourth h2
should have the text \"17 USD\".');",
"assert($('h2').eq(4).text() == \"25 USD\", 'message: The fifth h2
should have the text \"25 USD\".');",
"assert($('h2').eq(5).text() == \"18 USD\", 'message: The sixth h2
should have the text \"18 USD\".');",
"assert($('h2').eq(6).text() == \"29 USD\", 'message: The seventh h2
should have the text \"29 USD\".');",
"assert($('h2').eq(7).text() == \"14 USD\", 'message: The eighth h2
should have the text \"14 USD\".');",
"assert($('h2').eq(8).text() == \"9 USD\", 'message: The ninth h2
should have the text \"9 USD\".');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 style()
method.",
"The style()
method takes a comma-separated key-value pair as an argument. Here's an example to set the selection's text color to blue:",
"selection.style(\"color\",\"blue\");
",
"style()
method to the code in the editor to make all the displayed text have a font-family
of verdana
."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('h2').css('font-family') == 'verdana', 'message: Your h2
elements should have a font-family
of verdana.');",
"assert(code.match(/\\.style/g), 'message: Your code should use the style()
method.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 style()
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 style()
method and include the conditional logic. The callback function uses the d
parameter to represent the data point:",
"selection.style(\"color\", (d) => {", "The
/* Logic that returns the color based on a condition */
});
style()
method is not limited to setting the color
- it can be used with other CSS properties as well.",
"style()
method to the code in the editor to set the color
of the h2
elements conditionally. Write the callback function so if the data value is less than 20, it returns \"red\", otherwise it returns \"green\".",
"Noteh2
should have a color
of red.');",
"assert($('h2').eq(1).css('color') == \"rgb(0, 128, 0)\", 'message: The second h2
should have a color
of green.');",
"assert($('h2').eq(2).css('color') == \"rgb(0, 128, 0)\", 'message: The third h2
should have a color
of green.');",
"assert($('h2').eq(3).css('color') == \"rgb(255, 0, 0)\", 'message: The fourth h2
should have a color
of red.');",
"assert($('h2').eq(4).css('color') == \"rgb(0, 128, 0)\", 'message: The fifth h2
should have a color
of green.');",
"assert($('h2').eq(5).css('color') == \"rgb(255, 0, 0)\", 'message: The sixth h2
should have a color
of red.');",
"assert($('h2').eq(6).css('color') == \"rgb(0, 128, 0)\", 'message: The seventh h2
should have a color
of green.');",
"assert($('h2').eq(7).css('color') == \"rgb(255, 0, 0)\", 'message: The eighth h2
should have a color
of red.');",
"assert($('h2').eq(8).css('color') == \"rgb(255, 0, 0)\", 'message: The ninth h2
should have a color
of red.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 attr()
method to add any HTML attribute to an element, including a class name.",
"The attr()
method works the same way that style()
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:",
"selection.attr(\"class\", \"container\");
",
"attr()
method to the code in the editor and put a class of bar
on the div
elements."
],
"challengeSeed": [
"",
"",
" ",
""
],
"tests": [
"assert($('div').attr('class') == \"bar\", 'message: Your div
elements should have a class of bar
.');",
"assert(code.match(/\\.attr/g), 'message: Your code should use the attr()
method.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 div
for each data point in the array",
"2) Give each div
a dynamic height, using a callback function in the style()
method that sets height equal to the data value",
"Recall the format to set a style using a callback function:",
"selection.style(\"cssProperty\", (d) => d)
",
"style()
method to the code in the editor to set the height
property for each element. Use a callback function to return the value of the data point with the string \"px\" added to it."
],
"challengeSeed": [
"",
"",
" ",
""
],
"tests": [
"assert($('div').eq(0).css('height') == '12px', 'message: The first div
should have a height
of 12 pixels.');",
"assert($('div').eq(1).css('height') == '31px', 'message: The second div
should have a height
of 31 pixels.');",
"assert($('div').eq(2).css('height') == '22px', 'message: The third div
should have a height
of 22 pixels.');",
"assert($('div').eq(3).css('height') == '17px', 'message: The fourth div
should have a height
of 17 pixels.');",
"assert($('div').eq(4).css('height') == '25px', 'message: The fifth div
should have a height
of 25 pixels.');",
"assert($('div').eq(5).css('height') == '18px', 'message: The sixth div
should have a height
of 18 pixels.');",
"assert($('div').eq(6).css('height') == '29px', 'message: The seventh div
should have a height
of 29 pixels.');",
"assert($('div').eq(7).css('height') == '14px', 'message: The eighth div
should have a height
of 14 pixels.');",
"assert($('div').eq(8).css('height') == '9px', 'message: The ninth div
should have a height
of 9 pixels.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 bar
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",
"margin
of 2px to the bar
class in the style
tag. Next, change the callback function in the style()
method so it returns a value 10 times the original data value (plus the \"px\").",
"Notediv
should have a height
of 120 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(1).css('height') == '310px' && $('div').eq(1).css('margin-right') == '2px', 'message: The second div
should have a height
of 310 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(2).css('height') == '220px' && $('div').eq(2).css('margin-right') == '2px', 'message: The third div
should have a height
of 220 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(3).css('height') == '170px' && $('div').eq(3).css('margin-right') == '2px', 'message: The fourth div
should have a height
of 170 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(4).css('height') == '250px' && $('div').eq(4).css('margin-right') == '2px', 'message: The fifth div
should have a height
of 250 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(5).css('height') == '180px' && $('div').eq(5).css('margin-right') == '2px', 'message: The sixth div
should have a height
of 180 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(6).css('height') == '290px' && $('div').eq(6).css('margin-right') == '2px', 'message: The seventh div
should have a height
of 290 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(7).css('height') == '140px' && $('div').eq(7).css('margin-right') == '2px', 'message: The eighth div
should have a height
of 140 pixels and a margin
of 2 pixels.');",
"assert($('div').eq(8).css('height') == '90px' && $('div').eq(8).css('margin-right') == '2px', 'message: The ninth div
should have a height
of 90 pixels and a margin
of 2 pixels.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 Scalable Vector Graphics
.",
"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 svg
tag.",
"CSS can be scalable when styles use relative units (such as vh
, vw
, or percentages), but using SVG is more flexible to build data visualizations.",
"svg
node to the body
using append()
. Give it a width
attribute of 500 and a height
attribute of 100 using the attr()
method for each. You'll see it in the output because there's a background-color
of pink applied to it in the style
tag.",
"Notesvg
element.');",
"assert($('svg').attr('width') == '500', 'message: The svg
element should have a width
attribute set to 500.');",
"assert($('svg').attr('height') == '100', 'message: The svg
element should have a height
attribute set to 100.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 svg
element with a given width and height, which was visible because it had a background-color
applied to it in the style
tag. The code made space for the given width and height.",
"The next step is to create a shape to put in the svg
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 (<rect>
) SVG shape could create a bar in a bar chart.",
"When you place a shape into the svg
area, you can specify where it goes with x
and y
coordinates. The origin point of (0, 0) is in the upper-left corner. Positive vales for x
push the shape to the right, and positive values for y
push the shape down from the origin point.",
"To place a shape in the middle of the 500 (width) x 100 (height) svg
from last challenge, the x
coordinate would be 250 and the y
coordinate would be 50.",
"An SVG rect
has four attributes. There are the x
and y
coordinates for where it is placed in the svg
area. It also has a height
and width
to specify the size.",
"rect
shape to the svg
using append()
, and give it a width
attribute of 25 and height
attribute of 100. Also, give the rect
x
and y
attributes each set to 0."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('rect').length == 1, 'message: Your document should have 1 rect
element.');",
"assert($('rect').attr('width') == '25', 'message: The rect
element should have a width
attribute set to 25.');",
"assert($('rect').attr('height') == '100', 'message: The rect
element should have a height
attribute set to 100.');",
"assert($('rect').attr('x') == '0', 'message: The rect
element should have an x
attribute set to 0.');",
"assert($('rect').attr('y') == '0', 'message: The rect
element should have a y
attribute set to 0.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 svg
element to represent a bar. Here, you'll combine what you've learned so far about data()
, enter()
, and SVG shapes to create and append a rectangle for each data point in dataset
.",
"A previous challenge showed the format for how to create and append a div
for each item in dataset
:",
"d3.select(\"body\").selectAll(\"div\")", "There are a few differences working with
.data(dataset)
.enter()
.append(\"div\")
rect
elements instead of divs
. The rects
must be appended to an svg
element, not directly to the body
. Also, you need to tell D3 where to place each rect
within the svg
area. The bar placement will be covered in the next challenge.",
"data()
, enter()
, and append()
methods to create and append a rect
for each item in dataset
. The bars should display all on top of each other, this will be fixed in the next challenge."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('rect').length == 9, 'message: Your document should have 9 rect
elements.');",
"assert(code.match(/\\.data/g), 'message: Your code should use the data()
method.');",
"assert(code.match(/\\.enter/g), 'message: Your code should use the enter()
method.');",
"assert(code.match(/\\.append/g), 'message: Your code should use the append()
method.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 svg
element for each point in dataset
to represent a bar. Unfortunately, they were all stacked on top of each other.",
"The placement of a rectangle is handled by the x
and y
attributes. They tell D3 where to start drawing the shape in the svg
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 y
value stays the same (at 0) for all bars. The x
value, however, needs to change as you add new bars. Remember that larger x
values push items farther to the right. As you go through the array elements in dataset
, the x value should increase.",
"The attr()
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 d
) and one for the index of the data point in the array. The second argument for the index is optional. Here's the format:",
"selection.attr(\"property\", (d, i) => {", "It's important to note that you do NOT need to write a
/*
* d is the data point value
* i is the index of the data point in the array
*/
})
for
loop or use forEach()
to iterate over the items in the data set. Recall that the data()
method parses the data set, and any method that's chained after data()
is run once for each item in the data set.",
"x
attribute callback function so it returns the index times 30.",
"Notex
value by 30 adds some space between the bars. Any value greater than 25 would work in this example."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('rect').eq(0).attr('x') == '0', 'message: The first rect
should have an x
value of 0.');",
"assert($('rect').eq(1).attr('x') == '30', 'message: The second rect
should have an x
value of 30.');",
"assert($('rect').eq(2).attr('x') == '60', 'message: The third rect
should have an x
value of 60.');",
"assert($('rect').eq(3).attr('x') == '90', 'message: The fourth rect
should have an x
value of 90.');",
"assert($('rect').eq(4).attr('x') == '120', 'message: The fifth rect
should have an x
value of 120.');",
"assert($('rect').eq(5).attr('x') == '150', 'message: The sixth rect
should have an x
value of 150.');",
"assert($('rect').eq(6).attr('x') == '180', 'message: The seventh rect
should have an x
value of 180.');",
"assert($('rect').eq(7).attr('x') == '210', 'message: The eighth rect
should have an x
value of 210.');",
"assert($('rect').eq(8).attr('x') == '240', 'message: The ninth rect
should have an x
value of 240.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 x
value was set dynamically.",
"selection.attr(\"property\", (d, i) => {", "
/*
* d is the data point value
* i is the index of the data point in the array
*/
})
height
attribute to return the data value times 3.",
"Noterect
should have a height
of 36.');",
"assert($('rect').eq(1).attr('height') == '93', 'message: The second rect
should have a height
of 93.');",
"assert($('rect').eq(2).attr('height') == '66', 'message: The third rect
should have a height
of 66.');",
"assert($('rect').eq(3).attr('height') == '51', 'message: The fourth rect
should have a height
of 51.');",
"assert($('rect').eq(4).attr('height') == '75', 'message: The fifth rect
should have a height
of 75.');",
"assert($('rect').eq(5).attr('height') == '54', 'message: The sixth rect
should have a height
of 54.');",
"assert($('rect').eq(6).attr('height') == '87', 'message: The seventh rect
should have a height
of 87.');",
"assert($('rect').eq(7).attr('height') == '42', 'message: The eighth rect
should have a height
of 42.');",
"assert($('rect').eq(8).attr('height') == '27', 'message: The ninth rect
should have a height
of 27.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 x
coordinate of 0 places a shape on the right edge of the SVG area. A y
coordinate of 0 places a shape on the top edge of the SVG area. Higher x
values push the rectangle to the right. Higher y
values push the rectangle down.",
"To make the bars right-side-up, you need to change the way the y
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 y
coordinate needs a value of 100. If the data point value were 1, you would start with a y
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 y
coordinate would be 99.",
"The y
coordinate that is y = heightOfSVG - heightOfBar
would place the bars right-side-up.",
"y
attribute to set the bars right-side-up. Remember that the height
of the bar is 3 times the data value d
.",
"Notey = h - m * d
, where m
is the constant that scales the data points."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('rect').eq(0).attr('y') == h - (dataset[0] * 3), 'message: The first rect
should have a y
value of 64.');",
"assert($('rect').eq(1).attr('y') == h - (dataset[1] * 3), 'message: The second rect
should have a y
value of 7.');",
"assert($('rect').eq(2).attr('y') == h - (dataset[2] * 3), 'message: The third rect
should have a y
value of 34.');",
"assert($('rect').eq(3).attr('y') == h - (dataset[3] * 3), 'message: The fourth rect
should have a y
value of 49.');",
"assert($('rect').eq(4).attr('y') == h - (dataset[4] * 3), 'message: The fifth rect
should have a y
value of 25.');",
"assert($('rect').eq(5).attr('y') == h - (dataset[5] * 3), 'message: The sixth rect
should have a y
value of 46.');",
"assert($('rect').eq(6).attr('y') == h - (dataset[6] * 3), 'message: The seventh rect
should have a y
value of 13.');",
"assert($('rect').eq(7).attr('y') == h - (dataset[7] * 3), 'message: The eighth rect
should have a y
value of 58.');",
"assert($('rect').eq(8).attr('y') == h - (dataset[8] * 3), 'message: The ninth rect
should have a y
value of 73.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 rect
shape is colored with the fill
attribute. It supports hex codes, color names, and rgb values, as well as more complex options like gradients and transparency.",
"attr()
method to set the \"fill\" of all the bars to the color \"navy\"."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('rect').css('fill') == \"rgb(0, 0, 128)\", 'message: The bars should all have a fill
color of navy.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 text
element.",
"Like the rect
element, a text
element needs to have x
and y
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.",
"text
element. First, append text
nodes to the svg
. Next, add attributes for the x
and y
coordinates. They should be calculated the same way as the rect
ones, except the y
value for the text
should make the label sit 3 units higher than the bar. Finally, use the D3 text()
method to set the label equal to the data point value.",
"Notey
value for the text
should be 3 greater or 3 less than the y
value for the bar."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('text').eq(0).text() == '12' && $('text').eq(0).attr('y') == '61', 'message: The first text
element should have a label of 12 and a y
value of 61.');",
"assert($('text').eq(1).text() == '31' && $('text').eq(1).attr('y') == '4', 'message: The second text
element should have a label of 31 and a y
value of 4.');",
"assert($('text').eq(2).text() == '22' && $('text').eq(2).attr('y') == '31', 'message: The third text
element should have a label of 22 and a y
value of 31.');",
"assert($('text').eq(3).text() == '17' && $('text').eq(3).attr('y') == '46', 'message: The fourth text
element should have a label of 17 and a y
value of 46.');",
"assert($('text').eq(4).text() == '25' && $('text').eq(4).attr('y') == '22', 'message: The fifth text
element should have a label of 25 and a y
value of 22.');",
"assert($('text').eq(5).text() == '18' && $('text').eq(5).attr('y') == '43', 'message: The sixth text
element should have a label of 18 and a y
value of 43.');",
"assert($('text').eq(6).text() == '29' && $('text').eq(6).attr('y') == '10', 'message: The seventh text
element should have a label of 29 and a y
value of 10.');",
"assert($('text').eq(7).text() == '14' && $('text').eq(7).attr('y') == '55', 'message: The eighth text
element should have a label of 14 and a y
value of 55.');",
"assert($('text').eq(8).text() == '9' && $('text').eq(8).attr('y') == '70', 'message: The ninth text
element should have a label of 9 and a y
value of 70.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 fill
attribute sets the color of the text for a text
node. The style()
method sets CSS rules for other styles, such as \"font-family\" or \"font-size\".",
"font-size
of the text
elements to 25px, and the color of the text to red."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('text').css('fill') == 'rgb(255, 0, 0)', 'message: The labels should all have a fill
color of red.');",
"assert($('text').css('font-size') == '25px', 'message: The labels should all have a font-size
of 25 pixels.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 attr()
method. Then the :hover
pseudo-class for your new class holds the style rules for any hover effects.",
"attr()
method to add a class of bar
to all the rect
elements. This changes the fill
color of the bar to brown when you mouse over it."
],
"challengeSeed": [
"",
"",
" ",
""
],
"tests": [
"assert($('rect').attr('class') == \"bar\", 'message: Your rect
elements should have a class of bar
.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 title
element.",
"title
pairs with the text()
method to dynamically add data to the bars.",
"title
element under each rect
node. Then call the text()
method with a callback function so the text displays the data value."
],
"challengeSeed": [
"",
"",
" ",
""
],
"tests": [
"assert($('title').length == 9, 'message: Your code should have 9 title
elements.');",
"assert($('title').eq(0).text() == '12', 'message: The first title
element should have tooltip text of 12.');",
"assert($('title').eq(1).text() == '31', 'message: The second title
element should have tooltip text of 31.');",
"assert($('title').eq(2).text() == '22', 'message: The third title
element should have tooltip text of 22.');",
"assert($('title').eq(3).text() == '17', 'message: The fourth title
element should have tooltip text of 17.');",
"assert($('title').eq(4).text() == '25', 'message: The fifth title
element should have tooltip text of 25.');",
"assert($('title').eq(5).text() == '18', 'message: The sixth title
element should have tooltip text of 18.');",
"assert($('title').eq(6).text() == '29', 'message: The seventh title
element should have tooltip text of 29.');",
"assert($('title').eq(7).text() == '14', 'message: The eighth title
element should have tooltip text of 14.');",
"assert($('title').eq(8).text() == '9', 'message: The ninth title
element should have tooltip text of 9.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 x
and y
axes, and are used to position the circle in the visualization.",
"SVG has a circle
tag to create the circle shape. It works a lot like the rect
elements you used for the bar chart.",
"data()
, enter()
, and append()
methods to bind dataset
to new circle
elements that are appended to the SVG canvas."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('circle').length == 10, 'message: Your code should have 10 circle
elements.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 circle
elements for each point in the dataset
, and appended them to the SVG canvas. But D3 needs more information about the position and size of each circle
to display them correctly.",
"A circle
in SVG has three main attributes. The cx
and cy
attributes are the coordinates. They tell D3 where to position the center of the shape on the SVG canvas. The radius (r
attribute) gives the size of the circle
.",
"Just like the rect
y
coordinate, the cy
attribute for a circle
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 data(dataset)
run once per item in dataset
. The d
parameter in the callback function refers to the current item in dataset
, which is an array for each point. You use bracket notation, like d[0]
, to access the values in that array.",
"cx
, cy
, and r
attributes to the circle
elements. The cx
value should be the first number in the array for each item in dataset
. The cy
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 r
value should be 5 for all circles."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('circle').length == 10, 'message: Your code should have 10 circle
elements.');",
"assert($('circle').eq(0).attr('cx') == '34' && $('circle').eq(0).attr('cy') == '422' && $('circle').eq(0).attr('r') == '5', 'message: The first circle
element should have a cx
value of 34, a cy
value of 422, and an r
value of 5.');",
"assert($('circle').eq(1).attr('cx') == '109' && $('circle').eq(1).attr('cy') == '220' && $('circle').eq(1).attr('r') == '5', 'message: The second circle
element should have a cx
value of 109, a cy
value of 220, and an r
value of 5.');",
"assert($('circle').eq(2).attr('cx') == '310' && $('circle').eq(2).attr('cy') == '380' && $('circle').eq(2).attr('r') == '5', 'message: The third circle
element should have a cx
value of 310, a cy
value of 380, and an r
value of 5.');",
"assert($('circle').eq(3).attr('cx') == '79' && $('circle').eq(3).attr('cy') == '89' && $('circle').eq(3).attr('r') == '5', 'message: The fourth circle
element should have a cx
value of 79, a cy
value of 89, and an r
value of 5.');",
"assert($('circle').eq(4).attr('cx') == '420' && $('circle').eq(4).attr('cy') == '280' && $('circle').eq(4).attr('r') == '5', 'message: The fifth circle
element should have a cx
value of 420, a cy
value of 280, and an r
value of 5.');",
"assert($('circle').eq(5).attr('cx') == '233' && $('circle').eq(5).attr('cy') == '355' && $('circle').eq(5).attr('r') == '5', 'message: The sixth circle
element should have a cx
value of 233, a cy
value of 355, and an r
value of 5.');",
"assert($('circle').eq(6).attr('cx') == '333' && $('circle').eq(6).attr('cy') == '404' && $('circle').eq(6).attr('r') == '5', 'message: The seventh circle
element should have a cx
value of 333, a cy
value of 404, and an r
value of 5.');",
"assert($('circle').eq(7).attr('cx') == '222' && $('circle').eq(7).attr('cy') == '167' && $('circle').eq(7).attr('r') == '5', 'message: The eighth circle
element should have a cx
value of 222, a cy
value of 167, and an r
value of 5.');",
"assert($('circle').eq(8).attr('cx') == '78' && $('circle').eq(8).attr('cy') == '180' && $('circle').eq(8).attr('r') == '5', 'message: The ninth circle
element should have a cx
value of 78, a cy
value of 180, and an r
value of 5.');",
"assert($('circle').eq(9).attr('cx') == '21' && $('circle').eq(9).attr('cy') == '377' && $('circle').eq(9).attr('r') == '5', 'message: The tenth circle
element should have a cx
value of 21, a cy
value of 377, and an r
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."
],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 (x
) and second (y
) fields of each item in dataset
.",
"The text
nodes need x
and y
attributes to position it on the SVG canvas. In this challenge, the y
value (which determines height) can use the same value that the circle
uses for its cy
attribute. The x
value can be slightly larger than the cx
value of the circle
, so the label is visible. This will push the label to the right of the plotted point.",
"text
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 x
attribute so it's 5 units more than the value you used for the cx
attribute on the circle
. Set the y
attribute the same way that's used for the cy
value on the circle
."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('text').length == 10, 'message: Your code should have 10 text
elements.');",
"assert($('text').eq(0).text() == '34, 78' && $('text').eq(0).attr('x') == '39' && $('text').eq(0).attr('y') == '422', 'message: The first label should have text of \"34, 78\", an x
value of 39, and a y
value of 422.');",
"assert($('text').eq(1).text() == '109, 280' && $('text').eq(1).attr('x') == '114' && $('text').eq(1).attr('y') == '220', 'message: The second label should have text of \"109, 280\", an x
value of 114, and a y
value of 220.');",
"assert($('text').eq(2).text() == '310, 120' && $('text').eq(2).attr('x') == '315' && $('text').eq(2).attr('y') == '380', 'message: The third label should have text of \"310, 120\", an x
value of 315, and a y
value of 380.');",
"assert($('text').eq(3).text() == '79, 411' && $('text').eq(3).attr('x') == '84' && $('text').eq(3).attr('y') == '89', 'message: The fourth label should have text of \"79, 411\", an x
value of 84, and a y
value of 89.');",
"assert($('text').eq(4).text() == '420, 220' && $('text').eq(4).attr('x') == '425' && $('text').eq(4).attr('y') == '280', 'message: The fifth label should have text of \"420, 220\", an x
value of 425, and a y
value of 280.');",
"assert($('text').eq(5).text() == '233, 145' && $('text').eq(5).attr('x') == '238' && $('text').eq(5).attr('y') == '355', 'message: The sixth label should have text of \"233, 145\", an x
value of 238, and a y
value of 355.');",
"assert($('text').eq(6).text() == '333, 96' && $('text').eq(6).attr('x') == '338' && $('text').eq(6).attr('y') == '404', 'message: The seventh label should have text of \"333, 96\", an x
value of 338, and a y
value of 404.');",
"assert($('text').eq(7).text() == '222, 333' && $('text').eq(7).attr('x') == '227' && $('text').eq(7).attr('y') == '167', 'message: The eighth label should have text of \"222, 333\", an x
value of 227, and a y
value of 167.');",
"assert($('text').eq(8).text() == '78, 320' && $('text').eq(8).attr('x') == '83' && $('text').eq(8).attr('y') == '180', 'message: The ninth label should have text of \"78, 320\", an x
value of 83, and a y
value of 180.');",
"assert($('text').eq(9).text() == '21, 123' && $('text').eq(9).attr('x') == '26' && $('text').eq(9).attr('y') == '377', 'message: The tenth label should have text of \"21, 123\", an x
value of 26, and a y
value of 377.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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. Scales
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 x
and y
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 scaleLinear()
:",
" const scale = d3.scaleLinear()
",
"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.",
"scale
variable to create a linear scale. Then set the output
variable to the scale called with an input argument of 50."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('h2').text() == '50', 'message: The text in the h2
should be 50.');",
"assert(code.match(/\\.scaleLinear/g), 'message: Your code should use the scaleLinear()
method.');",
"assert(output == 50 && code.match(/scale\\(\\s*?50\\s*?\\)/g), 'message: The output
variable should call scale
with an argument of 50.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 in the input information for a scale, and is also known as the domain.",
"You want to map those points along the x
axis on the SVG canvas, between 10 units and 500 units. This is the output information, which is also known as the range.",
"The domain()
and range()
methods set these values for the scale. Both methods take an array of at least two elements as an argument. Here's an example:",
"// Set a domain", "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.", "
// The domain covers the set of input values
scale.domain([50, 480]);
// Set a range
// The range covers the set of output values
scale.range([10, 500]);
scale(50) // Returns 10
scale(480) // Returns 500
scale(325) // Returns 323.37
scale(750) // Returns 807.67
d3.scaleLinear()
[250, 500]
and range to [10, 150]
.",
"Notedomain()
and range()
methods onto the scale
variable."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert(code.match(/\\.domain/g), 'message: Your code should use the domain()
method.');",
"assert(JSON.stringify(scale.domain()) == JSON.stringify([250, 500]), 'message: The domain()
of the scale should be set to [250, 500]
.');",
"assert(code.match(/\\.range/g), 'message: Your code should use the range()
method.');",
"assert(JSON.stringify(scale.range()) == JSON.stringify([10, 150]), 'message: The range()
of the scale should be set to [10, 150]
.');",
"assert($('h2').text() == '-102', 'message: The text in the h2
should be -102.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"id": "587d7fac367417b2b2512bdc",
"title": "Use the D3 Max and Min Functions",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The D3 methods domain()
and range()
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 - min()
and max()
to return this information. Here's an example:",
"const exampleData = [34, 234, 73, 90, 6, 52];", "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
d3.min(exampleData) // Returns 6
d3.max(exampleData) // Returns 234
min()
and max()
methods take a callback function.",
"In this example, the callback function's argument d
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:",
"const locationData = [[1, 7],[6, 3],[8, 3]];", "
// Returns the smallest number out of the first elements
const minX = d3.min(locationData, (d) => d[0]);
// minX compared 1, 6, and 8 and is set to 1
positionData
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 output
variable.",
"Noteh2
should be 8.');",
"assert(code.match(/\\.max/g), 'message: Your code should use the max()
method.')"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"id": "587d7fac367417b2b2512bdd",
"title": "Use Dynamic Scales",
"required": [
{
"src": "https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js"
}
],
"description": [
"The D3 min()
and max()
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 domain()
method passes information to the scale about the raw data values for the plot. The range()
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 max()
method with a callback function based on the x values in the arrays. The range uses the SVG canvas' width (w
), but it includes some padding, too. This puts space between the scatter plot dots and the edge of the SVG canvas.",
"const dataset = [", "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
[ 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;
const xScale = d3.scaleLinear()
.domain([0, d3.max(dataset, (d) => d[0])])
.range([padding, w - padding]);
range()
method forces the plot to start at 30 along that line (instead of 0), and end at 470 (instead of 500).",
"yScale
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 (h
) and include padding.",
"Noteh2
should be 30.');",
"assert(JSON.stringify(yScale.domain()) == JSON.stringify([0, 411]), 'message: The domain()
of yScale should be equivalent to [0, 411]
.');",
"assert(JSON.stringify(yScale.range()) == JSON.stringify([470, 30]), 'message: The range()
of yScale should be equivalent to [470, 30]
.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 x
and y
attributes for rect
or text
elements, or cx
and cy
for circles
. Here's an example:",
"shape", "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
.attr(\"x\", (d) => xScale(d[0]))
text()
method for a tooltip or label.",
"xScale
and yScale
to position both the circle
and text
shapes onto the SVG canvas. For the circles
, apply the scales to set the cx
and cy
attributes. Give them a radius of 5 units, too.",
"For the text
elements, apply the scales to set the x
and y
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 xScale
."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert($('circle').length == 10, 'message: Your code should have 10 circle
elements.');",
"assert(Math.round($('circle').eq(0).attr('cx')) == '91' && Math.round($('circle').eq(0).attr('cy')) == '368' && $('circle').eq(0).attr('r') == '5', 'message: The first circle
element should have a cx
value of approximately 91 and a cy
value of approximately 368 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(1).attr('cx')) == '159' && Math.round($('circle').eq(1).attr('cy')) == '181' && $('circle').eq(1).attr('r') == '5', 'message: The second circle
element should have a cx
value of approximately 159 and a cy
value of approximately 181 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(2).attr('cx')) == '340' && Math.round($('circle').eq(2).attr('cy')) == '329' && $('circle').eq(2).attr('r') == '5', 'message: The third circle
element should have a cx
value of approximately 340 and a cy
value of approximately 329 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(3).attr('cx')) == '131' && Math.round($('circle').eq(3).attr('cy')) == '60' && $('circle').eq(3).attr('r') == '5', 'message: The fourth circle
element should have a cx
value of approximately 131 and a cy
value of approximately 60 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(4).attr('cx')) == '440' && Math.round($('circle').eq(4).attr('cy')) == '237' && $('circle').eq(4).attr('r') == '5', 'message: The fifth circle
element should have a cx
value of approximately 440 and a cy
value of approximately 237 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(5).attr('cx')) == '271' && Math.round($('circle').eq(5).attr('cy')) == '306' && $('circle').eq(5).attr('r') == '5', 'message: The sixth circle
element should have a cx
value of approximately 271 and a cy
value of approximately 306 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(6).attr('cx')) == '361' && Math.round($('circle').eq(6).attr('cy')) == '351' && $('circle').eq(6).attr('r') == '5', 'message: The seventh circle
element should have a cx
value of approximately 361 and a cy
value of approximately 351 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(7).attr('cx')) == '261' && Math.round($('circle').eq(7).attr('cy')) == '132' && $('circle').eq(7).attr('r') == '5', 'message: The eighth circle
element should have a cx
value of approximately 261 and a cy
value of approximately 132 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(8).attr('cx')) == '131' && Math.round($('circle').eq(8).attr('cy')) == '144' && $('circle').eq(8).attr('r') == '5', 'message: The ninth circle
element should have a cx
value of approximately 131 and a cy
value of approximately 144 after applying the scales. It should also have an r
value of 5.');",
"assert(Math.round($('circle').eq(9).attr('cx')) == '79' && Math.round($('circle').eq(9).attr('cy')) == '326' && $('circle').eq(9).attr('r') == '5', 'message: The tenth circle
element should have a cx
value of approximately 79 and a cy
value of approximately 326 after applying the scales. It should also have an r
value of 5.');",
"assert($('text').length == 10, 'message: Your code should have 10 text
elements.');",
"assert(Math.round($('text').eq(0).attr('x')) == '100' && Math.round($('text').eq(0).attr('y')) == '368', 'message: The first label should have an x
value of approximately 100 and a y
value of approximately 368 after applying the scales.');",
"assert(Math.round($('text').eq(1).attr('x')) == '168' && Math.round($('text').eq(1).attr('y')) == '181', 'message: The second label should have an x
value of approximately 168 and a y
value of approximately 181 after applying the scales.');",
"assert(Math.round($('text').eq(2).attr('x')) == '350' && Math.round($('text').eq(2).attr('y')) == '329', 'message: The third label should have an x
value of approximately 350 and a y
value of approximately 329 after applying the scales.');",
"assert(Math.round($('text').eq(3).attr('x')) == '141' && Math.round($('text').eq(3).attr('y')) == '60', 'message: The fourth label should have an x
value of approximately 141 and a y
value of approximately 60 after applying the scales.');",
"assert(Math.round($('text').eq(4).attr('x')) == '449' && Math.round($('text').eq(4).attr('y')) == '237', 'message: The fifth label should have an x
value of approximately 449 and a y
value of approximately 237 after applying the scales.');",
"assert(Math.round($('text').eq(5).attr('x')) == '280' && Math.round($('text').eq(5).attr('y')) == '306', 'message: The sixth label should have an x
value of approximately 280 and a y
value of approximately 306 after applying the scales.');",
"assert(Math.round($('text').eq(6).attr('x')) == '370' && Math.round($('text').eq(6).attr('y')) == '351', 'message: The seventh label should have an x
value of approximately 370 and a y
value of approximately 351 after applying the scales.');",
"assert(Math.round($('text').eq(7).attr('x')) == '270' && Math.round($('text').eq(7).attr('y')) == '132', 'message: The eighth label should have an x
value of approximately 270 and a y
value of approximately 132 after applying the scales.');",
"assert(Math.round($('text').eq(8).attr('x')) == '140' && Math.round($('text').eq(8).attr('y')) == '144', 'message: The ninth label should have an x
value of approximately 140 and a y
value of approximately 144 after applying the scales.');",
"assert(Math.round($('text').eq(9).attr('x')) == '88' && Math.round($('text').eq(9).attr('y')) == '326', 'message: The tenth label should have an x
value of approximately 88 and a y
value of approximately 326 after applying the scales.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
},
{
"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 axisLeft()
and axisBottom()
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 xScale
in the previous challenges:",
"const xAxis = d3.axisBottom(xScale);
",
"The next step is to render the axis on the SVG canvas. To do so, you can use a general SVG component, the g
element. The g
stands for group.",
"Unlike rect
, circle
, and text
, an axis is just a straight line when it's rendered. Because it is a simple shape, using g
works.",
"The last step is to apply a transform
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 transforms
, but positioning an axis needs translate
. When it's applied to the g
element, it moves the whole group over and down by the given amounts. Here's an example:",
"const xAxis = d3.axisBottom(xScale);", "The above code places the x-axis at the bottom of the SVG canvas. Then it's passed as an argument to the
svg.append(\"g\")
.attr(\"transform\", \"translate(0, \" + (h - padding) + \")\")
.call(xAxis);
call()
method.",
"The y-axis works is the same way, except the translate
argument is in the form (x, 0). Because translate
is a string in the attr()
method above, you can use concatenation to include variable values for its arguments.",
"yAxis
using the axisLeft()
method. Then render the axis using a g
element. Make sure to use a transform
attribute to translate the axis by the amount of padding units right, and 0 units down. Remember to call()
the axis."
],
"challengeSeed": [
"",
" ",
""
],
"tests": [
"assert(code.match(/\\.axisLeft\\(yScale\\)/g), 'message: Your code should use the axisLeft()
method with yScale
passed as the argument.');",
"assert($('g').eq(1).attr('transform').match(/translate\\(60\\s*?,\\s*?0\\)/g), 'message: The y-axis g
element should have a transform
attribute to translate the axis by (60, 0).');",
"assert(code.match(/\\.call\\(yAxis\\)/g), 'message: Your code should call the yAxis
.');"
],
"solutions": [],
"hints": [],
"type": "waypoint",
"challengeType": 0,
"translations": {}
}
]
}