This website is for Vega-Lite v2. Go to the main Vega-Lite homepage for the latest release.

Nearest Value to a Discrete Selection

Edit this page

The nearest selection transform is a simple boolean operator that can be applied to discrete selection types (single and multi). It is false by default which means that data values must be interacted with directly (e.g., clicked on) to be added to the selection. When nearest is set to true, an invisible Voronoi diagram is computed and used to accelerate the selection – the value nearest the mouse cursor will be added to the selection.

Examples

In the scatterplot below, points the mouse cursor are highlighted as it moves.

The nearest transform also respects any position encoding projections applied to the selection. For instance, in the example below, moving the mouse cursor back-and-forth snaps the vertical rule and label to the nearest date value.

Current Limitations

  • The nearest transform is not supported for continuous mark types (i.e., line and area). For these mark types, consider layering a discrete mark type (e.g., point) with a 0-value opacity as in the last example above.