Public
Edited
Nov 9, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
db
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
db
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
db
SELECT t."user_id",t."ga_session_id",t."country",t."device",t."type",t."item_id",t."date" FROM "events" t
WHERE t.type = ${"purchase"}
LIMIT 100 OFFSET 0
Insert cell
Insert cell
db
select *
from events
where type = 'purchase'
Insert cell
Insert cell
db
select *
from events
join items on events.item_id = items.id
where type = 'purchase'
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.dot(purchases, {x: "date", y: "price_in_usd"})
]
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(
purchases,
Plot.groupY(
{ x: "sum" },
{
x: "price_in_usd", // the price will be summed for each group
y: "name", // grouping by item "name"
sort: { y: "x", reverse: true, limit: 20 }
}
)
),
Plot.ruleX([0])
],
marginLeft: 200 // customized margin to make room for long item names
})
Insert cell
Insert cell
db
select name,
sum(price_in_usd)::int as revenue
from events
join items on events.item_id = items.id
where type = 'purchase'
group by name
order by revenue desc
Insert cell
Insert cell
categoryChart = Plot.plot({
marks: [
Plot.barX(
purchases,
Plot.groupY(
{ x: "sum" },
{
x: "price_in_usd",
y: "category",
fill: "category", // color by the category
sort: { y: "x", reverse: true, limit: 20 }
}
)
),
Plot.text(
purchases,
Plot.groupY(
{ x: "sum", text: "sum" },
{
x: "price_in_usd",
y: "category",
text: "price_in_usd",
textAnchor: "start",
dx: 5
}
)
),
Plot.ruleX([0])
],
marginLeft: 200,
marginRight: 100
})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.barX(
purchases,
Plot.groupY(
{ x: "sum" },
{
x: "price_in_usd",
y: "name",
fill: "category",
sort: { y: "x", reverse: true, limit: 20 }
}
)
),
Plot.text(
purchases,
Plot.groupY(
{ x: "sum", text: "sum" },
{
x: "price_in_usd",
y: "name",
text: "price_in_usd",
textAnchor: "start",
dx: 5
}
)
),
Plot.ruleX([0])
],
color: { legend: true, columns: "100px" },
marginLeft: 200,
marginRight: 100
})
Insert cell
Insert cell
db
select category,
sum(price_in_usd)::int as revenue
from events
join items on events.item_id = items.id
where type = 'purchase'
group by category
order by revenue desc
Insert cell
select // when the dropdown changes, the value of the "select" variable will update
Insert cell
viewof select = Inputs.select(categories.map(d => d.category), {label: "Select a category"})
Insert cell
Plot.plot({
marks: [
Plot.barX(
purchases,
Plot.groupY(
{ x: "sum" },
{
filter: (d) => d.category == select,
x: "price_in_usd",
y: "name",
fill: "category",
sort: { y: "x", reverse: true, limit: 20 }
}
)
),
Plot.text(
purchases,
Plot.groupY(
{ x: "sum", text: "sum" },
{
filter: (d) => d.category == select,
x: "price_in_usd",
y: "name",
text: "price_in_usd",
textAnchor: "start",
dx: 5
}
)
),
Plot.ruleX([0])
],
color: { ...categoryChart.scale("color"), legend: true, columns: "100px" },
marginLeft: 200,
marginRight: 100
})
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more