Published
Edited
Aug 11, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function map(width, height) {
// This is exactly the same!
projection.fitSize([width - margin * 2, height - margin * 2], data);

const svg = d3
.create('svg')
.attr('viewBox', [0, 0, width, height])
.attr('width', width)
.attr('height', height)
.style('border', '1px dotted rgba(0, 0, 0, 50%)')
.style('box-sizing', 'border-box');

const g = svg
.append('g')
.attr('transform', `translate(${margin},${margin})`);

g.selectAll('path')
.data(data.features ? data.features : [data]) // just in case someone passes a single Feature
.join('path')
.attr('fill', '#ccc')
.attr('stroke', '#eee')
.attr('d', path);

return svg.node();
}
Insert cell
Insert cell
projection = d3.geoIdentity().reflectY(true)
Insert cell
Insert cell
path = d3.geoPath(projection)
Insert cell
Insert cell
data = d3.json(customGeoJSONPath || defaultGeoJSONPath)
Insert cell
Insert cell
mapWidth = Math.min(userWidth, width)
Insert cell
heightRatio = {
const bounds = path.bounds(data);
return (bounds[1][1] - bounds[0][1]) / (bounds[1][0] - bounds[0][0]);
}
Insert cell
mapHeight = Math.floor(mapWidth * heightRatio)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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