function density1d(
{ z, fill, stroke, x, y, x1, x2, y1, y2 } = { x: "point", y: "density" },
{
x: inputX,
weight,
bandwidth,
adjust,
extent,
pad,
bins,
...options
} = {}
) {
const outputs = Object.fromEntries(
Object.entries({ z, fill, stroke, x, y, x1, x2, y1, y2 }).filter(
([key, value]) => value === "point" || value === "density"
)
);
return {
...options,
...outputs,
transform: (data, facets) => {
const densities = facets.map((facet) => {
const facetData = [...facet].map((index) => data[index]);
return [
...kde
.density1d(facetData, {
x: inputX,
weight,
bandwidth,
adjust,
extent,
pad,
bins
})
.points("point", "density")
];
});
let newData = [];
let newFacets = [];
let index = 0;
for (let density of densities) {
let facet = [];
for (let facetIndex = 0; facetIndex < density.length; facetIndex++) {
newData.push(density[facetIndex]);
facet.push(index);
index++;
}
newFacets.push(facet);
}
return {
data: newData,
facets: newFacets
};
}
};
}