D3: Add Labels to Scatter Plot Circles

D3: Add Labels to Scatter Plot Circles
0

#1

Tell us what’s happening:
My code gives me coordinates next to plotted circles but I’m not passing the tests. My guess is that the label doesn’t have a space in it. I’ve tried variations of x + " " + y or d[0] + " " + d[1] in different places to try and manipulated the label text to no avail.

Your code so far


<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
       .text((d) => d)
       .attr("x", (d, i) => d[0] + 5)
       .attr("y", (d, i) => h - d[1])
       
       // Add your code above this line
  </script>
</body>

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/add-labels-to-scatter-plot-circles


#2

@tlc35us,

you are really close with your proposed solution.

Take another look at what the instructions say :

For example, the label for the first point is "34, 78"

There is a space after the comma, meaning the text should be 34,SPACE 78

Hope that helps (You need to have the comma as part of the text).


#3

That was the big light bulb for me. I thought I had got the comma for free the way the coordinates displayed. Thanks for your help.