Tutorial

How to Use visx Data Visualization in a React App

visx is a collection of small React packages from Airbnb that turn D3's maths into React components. It does not hand you a finished chart. You build the chart from parts, so you keep full control over layout, styling and behaviour. This article walks through that workflow with a bar chart you can adapt to almost any dataset.

The visx mental model

Every visx chart follows the same four steps:

  1. Data: an array of plain objects, such as { label: 'Jan', value: 42 }.
  2. Scales: functions that convert data values into pixel positions (@visx/scale).
  3. Shapes: SVG elements positioned with those scales (@visx/shape).
  4. Helpers: axes, grids, tooltips and responsive wrappers you add as needed.

React owns the DOM the whole time. D3 only does the arithmetic, which is why visx fits naturally into component-based apps and avoids the "two libraries fighting over the DOM" problem you get when mixing raw D3 with React.

Step 1: Install only what you need

visx is split into many packages so your bundle only includes what you import. For a basic bar chart with axes you need five:

npm install @visx/group @visx/shape @visx/scale @visx/axis @visx/grid

visx 4 requires React 18 or 19. If your project is still on React 16 or 17, stay on visx 3 until you upgrade React. Our installation article covers Yarn, pnpm and the all-in-one @visx/visx package.

Step 2: Build your scales

A band scale spreads categories evenly across the width. A linear scale maps numbers to heights. Note the reversed range on the y scale: SVG coordinates start at the top, so the largest value needs the smallest y.

import { scaleBand, scaleLinear } from '@visx/scale';

const xScale = scaleBand<string>({
  domain: data.map((d) => d.label),
  range: [0, innerWidth],
  padding: 0.3,
});

const yScale = scaleLinear<number>({
  domain: [0, Math.max(...data.map((d) => d.value))],
  range: [innerHeight, 0],
  nice: true,
});

nice: true rounds the top of the domain to a clean number, so your axis ends at 100 instead of 91.

Step 3: Draw the bars

import { Group } from '@visx/group';
import { Bar } from '@visx/shape';

<svg width={width} height={height}>
  <Group left={margin.left} top={margin.top}>
    {data.map((d) => {
      const barHeight = innerHeight - (yScale(d.value) ?? 0);
      return (
        <Bar
          key={d.label}
          x={xScale(d.label)}
          y={innerHeight - barHeight}
          width={xScale.bandwidth()}
          height={barHeight}
          fill="#4f46e5"
          rx={4}
        />
      );
    })}
  </Group>
</svg>

Group is a <g> element with a transform. Shifting everything by the margins gives the axes room to breathe.

Step 4: Add axes and a grid

import { AxisBottom, AxisLeft } from '@visx/axis';
import { GridRows } from '@visx/grid';

<GridRows scale={yScale} width={innerWidth} stroke="#e2e8f0" />
<AxisLeft scale={yScale} numTicks={5} />
<AxisBottom top={innerHeight} scale={xScale} />

Put the grid first so it sits behind the bars. Axes accept tickLabelProps to style the labels, either as an object or as a function when you need per-tick styling.

Step 5: Make it responsive

Charts take pixel sizes, so wrap them in ParentSize from @visx/responsive to read the container width:

import { ParentSize } from '@visx/responsive';

<div style={{ height: 320 }}>
  <ParentSize>
    {({ width }) => <BarChart data={data} width={width} height={320} />}
  </ParentSize>
</div>

visx 4 detail: ParentSize now renders a height: 100% wrapper and draws your chart inside an absolutely positioned box. If the parent has no explicit height, the chart collapses to zero. We hit this ourselves while testing the starter kit, which is why the snippet sets height: 320 on the wrapper.

Step 6: Add a tooltip

@visx/tooltip gives you a useTooltip hook and positioned tooltip components, and @visx/event gives you localPoint to read the cursor position relative to the SVG. The BarChart.tsx component in our starter kit shows the full pattern in about 15 lines.

Where to go next

Once these six steps feel familiar, every other visx chart (lines, areas, pies, heatmaps) is a variation on the same pattern with a different shape component.