facets = ({
controls: [
{ type: "text", value: "Plot.plot({" },
{ type: "text", value: "facet: {", indent: 1 },
{ param: "data", value: "data" },
{
param: "x",
type: "select",
value: "brand",
options: new Map([
["brand", "brand"],
["d => d3.utcMonth(d.date)", (d) => d3.utcMonth(d.date)],
["d => d3.utcYear(d.date)", (d) => d3.utcYear(d.date)],
["no faceting", ""]
])
},
{
param: "y",
type: "select",
value: "",
options: new Map([
["brand", "brand"],
["d => d3.utcMonth(d.date)", (d) => d3.utcMonth(d.date)],
["d => d3.utcYear(d.date)", (d) => d3.utcYear(d.date)],
["no faceting", ""]
])
},
{
param: "marginRight",
type: "range",
value: 50,
min: 0,
max: 100,
step: 1
},
{
param: "marginLeft",
type: "range",
value: 50,
min: 0,
max: 100,
step: 1
},
{ type: "text", value: "},", indent: 1 },
// fx
{ type: "text", value: "fx: {", indent: 1 },
{ param: "insetLeft", type: "range", value: 10, min: 0, max: 100, step: 1 },
{
param: "insetRight",
type: "range",
value: 10,
min: 0,
max: 100,
step: 1
},
{
param: "fxPadding",
label: "padding",
type: "range",
value: 0.1,
min: 0,
max: 1,
step: 0.01
},
{ param: "fxLabel", label: "label", type: "textInput" },
{
param: "fxTickFormat",
label: "tickFormat",
type: "select",
value: "",
options: new Map([
["default", ""],
["d => d.toUpperCase()", (d) => d.toUpperCase()],
["d3.utcFormat('%B')", (d) => d3.utcFormat("%B")(d)],
["d3.utcFormat('%Y')", (d) => d3.utcFormat("%Y")(d)]
])
},
{ type: "text", value: "},", indent: 1 },
// fy
{ type: "text", value: "fy: {", indent: 1 },
{ param: "insetTop", type: "range", value: 10, min: 0, max: 100, step: 1 },
{
param: "insetBottom",
type: "range",
value: 10,
min: 0,
max: 100,
step: 1
},
{
param: "fyPadding",
label: "padding",
type: "range",
value: 0.01,
min: 0,
max: 1,
step: 0.1
},
{ param: "fyLabel", label: "label", type: "textInput" },
{
param: "fyTickFormat",
label: "tickFormat",
type: "select",
value: "",
options: new Map([
["default", ""],
["d => d.toUpperCase()", (d) => d.toUpperCase()],
["d3.utcFormat('%B')", (d) => d3.utcFormat("%B")(d)],
["d3.utcFormat('%Y')", (d) => d3.utcFormat("%Y")(d)]
])
},
{ type: "text", value: "}})" },
// Extra options
{ type: "text", value: "\n\n// additional options" },
{
param: "background",
label: "show full histogram backround",
type: "toggle",
value: true
},
{ param: "frame", label: "show frames", type: "toggle", value: true }
],
plot: (config) => {
// Construct the marks option with the proper indentation
const rects = `Plot.rectY(data, Plot.binX({ y: "sum" }, { x: "date", y: "value" }))`
const backgroundRects = `Plot.rectY(
data,
Plot.binX(
{ y: "sum" },
{ x: "date", y: "value", fillOpacity: 0.1, facet: "exclude" }
)
)`
const frames = `Plot.frame()`
let marks = ''
if(!config.background && !config.frame) marks = `marks: [${rects}]`;
else {
marks = `marks: [\n\t\t${rects}`
if(config.background) marks += `,\n\t\t${backgroundRects}`
if(config.frame) marks += `,\n\t\t${frames}`
marks += `\n\t]`
}
return `Plot.plot({
${marks},
facet: {
data: data,
x: ${typeof config.x === "string" ? `"${config.x}"` : config.x},
y: ${typeof config.y === "string" ? `"${config.y}"` : config.y},
marginRight: ${config.marginRight},
marginLeft: ${config.marginLeft}
},
fx: {
insetLeft: ${config.insetLeft},
insetRight: ${config.insetRight},
padding: ${config.fxPadding}${config.fxLabel ? `,\n\t\tlabel: "${config.fxLabel}"` : ""}${config.fxTickFormat ? `,\n\t\ttickFormat: ${config.fxTickFormat}` : ""}
},
fy: {
insetTop: ${config.insetTop},
insetBottom: ${config.insetBottom},
padding: ${config.fyPadding}${config.fyLabel ? `,\n\t\tlabel: "${config.fyLabel}"` : ""}${config.fyTickFormat ? `,\n\t\ttickFormat: ${config.fyTickFormat}` : ""}
},
width: ${plotWidth}
})`}})