Public
Edited
Jul 8, 2021
5 stars
Insert cell
Insert cell
Insert cell
height = width / 5
Insert cell
function svgContext() {
return DOM.svg(width, height);
}
Insert cell
Insert cell
function drawCircle(ctx) {
d3.select(ctx)
.append("circle")
.attr("r", height / 2 - 1)
.attr("cx", height / 2)
.attr("cy", height / 2)
.attr("fill", "rgb(200,100,50)")
.attr("stroke", "black")
.attr("stroke-width", 0.5);
return ctx;
}
Insert cell
Insert cell
drawCircle(svgContext())
Insert cell
Insert cell
function drawPoly1(coords, ctx) {
d3.select(ctx)
.append("path")
.attr("d", `M${coords.join("L")}Z`)
.attr("fill", "rgb(200,100,50)")
.attr("stroke", "black")
.attr("stroke-width", 0.5);
return ctx;
}
Insert cell
drawPoly1(
[
[50, 10],
[160, 60],
[210, 150],
[100, 100],
[10, 120]
],
svgContext()
)
Insert cell
Insert cell
coords1 = [
[105000, 14000],
[116000, 9000],
[121000, 0],
[110000, 5000],
[101000, 3000]
]
Insert cell
coords2 = [
[130000, 15000],
[108000, 10000],
[114000, 3000]
]
Insert cell
Insert cell
function scaleFn(coords, w, h) {
const [xMin, xMax] = d3.extent(coords, (p) => p[0]);
const [yMin, yMax] = d3.extent(coords, (p) => p[1]);
const scaleFactor = Math.min(w / (xMax - xMin), h / (yMax - yMin));
return ([x, y]) => [scaleFactor * (x - xMin), h - scaleFactor * (y - yMin)];
}
Insert cell
function drawPoly2(coords, ctx) {
const scCoords = coords.map(scaleFn(coords, width, height));
d3.select(ctx)
.append("path")
.attr("d", `M${scCoords.join("L")}Z`)
.attr("fill", "rgb(200,100,50)")
.attr("stroke", "black")
.attr("stroke-width", 0.5);
return ctx;
}
Insert cell
drawPoly2(coords1, svgContext())
Insert cell
Insert cell
function drawPoly3(coords, sc, ctx) {
d3.select(ctx)
.append("path")
.attr("d", `M${coords.map(sc).join("L")}Z`);
return ctx;
}
Insert cell
{
const sc = scaleFn(coords1, width, height);
const ctx = drawPoly3(coords1, sc, svgContext());
d3.select(ctx).attr("fill", "rgb(200,100,50)").attr("fill-opacity", 0.5);
return ctx;
}
Insert cell
Insert cell
world = d3.json(
"https://unpkg.com/visionscarto-world-atlas@0.0.6/world/110m_land.geojson"
)
Insert cell
Insert cell
function projection(features) {
return d3.geoEqualEarth().fitSize([height * 2, height], features);
}
Insert cell
Insert cell
function drawMap(features, ctx) {
d3.select(ctx)
.append("path")
.datum(features)
.attr("d", d3.geoPath(projection(features)));
return ctx;
}
Insert cell
{
const ctx = drawMap(world, svgContext());
d3.select(ctx).attr("fill", "rgb(200,100,50)").attr("fill-opacity", 0.5);
return ctx;
}
Insert cell
Insert cell
topojson = require("topojson-client@3")
Insert cell
Insert cell
london = d3
.json("https://gicentre.github.io/data/geoTutorials/londonBoroughs.json")
.then((feats) => topojson.feature(feats, feats.objects.boroughs))
Insert cell
Insert cell
{
const ctx = drawMap(london, svgContext());
d3.select(ctx)
.attr("fill", "rgb(200, 100, 50)")
.attr("fill-opacity", 0.5)
.attr("stroke", "white");
return ctx;
}
Insert cell
Insert cell
function canvasContext() {
return DOM.context2d(width, height);
}
Insert cell
Insert cell
function drawPoly4(coords, sc, ctx) {
const scCoords = coords.map(sc);
ctx.beginPath();
for (let c of scCoords) {
ctx.lineTo(c[0], c[1]);
}
ctx.closePath();
}
Insert cell
Insert cell
{
const sc = scaleFn(coords1.concat(coords2), width, height);
const ctx = canvasContext();

drawPoly4(coords1, sc, ctx);
ctx.stroke();
ctx.fillStyle = "rgb(200, 100, 50)";
ctx.fill();

drawPoly4(coords2, sc, ctx);
ctx.fillStyle = "black";
ctx.globalAlpha = 0.3;
ctx.fill();

return ctx.canvas;
}
Insert cell
Insert cell
{
const ctx = canvasContext();
const land = await d3.json(
"https://unpkg.com/visionscarto-world-atlas@0.0.6/world/110m_land.geojson"
);
const projection = d3.geoEqualEarth().fitSize([height * 2, height], land);
d3.geoPath(projection, ctx)(land);

ctx.fillStyle = "rgb(200, 100, 50)";
ctx.globalAlpha = 0.5;
ctx.fill();

return ctx.canvas;
}
Insert cell
{
const ctx = canvasContext();

d3.geoPath(projection(london), ctx)(london);
ctx.fillStyle = "rgb(200, 100, 50)";
ctx.strokeStyle = "white";
ctx.fill();
ctx.stroke();
return ctx.canvas;
}
Insert cell
Insert cell
Insert cell
Insert cell
new Renderer(width, width / 5, "canvas")
.fit(coords1.concat(coords2), 15)
.strokeWidth(3)
.fill("rgb(180,90,45)")
.polygon(coords1)

.stroke("")
.fill("rgba(0,0,0,0.3)")
.polygon(coords2)

.render()
Insert cell
Insert cell
new Renderer(0.3 * width, 0.2 * width, "svg")
.stroke("white")
.fill("rgb(200,100,50)")
.geoShape(london)
.render()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more