React Charts: Simple, Immersive, Interactive Data Visualization (2024)

Simple, immersive and interactive charts for React

Enjoy this library? Try them all! React Table, React Query, React Form

Become a Sponsor

Features

  • Line, Bar, Bubble, & Area Charts
  • Hyper Responsive
  • Powered by D3
  • Fully Declarative
  • Flexible data model

Github Sponsors

This library is being built and maintained by me, @tannerlinsley and I am always in need of more support to keep this project afloat. If you would like to get additional support, add your logo or name on this README, or simply just contribute to my open source Sponsorship goal, visit my Github Sponsors page!

Intro

React Charts is currently in beta! This means:

  • The existing API is mostly stable. Expect only subtle changes/additions as use-cases become polished.
  • It's safe for most production sites, as long as you lock in the alpha version.

Installation

$ yarn add react-charts# or$ npm i react-charts --save

Quick Start

React

This will render a very basic line chart:

import React from 'react'import { Chart } from 'react-charts'function MyChart() { const data = React.useMemo( () => [ { label: 'Series 1', data: [ [0, 1], [1, 2], [2, 4], [3, 2], [4, 7], ], }, { label: 'Series 2', data: [ [0, 3], [1, 1], [2, 5], [3, 6], [4, 4], ], }, ], [] ) const axes = React.useMemo( () => [ { primary: true, type: 'linear', position: 'bottom' }, { type: 'linear', position: 'left' }, ], [] ) const lineChart = ( // A react-chart hyper-responsively and continuously fills the available // space of its parent element automatically <div style={{ width: '400px', height: '300px', }} > <Chart data={data} axes={axes} /> </div> )}

Documentation

Complete documentation is coming soon. The most detailed usage examples are visible by browsing the website's examples.

Any sparse documentation available in this Readme is being progressively improved as the API evolves.

API

React Charts exposes these top-level exports:

  • Chart - The Chart component used to render charts
  • Series Type Components
    • Line
    • Bar
    • Bubble
    • Area
  • Curve Functions
    • curveBasisClosed
    • curveBasisOpen
    • curveBasis
    • curveBundle
    • curveCardinalClosed
    • curveCardinalOpen
    • curveCardinal
    • curveCatmullRomClosed
    • curveCatmullRomOpen
    • curveCatmullRom
    • curveLinearClosed
    • curveLinear
    • curveMonotoneX
    • curveMonotoneY
    • curveNatural
    • curveStep
    • curveStepAfter
    • curveStepBefore
  • Position Constants
    • positionTop
    • positionRight
    • positionBottom
    • positionLeft
  • Grouping Constants
    • groupingSingle
    • groupingSeries
    • groupingPrimary
    • groupingSecondary
  • Tooltip Alignment Constants
    • alignAuto
    • alignRight
    • alignTopRight
    • alignBottomRight
    • alignLeft
    • alignTopLeft
    • alignBottomLeft
    • alignTop
    • alignBottom
  • Axis Type Constants
    • axisTypeOrdinal
    • axisTypeTime
    • axisTypeUtc
    • axisTypeLinear
    • axisTypeLog
  • Tooltip Anchor Constants
    • anchorPointer
    • anchorClosest
    • anchorCenter
    • anchorTop
    • anchorBottom
    • anchorLeft
    • anchorRight
    • anchorGridTop
    • anchorGridBottom
    • anchorGridLeft
    • anchorGridRight
  • Focus Mode Constants
    • focusAuto
    • focusClosest
    • focusElement

Memoize your Props!

As you'll see in every example, the React Charts <Chart> component expects all props and options to be memoized using either React.useMemo or React.useCallback. While passing an unmemoized option/prop to the <Chart> component won't severly break any visible functionality, your charts will be severly non-performant. Internally, React Charts uses the immutable nature of thes options/props to detect changes to the configuration and update accordingly.

While this may feel heavy at first, it gives you, the dev, full control over when you want to update your charts. To trigger and update, simply trigger one of your React.useMemo or React.useCallback hooks on the part of the config that you would like to update!

Data Model

React Charts uses a common and very flexible data model based on arrays of series and arrays of datums. You can either use the model defaults directly, or use data accessors to materialize this structure.

Typical visualization data can come in practically any shape and size. The following examples show data structures that are all reasonably equivalent at some level since they each contain an array of series[] and datums[]. They also show how to parse that data.

In the following example, there is no need to use any accessors. The default accessors are able to easily understand this format:

function MyChart() { const data = React.useMemo( () => [ { label: 'Series 1', data: [ { x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }, ], }, { label: 'Series 2', data: [ { x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }, ], }, { label: 'Series 3', data: [ { x: 1, y: 10 }, { x: 2, y: 10 }, { x: 3, y: 10 }, ], }, ], [] ) const axes = React.useMemo( () => [ { primary: true, type: 'linear', position: 'bottom' }, { type: 'linear', position: 'left' }, ], [] ) return ( <div style={{ width: '400px', height: '300px', }} > <Chart data={data} axes={axes} /> </div> )}

In the following example, there is no need to use any accessors. The default accessors are able to easily understand this format, but please note that this format limits you from passing any meta data about your series and datums.

function MyChart() { const data = React.useMemo( () => [ [ [1, 10], [2, 10], [3, 10], ], [ [1, 10], [2, 10], [3, 10], ], [ [1, 10], [2, 10], [3, 10], ], ], [] ) const axes = React.useMemo( () => [ { primary: true, type: 'linear', position: 'bottom' }, { type: 'linear', position: 'left' }, ], [] ) return ( <div style={{ width: '400px', height: '300px', }} > <Chart data={data} axes={axes} /> </div> )}

Data Accessors

When data isn't in a convenient format for React Charts, your first instinct will be to transform your data into the above formats. Don't do that! There is an easier way 🎉 We can use the Chart components' accessor props to point things in the right direction. Accessor props pass the original data and the series/datums you return down the line to form a new data model. See the <Chart> component for all available accessors.

In the following example, the data is in a very funky format, but at it's core is the same as the previous examples.

function MyChart() { // Use any data object you want const originalData = React.useMemo( () => ({ axis: [1, 2, 3], lines: [ { data: [{ value: 10 }, { value: 10 }, { value: 10 }] }, { data: [{ value: 10 }, { value: 10 }, { value: 10 }] }, { data: [{ value: 10 }, { value: 10 }, { value: 10 }] }, ], }), [] ) // Make data.lines represent the different series const data = React.useMemo(data => originalData.lines, [originalData]) // Use data.lines[n].data to represent the different datums for each series const getDatums = React.useCallback(series => series.data, []) // Use the original data object and the datum index to reference the datum's primary value. const getPrimary = React.useCallback( (datum, i, series, seriesIndex, data) => originalData.axis[i], [] ) // Use data.lines[n].data[n].value as each datums secondary value const getSecondary = React.useCallback(datum => datum.value, []) return ( <div style={{ width: '400px', height: '300px', }} > <Chart data={data} getSeries={getSeries} getDatums={getDatums} getPrimary={getPrimary} getSecondary={getSecondary} /> </div> )}

Series Labels

Multiple series are often useless without labels. By default, React Charts looks for the label value on the series object you pass it. If not found, it will simply label your series as Series [n], where [n] is the zero-based index of the series, plus 1.

If the default label accessor doesn't suit your needs, then you can use the <Chart> component's getLabel accessor prop:

function MyChart() { const data = React.useMemo( () => [ { specialLabel: 'Hello World!', data: [ //... ], }, ], [] ) const getLabel = React.useCallback(series => series.specialLabel, []) return ( <div style={{ width: '400px', height: '300px', }} > <Chart data={data} getLabel={getLabel} /> </div> )}

Axes & Scales

React Charts supports an axes prop that handles both the underlying scale and visual rendering. These axes can be combined and configured to plot data in many ways. To date, we have the following scale types available:

  • Cartesian
    • linear - A continuous axis used for plotting numerical data on an evenly distributed scale. Works well both as a primary and secondary axis.
    • ordinal - A banded axis commonly used to plot categories or ordinal information. Works well as the primary axis for bar charts.
    • time - A continuous axis used for plotting localized times and dates on an evenly distributed scale. Works well as a primary axis.
    • utc - Similar to the time scale, but supports UTC datetimes instead of localized datetimes. Works well as a primary axis.
    • log - A continuous axis used for plotting numerical data on a logarithmically distributed scale. Works well as a secondary axis

Download Details:

Author: TanStack

Official Github: https://github.com/TanStack/react-charts

License: MIT

#typescript #react

React Charts: Simple, Immersive, Interactive Data Visualization (2024)

FAQs

Is React good for data visualization? ›

React chart libraries have gained popularity due to their ability to offer powerful data visualization tools for developers, making it easier to create interactive and visually appealing charts, with a wide array of options available, such as: Recharts. Chartjs. React-chartjs-2.

What is the best React data visualization library? ›

What is the best React charting library? Answer: Some of the finest and most popular react charting libraries for executing data visualization projects are recharts, react-chartjs-2, Victory, visx, nivo, react-vis, BizCharts, Rumble Charts, ant design charts, react-gauge chart, and echart for react, to name a few.

How do you use React charts? ›

The Future of Data Visualization: Exploring Echarts for React Development
  1. How to Install and Import ECharts for React. ...
  2. Installing ECharts for React. ...
  3. Importing ECharts into Your React Component. ...
  4. Creating Your First Chart with ECharts for React. ...
  5. Define the Options Object. ...
  6. Render the ReactECharts Component.
Feb 8, 2024

What is the schedule chart in React? ›

The React Gantt Chart is a project planning and management tool used to display and manage hierarchical tasks with timeline details. It helps assess how long a project should take, determine the resources needed, manage the dependencies between tasks, and plan the order in which the tasks should be completed.

When should you not use React? ›

When you are making an app like a game or a demanding creative app, React is not the best choice. This problem stems from the fact that it uses a Virtual DOM. Virtual DOMs, or VDOMs, are layers that help make unoptimized DOM manipulations faster.

What is the best programming language for interactive data visualization? ›

Data Analysts typically need a language that's intuitive to learn, easy to work with, has interactive capabilities, and includes libraries that are suited to creating dynamic data visualizations. Five of the most popular programming languages in 2021 for Data Analysts are Python, SQL, R, JavaScript, and Scala.

What are the best looking charts for React? ›

Best Charts Libraries in React JS
  • React-Chartjs-2: Bringing Chart. js to React. ...
  • Recharts: A Composable Charting Library. Recharts takes a composable approach to building charts in React. ...
  • Victory: Declarative Charts for React. ...
  • React-vis: Uber's Visualization Components. ...
  • Nivo: The Data Visualization Swiss Army Knife.
Dec 30, 2023

What is the most popular data visualization tool? ›

Some of the best data visualization tools include Google Charts, Tableau, Grafana, Chartist, FusionCharts, Datawrapper, Infogram, and ChartBlocks etc.

What is the world's most popular React UI library? ›

Ant Design is a well-documented React UI library with high-quality React components. It is a popular React library and one of the most used libraries, with over 87k stars on GitHub and more than 1 million weekly downloads on NPM. Ant Design is an excellent choice if you want to build enterprise-grade products.

How do I show charts in React JS? ›

js import BarChart from "./components/BarChart"; import LineChart from "./components/LineChart"; import PieChart from "./components/PieChart"; function App() { return ( <div> <LineChart /> <BarChart /> <PieChart /> </div> ); } export default App; After running your React app, you can see charts being displayed.

Where should I use React? ›

React is a JavaScript library used for building fast and scalable user interfaces for websites and applications. It allows developers to create large web applications that can change data without reloading a page. React focuses on building the view layer of the website—the parts people see and interact with.

What is the time picker in React? ›

Time Picker is a versatile component that can be customized to fit the needs of your React app. For example, you can set a default time, specify a time range, or even disable the time picker in certain situations. The time values selected by the user are easy to manage, making it a valuable tool for any React app.

Does React have a calendar? ›

A React calendar component is a React component that enables a user to explore a calendar (typically the Gregorian calendar) and pick a date or date range. Calendar components typically account for leap years and showcase days of the week to the user.

How do you visualize data in ReactJS? ›

React has a number of great charting options to choose from. Not all have the same charts available, but most touch on the staples, such as line, bar, and pie charts. We'll be focusing on an area chart, which is similar to a line chart, but with the area underneath the line shaded in.

Do data scientists use React? ›

The work of a web developer centers on coding languages like HTML, CSS, and JavaScript, as well as frameworks like React and Angular. Programming Languages: Data scientists generally use languages like Python and R optimized for statistical analysis, visualization, and machine learning model building.

Which tool is good for data visualization? ›

Some of the best data visualization tools include Google Charts, Tableau, Grafana, Chartist, FusionCharts, Datawrapper, Infogram, and ChartBlocks etc.

Does chart JS work with React? ›

Chart.js comes with built-in TypeScript typings and is compatible with all popular JavaScript frameworks including React , Vue , Svelte , and Angular .

Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6050

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.