vlChart = {
let data = countsByDate.filter(d => d.value > 0);
const maxDateWithValue = new Date(currentDate);
const aWeekBeforeMax = maxDateWithValue - 7 * 24 * 3600000;
const thresholdBeforeMax = maxDateWithValue - threeshold * 24 * 3600000;
data = countsByDate.map(d => ({
...d,
Confiabilidad:
d.key < thresholdBeforeMax
? 1
: (maxDateWithValue - d.key) / (threeshold * 24 * 3600000) / 2
}));
const gridCondition = {
condition: {
test: "+timeFormat(datum.value, '%d') <= 7",
value: []
},
value: [1, 5]
};
const base = vl.markBar({ tooltip: true }).encode(
vl
.x()
.fieldT("key")
.timeUnit("yearmonthdate")
.title(datesLabels[attrSelected])
.axis({
gridDash: gridCondition,
tickCount: "week",
tickDash: gridCondition,
labelAlign: "left",
labelExpr:
"[timeFormat(datum.value, '%d'), +timeFormat(datum.value, '%d') <= 7 ? timeFormat(datum.value, '%b') : '']",
labelOffset: 4,
labelPadding: -24,
tickSize: 30
}),
vl
.y()
.fieldQ("value")
.title("Casos Reportados")
.scale({ domainMin: 0 })
.axis({
tickMinStep: 100,
gridDash: {
condition: {
test: "datum.value % 1000==0",
value: []
},
value: [2, 10]
}
}),
vl
.opacity()
.fieldQ("Confiabilidad")
.scale({ domain: [0, 1], range: [0, 1] })
.legend(
width > 600
? {
format: "%",
symbolFillColor: "steelblue",
symbolStrokeColor: "steelblue"
}
: null
)
);
const bar = base.encode(
vl
.color()
.fieldQ("value")
.title("Casos Reportados")
.legend(width > 600)
.scale({ scheme: "lightgreyteal" })
);
const line = base
.markLine({
color: "firebrick",
size: 3,
interpolate: "basis",
point: { color: "firebrick" }
})
.transform(
vl.filter("datum.value>0"),
vl.window(vl.mean("value").as("rolling_mean")).frame([-days, 0])
)
.encode(vl.y().fieldQ("rolling_mean"));
return vl
.layer(bar, line)
.data(data)
.width(width - (width > 600 ? 200 : 50))
.height(400);
}