Introducing a new way to review your PRs.

Read the announcement

reflex-dev / xy

Ultra-fast and customizable Python charts

View on GitHub

20 documents · Updated Nov 18, 2026

docs / browser-rendering-runtime

Browser rendering runtime

The browser rendering runtime is the TypeScript client that turns a chart payload into an interactive chart in a browser. It keeps the current chart view, sends work that needs server or kernel data back through the host, draws data with GPU resources, and keeps labels and controls in browser-native layers.

Runtime boundary

The runtime sits between a host such as a notebook widget or a standalone HTML page and the chart payload produced by the data side. Its main boundary is a chart specification plus binary columns, followed by browser events and optional messages for refined data, exact values, selections, and durable view state.

flowchart LR
    P[Chart specification and binary columns] --> M[Browser runtime]
    H[Host messages and browser events] --> M
    M --> G[GPU data layer]
    M --> C[DOM and 2D chart layers]
    M --> O[Host messages and chart events]

The runtime rejects a chart when its protocol is not the one it understands. It also checks that the declared columns fit the supplied binary data before it applies an update. These checks keep a partial or old payload from becoming a chart that looks valid but shows the wrong data. References: js/…/50_chartview.ts• L587-593, js/…/00_header.ts• L133-171

Host entry points

The public entry points support two host modes. A widget mount receives a model and uses its message channel. A standalone mount receives a complete payload and works without a kernel.

The widget entry point also watches specification and buffer changes for streamed data. It waits when a host exposes those changes in different events, then applies the pair as one coherent update. The standalone entry point retains the small CPU views needed for browser-side hover when no kernel can provide exact values. References: js/…/60_entries.ts• L18-63, js/…/60_entries.ts• L65-99

Payload boundary

The payload boundary keeps chart structure and numeric data separate. The specification describes axes, traces, styles, and view rules. The binary columns carry the numeric channels without turning large data sets into JSON values.

The runtime supports one packed binary span or a list of column spans. A separate HTTP frame decoder can validate a framed response and expose its metadata and binary buffers without copying the frame. References: js/…/00_header.ts• L4-23, js/…/00_header.ts• L156-171, js/…/00_header.ts• L202-296

Chart view lifecycle

ChartView is the runtime's central chart object. It owns the chart root, the current and home view ranges, the payload, GPU trace records, browser listeners, and the host connection that can supply later data.

When a chart mounts, the runtime normalizes axes, computes a plot area, builds the browser layers, creates the GPU path, then starts the first draw. It retains the specification and payload so it can rebuild GPU state after a context loss. Responsive sizes and device-pixel changes schedule a new layout and draw instead of making each browser event perform work immediately. References: js/…/50_chartview.ts• L587-753, js/…/50_chartview.ts• L1790-1875

View state

The view state is the durable description of what part of the chart the user sees and which geometric selection is active. The runtime keeps this state in one form so pointer gestures, linked charts, history controls, host messages, and the public root.xy API reach the same update path.

The state can hold ranges for declared axes and a range or polygon selection. It keeps row selections as an opaque marker because the row index set belongs to the data side. History stores short-lived local snapshots and can move backward or forward without treating a linked update as a new local action. References: js/…/57_viewstate.ts• L3-50, js/…/57_viewstate.ts• L53-167, js/…/57_viewstate.ts• L169-229

Data update flow

When the host sends a new view or data result, the runtime first checks whether the result still belongs to the current view request and filter state. It then updates the affected trace or replaces its screen-bounded representation, schedules a draw, and asks for more detail only when the current view needs it.

Large density traces use a stepped refinement path instead of requesting a new aggregate for every small pan or zoom. The runtime keeps a covering aggregate, requests an aligned finer window only when the current cache is too coarse, and uses exact point windows when the view can support them. A reply for a covered view can update the point-count facts without replacing the stable texture. This avoids visual jumps while still moving toward exact points.

The runtime suppresses duplicate requests for the same window and screen size. It accepts a reply only for the current request, or for the original request still used by a duplicate-suppressed trace. Hidden-category filters form part of request identity, so an aggregate for a different legend state cannot replace the current view. References: js/…/54_kernel.ts• L21-185, js/…/54_kernel.ts• L195-235, js/…/54_kernel.ts• L795-846, js/…/45_lod.ts• L379-470, js/…/45_lod.ts• L1154-1229

Streaming append updates preserve the user's intent. A chart at its home view follows the new data range. A zoomed chart follows the new end only when the user had pinned that end. A chart used to inspect older data stays in place. References: js/…/54_kernel.ts• L26-150, js/…/54_kernel.ts• L406-513, js/…/54_kernel.ts• L730-849

Rendering layers

The runtime separates data paint from chart chrome. This lets the GPU handle dense data while the browser keeps text, controls, and semantic information crisp and accessible.

GPU data layer

The data layer draws trace geometry with WebGL2. The chart-kind registry maps each trace kind to a build and draw behavior, so the central view can run the same frame sequence for points, lines, areas, rectangular marks, flow bands, and other supported geometry.

