function Choropleth(data, {
id = d => d.ISO,
value,
title,
format,
scale = d3.scaleSequentialSqrt,
domain,
range = d3.interpolateGnBu,
width,
height,
projection,
features,
featureId,
borders,
outline = projection && projection.rotate ? {type: "Sphere"} : null,
unknown = "#AA0000",
fill = "white",
stroke = "#EEE",
strokeLinecap = "round",
strokeLinejoin = "round",
strokeWidth = 0.15,
strokeOpacity = 1,
backgroundColor = "#AAA",
landColor = "#444444",
landStroke = "#000000",
markerColor = "#E26F99",
} = {}) {
const N = d3.map(data, id);
const V = d3.map(data, value).map(d => d == null ? NaN : +d);
const Im = new d3.InternMap(N.map((id, i) => [id, i]));
const If = d3.map(features.features, featureId);
console.log(N)
// Compute default domains.
if (domain === undefined) domain = [d3.quantile(V,quantil[0]), d3.quantile(V, quantil[1])];//d3.extent(V);
// Construct scales.
const color = scale(domain, range);
if (color.unknown && unknown !== undefined) color.unknown(unknown);
// Compute titles.
if (title === undefined) {
format = color.tickFormat(100, format);
title = (d, i) => `${auswahl}\n${d.properties.name}\n${format(V[i])}`;
} else if (title !== null) {
const T = title;
const O = d3.map(data, d => d);
title = (f, i) => T(f, O[i]);
}
// Compute the default height. If an outline object is specified, scale the projection to fit
// the width, and then compute the corresponding height.
if (height === undefined) {
if (outline === undefined) {
height = width/2;
} else {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width, outline)).bounds(outline);
const dy = Math.ceil(y1 - y0), l = Math.min(Math.ceil(x1 - x0), dy);
projection.scale(projection.scale() * (l - 1) / l).precision(0.2);
height = dy;
}
}
// Construct a path generator.
const path = d3.geoPath(projection);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "width: 100%; height: auto; height: intrinsic;");
if (outline != null) svg.append("path")
.attr("fill", fill)
.attr("stroke", "currentColor")
.attr("d", path(outline));
svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr('fill', backgroundColor);
svg.append("g")
.selectAll("path")
.data(features.features)
.join("path")
.attr("fill", (d, i) => color(V[Im.get(If[i])]))
.attr("d", path)
.append("title")
.text((d, i) => title(d, Im.get(If[i])));
if (borders != null) svg.append("path")
.attr("pointer-events", "none")
.attr("fill", "none")
.attr("stroke", stroke)
.attr("stroke-linecap", strokeLinecap)
.attr("stroke-linejoin", strokeLinejoin)
.attr("stroke-width", strokeWidth)
.attr("stroke-opacity", strokeOpacity)
.attr("d", path(borders));
const key = Legend(color, { // https://observablehq.com/@d3/choropleth
title: auswahl+" ",
width: width/2,
size:100,
})
svg.append("g")
.attr("transform", "translate("+width/50+","+width/50+")")
.append(() => key);
return Object.assign(svg.node(), {scales: {color}});
}