To render a basic chart in React, use the Chart component from react-charts. You must provide two primary props:
data: An array of series objects. Each object contains a label and a data array of coordinate pairs (e.g., [x, y]).axes: An array of axis configurations. Each axis object defines its type (e.g., 'linear'), position (e.g., 'bottom', 'left'), and whether it is the primary axis.
The Chart component is hyper-responsive and will automatically fill the available space of its parent container. Ensure the parent element has defined dimensions (width and height) via CSS or inline styles.
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' },
],
[]
)
return (
<div
style={{
width: '400px',
height: '300px',
}}
>
<Chart data={data} axes={axes} />
</div>
)
}