This likely betrays my Javascript inexperience but I am wondering what the best approach is to conditionally return certain plots? My made up example is to say "produce one type of plot when one value is selected in Inputs.select
and produce another if more than one are selected. I don’t quite understand how one can assign a plot to a variable and use it later. Right now I am doing something like this:
{
if (islands.length === 1) {
return Plot.plot({
grid: true,
inset: 10,
color: {legend: true},
marks: [
Plot.frame(),
Plot.dot(pen_sub, {x: "bill_length_mm", y: "bill_depth_mm", stroke: "green"})
]
});
} else if (islands.length > 1) {
return Plot.plot({
grid: true,
inset: 10,
color: {legend: true},
marks: [
Plot.frame(),
Plot.dot(pen_sub, {x: "bill_length_mm", y: "bill_depth_mm", stroke: "species"})
]
});
}
However I am wondering if I can make that a little nicer by assigning the two plots to variables and then just reference those variables? I have a notebook here: penguins data / Sam Albers | Observable
I’ve just tried assigning them to a variable but I get an “undefined” output. For example I am not certain quite why this doesn’t work:
length_one_plot = Plot.plot({
grid: true,
inset: 10,
color: {legend: true},
marks: [
Plot.frame(),
Plot.dot(pen_sub, {x: "bill_length_mm", y: "bill_depth_mm", stroke: "green"})
]
})
length_gt_one_plot = Plot.plot({
grid: true,
inset: 10,
color: {legend: true},
marks: [
Plot.frame(),
Plot.dot(pen_sub, {x: "bill_length_mm", y: "bill_depth_mm", stroke: "species"})
]
});
{
if (islands.length === 1) {
return length_one_plot;
} else if (islands.length > 1) {
return length_gt_one_plot;
}
}
TIA.