{
const brush = vl.selectInterval()
.encodings('x');
const hover = vl.selectPoint('hover')
.encodings('x')
.on('mouseover')
.toggle(false)
.nearest(true);
const isHovered = hover.empty(false);
const plot1 = vl.markBar()
.data(gun_deaths)
.params(brush)
.encode(
vl.x().fieldQ('age').bin({maxbins:40}).title('Age'),
vl.y().count().title('# of Gun Deaths'),
vl.color().value('#737373')
)
.width(750)
.height(250)
.title('Frequency of Gun Deaths by Age')
;
const plot2 = vl.markLine()
.data(gun_deaths)
.transform(
vl.filter(brush)
)
.encode(
vl.x().fieldT('year_month').title('Date'),
vl.y().count().title('# of Gun Deaths'),
vl.color().fieldN('race')
)
.width(750)
.height(400)
.title('Trend of Gun Deaths by Race')
;
const base = plot2.transform(vl.filter(isHovered),vl.filter(brush));
const label = {align: 'left', dx: 5, dy: -5};
const white = {stroke: 'white', strokeWidth: 2};
return vl.vconcat(plot1,vl.data(gun_deaths)
.layer(
plot2,
vl.markRule({color: '#aaa'})
.transform(vl.filter(isHovered),vl.filter(brush))
.encode(vl.x().fieldT('year_month')),
plot2.markCircle()
.params(hover)
.encode(vl.opacity().if(isHovered, vl.value(1)).value(0)),
base.markText(label, white).encode(vl.text().count()),
base.markText(label).encode(vl.text().count())
)
.width(700)
.height(400)
).render()
}