{
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();
}