{
const bars = vl.markBar()
.encode(
vl.x().fieldT("DATE"),
vl.y().sum("NEW_IN"),
vl.opacity().value(0.2),
vl.tooltip([
{field: "DATE", type: "temporal", title: 'Date'},
{field: "NEW_IN", aggregate: "sum", title: "New hospitalisations" }
])
)
.width(800);
const average = vl.markLine({interpolate: "monotone"})
.transform(
vl.groupby("PROVINCE").window(vl.mean("NEW_IN").as("AVG_IN")).frame([-3,3]),
vl.groupby("DATE").aggregate(vl.sum("AVG_IN").as("AVG_IN_BY_DATE"))
)
.encode(
vl.x()
.fieldT("DATE")
.title(null),
vl.y().sum("AVG_IN_BY_DATE").title('New hospitalisations per day')
);
const diff = vl.markBar()
.transform(
vl.groupby("PROVINCE").window(vl.mean("NEW_IN").as("AVG_IN")).frame([-3,3]),
vl.groupby("DATE").aggregate(vl.sum("AVG_IN").as("AVG_IN_BY_DATE")),
vl.window(vl.lag("AVG_IN_BY_DATE").as("PREV_AVG_IN_BY_DATE")).frame([-1,0]),
vl.calculate('datum.PREV_AVG_IN_BY_DATE == null ? 0 : (datum.AVG_IN_BY_DATE / (datum.PREV_AVG_IN_BY_DATE)) - 1').as('DIFF')
)
.encode(
vl.x().fieldT("DATE").title(null),
vl.y().sum("DIFF")
.title('Percentual difference')
.axis({format: ".0%"}),
vl.color()
.condition([
{ test: "dayofyear(now()) - dayofyear(datum.DATE) < 4", value: "lightgray" },
{ test: "datum.DIFF > 0", value: "orange" }
])
.value("green"),
vl.tooltip([
{field: "DATE", type: "temporal", title: 'Date'},
{field: "DIFF", format: ".2%", title: 'Change'}
])
)
.width(800)
.height(150);
return vl.vconcat(vl.layer(bars, average), diff)
.data(hospitalisations_data)
.render()
}