{
const plot1 = vl
.markBar({ filled: true })
.transform(
vl
.aggregate({ sum_votos: "sum(votos)" })
.groupby(["candidato", "comuna"]),
vl
.window([{ op: "row_number", as: "ranking" }])
.sort([{ field: "sum_votos", order: "descending" }])
.groupby(["comuna"]),
vl
.calculate("datum.ranking <= 5 ? datum.candidato : 'otros'")
.as("candidato_filtrado")
)
.encode(
vl
.x()
.fieldN("comuna")
.axis({ title: "Comuna" })
.sort({ op: "sum", field: "votos", order: "descending" }),
vl.y().fieldQ("sum_votos").axis({ title: "Votación" }),
vl.color().fieldN("candidato_filtrado").title("Candidato").legend(null)
)
.width(width)
.height(300);
return plot1
.data(al)
.autosize({ type: "fit-x", contains: "padding" })
.render();
}