yes
. However, this answer is not giving you a complete picture of any React chart. While you can create a charts in React with the Chart.js component, it may lack many of the desirable features that you get with a more powerful charting library. That React chart library is FusionCharts. In this guide, we’ll show you how to create a React chart with both Chart.js and FusionCharts. This blog will convince you why you should prefer FusionCharts over Chart.js because of its many unique and great features. Table of Contents
npm i react-chartjs-2 chart.js
npx create-react-app chart-projectThe above command will create a new React project called
chart-project
in a directory with the same name. Switch to this directory and run the app by typing: npm start
App.js
file in the src
directory. In this file, specify the various properties of the bar chart as shown below. import React from 'react'; import {Bar} from 'react-chartjs-2'; const state = { labels: ['North America', 'South America', 'Asia', 'Europe', 'Africa', 'Australia'], datasets: [ { label: 'Land area', backgroundColor: 'rgba(175,25,192,1)', borderColor: 'rgba(0,20,0,1)', borderWidth: 2, data: [16, 12, 1, 1, 1, 1] } ] } export default class App extends React.Component { render() { return ( <div> <Bar data={state} options={{ title:{ display:true, text:'Percentage of Land Area (from Enchanted Learning)', fontSize:20 }, legend:{ display:true, position:'right' } }} /> </div> ); } }
index.js
file, delete all its contents, and paste the following contents in this file. import React from 'react'; import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );Now if you refresh your browser at the page https://localhost:3000/, your bar chart created with Chart.js will show up.
App.js
file as described below. App.js
file in the src
folder, delete its contents, and paste the following code to it: // *** Include Dependencies *** // Include react import React from "react"; import ReactDOM from "react-dom"; // Include the react-fusioncharts component import ReactFC from "react-fusioncharts"; // Include the fusioncharts library import FusionCharts from "fusioncharts"; // Include the chart type import Column2D from "fusioncharts/fusioncharts.charts"; // Include the theme as fusion import FusionTheme from "fusioncharts/themes/fusioncharts.theme.fusion"; // Adding the chart and theme as dependency to the core fusioncharts // You can omit the components you don't need (column2D, FusionMaps, World) ReactFC.fcRoot(FusionCharts, Column2D, FusionTheme); // *** Add data source *** const continentData = [ { id: "NA", label: "North America", value: "16.3", showLabel: "1" }, { id: "SA", label: "South America", value: "12.0", showLabel: "1" }, { id: "AS", label: "Asia", value: "30.0", showLabel: "1" }, { id: "EU", label: "Europe", value: "6.7", showLabel: "1" }, { id: "AF", label: "Africa", value: "20.3", showLabel: "1" }, { id: "AU", label: "Australia", value: "5.2", showLabel: "1" } ]; // *** Add JSON object for the React Graph configurations *** const reactGraphConfigs = { type: "column2d", // The chart type width: "700", // Width of the chart height: "600", // Height of the chart dataFormat: "json", // Data type dataSource: { // Chart Configuration chart: { caption: "Percentage of Land Area on Planet Earth", subCaption: "Data Source: Enchanted Learning", xAxisName: "Continent", yAxisName: "Percentage Land Area", numberSuffix: "%", theme: "fusion" }, // Chart Data data: continentData } }; // *** Creating the DOM element to pass the react-fusioncharts component *** class App extends React.Component { render() { //replace by reactGraphConfigs to display the bar chart return (<ReactFC {...reactGraphConfigs} />); } } export default App;As you can see from the code, it is very easy to create a React chart using FusionCharts. All you have to do is specify the data points, along with the various chart configurations. The above code will render the following chart:
chart
JSON as: theme: "fusion"This displays the same chart using a different theme as shown below:
Ever had a data set that seemed more complicated than a Rubik's cube? You’re not…
We’ve all seen them in textbooks or presentations—those overlapping circles that simplify complex information into…
We’re excited to announce the upcoming release of FusionCharts v4.1—a groundbreaking step forward in the…
Have you ever been overwhelmed by a massive data set and wondered, "How do I…
If you’ve ever tried to make sense of the stock market, you’ve probably come across…
Imagine you’re comparing the sales performance of your top product lines across different regions, or…