Interaction and view state
Interaction and view state connect Python chart policy to browser input and back to application state. The system keeps fast local gestures in the browser while exposing semantic hover, click, selection, and view events to notebooks and live app hosts.
Interaction model
Python interaction policy
The Python chart API declares which user actions are available and which axes they can affect. Authors can set these options on a chart or group them in xy.interaction_config(...); callbacks also turn on the matching event surface. The policy crosses the chart payload as a sparse description, so the browser supplies defaults for options that were not set.
The main policy groups are:
| Policy | Role |
|---|---|
hover, click, crosshair | Point readout, click events, and plot-aligned guides |
select, brush | Box, lasso, x-range, and y-range selection |
navigation, pan, zoom | Master navigation and its pan or zoom actions |
pan_axes, zoom_axes, reset_axes | Exact axis IDs affected by each action |
wheel_zoom, box_zoom, zoom_buttons, double_click_reset | Input-specific navigation controls |
default_drag_action | Action used by an unmodified primary-button drag |
zoom_limits | Magnification limits relative to the home view |
link_group, link_axes | Browser-local range sharing between charts |
history | Client-local Back and Next view history |
navigation=False blocks local viewport input but does not block hover, click, selection, linked ranges, or application-driven ranges. Axis policy filters an enabled action; it does not enable an action by itself. Python validates axis names and navigation values before the chart reaches the browser. See the Python chart API for the wider figure and configuration model.
References: python/…/components.py• L3475-3567 python/…/_figure.py• L485-549 spec/…/interaction.md• L20-71
Browser interaction surface
The browser owns the live view and handles pointer, wheel, keyboard, and modebar input without a Python round trip for each frame. Each committed change uses the same range checks and axis bounds, regardless of whether a user gesture or an application command caused it.
The common Cartesian gesture map is:
| Gesture | Result |
|---|---|
| Plain drag | Pan by default, or the configured drag action |
| Shift-drag | Box selection when selection and pickable data are available |
| Wheel or trackpad | Cursor-anchored zoom on the enabled axes |
| Box zoom | Fit the selected rectangle to the enabled zoom axes |
| Double-click | Reset the configured axes, unless a selection mode uses it to clear selection |
| Click or keyboard activation | Pick one point and emit a click event when click is enabled |
Arrow keys, Home, and End | Move through pickable points in data order |
Escape | Close the active point readout |
The modebar exposes the navigation and selection actions that the current policy permits. Its zoom menu can include Back, Next, Zoom In, Zoom Out, Box Zoom, and reset commands. Its selection menu can include box, lasso, x-range, and y-range modes. A chart can hide the modebar without losing direct gestures. The same toolbar can expose configured client-side export actions; export behavior belongs to export and static rendering, not to view state.
An explicit default_drag_action="none" is an opt-out from local drag and wheel navigation for embedded charts. A chart whose resolved drag mode is none because it has no Cartesian drag tools, such as a polar chart, keeps its supported wheel and reset gestures. This distinction prevents a default mode from disabling a different navigation path.
Polar charts have a narrower contract. Hover remains available and radial zoom is off by default because zooming a fixed-radius composition would crop its frame; wind_rose and an explicit zoom=True opt in to radial zoom. Polar charts do not yet provide Cartesian pan, box selection, brushing, or crosshair behavior.
The following diagram shows how a Python policy becomes a local browser action and then an optional application event.
flowchart LR
A[Python chart policy] --> B[Chart payload]
B --> C[Browser view and controls]
C --> D[Pointer wheel keyboard gesture]
D --> E[Validated committed state]
E --> F[DOM event and optional transport]
F --> G[Notebook callback or Reflex handler]
References: js/…/53_interaction.ts• L231-456 js/…/53_interaction.ts• L1301-1591 spec/…/interaction.md• L281-355
Point and selection state
Hover and point readout
Hover is short-lived view context. It records the point under the pointer or keyboard focus and drives the tooltip and optional crosshair, but it is not part of durable view state or linked view state.
The browser can first report a resident point and then report a more exact row after the Python side resolves it. The core hover and click callbacks receive a data row rather than a DOM event. A row includes its trace and canonical source index, plus the mark's data channels when they apply. This lets application code use source data instead of screen coordinates or formatted tooltip text.
The structured Reflex on_hover event adds cursor pixels, cursor data by axis ID, and resolved points. The older on_point_hover prop keeps its narrow row shape. Keyboard traversal uses the same point readout path and keeps the readout until Escape; pointer exit sends an inactive hover state. A document-level exit check also clears pointer-owned hover when the pointer leaves the chart even if the browser misses the canvas boundary event. It does not close a keyboard-owned readout merely because the mouse moves elsewhere.
References: python/…/interaction.py• L175-273 js/…/53_interaction.ts• L459-616 spec/…/view-state.md• L282-340
Selection geometry and rows
Selection starts as data-space geometry. A box or x/y range becomes four bounds; a lasso becomes a polygon. The browser paints the active geometry and sends it for row resolution when a live kernel is available. The core on_brush callback receives the geometry, and on_select then receives a Selection containing canonical row indices grouped by trace. on_brush always runs before on_select for the same gesture.
Selection replaces the prior selection. A double-click in a selection mode clears it, while a double-click in pan mode resets the view without clearing it. Lasso vertices remain editable: a drag changes the polygon and a double-click removes a vertex when the polygon can remain valid. A no-op or cancelled brush restores the prior geometry. The selection overlay is reprojected when the view changes. A linked selection is opt-in and is separate from the default linked range behavior.
The event payload used by Reflex contains a bounded row projection and canonical row IDs. When the payload is truncated, reflex_xy.resolve_selection(event) resolves the complete selection against the live figure. Static charts can render selection visuals only when the required resident point data is present; point and selection callbacks need a live source with a kernel.
References: python/…/channel.py• L356-394 python/…/interaction.py• L276-378 python/…/events.py• L118-177 python/…/selections.py• L14-48 js/…/53_interaction.ts• L686-991
Drill-aware interaction
Large point sets can use a density view at broad scales and switch to an exact point window when the current view becomes small enough. Pan and zoom therefore change not only the visible range but also the detail tier that can support picking and selection.
The system keeps this change coherent for users. A density trace becomes pickable when it drills into points and loses point picking when it returns to density. A selection's data-space bounds or polygon remains in place across that change, so the new point window can rebuild its highlight while the authoritative row result is still in flight. A pick from an old point window is rejected or resolved only through a known recent window; it cannot silently become a different source row.
This detail policy belongs with level of detail, density, and streaming. The interaction layer consumes its result and preserves the user-facing meaning of hover and selection.
References: python/…/interaction.py• L175-213 python/…/interaction.py• L882-1084 js/…/53_interaction.ts• L927-1081 js/…/45_lod.ts• L676-731
Viewport state
Navigation and axis ranges
The current view is a range for each declared axis. The home view is the initial range used by reset and by magnification limits. Pan moves ranges, zoom changes their span, and reset restores selected axes to home.
Every navigation source follows the same high-level sequence:
- Read the requested ranges.
- Apply the selected axis policy.
- Clamp the ranges to zoom limits and axis bounds.
- Commit the changed ranges.
- Emit a semantic view event and notify linked charts.
View events identify the source, the axes that changed, the gesture phase, and the interaction_id shared by all frames of one continuous gesture. Continuous pan and wheel actions can emit update events; every settled gesture emits one end event. Applications that mirror a view back into the chart should filter the source of their own writes to avoid an event loop.
Axis bands extend the same model to individual axes. A wheel over an axis band zooms that axis, a drag along it pans that axis, and a drag across it makes a one-axis zoom span. Secondary axes use their own side of the chart, so the gesture does not need a modifier key. A cancelled pan keeps the range reached by its update frames but does not emit a public end event. Animated programmatic changes use the same committed view path; reduced-motion preferences can make them settle without animation.
References: js/…/50_chartview.ts• L1556-1629 js/…/53_interaction.ts• L405-437 js/…/53_interaction.ts• L1986-2423 spec/…/pan-and-zoom-configuration.md• L120-190 spec/…/view-state.md• L257-280
Durable state and history
Durable state contains axis ranges and geometric selection. Hover and cursor data stay ephemeral. The durable state can be serialized, restored, written by an application, and placed in the client-local history stack.
The state uses merge-patch rules: an absent axis stays unchanged, an absent selection stays unchanged, and an explicit null clears the selection. Geometric changes can be undone. Row-index selections are intentionally non-durable because an arbitrary set of source indices does not fit a small view document. History is local to each browser client, coalesces frames from one gesture, and does not record linked updates.
The modebar provides Back and Next when history is enabled. A static chart can use the browser state handle, while a kernel-connected figure exposes set_view, reset_view, select, clear_selection, and view_state. view_state() reports the last committed state observed by the kernel, so reads can briefly lag a write until the browser confirms the clamped result.
References: python/…/widget.py• L135-183 python/…/_figure.py• L2108-2223 spec/…/view-state.md• L38-164
Programmatic view control
Programmatic control is the write-side partner of browser events. A Python handler, background task, or host application can set a partial range, reset selected axes, apply geometric selection, apply canonical row selection, or clear selection without rebuilding the chart.
Kernel-connected figures send these commands through the same browser state path as gestures. The Reflex out-of-band API applies the same commands to all subscribers for a figure token. Programmatic range changes are tagged as API changes in view events, and callers can choose whether a range or geometric selection change enters history. Row selection resolves to selection masks but does not enter durable history.
References: python/…/widget.py• L137-179 python/…/app.py• L297-330 spec/…/view-state.md• L166-255
Linked view state
Charts join a view group with link_group and limit synchronization with link_axes. A range change from one chart is copied to matching axes in the other charts, then clamped to each chart's own bounds. A chart with local navigation disabled can still follow linked ranges.
Linked ranges do not mean linked application data. By default they synchronize only viewport position. Use callbacks and host state for cross-filtering. link_select adds data-space selection sharing, and facet_chart(..., link=..., link_select=...) applies the same relationship to generated facet panels. Linked updates do not echo back into the sender and do not create a new history entry on the receiving chart.
References: js/…/50_chartview.ts• L1632-1705 python/…/components.py• L5364-5533 spec/…/interaction.md• L249-279
Event and host boundaries
Browser event contracts
The chart root emits bubbling xy: custom events for local consumers. These events carry semantic data in chart coordinates and include the current view where that context matters.
| Event | Main meaning |
|---|---|
xy:hover and xy:leave | Active or inactive point readout |
xy:click | Data-space click with an optional picked row |
xy:brush | Box, axis-range, or polygon geometry |
xy:select | Resolved selected count and view |
xy:view_change | Ranges, source, changed axes, phase, and gesture identity |
Context-loss events are separate from user interaction. They report the graphics context lifecycle and carry no view state. Browser consumers that need source rows should use Python callbacks or the appropriate live adapter event instead of treating a resident GPU index as a canonical row.
References: spec/…/interaction.md• L155-206 js/…/50_chartview.ts• L1570-1629 js/…/53_interaction.ts• L645-684
Notebook and Reflex event surfaces
The notebook widget sends semantic messages to the Python channel, which resolves picks and selections against the figure's canonical data. It always records committed view ranges for the figure's state cache; update-phase view traffic is sent only when a view listener is present.
Reflex uses adapter names that match its component boundary. on_point_hover and on_point_click report point envelopes, on_select_end reports selection summaries, and on_view_change reports viewport changes. The newer on_hover surface provides structured hover data and can work on static charts because the browser can resolve its resident data. Static sources cannot provide kernel-backed point or selection callbacks.
The distinction matters when choosing a host:
| Source | Local gestures | Point and selection callbacks | View-change callback |
|---|---|---|---|
| Standalone HTML or static Reflex source | Yes | No kernel-backed callback | Yes, from the browser |
| Live notebook widget | Yes | Yes, through Python | Yes, with semantic ranges |
| Live Reflex figure or token | Yes | Yes, through adapter events | Yes, through the adapter |
References: python/…/widget.py• L44-95 python/…/channel.py• L320-385 python/…/component.py• L24-39 python/…/component.py• L104-151
Related pages
- Python chart API and figure model
- Data ingestion and payload construction
- Chart families, facets, and layout
- Styling, themes, and annotations
- Level of detail, density, and streaming
- Export and static rendering
- Browser interaction and animation
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Python interaction keys, sparse payload resolution, and browser fallback behavior aligned.
- Keep axis policy validation aligned with every axis ID that chart families and facets can declare.
- Preserve the view-event fields and source values when changing a gesture, programmatic command, or linked update.
- Preserve the
on_brushbeforeon_selectordering when selection resolution or transport changes. - Keep canonical row IDs and drilled-window IDs consistent across hover, click, selection masks, and stale-reply handling.
- Keep pointer and keyboard click paths aligned so both report the same semantic point contract.
- Keep lasso edit, cancellation, and double-click removal behavior aligned with durable polygon state.
- Keep the explicit
default_drag_action="none"opt-out distinct from a chart whose resolved drag mode isnonebecause it has no drag tools. - Keep static-source event limits distinct from live kernel-backed event requirements in the Reflex adapter.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An axis policy names an undeclared axis | Chart construction fails instead of sending an unusable navigation rule | Declare the axis first and use its exact ID |
navigation=False is treated as a full interaction disable | Hover, selection, or linked and programmatic ranges stop working unexpectedly | Treat navigation as the local viewport-input gate |
A view bridge writes every on_view_change event back to the chart | The application can create a feedback loop | Ignore events whose source is the bridge's own write |
A selection event is used as a complete row list when truncated is true | The application acts on only the bounded payload projection | Call reflex_xy.resolve_selection(event) |
| A stale pick is read as a current drilled index | The UI can display the wrong canonical source row | Require the matching drill sequence or reject the pick |
| Kernel-backed Reflex callbacks are attached to a static source | Point and selection handlers never fire | Use a live figure, token, or data-bound source |
Every resolved dragMode === "none" is treated as an explicit navigation opt-out | Polar wheel zoom or double-click reset becomes unreachable | Check whether none was requested before suppressing those gestures |
| A cancelled pan is treated as a settled gesture | The host records a view end that the browser did not publish | Use the view-event phase and do not infer an end from cancellation |
Key files
| File | Why to read it |
|---|---|
| python/…/components.py | Public interaction configuration, chart callback surface, and facet linking |
| python/…/_figure.py | Figure-level policy normalization, state messages, and durable-state cache |
| python/…/widget.py | Live notebook transport and programmatic view and selection controls |
| python/…/channel.py | Browser message validation, canonical selection resolution, and Python callback dispatch |
| python/…/interaction.py | Canonical point lookup, selection geometry, and density-view interaction decisions |
| js/…/53_interaction.ts | Browser gesture handling, accessibility readout, selection overlays, modebar actions, and animated view changes |
| js/…/50_chartview.ts | Range state, clamping boundary, view events, and linked chart updates |
| js/…/45_lod.ts | Client-side detail-tier and drilled-window lifecycle |
| python/…/component.py | Reflex source modes and adapter event constraints |
| python/…/app.py | Out-of-band Reflex view and selection commands |
| spec/…/interaction.md | Shipped interaction, event, linking, and gesture contracts |