{"id":20089,"date":"2020-07-14T12:00:27","date_gmt":"2020-07-14T11:00:27","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=20089"},"modified":"2026-01-13T09:54:32","modified_gmt":"2026-01-13T09:54:32","slug":"firestore-angular-and-highcharts","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/firestore-angular-and-highcharts\/","title":{"rendered":"Firestore, Angular and Highcharts"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>In this tutorial, we will learn how to visualize real-time database updates using <a href=\"https:\/\/www.npmjs.com\/package\/@angular\/fire\" target=\"_blank\" rel=\"noopener noreferrer\">Firestore<\/a> and the official <a href=\"https:\/\/github.com\/highcharts\/highcharts-angular\" target=\"_blank\" rel=\"noopener noreferrer\">Highcharts Angular wrapper<\/a>. The entire project is in this <a href=\"https:\/\/github.com\/haroon786\/Firestore-Angular-Highcharts\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub link<\/a>.<\/p>\n<p>We will cover the following points in this article:<\/p>\n<ol>\n<li>Setup a Cloud Firestore project.<\/li>\n<li>Setup an Angular project with Firestore.<\/li>\n<li>Integrate Highcharts with Angular and Firestore.<\/li>\n<\/ol>\n<h2>Setup a Cloud Firestore project<\/h2>\n<ol>\n<li>The first thing to do is to create a project, and here are the steps to do that: Login to https:\/\/console.firebase.google.com<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20648\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14104950\/A.jpg\" alt=\"\" width=\"409\" height=\"195\" \/><\/li>\n<li>Click on Add project.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20650\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105055\/B.jpg\" alt=\"\" width=\"416\" height=\"199\" \/><\/li>\n<li>Create a database.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20651\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105200\/C.jpg\" alt=\"\" width=\"418\" height=\"230\" \/><\/li>\n<li>Select \u201cstart in test mode\u201d.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20652\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105249\/D.jpg\" alt=\"\" width=\"421\" height=\"184\" \/><\/li>\n<li>Create a collection for the database to have multiple documents to store the data.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20653\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105340\/E.jpg\" alt=\"\" width=\"425\" height=\"203\" \/><\/li>\n<li>Add documents to the collection.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20654\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105417\/F.jpg\" alt=\"\" width=\"427\" height=\"180\" \/><\/li>\n<li>Add your <b>Angular App name<\/b> to <b>Firestore<\/b>. Be sure to select Web as we are using the web for now<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20655\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105601\/G.jpg\" alt=\"\" width=\"423\" height=\"150\" \/>.<\/li>\n<li>By registering Angular App Name into Firestore, we will be able to access all key capabilities out of the box.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20656\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105650\/H.jpg\" alt=\"\" width=\"431\" height=\"153\" \/><\/li>\n<li>Get the Configuration details and use the <code>firebaseConfig<\/code> object in the angular app. <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20657\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14105757\/I.jpg\" alt=\"\" width=\"632\" height=\"382\" \/><\/li>\n<\/ol>\n<h2>Setup an Angular project with Firestore<\/h2>\n<p>To set up an Angular project, follow the instructions in this <a href=\"https:\/\/angular.io\/guide\/setup-local\" target=\"_blank\" rel=\"noopener noreferrer\">link<\/a>.<br \/>\nBy the way, don\u2019t forget to run the following command <code>ng add @angular\/fire<\/code> when you are done with creating the Angular project, and here the reasons why you should do it:<\/p>\n<ol>\n<li>To list out all Firebase project. From that, we can choose our project for further configuration and development.<\/li>\n<li>To create a Firebase.json configuration file that lists your project configuration.<\/li>\n<li>To create a .firebaserc file that stores your project aliases.<\/li>\n<li>To update <code>angular.json<\/code> file.<\/li>\n<\/ol>\n<p>Once the setup is done, it is time to add Firebase configuration object into the environment (see below):<\/p>\n<pre>export const environment = {\r\n  production: false,\r\n  firebaseConfig: {\r\n    apiKey: \"AIzaSyBaGUX7iwBRthIKOE_uhyzs1aPEuKPlEAE\",\r\n    authDomain: \"firestore-angular-highcharts.firebaseapp.com\",\r\n    databaseURL: \"https:\/\/firestore-angular-highcharts.firebaseio.com\",\r\n    projectId: \"firestore-angular-highcharts\",\r\n    storageBucket: \"firestore-angular-highcharts.appspot.com\",\r\n    messagingSenderId: \"669023523724\",\r\n    appId: \"1:669023523724:web:43f04e6a2ce0a92c01fca9\",\r\n    measurementId: \"G-ZD6C3CEC85\"\r\n  }\r\n};<\/pre>\n<p>The last step in this sec HighchartService to interact with Cloud Firestore API (see below):<\/p>\n<pre>import {\r\n  Injectable\r\n} from '@angular\/core';\r\nimport {\r\n  Observable\r\n} from 'rxjs';\r\nimport {\r\n  AngularFirestore,\r\n  AngularFirestoreCollection\r\n} from \"@angular\/fire\/firestore\";\r\nimport {\r\n  map\r\n} from \"rxjs\/operators\";\r\n@Injectable({\r\n  providedIn: 'root'\r\n})\r\nexport class HighchartService {\r\n  private rateCollection: AngularFirestoreCollection &lt; chartModal &gt; ;\r\n  rates$: Observable &lt; chartModal[] &gt; ;\r\n  constructor(private readonly firestoreservice: AngularFirestore) {\r\n    this.rateCollection = firestoreservice.collection &lt; chartModal &gt; ('ChartData');\r\n    \/\/ .snapshotChanges() returns a DocumentChangeAction[], which contains\r\n    \/\/ a lot of information about \"what happened\" with each change. If you want to\r\n    \/\/ get the data and the id use the map operator.\r\n    this.rates$ = this.rateCollection.snapshotChanges().pipe(\r\n      map(actions =&gt; actions.map(a =&gt; {\r\n        const data = a.payload.doc.data() as chartModal;\r\n        const id = a.payload.doc.id;\r\n        return {\r\n          id,\r\n          ...data\r\n        };\r\n      }))\r\n    );\r\n  }\r\n}\r\nexport interface chartModal {\r\n  currency: string,\r\n    rate: number\r\n}<\/pre>\n<h2>Integrate Highcharts with Angular and Firestore<\/h2>\n<p>To visualize the data update on our project using Highcharts, we need to include Highcharts Angular wrapper. It allows us to access the highcharts library. <code>highchats-angular<\/code> is the official angular wrapper.<br \/>\nTo install highcharts-angular and the Highcharts library, just run the following instruction: <code>npm install highcharts-angular highcharts<\/code>.<br \/>\nTo use those packages, first, you have to import them. So, in the app.module.ts file, we import the module <code>HighchartsChartModule<\/code> from the highcharts-angular package.<br \/>\nAlso, we import <code>AngularFirestoreModule<\/code> and <code>initializeApp<\/code> with firebaseConfig (see below):<\/p>\n<pre>  import {\r\n    BrowserModule\r\n  } from \"@angular\/platform-browser\";\r\n  import {\r\n    NgModule\r\n  } from \"@angular\/core\";\r\n  import {\r\n    HighchartsChartModule\r\n  } from \"highcharts-angular\";\r\n  import {\r\n    AppRoutingModule\r\n  } from \".\/app-routing.module\";\r\n  import {\r\n    AppComponent\r\n  } from \".\/app.component\";\r\n  import {\r\n    AngularFireModule\r\n  } from \"@angular\/fire\";\r\n  import {\r\n    environment\r\n  } from \"src\/environments\/environment\";\r\n  import {\r\n    AngularFireAnalyticsModule\r\n  } from \"@angular\/fire\/analytics\";\r\n  import {\r\n    AngularFirestoreModule\r\n  } from \"@angular\/fire\/firestore\";\r\n  @NgModule({\r\n    declarations: [AppComponent],\r\n    imports: [\r\n      BrowserModule,\r\n      AppRoutingModule,\r\n      HighchartsChartModule,\r\n      AngularFireAnalyticsModule,\r\n      AngularFirestoreModule,\r\n      AngularFireModule.initializeApp(environment.firebaseConfig),\r\n    ],\r\n    providers: [],\r\n    bootstrap: [AppComponent],\r\n  })\r\n  export class AppModule {}<\/pre>\n<p>Let&#8217;s walk through what we are doing in <a href=\"https:\/\/gist.github.com\/haroon786\/35ef27d808de0143b494155e8cca93d8\" target=\"_blank\" rel=\"noopener noreferrer\">app.component.ts<\/a>.<\/p>\n<p>Importing <code>Highcharts<\/code> from <code>import * as Highcharts from \u201chighcharts\u201d; <\/code>.<br \/>\nImporting service and modal from <code>highchart.service<\/code>.<br \/>\nOn <code>ngOnInit()<\/code> we are subscribing the emitting values from filestore database and pushing new values to <code>chartdata<\/code> array and then Calling <code>getChart()<\/code> and initializing chart data.<\/p>\n<pre>import {\r\n  Component,\r\n  OnInit\r\n} from \"@angular\/core\";\r\nimport {\r\n  HighchartService,\r\n  chartModal\r\n} from \".\/highchart.service\";\r\nimport * as Highcharts from \"highcharts\";\r\n\r\n@Component({\r\n  selector: \"app-root\",\r\n  templateUrl: \".\/app.component.html\",\r\n  styleUrls: [\".\/app.component.css\"],\r\n})\r\nexport class AppComponent implements OnInit {\r\n  title = \"Firestore-Angular-Highcharts\";\r\n  items$: chartModal[];\r\n  Highcharts: typeof Highcharts = Highcharts;\r\n  chardata: any[] = [];\r\n  chartOptions: any;\r\n  constructor(private highchartservice: HighchartService) {}\r\n  ngOnInit() {\r\n    this.highchartservice.rates$.subscribe((assets) =&gt; {\r\n      this.items$ = assets;\r\n      if (this.items$) {\r\n        this.items$.forEach((element) =&gt; {\r\n          this.chardata.push(element.rate);\r\n        });\r\n        this.getChart();\r\n      }\r\n    });\r\n  }\r\n  getChart() {\r\n    this.chartOptions = {\r\n      series: [{\r\n        data: this.chardata,\r\n      }, ],\r\n      chart: {\r\n        type: \"bar\",\r\n      },\r\n      title: {\r\n        text: \"barchart\",\r\n      },\r\n    };\r\n  }\r\n}<\/pre>\n<p>The last step to do in this project is to go to <a href=\"https:\/\/gist.github.com\/haroon786\/3f4586e8a14c09a353017c407c85fcdf#file-app-component-html\" target=\"_blank\" rel=\"noopener noreferrer\">app.component.html<\/a> to add <code>highcharts-chart<\/code> directive and some property binding to initialize the chart data and options:<\/p>\n<pre>&lt;highcharts-chart *ngIf=\"chartOptions\"\r\n  [Highcharts]=\"Highcharts\"\r\n  [options]=\"chartOptions\"\r\n  style=\"width: 100%; height: 400px; display: block;\"&gt;\r\n&lt;\/highcharts-chart&gt;\r\n<\/pre>\n<p>And here is the final result:<br \/>\nAs you can see on the GIF below, the bar chart gets the data from the database hosted on Firebase. When a new value is added to the database using the Firebase UI, the chart\u2019s data is updated and displayed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20663\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/14112619\/image6.gif\" alt=\"\" width=\"885\" height=\"407\" \/><\/p>\n<p>Likewise in the below example, we are plotting a Line chart with new values to the same line chart.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20105\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2020\/07\/16085841\/image14.gif\" alt=\"\" width=\"886\" height=\"408\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Well, that is it. Now, you know how to update a real-time database using Firestore, and how to visualize the update with Highcharts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check out how you can combine Cloud Firestore, Highcharts, and Angular to update a database in real-time.<\/p>\n","protected":false},"author":249,"featured_media":20549,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[1105],"tags":[822,1094,1031],"coauthors":[779],"class_list":["post-20089","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","tag-angular","tag-highcharts-core","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=20089"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20089\/revisions"}],"predecessor-version":[{"id":29308,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/20089\/revisions\/29308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/20549"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=20089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=20089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=20089"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=20089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}