Articles in this section
Category / Section

How to Pass a JSON array to a React Chart?

3 mins read

This article explains how to pass a JSON array to the React Chart.

Customize the data using JSON

You can link a chart to your dataset by providing a JSON array through the dataSource property in the SeriesDirective. Then, map the JSON fields to the chart axes by assigning them to the xName and yName properties. This enables the chart to render your JSON data effectively.

Example: Pass JSON Data to React Chart

Below is a complete example of how to bind a JSON array to a Column Chart using Syncfusion’s React Chart component.

Index.js

import { createRoot } from 'react-dom/client';
import * as React from 'react';
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, ColumnSeries, Category
} from '@syncfusion/ej2-react-charts';
import { data } from './data';

const Column = () => {
return (
      <div className="control-section">
       <ChartComponent
         id="charts"
         style={{ textAlign: 'center' }}
         primaryXAxis={{
           valueType: 'Category',
           interval: 1,
           labelPlacement: 'OnTicks',
         }}
         primaryYAxis={{
           minimum: 0,
           maximum: 65,
           interval: 5,
         }}
         chartArea={{ border: { width: 0 } }}
         load={load.bind(this)}
         width={Browser.isDevice ? '100%' : '75%'}
         loaded={loaded.bind(this)}
       >
         <Inject services={[ColumnSeries, Category]} />
         <SeriesCollectionDirective>
           <SeriesDirective
             dataSource={data}
             xName="x"
             yName="y"
             type="Column"
             fill="#febe17"
           />
         </SeriesCollectionDirective>
       </ChartComponent>
     </div>
   );
};
export default Column;

const root = createRoot(document.getElementById('sample'));
root.render(<column>);

JSON data

exports.data = [
 {
   x: 'USA',
   y: 46
 },
 {
   x: 'China',
   y: 36
 },
 {
   x: 'Japan',
   y: 63.5
 },
 {
   x: 'Australia',
   y: 54
 },
 {
   x: 'France',
   y: 50
 }
];

Output

The following screenshot shows the chart rendered using the JSON data above.

React chart json data

View Sample in Stackblitz

Conclusion

We hope you enjoyed learning about How to pass a JSON array to a React Chart.

You can refer to our React Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied