If you know some React but have never drawn a chart by hand, visx can feel abstract at first. The good news: you only need three ideas to get going. This article explains them without jargon, then builds a real line chart.
Idea 1: SVG is just positioned shapes
An <svg> element is a drawing canvas measured in pixels. The point (0, 0) is the top-left corner, and y grows downward. A circle at cx=50 cy=20 sits 50px from the left and 20px from the top. Every chart, however fancy, is lines, rectangles, circles and paths placed at the right coordinates.
Idea 2: Scales translate data into pixels
Your data says "May had 74 sessions". The SVG needs "draw a dot 212px from the top". A scale is a function that does that translation:
const yScale = scaleLinear({ domain: [0, 100], range: [300, 0] });
yScale(0); // 300 (bottom of the chart)
yScale(100); // 0 (top of the chart)
yScale(74); // 78
The domain is your data's range. The range is the pixel space. The y range runs from 300 to 0 because higher values should appear higher on screen, and SVG's y-axis points down.
Idea 3: Margins make room for axes
Axis labels need space. The common pattern is to define a margin, work out the inner drawing area, and shift everything into it with a Group:
const margin = { top: 16, right: 16, bottom: 36, left: 44 };
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
Putting it together: a line chart
Install the packages:
npm install @visx/group @visx/shape @visx/scale @visx/axis @visx/curve
Then create LineChart.tsx:
import { Group } from '@visx/group';
import { LinePath } from '@visx/shape';
import { scalePoint, scaleLinear } from '@visx/scale';
import { AxisBottom, AxisLeft } from '@visx/axis';
import { curveMonotoneX } from '@visx/curve';
type Datum = { label: string; value: number };
export default function LineChart({ data, width, height }: {
data: Datum[]; width: number; height: number;
}) {
const margin = { top: 16, right: 16, bottom: 36, left: 44 };
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const x = scalePoint<string>({
domain: data.map((d) => d.label),
range: [0, innerWidth],
padding: 0.5,
});
const y = scaleLinear<number>({
domain: [0, Math.max(...data.map((d) => d.value))],
range: [innerHeight, 0],
nice: true,
});
return (
<svg width={width} height={height}>
<Group left={margin.left} top={margin.top}>
<LinePath
data={data}
x={(d) => x(d.label) ?? 0}
y={(d) => y(d.value) ?? 0}
curve={curveMonotoneX}
stroke="#4f46e5"
strokeWidth={2.5}
/>
<AxisLeft scale={y} numTicks={5} />
<AxisBottom top={innerHeight} scale={x} />
</Group>
</svg>
);
}
Use it like any component: <LineChart data={data} width={600} height={300} />.
Three beginner mistakes to avoid
- Forgetting to flip the y range. If your chart is upside down, swap the range to
[innerHeight, 0]. - Passing a width of 0. If you use
ParentSize, give its parent a real width and, in visx 4, an explicit height. - Using a band scale for a line. Band scales return the left edge of each slot. For points, use
scalePoint, or addbandwidth() / 2.
Practise without setting anything up
Open the visx chart playground, choose "Line Chart" and paste your own numbers. The generated code shows exactly how to call the matching component from our starter kit. When you're ready for more, continue with how to use visx in a React app.