# freeCodeCamp Challenge Guide: Use a Pre-Defined Scale to Place Elements

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-a-pre-defined-scale-to-place-elements/301494>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 1:08pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-a-pre-defined-scale-to-place-elements/301494 "2019-08-03T13:08:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 1:08pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-a-pre-defined-scale-to-place-elements/301494/1 "2019-08-03T13:08:32Z")

</div>

# [Use a Pre-Defined Scale to Place Elements](https://www.freecodecamp.org/learn/data-visualization/data-visualization-with-d3/use-a-pre-defined-scale-to-place-elements)

* * *

## Hints

### Hint 1

Use the `.attr()` function, which accepts 2 parameters.

### Hint 2

Use a callback function on the `.attr()` functions.

### Hint 3

The radius is set using the `r`attribute.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> Change the `svg.selectAll("circle")` and `svg.selectAll("text")` codeblocks to the following to complete the challenge, this has been done by incorporating all the hints from above:
> 
> ```js
> svg.selectAll("circle")
> .data(dataset)
> .enter()
> .append("circle")
> .attr("cx", (d) => xScale(d[0]))
> .attr("cy", (d) => yScale(d[1]))
> .attr("r", 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]));
> 
> ```
> 
> The full solution looks like:
> 
> ```html
> <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", 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]));
> </script>
> </body>
> 
> ```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:58pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-a-pre-defined-scale-to-place-elements/301494/2 "2025-01-08T15:58:11Z")

</div>


