Skip to content

Commit e8825f7

Browse files
committed
almost there, but still a bit off with faceting
1 parent 2872a89 commit e8825f7

File tree

7 files changed

+92
-28
lines changed

7 files changed

+92
-28
lines changed

src/dimensions.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ export function Dimensions(scales, geometry, axes, options = {}) {
7575
}
7676

7777
function autoHeight({y, fy, fx}, projectionRatio, width) {
78+
const nfy = fy ? fy.scale.domain().length : 1;
7879
if (projectionRatio > 0) {
79-
return Math.max(
80-
200,
81-
Math.min(
82-
1200,
83-
Math.ceil((fx ? fx.scale.bandwidth() : 1) * width * projectionRatio) / (fy ? fy.scale.bandwidth() : 1)
80+
return Math.ceil(
81+
Math.max(
82+
200,
83+
Math.min(1200, (width * projectionRatio * (fx ? fx.scale.bandwidth() : 1)) / (fy ? fy.scale.bandwidth() : 1))
8484
)
8585
);
8686
}
87-
const nfy = fy ? fy.scale.domain().length : 1;
8887
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
8988
return !!(y || fy) * Math.max(1, Math.min(60, ny * nfy)) * 20 + !!fx * 30 + 60;
9089
}

src/projection.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,52 +110,40 @@ export function Projection(
110110
return {stream: (s) => projection.stream(transform.stream(clip(s))), ratio};
111111
}
112112

113-
// When a projection is specified, try to determine a good value for the
114-
// projection’s height, if it is a named projection. When we don’t have a way to
115-
// know, the golden ratio is our best guess.
116-
export function projectionAspectRatio({projection} = {}, geometry) {
117-
projection = Projection(
118-
{projection},
119-
{width: 100, height: 300, marginLeft: 0, marginRight: 0, marginTop: 0, marginBottom: 0}
120-
);
121-
if (projection == null) return geometry ? golden - 1 : 0;
122-
return projection.ratio ?? golden - 1;
123-
}
124-
125113
function namedProjection(projection) {
126114
switch (`${projection}`.toLowerCase()) {
127115
case "albers-usa":
128-
return scaleProjection(geoAlbersUsa, 0.7463, 0.4673);
116+
return scaleProjection(geoAlbersUsa, 0.7463, 0.4673, 610 / 975);
129117
case "albers":
130-
return conicProjection(geoAlbers, 0.7463, 0.4673);
118+
return conicProjection(geoAlbers, 0.7463, 0.4673, 610 / 975);
131119
case "azimuthal-equal-area":
132-
return scaleProjection(geoAzimuthalEqualArea, 4, 4);
120+
return scaleProjection(geoAzimuthalEqualArea, 4, 4, 1);
133121
case "azimuthal-equidistant":
134-
return scaleProjection(geoAzimuthalEquidistant, tau, tau);
122+
return scaleProjection(geoAzimuthalEquidistant, tau, tau, 1);
135123
case "conic-conformal":
136124
return conicProjection(geoConicConformal, tau, tau);
137125
case "conic-equal-area":
138126
return conicProjection(geoConicEqualArea, 6.1702, 2.9781);
139127
case "conic-equidistant":
140128
return conicProjection(geoConicEquidistant, 7.312, 3.6282);
141129
case "equal-earth":
142-
return scaleProjection(geoEqualEarth, 5.4133, 2.6347);
130+
return scaleProjection(geoEqualEarth, 5.4133, 2.6347, 0.4867);
143131
case "equirectangular":
144-
return scaleProjection(geoEquirectangular, tau, pi);
132+
return scaleProjection(geoEquirectangular, tau, pi, 0.5);
145133
case "gnomonic":
146-
return scaleProjection(geoGnomonic, 3.4641, 3.4641);
134+
return scaleProjection(geoGnomonic, 3.4641, 3.4641, 1);
147135
case "identity":
148136
return identity;
149137
case "reflect-y":
150138
return reflectY;
151139
case "mercator":
152140
return scaleProjection(geoMercator, tau, tau, 1);
153141
case "orthographic":
154-
return scaleProjection(geoOrthographic, 2, 2);
142+
return scaleProjection(geoOrthographic, 2, 2, 1);
155143
case "stereographic":
156-
return scaleProjection(geoStereographic, 2, 2);
144+
return scaleProjection(geoStereographic, 2, 2, 1);
157145
case "transverse-mercator":
158-
return scaleProjection(geoTransverseMercator, tau, tau);
146+
return scaleProjection(geoTransverseMercator, tau, tau, 1);
159147
default:
160148
throw new Error(`unknown projection type: ${projection}`);
161149
}
@@ -226,3 +214,15 @@ export function applyProjection(values, projection) {
226214
stream.point(x[i], y[i]);
227215
}
228216
}
217+
218+
// When a projection is specified, try to determine a good value for the
219+
// projection’s height, if it is a named projection. When we don’t have a way to
220+
// know, the golden ratio is our best guess.
221+
export function projectionAspectRatio({projection} = {}, geometry) {
222+
projection = Projection(
223+
{projection},
224+
{width: 100, height: 300, marginLeft: 0, marginRight: 0, marginTop: 0, marginBottom: 0}
225+
);
226+
if (projection == null) return geometry ? golden - 1 : 0;
227+
return projection.ratio ?? golden - 1;
228+
}

test/plots/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ export {default as projectionFitBertin1953} from "./projection-fit-bertin1953.js
181181
export {default as projectionFitConic} from "./projection-fit-conic.js";
182182
export {default as projectionFitIdentity} from "./projection-fit-identity.js";
183183
export {default as projectionFitUsAlbers} from "./projection-fit-us-albers.js";
184+
export {default as projectionHeightAlbers} from "./projection-height-albers.js";
185+
export {default as projectionHeightEqualEarth} from "./projection-height-equal-earth.js";
186+
export {default as projectionHeightMercator} from "./projection-height-mercator.js";
187+
export {default as projectionHeightOrthographic} from "./projection-height-orthographic.js";
184188
export {default as randomBins} from "./random-bins.js";
185189
export {default as randomBinsXY} from "./random-bins-xy.js";
186190
export {default as randomQuantile} from "./random-quantile.js";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
import {mesh} from "topojson-client";
4+
5+
export default async function () {
6+
const [conus, countymesh] = await d3
7+
.json("data/us-counties-10m.json")
8+
.then((us) => [mesh(us, us.objects.states, (a, b) => a === b), mesh(us, us.objects.counties, (a, b) => a !== b)]);
9+
return Plot.plot({
10+
width: 975,
11+
// expected height: 610,
12+
projection: {
13+
type: "albers-usa"
14+
},
15+
marks: [
16+
Plot.geo(conus, {strokeWidth: 1.5}),
17+
Plot.geo(countymesh, {strokeOpacity: 0.1}),
18+
Plot.frame({stroke: "red"})
19+
]
20+
});
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
import {feature} from "topojson-client";
4+
5+
export default async function () {
6+
const world = await d3.json("data/countries-110m.json");
7+
const land = feature(world, world.objects.land);
8+
return Plot.plot({
9+
facet: {data: [0, 1], x: [0, 1]},
10+
projection: "equal-earth",
11+
marks: [Plot.geo(land, {fill: "currentColor"}), Plot.graticule(), Plot.sphere(), Plot.frame({stroke: "red"})]
12+
});
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
import {feature} from "topojson-client";
4+
5+
export default async function () {
6+
const world = await d3.json("data/countries-110m.json");
7+
const land = feature(world, world.objects.land);
8+
return Plot.plot({
9+
width: 400,
10+
facet: {data: [0, 1], y: [0, 1]},
11+
projection: "mercator",
12+
marks: [Plot.geo(land, {fill: "currentColor"}), Plot.graticule(), Plot.sphere(), Plot.frame({stroke: "red"})]
13+
});
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
import {feature} from "topojson-client";
4+
5+
export default async function () {
6+
const world = await d3.json("data/countries-110m.json");
7+
const land = feature(world, world.objects.land);
8+
return Plot.plot({
9+
facet: {data: [0, 1, 2, 3], x: (d) => d % 2, y: (d) => d >> 1},
10+
projection: "orthographic",
11+
marks: [Plot.geo(land, {fill: "currentColor"}), Plot.graticule(), Plot.sphere(), Plot.frame({stroke: "red"})]
12+
});
13+
}

0 commit comments

Comments
 (0)