Thanks so much, I appreciate it. I also read through all those older posts you made, and your input is really helpful. You could easily create your own D3 tutorials that are easy to follow!
I’m so close to finishing this, and my legend LOOKS like it has appropriate HTML created, but it’s not appearing on the final map.
My guess is that it could have something to do with the size if my original SVG and that it’s showing outside of it?
But I fell like I did the same thing with my heat map map.
Would you mind taking a look ?
Here is my code for the legend:
/Legend
//create div
let lh = 20
let lw = 100
var legend = d3
.select("body")
.append("div")
.attr("id", "legend")
.attr("height", lh)
.attr("width", lw)
legend
.selectAll("rect")
.data([20,40,60,80,100])
.enter()
.append("rect")
.attr("width", 20)
.attr("height", 20)
.attr("fill", (d) => colorScale(d))
And the resulting HTML, which looks like it should display what I want:
<div id="legend" height="20" width="100">
<rect width="20" height="20" fill="#662506"></rect>
<rect width="20" height="20" fill="#fe9929"></rect>
<rect width="20" height="20" fill="#cc4c02"></rect>
<rect width="20" height="20" fill="#662506"></rect>
<rect width="20" height="20" fill="#662506"></rect>
</div>
But it’s not showing at all.
I’ve seen some solutions having a vertically aligned legend. Would you just swap the width and height of the bounding element?
And a final question (ok, probably not my last question)…actually maybe this will solve it…
How would I position my legend over the SVG, rather than as an adjacent element / div?
I guess I can look up absolute positioning for SVG… maybe it’s as simple as just using CSS to position it. I’ll give it a try!