DEV Community

Eric Lewis
Eric Lewis

Posted on

Getting Started with React Timeseries Charts: Visualizing Time-Based Data in React

React Timeseries Charts is a React library for creating time-series visualizations. It provides components for displaying time-based data with features like zooming, brushing, and interactive tooltips. This guide walks through setting up and creating time-series charts using React Timeseries Charts with React, from installation to a working implementation.

Demo animation

Prerequisites

Before you begin, make sure you have:

  • Node.js version 14.0 or higher installed
  • npm, yarn, or pnpm package manager
  • A React project (version 16.8 or higher) or create-react-app setup
  • Basic knowledge of React components
  • Familiarity with JavaScript/TypeScript
  • Understanding of time-series data

Installation

Install React Timeseries Charts and required dependencies:

npm install react-timeseries-charts pondjs
Enter fullscreen mode Exit fullscreen mode

Or with yarn:

yarn add react-timeseries-charts pondjs
Enter fullscreen mode Exit fullscreen mode

Or with pnpm:

pnpm add react-timeseries-charts pondjs
Enter fullscreen mode Exit fullscreen mode

After installation, your package.json should include:

{
  "dependencies": {
    "react-timeseries-charts": "^0.16.0",
    "pondjs": "^0.9.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Project Setup

React Timeseries Charts requires minimal setup. Import the components and you're ready to use them.

First Example / Basic Usage

Let's create a simple time-series chart. Create a new file src/ChartExample.jsx:

// src/ChartExample.jsx
import React from 'react';
import { TimeSeries, ChartContainer, ChartRow, Charts, YAxis, LineChart, Resizable } from 'react-timeseries-charts';
import { TimeRange, Index } from 'pondjs';

function ChartExample() {
  const points = [
    [new Date('2024-01-01'), 35],
    [new Date('2024-01-02'), 28],
    [new Date('2024-01-03'), 34],
    [new Date('2024-01-04'), 32],
    [new Date('2024-01-05'), 40],
    [new Date('2024-01-06'), 32]
  ];

  const series = new TimeSeries({
    name: 'sales',
    columns: ['time', 'value'],
    points: points.map(([time, value]) => [time.getTime(), value])
  });

  const timeRange = series.range();

  return (
    <div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
      <h2>Basic Time-Series Chart Example</h2>
      <Resizable>
        <ChartContainer timeRange={timeRange} width={800}>
          <ChartRow height="200">
            <YAxis
              id="sales"
              label="Sales"
              min={0}
              max={50}
              width="60"
            />
            <Charts>
              <LineChart
                axis="sales"
                series={series}
                columns={['value']}
              />
            </Charts>
          </ChartRow>
        </ChartContainer>
      </Resizable>
    </div>
  );
}

export default ChartExample;
Enter fullscreen mode Exit fullscreen mode

Update your App.jsx:

// src/App.jsx
import React from 'react';
import ChartExample from './ChartExample';
import './App.css';

function App() {
  return (
    <div className="App">
      <ChartExample />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

This creates a basic time-series line chart displaying sales data over time.

Understanding the Basics

React Timeseries Charts provides several components:

  • ChartContainer: Main chart container with time range
  • ChartRow: Row container for charts
  • Charts: Container for chart components
  • LineChart: Line chart component
  • YAxis: Y-axis component
  • TimeSeries: Data structure for time-series data
  • Resizable: Makes chart resizable

Key concepts:

  • TimeSeries: Create time-series data using pondjs TimeSeries
  • Time range: Define the time range for the chart
  • Columns: Specify data columns
  • Points: Array of [timestamp, value] pairs
  • Axis configuration: Configure Y-axis with min, max, label

Here's an example with multiple series:

// src/MultipleSeriesExample.jsx
import React from 'react';
import { TimeSeries, ChartContainer, ChartRow, Charts, YAxis, LineChart, Resizable } from 'react-timeseries-charts';
import { TimeRange } from 'pondjs';

function MultipleSeriesExample() {
  const salesPoints = [
    [new Date('2024-01-01'), 35],
    [new Date('2024-01-02'), 28],
    [new Date('2024-01-03'), 34],
    [new Date('2024-01-04'), 32],
    [new Date('2024-01-05'), 40]
  ];

  const targetPoints = [
    [new Date('2024-01-01'), 40],
    [new Date('2024-01-02'), 35],
    [new Date('2024-01-03'), 38],
    [new Date('2024-01-04'), 36],
    [new Date('2024-01-05'), 42]
  ];

  const salesSeries = new TimeSeries({
    name: 'sales',
    columns: ['time', 'value'],
    points: salesPoints.map(([time, value]) => [time.getTime(), value])
  });

  const targetSeries = new TimeSeries({
    name: 'target',
    columns: ['time', 'value'],
    points: targetPoints.map(([time, value]) => [time.getTime(), value])
  });

  const timeRange = salesSeries.range();

  return (
    <div style={{ padding: '20px', maxWidth: '900px', margin: '0 auto' }}>
      <h2>Multiple Series Example</h2>
      <Resizable>
        <ChartContainer timeRange={timeRange} width={800}>
          <ChartRow height="200">
            <YAxis
              id="sales"
              label="Sales"
              min={0}
              max={50}
              width="60"
            />
            <Charts>
              <LineChart
                axis="sales"
                series={salesSeries}
                columns={['value']}
                style={{ value: { stroke: '#1f77b4' } }}
              />
              <LineChart
                axis="sales"
                series={targetSeries}
                columns={['value']}
                style={{ value: { stroke: '#ff7f0e' } }}
              />
            </Charts>
          </ChartRow>
        </ChartContainer>
      </Resizable>
    </div>
  );
}

export default MultipleSeriesExample;
Enter fullscreen mode Exit fullscreen mode

Practical Example / Building Something Real

Let's build a comprehensive dashboard with time-series charts:

// src/DashboardCharts.jsx
import React, { useState } from 'react';
import { TimeSeries, ChartContainer, ChartRow, Charts, YAxis, LineChart, Resizable } from 'react-timeseries-charts';
import { TimeRange } from 'pondjs';

function DashboardCharts() {
  const [selectedPeriod, setSelectedPeriod] = useState('daily');

  const dailyPoints = [
    [new Date('2024-01-01'), 35],
    [new Date('2024-01-02'), 28],
    [new Date('2024-01-03'), 34],
    [new Date('2024-01-04'), 32],
    [new Date('2024-01-05'), 40],
    [new Date('2024-01-06'), 32],
    [new Date('2024-01-07'), 38]
  ];

  const weeklyPoints = [
    [new Date('2024-01-01'), 210],
    [new Date('2024-01-08'), 240],
    [new Date('2024-01-15'), 230],
    [new Date('2024-01-22'), 250]
  ];

  const salesPoints = selectedPeriod === 'daily' ? dailyPoints : weeklyPoints;

  const salesSeries = new TimeSeries({
    name: 'sales',
    columns: ['time', 'value'],
    points: salesPoints.map(([time, value]) => [time.getTime(), value])
  });

  const revenuePoints = [
    [new Date('2024-01-01'), 12000],
    [new Date('2024-01-02'), 9800],
    [new Date('2024-01-03'), 13400],
    [new Date('2024-01-04'), 12800],
    [new Date('2024-01-05'), 16000],
    [new Date('2024-01-06'), 12800]
  ];

  const revenueSeries = new TimeSeries({
    name: 'revenue',
    columns: ['time', 'value'],
    points: revenuePoints.map(([time, value]) => [time.getTime(), value])
  });

  const timeRange = salesSeries.range();

  return (
    <div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
      <h1>Time-Series Dashboard</h1>
      <div style={{ marginBottom: '20px', textAlign: 'center' }}>
        <button
          onClick={() => setSelectedPeriod('daily')}
          style={{
            padding: '8px 16px',
            margin: '0 5px',
            backgroundColor: selectedPeriod === 'daily' ? '#007bff' : '#6c757d',
            color: 'white',
            border: 'none',
            borderRadius: '4px',
            cursor: 'pointer'
          }}
        >
          Daily
        </button>
        <button
          onClick={() => setSelectedPeriod('weekly')}
          style={{
            padding: '8px 16px',
            margin: '0 5px',
            backgroundColor: selectedPeriod === 'weekly' ? '#007bff' : '#6c757d',
            color: 'white',
            border: 'none',
            borderRadius: '4px',
            cursor: 'pointer'
          }}
        >
          Weekly
        </button>
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: '40px' }}>
        <div>
          <h2>Sales Over Time</h2>
          <Resizable>
            <ChartContainer timeRange={timeRange} width={1000}>
              <ChartRow height="200">
                <YAxis
                  id="sales"
                  label="Sales"
                  min={0}
                  max={50}
                  width="60"
                />
                <Charts>
                  <LineChart
                    axis="sales"
                    series={salesSeries}
                    columns={['value']}
                    style={{ value: { stroke: '#1f77b4', strokeWidth: 2 } }}
                  />
                </Charts>
              </ChartRow>
            </ChartContainer>
          </Resizable>
        </div>
        <div>
          <h2>Revenue Over Time</h2>
          <Resizable>
            <ChartContainer timeRange={revenueSeries.range()} width={1000}>
              <ChartRow height="200">
                <YAxis
                  id="revenue"
                  label="Revenue ($)"
                  min={0}
                  max={20000}
                  width="60"
                />
                <Charts>
                  <LineChart
                    axis="revenue"
                    series={revenueSeries}
                    columns={['value']}
                    style={{ value: { stroke: '#ff7f0e', strokeWidth: 2 } }}
                  />
                </Charts>
              </ChartRow>
            </ChartContainer>
          </Resizable>
        </div>
      </div>
    </div>
  );
}

export default DashboardCharts;
Enter fullscreen mode Exit fullscreen mode

Update your App.jsx:

// src/App.jsx
import React from 'react';
import DashboardCharts from './DashboardCharts';
import './App.css';

function App() {
  return (
    <div className="App">
      <DashboardCharts />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

This example demonstrates:

  • Time-series visualization
  • Multiple series
  • Dynamic time ranges
  • Interactive controls
  • Custom styling
  • Responsive design

Common Issues / Troubleshooting

  1. Chart not displaying: Make sure you've installed both react-timeseries-charts and pondjs. The charts require pondjs for data structures.

  2. Data format error: Ensure data is properly formatted as TimeSeries. Use new TimeSeries() with columns and points arrays.

  3. Time range not working: Set the timeRange prop on ChartContainer. Use series.range() to get the time range from your data.

  4. Styling issues: Customize colors and styles through the style prop. Use stroke and strokeWidth for line styling.

  5. Axis not showing: Configure YAxis with proper id, label, min, and max values. The axis id must match the chart axis prop.

  6. Performance: For large datasets, consider data aggregation or sampling. Time-series charts handle large datasets well, but optimization may be needed for very large datasets.

Next Steps

Now that you have a basic understanding of React Timeseries Charts:

  • Explore all available chart types
  • Learn about advanced time-series operations
  • Implement zooming and brushing
  • Add interactive tooltips
  • Create real-time updating charts
  • Learn about other time-series libraries
  • Check the official repository: https://github.com/esnet/react-timeseries-charts

Summary

You've successfully set up React Timeseries Charts in your React application and created time-series visualizations, dashboards, and interactive charts. React Timeseries Charts provides a powerful solution for visualizing time-based data with extensive customization options.

SEO Keywords

react-timeseries-charts
React time series
react-timeseries-charts tutorial
React time-based charts
react-timeseries-charts installation
React time series visualization
react-timeseries-charts example
React temporal charts
react-timeseries-charts setup
React time data charts
react-timeseries-charts customization
React chart component
react-timeseries-charts dashboard
React time series library
react-timeseries-charts getting started

Top comments (0)