-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
5,226 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {ascending} from "d3-array"; | ||
import {geoPath} from "d3-geo"; | ||
import {create} from "d3-selection"; | ||
import {identity} from "../mark.js"; | ||
import {filter, nonempty, positive} from "../defined.js"; | ||
import {Mark, number, maybeColor, maybeNumber} from "../mark.js"; | ||
import {Style, applyDirectStyles, applyIndirectStyles} from "../style.js"; | ||
|
||
export class Carto extends Mark { | ||
constructor( | ||
data, | ||
{ | ||
feature = identity, | ||
z, | ||
r, | ||
fill, | ||
stroke, | ||
title, | ||
insetTop = 0, | ||
insetRight = 0, | ||
insetBottom = 0, | ||
insetLeft = 0, | ||
transform, | ||
...style | ||
} = {} | ||
) { | ||
const [vr, cr = vr == null ? 3 : undefined] = maybeNumber(r); | ||
const [vfill, cfill = vfill == null ? "none" : undefined] = maybeColor(fill); | ||
const [vstroke, cstroke = vstroke == null && cfill === "none" ? "currentColor" : undefined] = maybeColor(stroke); | ||
super( | ||
data, | ||
[ | ||
{name: "d", value: feature, scale: "projection", label: feature.label}, | ||
{name: "z", value: z, optional: true}, | ||
{name: "r", value: vr, scale: "r", optional: true}, | ||
{name: "fill", value: vfill, scale: "color", optional: true}, | ||
{name: "stroke", value: vstroke, scale: "color", optional: true}, | ||
{name: "title", value: title, optional: true} | ||
], | ||
transform | ||
); | ||
Style(this, {fill: cfill, stroke: cstroke, ...style}); | ||
this.r = cr; | ||
this.insetTop = number(insetTop); | ||
this.insetRight = number(insetRight); | ||
this.insetBottom = number(insetBottom); | ||
this.insetLeft = number(insetLeft); | ||
} | ||
render( | ||
I, | ||
{color, r, projection}, | ||
{d: D, z: Z, r: R, fill: F, stroke: S, strokeWidth: LW, title: L}, | ||
{height, marginBottom, marginLeft, marginRight, marginTop, width} | ||
) { | ||
let index = filter(I, D, F, S); | ||
if (R) index = index.filter(i => positive(R[i])); | ||
if (Z) index.sort((i, j) => ascending(Z[i], Z[j])); | ||
|
||
if (projection.fitFeature) { | ||
projection.fitExtent([ | ||
[marginLeft, marginTop], | ||
[width - marginRight, height - marginBottom] | ||
], projection.fitFeature); | ||
delete projection.fitFeature; | ||
} | ||
const path = geoPath(projection).pointRadius(this.r); | ||
|
||
return create("svg:g") | ||
.call(applyIndirectStyles, this) | ||
.call(g => g.selectAll() | ||
.data(I) | ||
.join("path") | ||
.call(applyDirectStyles, this) | ||
.attr("d", i => (R ? path.pointRadius(r(R[i])) : path)(D[i])) | ||
.attr("fill", F && (i => color(F[i]))) | ||
.attr("stroke", S && (i => color(S[i]))) | ||
.attr("strokeWidth", LW && (i => LW[i])) | ||
.call(L ? path => path | ||
.filter(i => nonempty(L[i])) | ||
.append("title") | ||
.text(i => L[i]) : () => {})) | ||
.node(); | ||
} | ||
} | ||
|
||
export function carto(data, options) { | ||
return new Carto(data, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {geoMercator, geoOrthographic, geoEqualEarth} from "d3-geo"; | ||
|
||
// TODO Allow this to be extended. | ||
const projections = new Map([ | ||
["mercator", geoMercator], | ||
["orthographic", geoOrthographic], | ||
["equalEarth", geoEqualEarth] | ||
]); | ||
|
||
export function ScaleProjection(key, channels, { projection, rotate, precision }) { | ||
// TODO: use the channels to set up the projection's extent | ||
// note: we don't yet know the canvas dimensions… | ||
projection = typeof projection === "function" ? projection | ||
: (projections.get(projection) || geoMercator)(); | ||
if (rotate && projection.rotate) projection.rotate(rotate); | ||
if (precision && projection.precision) projection.precision(precision); | ||
projection.fitFeature = channels[0].value[0] || {type:"Sphere"}; | ||
return {type: "projection", scale: projection}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<body> | ||
<script src="https://cdn.jsdelivr.net/npm/d3@6/dist/d3.js"></script> | ||
<script src="../dist/@observablehq/plot.umd.js"></script> | ||
<script> | ||
|
||
const url = "https://unpkg.com/[email protected]/world/110m_land.geojson"; | ||
|
||
(async function() { | ||
const world = await d3.json(url); | ||
|
||
document.body.appendChild(Plot.plot({ | ||
marks: [ | ||
// 🚀 the very first mark defines the region of interest (TODO: design this) | ||
Plot.carto([{type: "Sphere"}], { fill: "none" }), | ||
Plot.carto([world], { fill: "black" }), | ||
Plot.carto([{type: "Sphere"}], { strokeWidth: 2 }), | ||
], | ||
width: 600, | ||
height: 600, | ||
})); | ||
|
||
})(); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<body> | ||
<script src="https://cdn.jsdelivr.net/npm/d3@6/dist/d3.js"></script> | ||
<script src="../dist/@observablehq/plot.umd.js"></script> | ||
<script> | ||
|
||
const url = "https://unpkg.com/[email protected]/world/110m_countries.geojson"; | ||
|
||
(async function() { | ||
const world = await d3.json(url); | ||
|
||
document.body.appendChild(Plot.plot({ | ||
projection: { | ||
projection: "equalEarth", | ||
rotate: [-10, 0] | ||
}, | ||
color: { scheme: "rdylgn", }, | ||
marks: [ | ||
Plot.carto([{type: "Sphere"}], { fill: "lightblue" }), | ||
Plot.carto([d3.geoGraticule10()], { stroke: "#fff", strokeWidth: .25 }), | ||
Plot.carto(world.features, | ||
{ | ||
fill: d => d.properties.income_grp, | ||
strokeWidth: .25, | ||
stroke: "#000" | ||
}), | ||
Plot.carto([{type: "Sphere"}], { strokeWidth: 2 }), | ||
], | ||
width: document.body.offsetWidth, | ||
height: document.body.offsetWidth / 2, | ||
})); | ||
|
||
})(); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<body> | ||
<script src="https://cdn.jsdelivr.net/npm/d3@6/dist/d3.js"></script> | ||
<script src="../dist/@observablehq/plot.umd.js"></script> | ||
<script> | ||
|
||
const url = "https://unpkg.com/[email protected]/world/110m_countries.geojson"; | ||
|
||
(async function() { | ||
const world = await d3.json(url); | ||
const cities = await d3.csv("data/cities-5000.csv"); | ||
|
||
document.body.appendChild(Plot.plot({ | ||
projection: { | ||
projection: "equalEarth", | ||
rotate: [-10, 0] | ||
}, | ||
r: { domain: [0, 5e6] }, | ||
marks: [ | ||
// 🚀 the very first mark defines the region of interest (TODO: design this) | ||
Plot.carto([{type: "Sphere"}], { fill: "none" }), | ||
Plot.carto([world], { fill: "black" }), | ||
Plot.carto(cities, { | ||
transform: data => data.map(({lng, lat, ...properties}) => ({ | ||
type: "Point", coordinates: [lng, lat], properties | ||
})), | ||
fill: "orange", | ||
title: d => d.properties.city, | ||
r: d => d.properties.population | ||
}), | ||
Plot.carto([{type: "Sphere"}], { strokeWidth: 2 }), | ||
], | ||
width: 600, | ||
height: 600, | ||
marginLeft: 1, | ||
marginRight: 1, | ||
marginTop: 1, | ||
marginBottom: 1, | ||
width: document.body.offsetWidth, | ||
height: document.body.offsetWidth / 2, | ||
})); | ||
|
||
})(); | ||
|
||
</script> |
Oops, something went wrong.