Add unified hover mode (x/y unified) matching Plotly's hovermode

Closed
FA
1h ago
+1803-56

Implements Plotly-compatible unified hover modes ("x unified" and "y unified") that snap the cursor to the nearest data position along one dimension and report every series' value at that position in a single tooltip, with a spike line marking the snapped position.

Summary

This adds the hover_mode interaction setting to charts and xy.interaction_config(), allowing users to choose between "closest" (default, mark-under-cursor) and unified modes that improve multi-series comparison by eliminating the need to hover each point individually.

Key Changes

Python API:

  • Added hover_mode parameter to Figure.set_interaction() and xy.interaction_config()
  • Validates mode is one of "closest", "x unified", or "y unified"
  • Rejects Plotly's per-series "x"/"y" modes with a helpful error pointing to unified spelling
  • Added silent flag to pick messages so unified readout refinements don't re-fire on_hover

Browser/TypeScript:

  • Implemented _unifiedHover() to snap cursor to nearest data position along the hover dimension
  • _unifiedHits() collects all series within 20px (Plotly's default hoverdistance) of the snapped position
  • Binary search optimization for sorted columns (O(log n) vs O(n) per pointermove)
  • Renders unified tooltip with series color swatches, names, and values under a shared heading
  • Reuses crosshair line as spike indicator; hides it when crosshair interaction is off
  • Tooltip positioning flips horizontally to avoid covering the spike line
  • Kernel pick replies refine individual rows via sequence numbers; exact event fires once all rows answered

Interaction:

  • Polar charts resolve unified modes to "closest" client-side (no single x to align on)
  • Series excluded: density tiers, legend-hidden, error bands, area-shaped geometry without single values per position
  • Falls back to closest-point hover if no series answer the unified readout
  • Vertical movement at same x keeps readout stable (no new hover event)

Styling:

  • New tooltip_swatch DOM slot for series color chip
  • Color rides --xy-tooltip-swatch-paint custom property for restyle flexibility
  • Spike line styled via existing crosshair_x/crosshair_y slots and --chart-crosshair token

Documentation & Tests:

  • Added comprehensive browser probe tests covering snap behavior, distance filtering, kernel refinement, and y-unified transpose
  • Updated interaction spec (§7.1), wire protocol, and styling docs
  • Added demo in tooltips.md showing unified hover on weekly momentum chart

Summary by CodeRabbit

  • New Features
    • Added unified x- and y-hover modes, combining nearby series in a shared tooltip with a spike line and color swatches.
    • Line and area charts default to unified x-hover unless custom tooltip settings are used. Other chart types and polar charts retain closest-point hover.
    • Added hover_mode configuration and a styleable tooltip color swatch.
  • Bug Fixes
    • Hover tooltips and highlights now clear or refresh when chart data or traces change, preventing stale results during interactions.
  • Documentation
    • Expanded guidance on hover modes, defaults, formatting, styling, and chart-specific behavior.