Public
Edited
Nov 1
Insert cell
Insert cell
vl
.markBar()
.transform([
vl.joinaggregate(vl.sum("Time").as("TotalTime")),
vl.calculate("datum.Time/datum.TotalTime*100").as("PercentOfTotal")
])
.encode(vl.x().fieldQ("PercentOfTotal").title("% of Total Time"), vl.y().field("Activity"))
.data([
{ Activity: "Sleeping", Time: 8 },
{ Activity: "Eating", Time: 2 },
{ Activity: "TV", Time: 4 },
{ Activity: "Work", Time: 8 },
{ Activity: "Exercise", Time: 2 }
])
.height({ step: 12 })
.render()
Insert cell
Insert cell
{
const bar = vl
.markBar()
.encode(
vl
.x()
.fieldQ("PercentOfTotal")
.axis({ format: ".1%" })
.title("% of Total Time"),
vl.y().field("Activity")
);

const label = bar
.markText({ align: "start", dx: 2 })
.encode(vl.text().fieldQ("PercentOfTotal").format(".2%"));

return vl
.layer([bar, label])
.transform([
vl.joinaggregate(vl.sum("Time").as("TotalTime")),
vl.calculate("datum.Time/datum.TotalTime").as("PercentOfTotal")
])
.data([
{ Activity: "Sleeping", Time: 8 },
{ Activity: "Eating", Time: 2 },
{ Activity: "TV", Time: 4 },
{ Activity: "Work", Time: 8 },
{ Activity: "Exercise", Time: 2 }
])
.height({ step: 12 })
.render();
}
Insert cell
Insert cell
vl
.markBar()
.transform([
vl.joinaggregate(vl.count().as("TotalCount")),
vl.calculate("1/datum.TotalCount*100").as("PercentOfTotal")
])
.encode(
vl.x().sum("PercentOfTotal").title("Percent of Total"),
vl.y().field("species")
)
.data(penguins)
.render()
Insert cell
{
const bar = vl
.markBar()
.encode(
vl.x().sum("PercentOfTotal").title("Percent of Total"),
vl.y().field("species")
);

const label = bar
.markText({ align: "start", dx: 2 })
.encode(vl.text().sum("PercentOfTotal").format(".2%"));

return vl
.layer([bar, label])
.transform([
vl.joinaggregate(vl.count().as("TotalCount")),
vl.calculate("1/datum.TotalCount").as("PercentOfTotal")
])
.data(penguins)
.render();
}
Insert cell
import {vl} from "@vega/vega-lite-api-v5"
Insert cell
Insert cell
({
data: {
values: [
{ Activity: "Sleeping", Time: 8 },
{ Activity: "Eating", Time: 2 },
{ Activity: "TV", Time: 4 },
{ Activity: "Work", Time: 8 },
{ Activity: "Exercise", Time: 2 }
]
},
transform: [
{
joinaggregate: [
{
op: "sum",
field: "Time",
as: "TotalTime"
}
]
},
{
calculate: "datum.Time/datum.TotalTime * 100",
as: "PercentOfTotal"
}
],
height: { step: 12 },
mark: "bar",
encoding: {
x: {
field: "PercentOfTotal",
type: "quantitative",
title: "% of total Time"
},
y: {
field: "Activity",
type: "nominal"
}
}
})
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