Blog>
Snippets

Implementing Zoom and Pan Features on Financial Charts

Show how to enhance interactivity in financial charts by implementing zoom and pan features with TanStack React Charts, enabling users to explore data in detail.
import { useChart } from 'react-charts';

function MyChart({ data }) {
  const { getChartProps } = useChart({
    data,
    // other chart config
  });

  // Implement the zoom and pan functionality
  const handleZoom = useCallback((zoomFactor) => {
    // Logic to zoom in or out
  }, []);

  const handlePan = useCallback((direction) => {
    // Logic to pan the chart
  }, []);

  return (
    <div {...getChartProps()}>
      <button onClick={() => handleZoom(1.1)}>Zoom In</button>
      <button onClick={() => handleZoom(0.9)}>Zoom Out</button>
      <button onClick={() => handlePan('left')}>Pan Left</button>
      <button onClick={() => handlePan('right')}>Pan Right</button>
    </div>
  );
}
This code snippet shows how to set up a basic chart with TanStack React Charts and add zoom and pan functionality. 'useChart' hook initializes the chart with provided 'data'. Zoom and pan handlers ('handleZoom', 'handlePan') are outlined for further implementation according to the desired functionality, enabling interactive exploration of financial data. UI control is demonstrated with buttons to zoom in/out and pan left/right.