curriculum/challenges/english/blocks/data-visualization-with-d3/587d7fab367417b2b2512bd9.md
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. 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.
Label each point on the scatter plot using the 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.
Your code should have 10 text elements.
assert.lengthOf(document.querySelectorAll('text'), 10);
The first label should have text of 34, 78, an x value of 39, and a y value of 422.
const labelOne = document.querySelector('text');
assert.strictEqual(labelOne?.textContent, '34, 78');
assert.strictEqual(labelOne?.getAttribute('x'), '39');
assert.strictEqual(labelOne?.getAttribute('y'), '422');
The second label should have text of 109, 280, an x value of 114, and a y value of 220.
const labelTwo = document.querySelectorAll('text')[1];
assert.strictEqual(labelTwo?.textContent, '109, 280');
assert.strictEqual(labelTwo?.getAttribute('x'), '114');
assert.strictEqual(labelTwo?.getAttribute('y'), '220');
The third label should have text of 310, 120, an x value of 315, and a y value of 380.
const labelThree = document.querySelectorAll('text')[2];
assert.strictEqual(labelThree?.textContent, '310, 120');
assert.strictEqual(labelThree?.getAttribute('x'), '315');
assert.strictEqual(labelThree?.getAttribute('y'), '380');
The fourth label should have text of 79, 411, an x value of 84, and a y value of 89.
const labelFour = document.querySelectorAll('text')[3];
assert.strictEqual(labelFour?.textContent, '79, 411');
assert.strictEqual(labelFour?.getAttribute('x'), '84');
assert.strictEqual(labelFour?.getAttribute('y'), '89');
The fifth label should have text of 420, 220, an x value of 425, and a y value of 280.
const labelFive = document.querySelectorAll('text')[4];
assert.strictEqual(labelFive?.textContent, '420, 220');
assert.strictEqual(labelFive?.getAttribute('x'), '425');
assert.strictEqual(labelFive?.getAttribute('y'), '280');
The sixth label should have text of 233, 145, an x value of 238, and a y value of 355.
const labelSix = document.querySelectorAll('text')[5];
assert.strictEqual(labelSix?.textContent, '233, 145');
assert.strictEqual(labelSix?.getAttribute('x'), '238');
assert.strictEqual(labelSix?.getAttribute('y'), '355');
The seventh label should have text of 333, 96, an x value of 338, and a y value of 404.
const labelSeven = document.querySelectorAll('text')[6];
assert.strictEqual(labelSeven?.textContent, '333, 96');
assert.strictEqual(labelSeven?.getAttribute('x'), '338');
assert.strictEqual(labelSeven?.getAttribute('y'), '404');
The eighth label should have text of 222, 333, an x value of 227, and a y value of 167.
const labelEight = document.querySelectorAll('text')[7];
assert.strictEqual(labelEight?.textContent, '222, 333');
assert.strictEqual(labelEight?.getAttribute('x'), '227');
assert.strictEqual(labelEight?.getAttribute('y'), '167');
The ninth label should have text of 78, 320, an x value of 83, and a y value of 180.
const labelNine = document.querySelectorAll('text')[8];
assert.strictEqual(labelNine?.textContent, '78, 320');
assert.strictEqual(labelNine?.getAttribute('x'), '83');
assert.strictEqual(labelNine?.getAttribute('y'), '180');
The tenth label should have text of 21, 123, an x value of 26, and a y value of 377.
const labelTen = document.querySelectorAll('text')[9];
assert.strictEqual(labelTen?.textContent, '21, 123');
assert.strictEqual(labelTen?.getAttribute('x'), '26');
assert.strictEqual(labelTen?.getAttribute('y'), '377');
<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>
<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')
.attr('x', d => d[0] + 5)
.attr('y', d => h - d[1])
.text(d => d[0] + ', ' + d[1]);
</script>
</body>