chart = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height + 150)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;")
const xAxis = svg.append("g")
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x));
const yAxis = svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yName));
const kde = kernelDensityEstimator(kernelEpanechnikov(7), x.ticks(40))
const allDensity = []
for (let i = 0; i < n; i++) {
let key = categories[i]
let density = kde( data.map(function(d){ return d[key]; }) )
allDensity.push({key: key, density: density})
}
const area = svg.selectAll("areas")
.data(allDensity)
.join("path")
.attr("transform", function(d){return(`translate(0, ${(yName(d.key) - height + margin.bottom)})`)})
.datum(function(d){return(d.density)})
.attr("fill", "#47A8BD")
.attr("stroke", "#000")
.attr("stroke-width", 1)
.attr("d", d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); })
)
function kernelDensityEstimator(kernel, X) {
return function(V) {
return X.map(function(x) {
return [x, d3.mean(V, function(v) { return kernel(x - v); })];
});
};
}
function kernelEpanechnikov(k) {
return function(v) {
return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0;
};
}
return svg.node();
}