ChartGPUHandle is the ref type exposed by the ChartGPU component. It provides a small, safe imperative surface for:
- accessing the underlying
ChartGPUInstance - accessing the container element
- streaming/append updates (
appendData) - external render mode (
renderFrame,needsRender,getRenderMode,setRenderMode) - replacing options (
setOption) - programmatic zoom control (
setZoomRange) - programmatic crosshair/tooltip (
setInteractionX,getInteractionX) - hit testing (
hitTest)
Related:
ChartGPUcomponent- Streaming recipe
- Annotation authoring recipe
- dataZoom basics
- LLM entrypoint:
llm-context.md
import { ChartGPU } from 'chartgpu-react';
import type { ChartGPUHandle } from 'chartgpu-react';Returns the underlying @chartgpu/chartgpu instance once initialized, otherwise null.
Notes:
- The wrapper returns
nullwhen the chart has not initialized yet. - The wrapper guards calls using
instance && !instance.disposedinternally forappendData/setOption, butgetChart()itself can return a disposed instance if your code retains it—always checkdisposedbefore using it.
Returns the container <div> used to mount the chart.
Common use case: pass it to helpers like createAnnotationAuthoring(container, chart, ...).
Appends points to an existing series, delegating to ChartGPUInstance.appendData(...).
seriesIndex: zero-based index intooptions.series.newPoints: Cartesian series data superset (DataPoint[],XYArraysData,InterleavedXYData) orOHLCDataPoint[]for candlesticks.
This is typically more efficient than replacing the entire options.series[n].data array.
Renders a single frame. Intended for external render mode.
- Returns:
trueif a frame was rendered,falseif already clean.
Returns whether the chart has pending changes that require rendering.
Returns the current render mode ('auto' or 'external').
Sets the render mode. Use 'external' to drive frames manually via renderFrame() based on needsRender().
Replaces the chart options, delegating to ChartGPUInstance.setOption(options).
Important: setOption is treated as a full replacement, not a partial merge.
Programmatically sets the zoom range in percent-space.
start: start of zoom range (0–100).end: end of zoom range (0–100).
No-op when zoom is disabled on the chart (i.e. no dataZoom configured).
Programmatically drives the crosshair / tooltip to a domain-space x value. Pass null to clear the crosshair.
x: domain-space x value, ornullto clear.source(optional): source identifier, useful for sync disambiguation (e.g. avoiding echo loops withconnectCharts).
Reads the current crosshair / interaction x in domain units. Returns null when the crosshair is inactive.
Performs hit-testing on a pointer or mouse event. Returns coordinates and any matched chart element.
e: a nativePointerEventorMouseEventfrom the chart container.- Returns:
ChartGPUHitTestResultwith pixel coordinates, domain-space coordinates, and an optional match object. - If the chart is not initialized or disposed, returns a sentinel with
isInGrid: falseandNaNcoordinates.
React ergonomics note: React synthetic events (React.PointerEvent, React.MouseEvent) are not the same as native DOM events. Pass e.nativeEvent instead:
<div onPointerDown={(e) => {
const result = chartRef.current?.hitTest(e.nativeEvent);
// ...
}} />Import the result type if needed:
import type { ChartGPUHitTestResult } from 'chartgpu-react';Set options.renderMode = 'external' or call setRenderMode('external'), then drive frames yourself:
if (handle.current?.needsRender()) {
handle.current.renderFrame();
}Useful when you need precise control over when the chart renders (e.g. coordinating with other animations or a custom render loop).
import { useEffect, useMemo, useRef } from 'react';
import { ChartGPU } from 'chartgpu-react';
import type { ChartGPUHandle, ChartGPUOptions } from 'chartgpu-react';
import type { OHLCDataPoint } from 'chartgpu-react';
export function StreamingCandles() {
const ref = useRef<ChartGPUHandle>(null);
const options: ChartGPUOptions = useMemo(
() => ({
xAxis: { type: 'time' },
yAxis: { type: 'value' },
autoScroll: true,
dataZoom: [{ type: 'inside' }, { type: 'slider' }],
series: [
{
type: 'candlestick',
sampling: 'ohlc',
data: [],
},
],
}),
[]
);
useEffect(() => {
const timer = window.setInterval(() => {
const next: OHLCDataPoint = {
timestamp: Date.now(),
open: 100,
close: 101,
high: 102,
low: 99,
};
ref.current?.appendData(0, [next]);
}, 500);
return () => window.clearInterval(timer);
}, []);
return <ChartGPU ref={ref} options={options} style={{ height: 420 }} theme="dark" />;
}