The current view changes the mapping from data space to the plot. It does not need to rewrite the resident data for ordinary pan and zoom. A separate pick pass gives point-capable traces stable row identities for hover and selection. Polar charts use the same data layer with a radial and angular projection instead of a rectangular one. See WebGL rendering resources for the resource lifetime and GPU drawing model. References: js/…/55_marks.ts• L3-24, js/…/55_marks.ts• L26-35, js/…/55_marks.ts• L158-282, js/…/50_chartview.ts• L6443-6510, js/…/50_chartview.ts• L8510-8647

DOM and 2D layers

The DOM and 2D layers provide the parts of a chart that need text, layout, browser focus, or CSS. The root holds the background and grid canvas, the data canvas, the annotation canvas, labels, tooltip, legend, colorbar, badges, and modebar.

The runtime places labels and controls from the same plot geometry used by the data layer. CSS classes, inline structural styles, theme changes, responsive layout, and accessibility text therefore affect the browser presentation without moving data into the DOM. References: js/…/50_chartview.ts• L2529-2636, js/…/50_chartview.ts• L7766-7907, js/…/50_chartview.ts• L8127-8183

Shared GPU host

Charts in one top-level document can share one detached WebGL2 host while each chart keeps its own programs, buffers, uniforms, pick target, and visible 2D presentation canvas. The host runs one color or pick pass at a time and copies the result into the active chart surface. It can also cache immutable shader objects while leaving mutable programs under chart ownership.

This sharing reduces pressure on the browser's WebGL context limit. A context governor covers native per-chart fallbacks and coordinates live-context counts across same-origin frames when browser support allows it. It releases least-recently-used charts that are out of view first. When all candidates are visible, a chart can keep a static snapshot while its context is released. Context loss marks the chart as unavailable, cancels stale work, then rebuilds resources from the retained payload and view. See WebGL rendering resources for the shared host and recovery contract. References: js/…/42_glhost.ts• L88-115, js/…/42_glhost.ts• L173-215, js/…/42_glhost.ts• L218-302, js/…/42_glhost.ts• L496-572, js/…/50_chartview.ts• L312-355, js/…/50_chartview.ts• L357-525, js/…/50_chartview.ts• L4408-4462, js/…/50_chartview.ts• L4520-4532, js/…/50_chartview.ts• L1878-2109, js/…/50_chartview.ts• L2112-2250

Data scale and background work

The runtime aims to keep the browser responsive when a trace is too large for direct drawing. It uses the representation that matches the current view and asks the data side for a more suitable representation when the view changes.

Resolution tiers

Small or ordinary traces can draw their resident points or geometry. Larger traces can use a decimated view that covers the screen. Massive point traces start with a density surface, may move through a small number of aligned aggregate windows, and switch to an exact point window when the user zooms in far enough.

The runtime keeps exact point windows that the user has already visited. A later view inside one of those windows can restore the points locally instead of making another request. The cache is bounded and removes windows that are too far from the current points range, so retained detail does not grow without limit.

The runtime keeps a valid overview while a refined result is in flight. It crossfades density and point representations where possible and shows reduction badges when a sample or aggregate limits what the chart can show. See Browser worker and kernels for the request and reply paths. References: js/…/45_lod.ts• L6-27, js/…/45_lod.ts• L355-470, js/…/45_lod.ts• L684-810, js/…/50_chartview.ts• L6477-6503, js/…/54_kernel.ts• L45-145

Worker refinement

A standalone density chart has no kernel to re-bin a retained sample after zoom. The runtime keeps the full-data overview for pans and zoom-outs, then sends the retained sample to a browser worker only when the view becomes tighter than the home view. The worker builds a local grid off the main thread and the runtime applies it through the same density display path used for a kernel reply.

Worker support is optional. If the browser or its content policy cannot create a worker from the bundled source, the chart keeps the shipped overview rather than failing to render. The chart marks the sample-based refinement so the reduced detail is visible to the user. See Browser worker and kernels for the worker and kernel roles. References: js/…/46_worker.ts• L1-22, js/…/46_worker.ts• L24-103, js/…/54_kernel.ts• L238-407

Browser behavior

The runtime coordinates browser input, chart events, animation, and cross-chart state around the current view. These behaviors operate on the same view and trace state as rendering, so the visible chart and host-facing events stay aligned.

Interaction surface

The interaction layer turns pointer, wheel, keyboard, and modebar actions into navigation, hover, selection, reset, and export behavior. It resolves the configured default drag action from the capabilities of the current chart, so a chart does not advertise a gesture that it cannot perform.

Hover uses the GPU pick surface when a trace supports row picking. Geometry that is not point-pickable uses a CPU containment or nearest-item path instead. The runtime sends chart events to the DOM and sends the host messages needed for exact hover values, selections, view changes, and linked charts. See Browser interaction and animation for gesture and event behavior. References: js/…/53_interaction.ts• L1-6, js/…/53_interaction.ts• L231-459, js/…/50_chartview.ts• L8770-8843, js/…/54_kernel.ts• L850-927

