Create a Chart in React using FusionCharts

Overview

FusionCharts 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 which provides bindings for FusionCharts. The react-fusioncharts component allows you to easily add rich and interactive charts to any React project.

In this page, we'll see how to install FusionCharts and render a chart using the react-fusionCharts component.

Installation

Install FusionCharts and the react-fusioncharts component using any of the following methods:

To install fusioncharts and the react-fusioncharts component via npm follow the steps below:
1. Install the react-fusioncharts module

    $ npm install react-fusioncharts --save
2. Install the fusioncharts JS files

    $ npm install fusioncharts --save
To install the FusionCharts Suite and the react-fusioncharts component follow the steps below:
  1. Include the React core library.
  2. Include Babel for JSX transpiling.
  3. Include the FusionCharts JavaScript files from CDN.
  4. Include the react-fusioncharts module.
  5. Include the theme file.
The consolidated code is shown below:

<head>
    <!-- Step 1 - Including react -->
    <script type="text/javascript" src=" https://unpkg.com/[email protected]/umd/react.development.js"></script> 
    <script type="text/javascript" src=" https://unpkg.com/[email protected]/umd/react-dom.development.js"></script> 
    <!-- Step 2 - Including Babel for JSX transpiling -->
    <script type="text/javascript" src=" https://unpkg.com/[email protected]/babel.min.js"></script> 
    <!-- Step 3 - Including the fusioncharts core library -->
    <script type="text/javascript" src=" http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script> 
    <!-- Step 4 - Including the react-fusioncharts component-->
    <script type="text/javascript" src=" https://unpkg.com/[email protected]/dist/react-fusioncharts.min.js"></script> 
    <!-- Step 5 - Including the fusion theme -->
    <script type="text/javascript" src=" http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.theme.fusion.js"></script> 
</head>
To install the FusionCharts Suite and the react-fusioncharts component follow the steps below:
  1. Include the React core library.
  2. Include Babel for JSX transpiling.
  3. Include the FusionCharts JavaScript files, which can be downloaded from here .
  4. Include the react-fusioncharts component.
  5. Include the FusionCharts theme file to apply style to the charts.
The consolidated code is shown below:

<head>
    <!-- Step 1 - Including react  -->
    <script type="text/javascript" src="path/to/local/react.js"></script>
    <script type="text/javascript" src="path/to/local/react-dom.js"></script>
    <!-- Step 2 - Including Babel for JSX transpiling -->
    <script type="text/javascript" src="path/to/local/babel-core.js"></script>
    <!-- Step 3 - Including the fusioncharts core library -->
    <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
    <!-- Step 4 - Including the react-fusioncharts component -->
    <script type="text/javascript" src="path/to/local/react-fusioncharts.js"></script>
    <!-- Step 5 - Including the fusion theme -->
    <script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
</head>

That completes the installation of FusionCharts and the react-fusioncharts component.

Create your first chart

Let's create a Column 2D chart using the react-fusioncharts component showing the "Countries With Most Oil Reserves".

FusionCharts Suite has 95+ chart types for you to explore. Find the complete list of chart types here .

The Column 2D chart is shown below:

FusionCharts will load here..

The data for the above chart is shown in the table below:

Country No. of Oil Reserves
Venezuela 290
Saudi 260
Canada 180
Iran 140
Russia 115
UAE 100
US 30
China 30

Convert tabular data into JSON format

Now that you have the tabular data ready, it's time to convert it into JSON format, as FusionCharts accepts data in JSON or XML format. In this example, we will use the JSON format, as shown below:

{
    // Chart Configuration
    "chart": {
        "caption": "Countries With Most Oil Reserves [2017-18]",
        "subCaption": "In MMbbl = One Million barrels",
        "xAxisName": "Country",
        "yAxisName": "Reserves (MMbbl)",
        "numberSuffix": "K",
        "theme": "fusion",
    },
    // Chart Data
    "data": [{
        "label": "Venezuela",
        "value": "290"
    }, {
        "label": "Saudi",
        "value": "260"
    }, {
        "label": "Canada",
        "value": "180"
    }, {
        "label": "Iran",
        "value": "140"
    }, {
        "label": "Russia",
        "value": "115"
    }, {
        "label": "UAE",
        "value": "100"
    }, {
        "label": "US",
        "value": "30"
    }, {
        "label": "China",
        "value": "30"
    }]
}

