Create a Chart in AngularJS Using FusionCharts
FusionCharts is a JavaScript charting library that lets you create interactive charts, gauges, maps, and dashboards using JavaScript. We have built a simple and lightweight Angularjs directive, which provides bindings for FusionCharts. The angularjs-fusioncharts
directive allows you to easily add rich and interactive charts to any Angularjs project.
In this page, you will see how to install FusionCharts and render a chart using the angularjs-fusionCharts
directive.
Installation
Install FusionCharts and the angularjs-fusioncharts
directive using any of the following methods:
angularjs-fusioncharts
directive via npm follow the steps below:angularjs
core library
$ npm install angular
angularjs-fusioncharts
$ npm install angularjs-fusioncharts
fusioncharts
package
$ npm install fusioncharts
angularjs-fusioncharts
component follow the steps below:- Include the AngularJS core library.
- Include the FusionCharts JavaScript files from CDN.
- Include the
angularjs-fusioncharts
directive. - Include the theme file.
<head>
<!-- Step 1 - Including AngularJS -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
<!-- Step 2 - Including the fusioncharts core library -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Step 3 - Including the angularjs-fusioncharts directive-->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/angularjs-fusioncharts.min.js"></script>
<!-- Step 4 - Including the fusion theme -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
</head>
angularjs-fusioncharts
component follow the steps below:
<head>
<!-- Step 1 - Including AngularJS -->
<script type="text/javascript" src="path/to/local/angular.min.js"></script>
<!-- Step 2 - Including the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Step 3 - Including the angularjs-fusioncharts directive-->
<script type="text/javascript" src="path/to/local/angularjs-fusioncharts.min.js"></script>
<!-- Step 4 - 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 angularjs-fusioncharts
directive.
Create Your First Chart
Let's create a Column 2D chart using the angularjs-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:
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, such as the ones for single-series,multi-series, and combination charts.
In the above JSON data:
Create the
chart
object to define the elements of the chart.Set the
caption
andsubcaption
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 thelabel
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, it's time to render the chart.
Render the Chart
To render the chart, follow the steps below:
Include
angularjs
usingrequire
.Include the
fusioncharts
library.Include
angularjs-fusioncharts
directive.Include
ng-fusioncharts
as a dependency in the application. Callangular.module()
to add the dependency.Include the chart type.
Include the FusionCharts theme file to apply style to the charts.
Store the chart configurations in a variable (
myApp
).Add the
<div>
with anfc-chart
directive in your HTML, assuming that it is inside a controller namedMyController
. In the<div>
:
- Set the chart type as
column2d
. Each chart type is represented with a unique chart alias. For Column 2D chart, the alias iscolumn2d
. Find the complete list of chart types with their respective aliases here. - Set the width and height (in pixels).
- Embed the json data as the value of the
dataSource
.
The consolidated code is shown below:
// Require AngularJS
var angular = require('angular');
// Require FusionCharts
var FusionCharts = require('fusioncharts');
// Require angularjs-fusioncharts
require('angularjs-fusioncharts');
// Require Chart modules
var Charts = require('fusioncharts/fusioncharts.charts');
// Require Fusion theme
var FusionTheme = require('fusioncharts/themes/fusioncharts.theme.fusion');
// Add charts and themes as dependencies
Charts(FusionCharts);
FusionTheme(FusionCharts);
var myApp = angular.module('myApp', ['ng-fusioncharts']);
myApp.controller('MyController', ['$scope', function($scope) {
$scope.dataSource = {
"chart": {
"caption": "Countries With Most Oil Reserves [2017-18]",
"subCaption": "In MMbbl = One Million barrels",
"xAxisName": "Country",
"yAxisName": "Reserves (MMbbl)",
"numberSuffix": "K",
"theme": "fusion"
},
"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()
<body ng-app="myApp">
<div ng-controller="MyController">
<div
fusioncharts
width="700"
height="400"
type="column2d"
datasource="{{myDataSource}}">
</div>
</div>
</body>
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>
<!-- Including AngularJS -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<!-- Including the angularjs-fusioncharts directive-->
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/angular-fusioncharts.js"></script>
<!-- Including the fusion theme -->
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', ['ng-fusioncharts']);
myApp.controller('MyController', ['$scope', function($scope) {
// datasource
$scope.myDataSource = {
"chart": {
"caption": "Countries With Most Oil Reserves [2017-18]",
"subCaption": "In MMbbl = One Million barrels",
"xAxisName": "Country",
"yAxisName": "Reserves (MMbbl)",
"numberSuffix": "K",
"theme": "fusion",
},
"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>
</head>
<body ng-app="myApp">
<div ng-controller="MyController">
<div fusioncharts id="my-chart-id" width="700" height="400" type="column2d" dataSource="{{myDataSource}}">
</div>
</div>
</body>
</html>
<html>
<head>
<!-- Including AngularJS -->
<script type="text/javascript" src="path/to/local/angular.min.js"></script>
<!-- Including the fusioncharts core library -->
<script type="text/javascript" src="path/to/local/fusioncharts.js"></script>
<!-- Including the angularjs-fusioncharts directive-->
<script type="text/javascript" src="path/to/local/angular-fusioncharts.js"></script>
<!-- Including the fusion theme -->
<script type="text/javascript" src="path/to/local/themes/fusioncharts.theme.fusion.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', ['ng-fusioncharts']);
myApp.controller('MyController', ['$scope', function($scope) {
// datasource
$scope.myDataSource = {
"chart": {
"caption": "Countries With Most Oil Reserves [2017-18]",
"subCaption": "In MMbbl = One Million barrels",
"xAxisName": "Country",
"yAxisName": "Reserves (MMbbl)",
"numberSuffix": "K",
"theme": "fusion",
},
"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>
</head>
<body ng-app="myApp">
<div ng-controller="MyController">
<div fusioncharts id="my-chart-id" width="700" height="400" type="column2d" dataSource="{{myDataSource}}">
</div>
</div>
</body>
</html>
That's it! Your first chart using angularjs-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.