React Charts
raw JSON →React Charts is a declarative charting library for React, leveraging D3 and React-Spring for highly customizable and performant data visualizations. It's designed to be lightweight, fast, and fully responsive, offering a wide range of chart types including line, bar, area, and bubble charts. The library is currently in an active beta phase for its v3 major version, with frequent updates addressing bugs and adding new features, as evidenced by the recent `3.0.0-beta.*` releases. It differentiates itself by providing a robust API for custom tooltip rendering, axis configuration, and interactive elements, aiming for a balance between ease of use and advanced customization. The current stable release appears to be `2.0.0-beta.7`, but active development is focused on the `3.x` branch.
Common errors
error TypeError: Cannot read properties of undefined (reading 'width') or Chart does not render. ↓
<Chart /> component in a div with defined width and height styles, e.g., <div style={{ width: '100%', height: '300px' }}><Chart ... /></div>. error Error: `getValue` is required for all axes. ↓
primaryAxis and all secondaryAxes objects include a getValue function, e.g., primaryAxis: { getValue: datum => datum.primary }. error Module not found: Can't resolve 'react-charts' in '...' or Cannot find module 'react-charts'. ↓
npm install react-charts or yarn add react-charts. If using TypeScript, ensure react-charts is listed in your tsconfig.json's types or typeRoots if custom type declarations are used. Warnings
breaking Major breaking changes exist between v2 and v3. The API surface for `Chart` options, data structure, and axis configuration has been significantly refactored. ↓
gotcha React Charts v3 is still in beta. While actively developed, breaking changes can occur between beta versions, and the API might not be entirely stable. ↓
gotcha Charts require explicit width and height. If the parent container does not have defined dimensions, the chart may not render or will render with a size of 0x0. ↓
gotcha Incorrect `elementType` for secondary axes can lead to unexpected rendering or errors, especially when mixing chart types or not providing a default. ↓
Install
npm install react-charts yarn add react-charts pnpm add react-charts Imports
- Chart wrong
const Chart = require('react-charts')correctimport { Chart } from 'react-charts' - AxisOptions
import type { AxisOptions } from 'react-charts' - Series
import type { Series } from 'react-charts'
Quickstart
import React from 'react';
import { Chart } from 'react-charts';
function MyChart() {
const data = React.useMemo(
() => [
{
label: 'Series 1',
data: [
{ primary: 'Apr', secondary: 10 },
{ primary: 'May', secondary: 15 },
{ primary: 'Jun', secondary: 12 },
{ primary: 'Jul', secondary: 18 },
],
},
{
label: 'Series 2',
data: [
{ primary: 'Apr', secondary: 5 },
{ primary: 'May', secondary: 7 },
{ primary: 'Jun', secondary: 9 },
{ primary: 'Jul', secondary: 11 },
],
},
],
[]
);
const primaryAxis = React.useMemo(
() => ({ getValue: datum => datum.primary }),
[]
);
const secondaryAxes = React.useMemo(
() => [{
getValue: datum => datum.secondary,
elementType: 'line'
}],
[]
);
return (
<div style={{ width: '400px', height: '300px' }}>
<Chart
options={{
data,
primaryAxis,
secondaryAxes,
}}
/>
</div>
);
}
export default MyChart;