{
const colors = {
domain: [0, 1],
range: ['darkgray', 'steelblue']
};
const dots = vl
.markCircle({ filled: true, size: 100 })
.data(sicily)
.encode(
vl
.x()
.fieldT('date')
.timeUnit('yearmonth')
.axis({ grid: false, format: '%b-%y' })
.title('Year'),
vl
.y()
.fieldQ("rate")
.scale({ domain: [150, 300] })
.axis({ grid: false })
.title("Rate"),
vl
.color()
.fieldN("smoke_ban")
.scale(colors)
);
const leastSquares = vl.markLine().encode(
vl
.x()
.fieldQ('x')
.axis(null)
.title(null),
vl
.y()
.fieldQ('y')
.title(null)
);
const colorZip = colors['domain'].map((d, i) => [d, colors['range'][i]]);
return vl
.layer(
dots,
colorZip.map(([domain, range]) =>
leastSquares
.data(
leastSquaresData(
sicily.filter(d => d.smoke_ban === domain),
domain,
'time',
'rate'
)[0]
)
.markLine({ color: range })
),
leastSquares
.data(leastSquaresData(sicily, 0 || 1, 'time', 'rate')[0])
.markLine({ color: 'black', strokeDash: [5, 4] })
)
.width(750)
.render();
}