Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix facet positioning of a rendered rendered SVGSVGElement #2219

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/features/marks.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Plot.plot({
```
:::

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.)
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.)

:::plot defer https://observablehq.com/@observablehq/plot-gradient-bars
```js
Expand Down
15 changes: 10 additions & 5 deletions src/facet.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ export function facetGroups(data, {fx, fy}) {
}

export function facetTranslator(fx, fy, {marginTop, marginLeft}) {
return fx && fy
? ({x, y}) => `translate(${fx(x) - marginLeft},${fy(y) - marginTop})`
: fx
? ({x}) => `translate(${fx(x) - marginLeft},0)`
: ({y}) => `translate(0,${fy(y) - marginTop})`;
const x = fx ? ({x}) => fx(x) - marginLeft : () => 0;
const y = fy ? ({y}) => fy(y) - marginTop : () => 0;
return function (d) {
if (this.tagName === "svg") {
this.setAttribute("x", x(d));
this.setAttribute("y", y(d));
} else {
this.setAttribute("transform", `translate(${x(d)},${y(d)})`);
}
};
}

// Returns an index that for each facet lists all the elements present in other
Expand Down
2 changes: 1 addition & 1 deletion src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export function plot(options = {}) {
}
}
}
g?.selectChildren().attr("transform", facetTranslate);
g?.selectChildren().each(facetTranslate);
}
}

Expand Down
Loading