visx

repository·master·Indexed 12 days ago

https://github.com/airbnb/visx

A collection of reusable, low-level visualization components that combine D3's mathematical capabilities with React's declarative DOM management. It includes specialized packages for accessibility (@visx/a11y), annotations (@visx/annotation), axes (@visx/axis), bounding rect utilities (@visx/bounds), and brushing (@visx/brush).

Tokens
97.9K
Snippets
373
Records
488
Agent score
97%

What's inside visx

  1. Use geographic projections with @visx/geo

    master

    The @visx/geo package provides components for rendering geographic projections. You can use the <Projection /> component to render several preset projections or use specialized convenience components.

    Preset Projections

    When using the <Projection /> component, you can specify one of the following via the projection prop:

    • orthographic
    • albers
    • albersUsa
    • mercator
    • naturalEarth
    • equalEarth

    Available Components

    • <Projection />: Renders preset projections and supports configurable <Graticule /> lines.
    • <Mercator />: A convenience component for Mercator projections.
    • <CustomProjection />: Used for implementing full custom projections.
    • <Graticule />: Used for rendering graticule lines (lines of latitude and longitude).
  2. Use axis components in visx

    master

    An axis component in visx consists of a line with ticks, tick labels, and an axis label to help interpret graphs.

    You have two ways to implement axes:

    1. Pre-made axes: Use one of the 4 built-in axis components for quick implementation.
    2. Custom axes: Create your own axis by using the <Axis /> element as a base.

    Note: If you require animated ticks, you can use the AnimatedAxis variant provided by the @visx/react-spring package.

  3. Use @visx/xychart for high-level Cartesian charts

    master

    Unlike other low-level visx packages, @visx/xychart provides a high-level API for creating x,y (cartesian coordinate) charts. It abstracts common visualization engineering complexities while remaining highly expressive through modularized React.context layers for theme, canvas dimensions, scales (x/y/color), data, events, and tooltips.

    Out-of-the-box support includes:

    • Various <*Series /> types (e.g., lines, bars) with optional animation.
    • <Axis /> (animated or non-animated).
    • <Grid /> (animated or non-animated).
    • <Annotation /> (animated or non-animated).
    • <Tooltip />.
    • themeing.
  4. What is @visx/delaunay?

    master
    The @visx/delaunay package is a wrapper around d3-delaunay that provides React-specific utilities. It allows you to partition a two-dimensional plane into regions based on input points. Each region represents all points closer to its specific input point than to any other. This is commonly used to create invisible interaction layers to make small data points easier to hover or click.
  5. How @visx/a11y works: Primitive-first design

    master

    The @visx/a11y package is a semantics layer rather than a chart framework. It does not ship a complete chart component or impose a specific SVG structure. Instead, it provides spreadable props, generated descriptions, and React helpers that you attach to your existing visx primitives.

    Key design principles:

    • Prop-driven: visx primitives remain prop-driven.
    • Server-safe: Server helpers are pure functions with no DOM or React hook requirements.
    • Client-side hooks: The client hook uses React useId() only when id is omitted.
    • Consumer ownership: You own the SVG render tree, focus ring styling, and the visibility of decorative elements.
    • Independent components: Data tables and announcers can be used independently.
    • Adaptive density: Dense charts automatically degrade to summary descriptions when they exceed the pointDescriptionThreshold.
    • Keyboard navigation: keyboardNavEnabled controls roving focus for point-by-point exploration.
  6. Add Tooltip State Logic with Hooks or HOCs

    master

    The package provides two ways to manage tooltip state (visibility, position, and data):

    1. useTooltip() (Recommended for Functional Components): Returns an object containing state and control functions. Note: You must wrap your component in an element (e.g., div) with position: relative for correct positioning, as this HOC does not do it automatically.
    2. withTooltip(BaseComponent [, containerProps [, renderContainer]]) (Recommended for Class Components): A Higher-Order Component that wraps your component in a div with relative positioning by default and injects tooltip props.

    Shared API Surface

    Both methods expose the following properties/functions:

    NameTypeDescription
    showTooltipfuncCall with ({ tooltipData, tooltipLeft, tooltipTop }) to open and set state.
    hideTooltipfuncCloses the tooltip.
    tooltipOpenboolCurrent visibility state.
    tooltipLeftnumberThe horizontal position passed to showTooltip.
    tooltipTopnumberThe vertical position passed to showTooltip.
    tooltipDataanyThe data passed to showTooltip for rendering.
    updateTooltipfuncCall with ({ tooltipOpen, tooltipLeft, tooltipTop, tooltipData }) to update state.