function geojson2svg(geojson, options = {}) {
const chartWidth = options.width || 300;
const chartHeight = options.height || 300;
const padding = options.padding !== undefined ? options.padding : 20;
const backgroundColor = options.backgroundColor || "#fff";
const fillColor = options.fillColor || "#09A573";
const strokeColor = options.strokeColor || "#FCF5E9";
const strokeWidth = options.strokeWidth || 1;
const labelColor = options.labelColor || "black";
const textAnchor = options.textAnchor || "start";
const dxText = options.dxText || 0;
const dyText = options.dyText || 0;
const svgTitle = options.title || "GeoJSON Feature";
const font =
options.font || "400 16px/1.5 'Source Sans Pro', 'Noto Sans', sans-serif";
if (Array.isArray(geojson)) {
geojson = { type: "FeatureCollection", features: geojson };
} else if (geojson.type !== "FeatureCollection") {
geojson = { type: "FeatureCollection", features: [geojson] };
}
const projection = d3.geoMercator().fitExtent(
[
[padding, padding],
[chartWidth - padding, chartHeight - padding]
],
geojson
);
const pathGenerator = d3.geoPath(projection);
const svg = d3
.create("svg")
.attr("title", svgTitle)
.attr("width", chartWidth)
.attr("height", chartHeight);
svg
.append("rect")
.attr("width", chartWidth)
.attr("height", chartHeight)
.attr("fill", backgroundColor);
svg
.selectAll("path")
.data(geojson.features)
.join("path")
.attr("d", pathGenerator)
.attr("fill", fillColor)
.attr("stroke", strokeColor)
.attr("stroke-width", strokeWidth);
if (options.label) {
svg
.selectAll("g.geojsonLabel text")
.data(geojson.features)
.join("text")
.attr("fill", labelColor)
.attr(
"transform",
(d) => `translate(${pathGenerator.centroid(maxInscribedCircle(d))})`
)
.attr("dx", dxText)
.attr("dy", dyText)
.attr("text-anchor", textAnchor)
.style("font", font)
.text(options.label);
}
return svg;
}