Hey Ron,
Sure thing: so this code seems to be based off of a D3 example (or based off an example based off an example, etc ) The error - which might be kind of hard to spot, is this:
let svg = d3.select(DOM.svg(width, height));
svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
So the intent of the original code was that everything in the chart goes inside of a g
element (a SVG group) and by doing so, they don’t need to take into account (in JavaScript-land) the left margin and top margin, because they’ll be offset by the SVG transform of the g element.
The bug was introduced when, instead of selecting an svg element on the page (probably) this was swapped top use DOM.svg. And the result is that, instead of appending everything in the chart inside of the g
, this adds the g
but never puts anything inside of it. So the left and top margins aren’t enacted anywhere.
Here’s a tweaked version of the notebook that corrects this error: https://beta.observablehq.com/compare/64a770278018c5f3...49a6853f19f1c2fc
The relevant sections are these:
let svg = d3.select(DOM.svg(width, height))
.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
And then
return svg.node().parentNode;
So, the svg
variable now refers to the g
(with its transform that sets the margins) and the return value needs to look for the parentNode of that g (which is the SVG element).
Hope that helps - this is admittedly a pretty sneaky one, because it doesn’t cause an error and is a difference of only a few characters.
-Tom