Hello !
I have the Choropleth Map completed and with all tests passing, but I have a problem with the legend part which I can’t resolve.
I’m able to show the legend’s colors, but I can’t find the way to show the corresponding text next to each color.
Here is my legend’s code:
const colors = [
{
"color": "#ffcccc",
"text": "Less than 15%"
},
{
"color": "#ff8080",
"text": "Less than 30%"
},
{
"color": "#ff0000",
"text": "Less than 45%"
},
{
"color": "#990000",
"text": "More than 45%"
}
]
const legendWidth = 220
const legendHeight = 120
const legendRectWidth = 30
const legendRectHeight = legendHeight / colors.length
let legend = d3.select("#legend")
.attr("width", legendWidth)
.attr("height", legendHeight)
.selectAll("rect")
.data(colors)
.enter()
.append("rect")
.attr("fill", (c, i) => colors[i].color)
.attr("x", 0)
.attr("y", (c, i) => legendRectHeight * i)
.attr("width", legendRectWidth)
.attr("height", legendRectHeight)
legend.selectAll("text")
.data(colors)
.enter()
.append("text")
.attr("fill", "black")
.attr("x", legendRectWidth + 10)
.attr("y", (c, i) => legendRectHeight * i)
.text((c, i) => colors[i].text)
.style("font-size", "12px")
.attr("alignment-baseline","middle")
And, also, the link to the project: https://codepen.io/pedrorv1998/pen/BaLvqez?editors=0010
I appreciate any help!