Published
Edited
Sep 9, 2022
7 forks
5 stars
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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more