Let's start with the implementation of different charts with Angular.
- Column Chart
- Bar Chart
- Pie Chart
- Line Chart
- Bubble Chart
- Doughnut Chart
- Combined Chart
For that, I am using a very lightweight extension, FusionCharts which are easy to implement and effective for the Angular environment.
Let's implement all charts one by one.
Step 1 Installation
To use FusionChart, we should include it via the following command using Angular CLI.
- npm install angular4-fusioncharts --save
After completion of the installation, you should import it in the module.ts file as below.
Also, in this app, I'm using some of the Angular Material Components like,
- MatToolbar
- MatButton
- MatMenu
- MatCard
To install Angular Material Components, refer to the following command,
- npm install --save @angular/material @angular/cdk @angular/animations
To use material components, it is required to use and import it into the module.ts file.
I have pasted the complete module.ts file code below.
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
-
-
- import { MatCardModule, MatToolbarModule, MatToolbar, MatButtonModule, MatButton, MatMenuModule } from '@angular/material';
- import { AppComponent } from './app.component';
-
-
- import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
-
-
- import { routing } from './routes';
-
-
- import * as FusionCharts from 'fusioncharts';
- import * as Charts from 'fusioncharts/fusioncharts.charts';
- import * as FintTheme from 'fusioncharts/themes/fusioncharts.theme.fint';
- import { FusionChartsModule } from 'angular4-fusioncharts';
- FusionChartsModule.fcRoot(FusionCharts, Charts, FintTheme);
-
-
- @NgModule({
- declarations: [
- AppComponent,
- ],
- imports: [
- routing,
- BrowserModule,
- MatCardModule,
- MatToolbarModule,
- MatButtonModule,
- MatMenuModule,
- FusionChartsModule,
- BrowserAnimationsModule
- ],
- exports: [
- BrowserModule,
- MatCardModule,
- MatToolbarModule,
- MatButtonModule,
- MatMenuModule
- ],
- providers: [
- ],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Step 2 [ Routing ]
As of now, I am also implementing Routing in my app.
For that, you should create a separate file named routes.ts [For batter practice, I would suggest this way]
routes.ts
- import { ModuleWithProviders } from '@angular/core';
- import { Routes, RouterModule } from '@angular/router';
- import { BarChartComponent } from './bar-chart/bar-chart.component';
- import { BubbleChartComponent } from './bubble-chart/bubble-chart.component';
- import { ColumnChartComponent } from './column-chart/column-chart.component';
- import { DoughuntChartComponent } from './doughunt-chart/doughunt-chart.component';
- import { LineChartComponent } from './line-chart/line-chart.component';
- import { PieChartComponent } from './pie-chart/pie-chart.component';
- import { SplineChartComponent } from './spline-chart/spline-chart.component';
-
- const appRoutes: Routes = [
-
-
- { path: '', redirectTo:'Bar',pathMatch:'full' },
- { path: 'Bar', component: BarChartComponent },
- { path: 'Pie', component: PieChartComponent },
- { path: 'Column', component: ColumnChartComponent },
- { path: 'Line', component: LineChartComponent },
- { path: 'Doughunt', component: DoughuntChartComponent },
- { path: 'Bubble', component: BubbleChartComponent },
- { path: 'Mixed', component: SplineChartComponent }
- ];
-
- export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
So, we are done with basic configuration for FusionChart, Material Component, and Routing.
Step 3
Now, we are going to configure our routing and the main page of our app.
app.component.html
- <mat-toolbar color="accent">
- <span>Manav Pandya - C#Corner</span>
- <span class="demo-toolbar"></span>
- <button mat-button href="www.asp-dotnet-mvc-tutorials.blogspot.in">Go To My Blog</button>
- <button mat-button [matMenuTriggerFor]="menu">Select Charts From Below Menu</button>
- <mat-menu #menu="matMenu">
- <button [routerLink]="['/Bar']" mat-menu-item>Bar Chart</button>
- <button [routerLink]="['/Pie']" mat-menu-item>Pie Chart</button>
- <button [routerLink]="['/Column']" mat-menu-item>Column Chart</button>
- <button [routerLink]="['/Line']" mat-menu-item>Line Chart</button>
- <button [routerLink]="['/Doughunt']" mat-menu-item>DougHunt Chart</button>
- <button [routerLink]="['/Bubble']" mat-menu-item>Bubble Chart</button>
- <button [routerLink]="['/Mixed']" mat-menu-item>Mixed Chart</button>
- </mat-menu>
- </mat-toolbar>
- <div>
- <router-outlet></router-outlet>
- </div>
Code explanation
- <mat-toolbar> : contains toolbar of material component
- <mat-menu> : side menu with many options given to them
- [routerLink] : routerLink meant to be link with their defined routes
- <router-outlet> : It is placeholder by which routes load their content
Step 4
Now, we are going to start our FusionChart implementation.
Bar Chart
I have created separate components for bar chart implementation, do as follows.
Open bar-chart.component.html file and paste,
-
- <mat-card>
- <div class="alert alert-info">
- <strong>Bar Chart</strong>
- </div>
- <fusioncharts [id]="id" [width]="width" [height]="height" [type]="type" [dataFormat]="dataFormat" [dataSource]="dataSource"></fusioncharts>
- </mat-card>
And in bar-chart.component.ts file, paste the below code inside component method.
- id = 'AngularChart1';
- width = 600;
- height = 400;
- type = 'bar2d';
- dataFormat = 'json';
- dataSource;
-
- constructor()
- {
-
- this.dataSource = {
- "chart": {
- "caption": "Unemployment Rate in a Country",
- "subcaption": "(Survey of 2015)",
- "yaxisname": "Unemployment Rate",
- "numbersuffix": "%",
- "basefontsize": "12",
- "basefontcolor": "#194920",
- "valuefontcolor": "#194920",
- "canvasBgColor": "#f3f5f6",
- "canvasBgAlpha": "100",
- "bgColor": "#f3f5f6",
- "BgAlpha": "100",
- "palettecolors": "#3A803D",
- "plottooltext": "$label Rate of Unemployment : $datavalue",
- "theme": "zune"
- },
- "data": [
- {
- "label": "Country A",
- "value": "5.4"
- },
- {
- "label": "Country B",
- "value": "1.7"
- },
- {
- "label": "Country C",
- "value": "3.8"
- },
- {
- "label": "Country D",
- "value": "2.8"
- },
- {
- "label": "Country E",
- "value": "5.0"
- },
- {
- "label": "Country F",
- "value": "2.4"
- },
- {
- "label": "Country G",
- "value": "8.0"
- },
- {
- "label": "Country H",
- "value": "1.5"
- }
- ]
- }
- }
As you can see, there are some setting properties that we should consider while using Chart implementation.
Some of the important options are like,
- Height , Width , Type = bar2d // Defines that it is bar chart
And, this.dataSource is used to render the dummy data for Bar Chart that you can find in the HTML portion of that.
That's it. We have created all necessary files; now, it's time to run our app by using ng serve.
Pie Chart
To implement a Pie Chart, create a new component.
Now, open the pie-chart.component.html file and paste the following code.
- <mat-card>
- <div class="alert alert-info">
- <strong>Pie Chart</strong>
- </div>
- <fusioncharts [id]="id" [width]="width" [height]="height" [type]="type" [dataFormat]="dataFormat" [dataSource]="dataSource"></fusioncharts>
- </mat-card>
Same as Bar chart, we are going to use dummy data in pie-chart.component.ts to populate the Pie Chart.
- id = 'AngularChart2';
- width = 600;
- height = 400;
- type = 'pie3d';
- dataFormat = 'json';
- dataSource;
-
- constructor() {
- this.dataSource = {
- "chart": {
- "caption": "Electronics Selling",
- "subCaption": "Top 5 stores in last month by revenue",
- "numberprefix": "Rs.(Crore) ",
- "theme": "fint"
- },
- "data": [{
- "label": "Redmi",
- "value": "95"
- },
- {
- "label": "IPhone",
- "value": "90"
- },
- {
- "label": "Motorola",
- "value": "76"
- },
- {
- "label": "Samsung",
- "value": "67"
- },
- {
- "label": "vivo",
- "value": "55"
- }
- ]
- }
- }
Notice that here, we have used type = 'pie3d' and dataformat = 'json' , you can see the output below.
Column Chart
Column chart is the most commonly used type of charts that shows bars with appropriate data. Just create a new component.
column-chart.component.html
- <mat-card>
- <div class="alert alert-info">
- <strong>Column Chart</strong>
- </div>
- <fusioncharts [id]="id" [width]="width" [height]="height" [type]="type" [dataFormat]="dataFormat" [dataSource]="dataSource"></fusioncharts>
- </mat-card>
column-chart.component.ts
- id = 'AngularChart3';
- width = 600;
- height = 400;
- type = 'column2d';
- dataFormat = 'json';
- dataSource;
-
- constructor() {
- this.dataSource = {
- "chart": {
- "caption": "Electronics Selling",
- "subCaption": "Top 5 stores in last month by revenue",
- "numberprefix": "Rs.(Crore) ",
- "theme": "fint"
- },
- "data": [{
- "label": "Redmi",
- "value": "95"
- },
- {
- "label": "IPhone",
- "value": "90"
- },
- {
- "label": "Motorola",
- "value": "76"
- },
- {
- "label": "Samsung",
- "value": "67"
- },
- {
- "label": "vivo",
- "value": "55"
- }
- ]
- }
- }
- Here, in column chart, I have used options like,
- type = 'column2d' // For 2 Dimentional presentation
- dataformat = 'json'
Line Chart
Line chart represents the growth of data through a line with its label values. Again, create a new component.
line-chart.component.html
- <mat-card>
- <div class="alert alert-info">
- <strong>Line Chart</strong>
- </div>
- <fusioncharts [id]="id" [width]="width" [height]="height" [type]="type" [dataFormat]="dataFormat" [dataSource]="dataSource"></fusioncharts>
- </mat-card>
line-chart.component.ts
- id = 'AngularChart4';
- width = 600;
- height = 400;
- type = 'line';
- dataFormat = 'json';
- dataSource;
-
- constructor() {
- this.dataSource = {
- "chart": {
- "caption": "Electronics Selling",
- "subCaption": "Top 5 stores in last month by revenue",
- "numberprefix": "Rs.(Crore) ",
- "theme": "fint"
- },
- "data": [{
- "label": "Redmi",
- "value": "95"
- },
- {
- "label": "IPhone",
- "value": "90"
- },
- {
- "label": "Motorola",
- "value": "76"
- },
- {
- "label": "Samsung",
- "value": "67"
- },
- {
- "label": "vivo",
- "value": "55"
- }
- ]
- }
- }
In Line Chart, I have used type = 'line' , to specify that this is line chart and it will adjust the data as per the populated data .
Conclusion
In this post, we have seen 4 types of Fusion Charts with their different options -
- Bar Chart
- Pie Chart
- Column Chart
- Line Chart
I have tried everything possible. If I missed anything, please let me know via comments or message.
Hope you have learned something. Stay tuned for Part 2!