Days
Hours
Minutes
Seconds
x

Froala Editor v4.2.0 is Here LEARN MORE

Skip to content

Create a Chart in React

Froala Charts is a JavaScript charting library that enables you to create interactive charts, gauges, maps and dashboards in JavaScript. We have built a simple and lightweight React component that provides bindings for Froala Charts. The react-froalacharts component allows you to easily add rich and interactive charts to any React project.

On this page, you'll see how to install Froala Charts and render a chart using the react-froalaCharts component.

Setting up a React project

You need to have a react project setup before proceeding any further. If not, you can follow the below steps to initiate the project.

Follow the steps given to initiate the project. One of the best ways to set up the development environment is using create-react-app. Find more about it here.

Open the terminal and run:

npx create-react-app first-froalacharts-project
cd first-froalacharts-project
npm start

first-froalacharts-project is the working directory where React Boilerplate will be installed along with all the utilities and dependencies.

The working directory should contain a package.json. If the package is not present, you can create it using the npm init -y command.

Now, open http://localhost:3000/ to see your React app.

Installation and including dependencies

Install the react-froalacharts and froalacharts modules using the following command:

npm install froalacharts react-froalacharts --save

After installing the froalacharts components, you can replace the code in App.js file with the code shown in the steps below to create your first chart. Import all the required dependencies to get started.

// Step 1 - Include react
import React from "react";

// Step 2 - Include the react-froalacharts component
import ReactFC from "react-froalacharts";

// Step 3 - Include the froalacharts library
import FroalaCharts from "froalacharts";

// Step 4 - Include the chart type
import Column from "froalacharts/froalacharts.charts";

// Step 5 - Include the theme as froala
import FroalaTheme from "froalacharts/themes/froalacharts.theme.froala";

// Step 6 - Adding the chart and theme as dependency to the core froalacharts
ReactFC.fcRoot(FroalaCharts, FroalaTheme);

Preparing the data

Let's create a chart showing a "Comparison of Quarterly Revenue".

Quarter Revenues
Q1 10000
Q2 11500
Q3 12500
Q4 15000

Since we are plotting a single dataset, create a column chart with 'Quarter' as data labels along the x-axis and 'Revenues (In USD)' as data values along y-axis.

Froala Charts accepts the data in JSON format. The data for the chart is represented in the following way:

const chartData = [
{
  data: [
  {
    value: "10000",
  },
  {
    value: "11500",
  },
  {
    value: "12500",
  },
  {
    value: "15000",
  }]
}];

Configure your chart

Next, work on the styling, positioning and giving your chart a context.

// Create a JSON object to store the chart configurations
const chartConfig = {
  //Specify the chart type
  type: "column",
  //Set the container object
  renderAt: "ft",
  //Specify chart dimensions
  width: "800",
  height: "500",
  //Set the type of data
  dataSource: {
    chart: {
      //Set the theme for your chart
      theme: "froala",
      //Set the chart caption
      caption: "Comparison of Quarterly Revenue",
      //Set the x-axis name
      xAxisname: "Quarter",
      //Set the y-axis name
      yAxisName: "Revenues (In USD)",
      numberPrefix: "$",
    },
    categories: [
      {
        category: [
          {
            label: "Q1",
          },
          {
            label: "Q2",
          },
          {
            label: "Q3",
          },
          {
            label: "Q4",
          }
        ]
      }
    ],
    dataset: chartData
  }
};

Render the chart

The consolidated code to render the chart is shown below:

//Step 1 - Include react
import React from "react";

//Include the react-froalacharts component
import ReactFC from "react-froalacharts";

//Include the froalacharts library
import FroalaCharts from "froalacharts";

//Include the chart type
import Column from "froalacharts/froalacharts.charts";

//Include the theme as froala
import FroalaTheme from "froalacharts/themes/froalacharts.theme.froala";

//Adding the chart and theme as dependency to the core froalacharts
ReactFC.fcRoot(FroalaCharts, FroalaTheme);

//Step 2 - Preparing the chart data
const chartData = [
{
  data: [
  {
    value: "10000",
  },
  {
    value: "11500",
  },
  {
    value: "12500",
  },
  {
    value: "15000",
  }]
}];

//Step 3 - Create a JSON object to store the chart configurations
const chartConfig = {
  
  //Specify the chart type
  type: 'column',
  
  //Set the container object
  renderAt: 'ft',
  
  //Specify chart dimensions
  width: '800',
  height: '500',
  
  //Set the type of data
  dataSource:
  {
    chart:
    {
      //Set the theme for your chart
      theme: "froala",
      //Set the chart caption
      caption: "Comparison of Quarterly Revenue",
      //Set the x-axis name
      xAxisname: "Quarter",
      //Set the y-axis name
      yAxisName: "Revenues (In USD)",
      numberPrefix: "$"
    },
    categories: [
    {
      category: [
      {
        label: "Q1"
      },
      {
        label: "Q2"
      },
      {
        label: "Q3"
      },
      {
        label: "Q4"
      }]
    }],
  }
};

//Step 4 - Creating the DOM element to pass the react-froalacharts component
class App extends React.Component
  {
    render() {
      return (<ReactFC {...chartConfigs} />);
      }
    }
    export default App;

See your chart

You should be able to see the chart as shown below.

Froala Charts will load here!

Next Steps

Explore the List of Charts.

Dive into the API documentation.

Learn about other Concepts.

[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_1380113973" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>
[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_327685422" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>
[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_1667853760" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>