select = ({
controls: [
{
type: "text",
value: `// Highlight max value
Plot.selectMaxY(
// Moving average
Plot.windowY({`
},
{ param: "reduce", value: "mean" },
{ param: "k", value: 7, type: "range", min: 1, max: 14, step: 1 },
{ type: "text", value: "// Sum by day", indent: 2 },
{ type: "text", value: "},Plot.binX({", indent: 2 },
{ param: "y", value: "sum" },
{ type: "text", value: "},{", indent: 2 },
{ param: "x", value: "date" },
{ param: "y", value: "price_in_usd" },
{
param: "textAnchor",
value: "middle",
type: "select",
options: ["start", "middle", "end"]
},
{ param: "dy", value: -5, type: "range", min: -15, max: 15, step: 1 },
{ type: "text", value: "})))" }
],
plot: (config) => {
const textFn = (d) => `Peak sales: ${d3.utcFormat("%b %d")(d.date)}`;
return `Plot.plot({
marks: [
Plot.line(
data,
Plot.windowY(
{ reduce: "mean", k: ${config.k}, anchor: "middle" },
Plot.binX(
{ y: "sum" },
{ x: "date", y: "price_in_usd", thresholds: d3.utcDay }
)
)
),
Plot.dot(
data,
Plot.selectMaxY(
Plot.windowY(
{ reduce: "mean", k: ${config.k}, anchor: "middle" },
Plot.binX(
{ y: "sum" },
{ x: "date", y: "price_in_usd", thresholds: d3.utcDay }
)
)
)
),
Plot.text(
data,
Plot.selectMaxY(
Plot.windowY(
{ reduce: "mean", k: ${config.k}, anchor: "middle" },
Plot.binX(
{ y: "sum", text: "first" },
{
x: "date",
y: "price_in_usd",
thresholds: d3.utcDay,
text: ${textFn},
textAnchor: "${config.textAnchor}",
dy: ${config.dy}
}
)
)
)
)
],
width: ${plotWidth}
})`;
}
})