Skip to content

Commit 9c3abb3

Browse files
authored
default scale projection (#1170)
* fix #1169; default scale projection * test scale projection
1 parent 680966f commit 9c3abb3

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
lines changed

src/marks/geo.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {geoGraticule10, geoPath} from "d3";
1+
import {geoGraticule10, geoPath, geoTransform} from "d3";
22
import {create} from "../context.js";
33
import {negative, positive} from "../defined.js";
44
import {identity, maybeNumberChannel} from "../options.js";
@@ -32,7 +32,7 @@ export class Geo extends Mark {
3232
}
3333
render(index, scales, channels, dimensions, context) {
3434
const {geometry: G, r: R} = channels;
35-
const path = geoPath(context.projection);
35+
const path = geoPath(context.projection ?? scaleProjection(scales));
3636
const {r} = this;
3737
if (negative(r)) index = [];
3838
else if (r !== undefined) path.pointRadius(r);
@@ -52,6 +52,20 @@ export class Geo extends Mark {
5252
}
5353
}
5454

55+
// If no projection is specified, default to a projection that passes points
56+
// through the x and y scales, if any.
57+
function scaleProjection({x: X, y: Y}) {
58+
if (X || Y) {
59+
X ??= (x) => x;
60+
Y ??= (y) => y;
61+
return geoTransform({
62+
point(x, y) {
63+
this.stream.point(X(x), Y(y));
64+
}
65+
});
66+
}
67+
}
68+
5569
export function geo(data, {geometry = identity, ...options} = {}) {
5670
switch (data?.type) {
5771
case "FeatureCollection":

test/output/walmartsDensityUnprojected.svg

Lines changed: 136 additions & 0 deletions
Loading

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export {default as vectorFrame} from "./vector-frame.js";
254254
export {default as walmarts} from "./walmarts.js";
255255
export {default as walmartsDecades} from "./walmarts-decades.js";
256256
export {default as walmartsDensity} from "./walmarts-density.js";
257+
export {default as walmartsDensityUnprojected} from "./walmarts-density-unprojected.js";
257258
export {default as wealthBritainBar} from "./wealth-britain-bar.js";
258259
export {default as wealthBritainProportionPlot} from "./wealth-britain-proportion-plot.js";
259260
export {default as wordCloud} from "./word-cloud.js";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function () {
5+
const walmarts = await d3.tsv("data/walmarts.tsv", d3.autoType);
6+
return Plot.plot({
7+
width: 960,
8+
height: 600,
9+
grid: true,
10+
color: {
11+
scheme: "blues"
12+
},
13+
marks: [
14+
Plot.density(walmarts, {x: "longitude", y: "latitude", bandwidth: 12, fill: "density"}),
15+
Plot.geo({type: "MultiPoint", coordinates: walmarts.map((d) => [d.longitude, d.latitude])})
16+
]
17+
});
18+
}

0 commit comments

Comments
 (0)