Limiting data by brush with Vega-Lite API

Here’s an example on Observable that I’ve roughly gleaned from the Navigation: Overview + Detail example on UW Interactive Data Lab’s Interaction page.

This works fine, but I’d like to add the feature of having the data flowing into the display chart “limited” or “cropped” by the brush selection such that the legend on the right would only list entries for what’s in the display chart.

Any thoughts on how I might do that? I have tried various things, but none have worked. I think that if I better understood how to transpose between Vega-Lite API and Vega-Lite JSON, I might be able to self-serve from the more robust Vega-Lite docs.

Solved… essentially, the solution was .transform(vl.filter(brush)) but I have an explanation and some wonderings on JSON vs API on the notebook.