Skip to content

Commit 419232b

Browse files
Filmbostock
andauthored
Fix facet positioning of a rendered rendered SVGSVGElement (#2219)
* Wrap any rendered SVGSVGElement in a SVGGElement closes #2218 * use x, y instead of a wrapper (review suggestion) * inline template --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent e5adcfe commit 419232b

File tree

6 files changed

+1074
-7
lines changed

6 files changed

+1074
-7
lines changed

docs/features/marks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Plot.plot({
114114
```
115115
:::
116116

117-
Marks may also be a function which returns an SVG element, if you wish to insert arbitrary content. (Here we use [Hypertext Literal](https://github.com/observablehq/htl) to generate an SVG gradient.)
117+
Marks may also be a function which returns an [SVG element](https://developer.mozilla.org/en-US/docs/Web/SVG/Element), if you wish to insert arbitrary content. (Here we use [Hypertext Literal](https://github.com/observablehq/htl) to generate an SVG gradient.)
118118

119119
:::plot defer https://observablehq.com/@observablehq/plot-gradient-bars
120120
```js

src/facet.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ export function facetGroups(data, {fx, fy}) {
6363
}
6464

6565
export function facetTranslator(fx, fy, {marginTop, marginLeft}) {
66-
return fx && fy
67-
? ({x, y}) => `translate(${fx(x) - marginLeft},${fy(y) - marginTop})`
68-
: fx
69-
? ({x}) => `translate(${fx(x) - marginLeft},0)`
70-
: ({y}) => `translate(0,${fy(y) - marginTop})`;
66+
const x = fx ? ({x}) => fx(x) - marginLeft : () => 0;
67+
const y = fy ? ({y}) => fy(y) - marginTop : () => 0;
68+
return function (d) {
69+
if (this.tagName === "svg") {
70+
this.setAttribute("x", x(d));
71+
this.setAttribute("y", y(d));
72+
} else {
73+
this.setAttribute("transform", `translate(${x(d)},${y(d)})`);
74+
}
75+
};
7176
}
7277

7378
// Returns an index that for each facet lists all the elements present in other

src/plot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export function plot(options = {}) {
317317
}
318318
}
319319
}
320-
g?.selectChildren().attr("transform", facetTranslate);
320+
g?.selectChildren().each(facetTranslate);
321321
}
322322
}
323323

0 commit comments

Comments
 (0)