Create a Chart in Angular 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 Angular component which provides bindings for FusionCharts. The angular-fusioncharts
component allows you to easily add rich and interactive charts to any Angular project.
In this page, we'll see how to install FusionCharts and render a chart using the angular-fusionCharts
component.
Installation
To install FusionCharts and the angular-fusioncharts
component via npm
follow the steps below:
Step 1: Install angular-fusioncharts
$ npm install angular-fusioncharts --save
Step 2: Install fusioncharts core library
$ npm install fusioncharts --save
That completes the installation of FusionCharts and the angular-fusioncharts
component.
Create Your First Chart
Let's create a Column 2D chart using the angular-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 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
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, let's see how to render the chart.
Render the Chart
To render the chart follow the steps below:
Step 1: In app.module.ts
include the necessary files and add the dependency.
Import
angular-fusioncharts
in the root@NgModule
.Import FusionCharts library in the root
@NgModule
.Import the chart type.
Include the FusionCharts theme file to apply style to the charts.
Pass the
fusioncharts
library and chart modules.
The consolidated code is shown below:
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { HttpModule } from "@angular/http";
import { AppComponent } from "./app.component";
// Import angular-fusioncharts
import { FusionChartsModule } from "angular-fusioncharts";
// Import FusionCharts library and chart modules
import * as FusionCharts from "fusioncharts";
import * as Charts from "fusioncharts/fusioncharts.charts";
import * as FusionTheme from "fusioncharts/themes/fusioncharts.theme.fusion";
// Pass the fusioncharts library and chart modules
FusionChartsModule.fcRoot(FusionCharts, Charts, FusionTheme);
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, FormsModule, HttpModule, FusionChartsModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
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
Step 2: Add data to the angular app.component.ts
Set the JSON data within the AppComponent
class as shown below:
The code below is same for Angular older and latest versions.
import { Component } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html"
})
export class AppComponent {
dataSource: Object;
constructor() {
this.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"
},
// 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"
}
]
}; // end of this.dataSource
} // end of constructor
} // end of class AppComponent
Step 3: Now use fusioncharts
component in app.component.html
.
Create the chart container and set the following:
Chart
width
andheight
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 alias here.The data source.
<h1>
{{title}}
</h1>
<fusioncharts
width="700"
height="400"
type="Column2d"
[dataSource]="dataSource"
>
</fusioncharts>
That's it! Your first chart using angular-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.