Accessibility surface

The chart root is a named region with a text summary. The plot canvas exposes a concise plot label, and pickable traces support keyboard movement through data points. A live status region announces readouts without making the visual canvas itself carry all meaning.

The runtime keeps tooltip and hover information separate from the durable view state. Exact values from the data side can replace approximate browser-side values without changing the user's current view. References: js/…/50_chartview.ts• L2551-2574, js/…/50_chartview.ts• L2638-2675, js/…/54_kernel.ts• L850-897

Animation clock

The animation layer uses the browser frame clock to animate view changes and data changes. It keeps old and new trace representations long enough to show an enter, update, or exit transition, then releases the old resources.

Rapid updates use a latest-state approach. A newer update cancels or replaces older transition work, and lifecycle events report start, end, and cancellation to both the chart DOM and the host. Reduced-motion settings can remove the time-based transition. See Browser interaction and animation for the animation contract. References: js/…/56_animation.ts• L1-10, js/…/56_animation.ts• L156-246, js/…/56_animation.ts• L248-324

Linked chart behavior

Charts can join a browser channel by link group. A chart publishes settled view changes and, when enabled, selections. Peer charts apply only the axes and selection parts allowed by their own configuration, while a source marker prevents an update from returning to its sender.

Linked updates do not create local history entries. This keeps one user action from appearing as a new action in every chart in the group. References: js/…/50_chartview.ts• L1592-1705

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the chart protocol version aligned with the producer. An older client can otherwise accept new fields and draw a plausible but wrong chart.
  • Keep packed and split payload handling aligned between first mount, streamed append, and HTTP frame decode paths.
  • Keep GPU and pick transforms aligned. A chart can look correct while hover reports the wrong row if the pick pass uses a different projection.
  • Keep refined windows tied to request sequence, actual request identity, and filter state. A late or duplicate-suppressed response must not replace a newer view or category mask.
  • Keep the aggregate ladder aligned with the exact point-window boundary. A new aggregate step should not cause a request for every small pan or hide a view that can already use exact points.
  • Keep cached exact windows and density windows paired with their coverage and current filter facts. A promoted window must describe the view it serves.
  • Keep shared-host resource ownership separate from per-chart resource ownership. A chart release must not dispose resources still used by another chart.
  • Keep shared-host and native-context recovery paths aligned. A host-wide loss must reach every client, while a client rebuild must not replace a healthy shared context.
  • Keep durable state and linked messages compatible with the public root.xy state surface.
  • Keep reduction badges aligned with the representation that is actually drawn. Do not hide sample-based or aggregate limits behind a visually similar frame.

Important failure modes

TriggerConsequenceGuard
Client and payload use different chart protocol versionsMount stops instead of drawing a silently wrong chartProtocol check in ChartView
Widget host publishes a specification and buffers as a torn pairAppend waits and leaves the last valid frame in placePayload coherence check and identity tracking
Browser has no usable WebGL2 contextChart construction fails and leaves a clear unavailable messageContext creation check and host cleanup
A late tier or density reply has an old sequence, request identity, or filter keyThe reply is ignored instead of replacing the current representationCurrent-request and filter-state checks
A density trace requests a new aggregate for every small view changeThe chart spends bandwidth on duplicate windows and can appear to jump between resolutionsAligned aggregate steps and duplicate-request suppression
A cached point window is used outside its coverage or precision rangeThe chart shows points that do not represent the current viewWindow containment and direct-point budget checks
A shared host becomes unavailable while a client is drawingThe affected pass stops without copying a partial frameHost readiness, client ownership, and pass checks
Browser or driver loses a WebGL contextGPU work stops and the chart enters a lost stateLoss handling cancels stale work and rebuilds from retained state
Worker or Blob creation is blocked in standalone HTMLZoom refinement stays at the shipped overviewOptional worker creation with a visible reduction path

Key files

FileWhy to read it
js/…/60_entries.tsShows how widget and standalone mounts create and destroy a chart view.
js/…/50_chartview.tsDefines the central chart lifecycle, layer stack, draw scheduling, layout, and recovery boundary.
js/…/00_header.tsDefines the client protocol, payload shapes, binary column access, and frame decoding boundary.
js/…/55_marks.tsShows how chart kinds map to shared build and draw behavior.
js/…/42_glhost.tsExplains shared WebGL ownership, color and pick passes, shader sharing, and context recovery.
js/…/45_lod.tsDescribes view-dependent aggregate and exact-point representations, their caches, and their transitions.
js/…/54_kernel.tsShows the browser-to-kernel request and reply lifecycle, append flow, duplicate suppression, and exact-value upgrades.
js/…/57_viewstate.tsDefines durable view state, public state control, and local history.
js/…/53_interaction.tsDefines browser gestures, selection, hover behavior, and chart controls.
js/…/56_animation.tsDefines view and data transition timing and lifecycle events.
js/…/46_worker.tsShows the optional standalone worker path for density refinement.