make_chart = (data, width, domain) => {
const bars = vl.markBar({ opacity: 0.1 }).encode(
vl
.x()
.fieldT('date')
.timeUnit('utcyearmonthdate')
.scale({ domain: date_extent.map(d3.utcFormat('%Y-%m-%d')) })
.axis({ tickCount: 'month', format: '%B', grid: false, tickSize: 6 })
.title(null),
vl
.y()
.fieldQ(type)
.scale({ domain, nice: false })
.axis({
tickCount: 4,
domain: false,
ticks: false,
labelAlign: 'left',
labelBaseline: 'bottom'
})
.title(null),
vl.color({ value: 'red' })
);
const xOffset = width / d3.timeDay.count(...date_extent);
const line = bars
.markLine({ interpolate: 'monotone', xOffset })
.encode(vl.y().fieldQ('mean'));
const area = line.markArea({ opacity: 0.2, xOffset });
return vl
.layer(bars, area, line)
.data(data)
.transform(
vl.window({ op: 'mean', field: type, as: 'mean' }).frame([-3, 3])
)
.width(width)
.height(width / 5)
.autosize({ type: 'fit-x', contains: 'padding' })
.config({
style: { cell: { stroke: 'transparent' } },
axis: { labelColor: '#929292', gridColor: '#ececec' },
padding: { top: 5, right: 0, bottom: 5, left: 0 }
})
.render();
}