Different types of charts in FusionCharts expect different JSON formats, based on their grouping. Explore different JSON formats, for example, single-series , multi-series , combination charts.

In the above JSON data:

  • Create the chart object to define the elements of the chart.

  • Specify the label and value of each column within the data array.

Both the chart object and the data array contain a set of key-value pairs known as attributes. These attributes are used to set the functional and cosmetic properties of the chart.

Now that you have converted the tabular data to JSON format, let's see how to render the chart.

Render the chart

To render the chart, follow the steps below:

  1. Include react

  2. Include react-fusioncharts

  3. Include the fusioncharts library

  4. Include the chart type

  5. Include the FusionCharts theme file to apply the style to the charts

  6. Add the chart and the theme as a dependency to the core

  7. Store the chart configurations in a JSON object. In this JSON object:

    • Set the chart type as column2d. Each chart type is represented with a unique chart alias. For Column 2D chart, the alias is column2d. Find the complete list of chart types with their respective alias here .
    • Set the width and height (in pixels).
    • Set the dataFormat as JSON.
    • Embed the json data as the value of the dataSource.
  8. Create the DOM element and pass the react-fusioncharts component directly to the ReactDOM.render() method.

The consolidated code is shown below:


// Step 1 - Including react
import React from 'react';
import ReactDOM from 'react-dom';

// Step 2 - Including the react-fusioncharts component
import ReactFC from 'react-fusioncharts';

// Step 3 - Including the fusioncharts library
import FusionCharts from 'fusioncharts';

// Step 4 - Including the chart type
import Column2D from 'fusioncharts/fusioncharts.charts';

// Step 5 - Including the theme as fusion
import FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion';

// Step 6 - Adding the chart as dependency to the core fusioncharts
ReactFC.fcRoot(FusionCharts, Column2D, FusionTheme);

// Step 7 - Creating the JSON object to store the chart configurations
const chartConfigs = {
    type: 'column2d',// The chart type
    width: '700', // Width of the chart
    height: '400', // Height of the chart
    dataFormat: 'json', // Data type
    dataSource: { 
        // Chart Configuration 
        "chart": {
            "caption": "Countries With Most Oil Reserves [2017-18]",
            "subCaption": "In MMbbl = One Million barrels",
            "xAxisName": "Country",
            "yAxisName": "Reserves (MMbbl)",
            "numberSuffix": "K",
            "theme": "fusion",
        },
        // Chart Data
        "data": [{
            "label": "Venezuela",
            "value": "290"
        }, {
            "label": "Saudi",
            "value": "260"
        }, {
            "label": "Canada",
            "value": "180"
        }, {
            "label": "Iran",
            "value": "140"
        }, {
            "label": "Russia",
            "value": "115"
        }, {
            "label": "UAE",
            "value": "100"
        }, {
            "label": "US",
            "value": "30"
        }, {
            "label": "China",
            "value": "30"
        }]
    }
};

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

export default App
To include the specific chart types, individually add the following files using import
  • PowerCharts - fusioncharts/fusioncharts.powercharts
  • Widgets - fusioncharts/fusioncharts.widgets
  • Gantt - fusioncharts/fusioncharts.gantt
  • Treemap - fusioncharts/fusioncharts.treemap
  • Zoomscatter - fusioncharts/fusioncharts.zoomscatter
  • Zoomline - fusioncharts/fusioncharts.zoomline
  • Overlapped Bar - fusioncharts/fusioncharts.overlappedbar2d
  • Overlapped Column - fusioncharts/fusioncharts.overlappedcolumn2d

