Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function autoHeight(
) {
const nfy = fy ? fy.scale.domain().length : 1;

// If a projection is specified, use its natural aspect ratio (if known).
// If a projection is specified, compute an aspect ratio based on the domain,
// defaulting to the projection’s natural aspect ratio (if known).
const ar = projectionAspectRatio(projection);
if (ar) {
const nfx = fx ? fx.scale.domain().length : 1;
Expand Down
19 changes: 15 additions & 4 deletions src/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ function scaleProjection(createProjection, kx, ky) {
if (precision != null) projection.precision?.(precision);
if (rotate != null) projection.rotate?.(rotate);
if (typeof clip === "number") projection.clipAngle?.(clip);
projection.scale(Math.min(width / kx, height / ky));
projection.translate([width / 2, height / 2]);
if (width && height) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check for nullish instead of zero?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! I was thinking aspectRatio, but this part returns the actual projection, so it should respect a collapsed (degenerate) projection.

I fixed it and added a test case (without the fix, the test case displays a map in the default scale, instead of a 0-height map…).

Note that the degenerate case is still slightly problematic, as its path’s d property is an invalid string "M NaN,NaN…" instead of being null. (A projection with a strictly negative scale returns null, so the "issue" is only for scale 0.)

projection.scale(Math.min(width / kx, height / ky));
projection.translate([width / 2, height / 2]);
}
return projection;
},
aspectRatio: ky / kx
Expand All @@ -183,7 +185,7 @@ function conicProjection(createProjection, kx, ky) {
const projection = type(options);
if (parallels != null) {
projection.parallels(parallels);
if (domain === undefined) {
if (domain === undefined && width && height) {
projection.fitSize([width, height], {type: "Sphere"});
}
}
Expand Down Expand Up @@ -243,7 +245,16 @@ export function hasProjection({projection} = {}) {
// the logic of this function exactly matches createProjection above!
export function projectionAspectRatio(projection) {
if (typeof projection?.stream === "function") return defaultAspectRatio;
if (isObject(projection)) projection = projection.type;
if (isObject(projection)) {
let domain, options;
({domain, type: projection, ...options} = projection);
if (domain != null && projection != null) {
const {type} = namedProjection(projection);
const [[x0, y0], [x1, y1]] = geoPath(type(options)).bounds(domain);
const r = (y1 - y0) / (x1 - x0);
return r && isFinite(r) ? (r < 0.2 ? 0.2 : r > 5 ? 5 : r) : defaultAspectRatio;
}
}
if (projection == null) return;
if (typeof projection !== "function") {
const {aspectRatio} = namedProjection(projection);
Expand Down
134 changes: 67 additions & 67 deletions test/output/geoText.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
208 changes: 104 additions & 104 deletions test/output/geoTip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
208 changes: 104 additions & 104 deletions test/output/geoTipCentroid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
208 changes: 104 additions & 104 deletions test/output/geoTipGeoCentroid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
208 changes: 104 additions & 104 deletions test/output/geoTipXY.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions test/output/projectionDomainRatioME.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading