* added solutions to data visualization with D3 * Update add-a-tooltip-to-a-d3-element.english.md * Update add-attributes-to-the-circle-elements.english.md * Update add-axes-to-a-visualization.english.md * Update add-document-elements-with-d3.english.md * Update add-inline-styling-to-elements.english.md * Update add-labels-to-scatter-plot-circles.english.md * Update change-styles-based-on-data.english.md * Update change-the-color-of-an-svg-element.english.md * Update change-the-presentation-of-a-bar-chart.english.md * Update create-a-bar-for-each-data-point-in-the-set.english.md * Update create-a-linear-scale-with-d3.english.md * Update create-a-scatterplot-with-svg-circles.english.md * Update display-shapes-with-svg.english.md * Update dynamically-change-the-height-of-each-bar.english.md * Update dynamically-set-the-coordinates-for-each-bar.english.md * Update invert-svg-elements.english.md * Update learn-about-svg-in-d3.english.md * Update select-a-group-of-elements-with-d3.english.md * Update set-a-domain-and-a-range-on-a-scale.english.md * Update style-d3-labels.english.md * Update work-with-dynamic-data-in-d3.english.md * Update use-a-pre-defined-scale-to-place-elements.english.md * Update use-dynamic-scales.english.md * Update use-the-d3.max-and-d3.min-functions-to-find-minimum-and-maximum-values-in-a-dataset.english.md * Update work-with-data-in-d3.english.md
2.7 KiB
2.7 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7fab367417b2b2512bda | Create a Linear Scale with D3 | 6 | 301483 |
Description
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.
Instructions
scale
variable to create a linear scale. Then set the output
variable to the scale called with an input argument of 50.
Tests
tests:
- text: The text in the <code>h2</code> should be 50.
testString: assert($('h2').text() == '50');
- text: Your code should use the <code>scaleLinear()</code> method.
testString: assert(code.match(/\.scaleLinear/g));
- 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));
Challenge Seed
<body>
<script>
// Add your code below this line
const scale = undefined; // Create the scale here
const output = scale(); // Call the scale with an argument here
// Add your code above this line
d3.select("body")
.append("h2")
.text(output);
</script>
</body>
Solution
<body>
<script>
const scale = d3.scaleLinear();
const output = scale(50);
d3.select("body")
.append("h2")
.text(output);
</script>
</body>