<html>
<head>
    <!-- Including react -->
    <script type="text/javascript" src="  https://unpkg.com/[email protected]/umd/react.development.js"></script> 
    <!-- Including react-dom -->
    <script type="text/javascript" src="  https://unpkg.com/[email protected]/umd/react-dom.development.js"></script> 
    <!-- Including babel -->
    <script type="text/javascript" src="  https://unpkg.com/[email protected]/babel.min.js"></script> 
    <!-- Including the fusioncharts core library -->
    <script type="text/javascript" src="  http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script> 
    <!-- Including the fusioncharts library to render charts -->
    <script type="text/javascript" src="  http://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.charts.js"></script> 
    <!-- Including react-fusioncharts component -->
    <script type="text/javascript" src="  https://unpkg.com/[email protected]/dist/react-fusioncharts.min.js"></script> 
    <!-- Including the fusion theme -->
    <script type="text/javascript" src="  http://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script> 
    <script type="text/jsx">
    ReactFC.fcRoot(FusionCharts);
    const chartConfigs = {
        type: 'column2d',
        renderAt: 'chart-container',
        width: '700',
        height: '400',
        dataFormat: 'json',
        dataSource: {
            // Chart Configuration
            "chart": {
                "caption": "Countries With Most Oil Reserves [2017-18]",
                "subCaption": "In MMbbl = One Million barrels",
                "xAxisName": "Country",
                "yAxisName": "Reserves (MMbbl)",
                "numberSuffix": "K",
                "theme": "fusion",
            },
            // Chart Data
            "data": [{
                "label": "Venezuela",
                "value": "290"
            }, {
                "label": "Saudi",
                "value": "260"
            }, {
                "label": "Canada",
                "value": "180"
            }, {
                "label": "Iran",
                "value": "140"
            }, {
                "label": "Russia",
                "value": "115"
            }, {
                "label": "UAE",
                "value": "100"
            }, {
                "label": "US",
                "value": "30"
            }, {
                "label": "China",
                "value": "30"
            }]
        }
    };
    </script>
    <script type="text/jsx">
    ReactDOM.render(
        <ReactFC {...chartConfigs} />,
        document.getElementById('chart-container')
    );
    </script>
</head>
<body>
    <div id='chart-container'></div>
</body>
</html>

<html>
<head>
    <!-- Including react -->
    <script type="text/javascript" src="path/to/local/react.development.js"></script>
    <!-- Including react-dom -->
    <script type="text/javascript" src="path/to/local/react-dom.development.js"></script>
    <!-- Including babel -->
    <script type="text/javascript" src="path/to/local/babel.min.js"></script>
    <!-- Including the fusioncharts core library -->
    <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
    <!-- Including the fusioncharts library to render charts -->
    <script type="text/javascript" src="path/to/local/fusioncharts.charts.js"></script>
    <!-- Including react-fusioncharts component -->
    <script type="text/javascript" src="path/to/local/react-fusioncharts.min.js"></script>
    <!-- Including the fusion theme -->
    <script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
    <script type="text/jsx">
    ReactFC.fcRoot(FusionCharts);
    const chartConfigs = {
        type: 'column2d',
        renderAt: 'chart-container',
        width: '700',
        height: '400',
        dataFormat: 'json',
        dataSource: {
            // Chart Configuration
            "chart": {
                "caption": "Countries With Most Oil Reserves [2017-18]",
                "subCaption": "In MMbbl = One Million barrels",
                "xAxisName": "Country",
                "yAxisName": "Reserves (MMbbl)",
                "numberSuffix": "K",
                "theme": "fusion",
            },
            // Chart Data
            "data": [{
                "label": "Venezuela",
                "value": "290"
            }, {
                "label": "Saudi",
                "value": "260"
            }, {
                "label": "Canada",
                "value": "180"
            }, {
                "label": "Iran",
                "value": "140"
            }, {
                "label": "Russia",
                "value": "115"
            }, {
                "label": "UAE",
                "value": "100"
            }, {
                "label": "US",
                "value": "30"
            }, {
                "label": "China",
                "value": "30"
            }]
        }
    };
    </script>
    <script type="text/jsx">
    ReactDOM.render(
        <ReactFC {...chartConfigs} />,
        document.getElementById('chart-container')
    );
    </script>
</head>
<body>
    <div id='chart-container'></div>
</body>
</html>

That's it! Your first chart using react-fusioncharts is ready.

Problem rendering the chart?

In case there is an error, and you are unable to see the chart, check for the following:

  • If you are getting a JavaScript error on your page, check your browser console for the exact error and fix accordingly. If you're unable to solve it, click here to get in touch with our support team.

  • If the chart does not show up at all, but there are no JavaScript errors, check if the FusionCharts Suite XT JavaScript library has loaded correctly. You can use developer tools within your browser to see if fusioncharts.js was loaded.

  • If you get a Loading Data or Error in loading data message, check whether your JSON data structure is correct, or there are conflicts related to quotation marks in your code.

Was this article helpful to you ?