Create a Chart using FusionCharts

FusionCharts Suite XT — the industry's most comprehensive JavaScript charting solution — is all about easing the whole process of data visualization through charts.

In this page, we'll see how to install FusionCharts library and all the other dependencies on your system and render a chart using Plain JavaScript.

Installation

Install FusionCharts using any of the following steps:

Prerequisites
  1. Before installing fusioncharts package via npm, make sure you have Node.js installed in your system.
  2. Make sure you have a bundler like webpack and parcel or have a browserify installed in your system.
Create a project folder using the following command:
$ mkdir projectName
Get inside the directory using cd command as shown below:
$ cd projectName
Now, to install the fusioncharts package via npm run the command below:
$ npm install fusioncharts
To install the FusionCharts Suite follow the steps below:
  1. Include the FusionCharts JavaScript files from CDN.
  2. Include the theme file.
The code is shown below:

<head>
    <!-- Step 1 - Including the fusioncharts core library -->
    <script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
    <!-- Step 2 - Including the fusion theme -->
    <script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>

To install the FusionCharts Suite follow the steps below:
  1. Include the FusionCharts JavaScript files, which can be downloaded from here.
  2. Include the FusionCharts theme file to apply style to the charts.
The code is shown below:

<head>
    <!-- Step 1 - Including the fusioncharts core library -->
    <script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
    <!-- Step 2 - 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 Suite.

Create your first chart

Let's create a Column 2D chart 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..

To understand the chart components, click here.

Chart data

The data to render the above chart is shown in the table below:

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

FusionCharts accepts data in JSON format. Following code is the JSON representation of the above table with the required attributes to render the above chart.

{
  // 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.

  • Set the caption and subcaption of the chart.

  • Set the value of xAxisName attribute to Country(first column of the table).

  • Set the value of yAxisName attribute to Reserves(second column of the table).

  • In the data array, create objects for each row and specify the label attribute to represent the Country. For example, Venezuela.

  • Similarly, specify the value attribute to set the value of Oil Reserves in respective countries. For example, 290K for Venezuela.

  • Set the numberSuffix attribute to set the unit of the values.

  • Set the theme attribute to apply the predefines themes to the chart.

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 the data in JSON format, let's see how to render the chart.

Render the chart

To render the chart, follow the steps below:

  1. Include the fusioncharts library.

  2. Include the chart type.

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

  4. Include the ExcelExport module to export chart data in XLSX format.

  5. Add the chart, theme and ExcelExport as dependencies to the core.

  6. 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.
  7. Add a container (instance) for the chart.

The consolidated code is shown below:

The fusioncharts package for npm can be used in two different ways:
  • FusionCharts ES module
  • FusionCharts CJS module
The steps to render the chart for both the modules are shown below:

ES6


// Include the core fusioncharts file from core  -
import FusionCharts from 'fusioncharts/core';

// Include the chart from viz folder
// E.g. - import ChartType from fusioncharts/viz/[ChartType]
import Column2D from 'fusioncharts/viz/column2d';

// Include the fusion theme
import FusionTheme from 'fusioncharts/themes/es/fusioncharts.theme.fusion';

// Include the ExcelExport
import ExcelExport from 'fusioncharts/features/excelexport';

// Add the chart, theme and ExcelExport as dependency
// E.g. FusionCharts.addDep(ChartType)
FusionCharts.addDep(Column2D);
FusionCharts.addDep(FusionTheme);
FusionCharts.addDep(ExcelExport);

// Create an Instance with chart options
var chartInstance = new FusionCharts({
type: 'Column2D',
width: '700', // Width of the chart
height: '400', // Height of the chart
dataFormat: 'json', // Data type
renderAt:'chart-container', //container where the chart will render
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"
}]
}
});
// Render
chartInstance.render();


CJS


var FusionCharts = require('fusioncharts');

// Require charts from fusioncharts
var Charts = require('fusioncharts/fusioncharts.charts');

// Require theme from fusioncharts
var FusionTheme = require('fusioncharts/themes/fusioncharts.theme.fusion');

// Require ExcelExport module from fusioncharts
var ExcelExport = require('fusioncharts/fusioncharts.excelexport');

// Add charts, themes and ExcelExport as dependency
Charts(FusionCharts);
FusionTheme(FusionCharts);
ExcelExport(FusionCharts);

// Create an Instance with chart options
var chartInstance = new FusionCharts({
type: 'Column2D',
renderAt: 'chart-container',
width: '700', // Width of the chart
height: '400', // Height of the chart
dataFormat: 'json', // Data type
renderAt:'chart-container', //container where the chart will render
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"
}]
}
});
// Render
chartInstance.render();


To include the specific chart types, individually add the following files using require
  • 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>
<title>My first chart using FusionCharts Suite XT</title>
<!-- Include fusioncharts core library -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Include fusion theme -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
<script type="text/javascript">
    FusionCharts.ready(function(){
    var fusioncharts = new FusionCharts({
    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"
        }]
    }
});
    fusioncharts.render();
    });
</script>
</head>
<body>
    <div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>


<html>
<head>
<title>My first chart using FusionCharts Suite XT</title>
<!-- Include fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Include fusion theme -->
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
<script type="text/javascript">
    FusionCharts.ready(function(){
    var fusioncharts = new FusionCharts({
    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"
        }]
    }
});
    fusioncharts.render();
    });
</script>
</head>
<body>
    <div id="chart-container">FusionCharts XT will load here!</div>
</body>
</html>

That's it! Your first chart using Plain JavaScript 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.