Create a Single Line Chart in React with @vx

This is part 2 of the Data Visualization with React and @vx series

  1. 1) Bar Chart in React with @vx
  2. 2) Create a Single Line Chart in React with @vx

Update: October 2020 - @vx is now @visx and is at 1.0. You can read about the update from Airbnb Engineering. As of this update, the api is all the same, and it has TypeScript support 👍.

We’ve built a bar chart in react using the data vis library @vx. The next chart that we can build to help tell our data’s story is a line chart. Line charts are great for showing trends over time and that’s what we’ll build today.

What We're Building TL;DR

Requirements

  • Display time along the x-axis
  • Display metrics along the y-axis
  • Show each data point on line

Packages

Let's start by getting the packages we need from @vx. We'll need shapes, scale, axis, gradient (easy background color), and some mock data to get started.

Or

Data

Now that we have our packages we can start stubbing out our data. We're going to use some mock data to get started so feel free to create your own or use this data set.

Now that we have the shape of our data we can add some helper functions that will access those items. This will help us add the date across the x-axis and the miles along the y-axis. We'll see how these come into play a little later.

Scales

We can now define the max height and max width that we would like our chart to be. Our component will take height and width as props and then we can add a little padding. This will help us as we define our scales for this chart.

The scales are where the magic really happen. It all comes down to domain and range. The general rule of thumb based on my understanding is that domain is the lowest and highest data points. The range is the pixel range we would like to plot these data points on.

In our scales below we can see that range (rangeRound) is from 0 to xMax which is the height bound of our chart. @vx gives us a helper, rangeRound, that prettifies the numbers.

The domain is an array of all data points which resolves to lowest (4.1) and highest (9.3) of the data set.

Building our Line Chart

Now we can start building the component. Let's start by setting up the svg that will hold our line and axes.

Looks good. The first thing we'll add is the y-axis. To do this we use AxisLeft from @vx. We need to pass it our yScale and we'll give it a few other props for styling. The numTicks limits the number of values shown on the y-axis and label is what will display along the axis.

Then we'll add the AxisBottom that has similar props to AxisLeft. The top is where it should start vertically from the top, which is the chart height in this case. The labelOffset prop dictates how much space is in between the ticks and the axis label. It should look like this:

  • Display time along the x-axis
  • Display metrics along the y-axis

Now we can add the line to the chart using LinePath from @vx/shapes and we'll pass it curveLinear from @vx/curve to dictate its shape.

It's looking like a nice, one-line chart now. We might want to add some dots to represent the data points. To do that we'll map over the data items and use the circle element positioned using each item's points.

  • Show each data point on line

Awesome, we fulfilled all of our requirements for this one-line chart. Here is all the code together.

Bonus

For better sizing/resizing we can use a resize observer hook in our component. I like to use the package use-resize-observer for this. Let's see how we can use it in our component.

Categorydev
Discuss this article on Twitter