{"id":21634,"date":"2021-11-30T14:46:13","date_gmt":"2021-11-30T14:46:13","guid":{"rendered":"https:\/\/www.highcharts.com\/blog\/?p=21634"},"modified":"2026-01-13T11:22:20","modified_gmt":"2026-01-13T11:22:20","slug":"create-charts-with-angular-nestjs-highchart-and-mongodb","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/create-charts-with-angular-nestjs-highchart-and-mongodb\/","title":{"rendered":"Create charts with Angular, NestJS, Highchart, and MongoDB"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>In this article, we will create a bar chart to visualize the department table of a particular student count. To do that, we will walk through the integration of <a href=\"https:\/\/nestjs.com\/\" target=\"_blank\" rel=\"noopener\">NestJS<\/a> Node.js framework for building efficient, reliable, and scalable server-side applications, and MongoDB (NoSQL database program) with the Angular Framework.<br \/>\nAlong with the <a href=\"https:\/\/github.com\/highcharts\/highcharts-angular\" target=\"_blank\" rel=\"noopener\">official Highcharts Angular wrapper<\/a>, we will use a bar chart to visualize data coming from the backend.<\/p>\n<p>Let\u2019s get started.<br \/>\nThis article is divided into three major parts:<\/p>\n<ol>\n<li>Set Up a NestJS project.<\/li>\n<li>Integration of NestJS app with MongoDB.<\/li>\n<li>Create an Angular project with Highcharts.<\/li>\n<\/ol>\n<h2>Set up a NestJS Application<\/h2>\n<ol>\n<li>To get started with NestJS, run the following command <code>npm i -g @NestJS\/cli<\/code><\/li>\n<li>To scaffold NestJS project(initial setup), which contains the base structure for your project, run the following command <code>nest new project-name<\/code>. Then open the editor to see the code base:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22871\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2021\/11\/30123159\/image1.jpg\" alt=\"\" width=\"383\" height=\"297\" \/><\/li>\n<li>Run the <code>npm start<\/code> command and open your browser and navigate to <code>http:\/\/localhost:3000\/<\/code>.<\/li>\n<li>Use the command <code>nest g resource<\/code> to generate any specific module in your application (such as the dashboard, employee, or department) <code>nest g resource<\/code> command not only scaffold all the NestJS building blocks (module, service, controller classes), but also an entity class such as DTO classes, and the testing <code>.spec<\/code> files out of the box.<\/li>\n<li>Let\u2019s create a Department module by running the <code>nest g resource Department<\/code> command:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22872\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2021\/11\/30124000\/image2.jpg\" alt=\"\" width=\"472\" height=\"121\" \/><\/li>\n<li>We are not going to use CRUD operation in this example, so go with the <code>no<\/code>:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-22873\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2021\/11\/30124033\/image4.jpg\" alt=\"\" width=\"511\" height=\"221\" \/><\/li>\n<\/ol>\n<h2>Setting Up NestJS with MongoDB Database<\/h2>\n<p>Here are the steps to create the MongoDB database:<\/p>\n<ol>\n<li>Login to <a href=\"https:\/\/www.mongodb.com\/\" target=\"_blank\" rel=\"noopener\">mongodb<\/a> and create an organisation and create a project inside the organisation.<\/li>\n<li>Click on Build a Database.<\/li>\n<li>Choose a region, cluster name, cloud provider and spec.<\/li>\n<li>Configure a user name and password for the database.<\/li>\n<li>Select Connect your application.<\/li>\n<li>Copy the connection string since we need it in the NestJS application.<\/li>\n<li>Integrate MongoDB by running <code>npm install --save @NestJS\/mongoose mongoose<\/code><\/li>\n<li>Import the <code>MongooseModule<\/code> into the root <code>AppModule<\/code> and pass the <a href=\"https:\/\/gist.github.com\/dd7111fb8b536b58032bd051cba62554.git\" target=\"_blank\" rel=\"noopener\">connection string<\/a><\/li>\n<li>Create <code>create-department.dto.ts<\/code> inside src\/dto <a href=\"https:\/\/gist.github.com\/f3d6ca661d0b06ca32eac530fd5def99.git\" target=\"_blank\" rel=\"noopener\">folder<\/a><\/li>\n<li>Create <code>department.entity.ts<\/code> inside <code>src\/entity<\/code> folder, the entity consists of two parts:Department interface and Department schema (<a href=\"https:\/\/gist.github.com\/9a82d376163b72091f360bd522509344.git\" target=\"_blank\" rel=\"noopener\">mongodb<\/a> table structure)<\/li>\n<li>Inside <code>departments.module.ts<\/code> add the <code>MongooseModule<\/code> which provides the <code>forFeature()<\/code> method to configure the schema, so that we can use in the current module scope and further export the same by adding <code>MongooseModule<\/code> to the <a href=\"https:\/\/gist.github.com\/8adb8b748b5928f247b60b9299797cfe.git\" target=\"_blank\" rel=\"noopener\">export array<\/a>.<\/li>\n<li>Once you&#8217;ve registered the schema, you can inject a Department model into the DepartmentsService using the <code>@InjectModel()<\/code> <a href=\"https:\/\/gist.github.com\/e6ceddaf7e8b507cb371f1681d3b2305.git\" target=\"_blank\" rel=\"noopener\">decorator<\/a>.<\/li>\n<li>A controller&#8217;s <code>departments.controller.ts<\/code> purpose is to receive specific requests for the application. For example, handling <a href=\"https:\/\/gist.github.com\/dfb3ffcbf4a22a41ab9ada2054f9ba87.git\" target=\"_blank\" rel=\"noopener\">http requests<\/a>.<\/li>\n<li>Lastly, set the <code>cors<\/code> property to <code>true<\/code>, to enable CORS with <a href=\"https:\/\/gist.github.com\/a44c0b3955621eed3be8da6f2b461a69.git\" target=\"_blank\" rel=\"noopener\">default settings<\/a>.<\/li>\n<\/ol>\n<h2>Set up an Angular project with Highcharts<\/h2>\n<ol>\n<li>To set up an Angular project, follow angular instructions <a href=\"https:\/\/angular.io\/guide\/setup-local\" target=\"_blank\" rel=\"noopener\">Setting up the local environment and workspace<\/a>. To visualize the data, include the <a href=\"https:\/\/github.com\/highcharts\/highcharts-angular\" target=\"_blank\" rel=\"noopener\">official Highcharts Angular wrapper<\/a>, then import the Highcharts module:\n<ol>\n<li>To install <code>highcharts-angular<\/code> and the Highcharts library, just run the following instruction: <code>npm install highcharts-angular --save<\/code>.<\/li>\n<li>To import the package go to <code>app.module.ts<\/code> file and import the module <code>HighchartsChartModule<\/code> from the <code>highcharts-angular<\/code> package.<\/li>\n<li>To build Highcharts charts, install Highcharts: <code>npm install highcharts --save<\/code>.<\/li>\n<\/ol>\n<p>Next, in the <code>app.component.ts<\/code>, in the top lines we imported Highcharts from <code>import * as Highcharts from \u201chighcharts\u201d<\/code> , and <a href=\"https:\/\/gist.github.com\/11418d037d87cd58d397b1714dab0d62.git\" target=\"_blank\" rel=\"noopener\">DataService<\/a>.<\/li>\n<li>Create the <a href=\"https:\/\/gist.github.com\/035c87582b26364d4c5dd7d6dc27c215.git\" target=\"_blank\" rel=\"noopener\">service<\/a>.<\/li>\n<li>The last step is to <a href=\"https:\/\/gist.github.com\/61d18e07f1d4ca0923abb2d6b631f04a.git\" target=\"_blank\" rel=\"noopener\">initialize<\/a> the chart data and options by adding the <code>highcharts-chart<\/code> directive and some property binding into <code>app.component.html<\/code>.<\/li>\n<\/ol>\n<p>And here is the final result:<br \/>\nAs you can see on the GIF below, the bar chart retrieves the data from the database<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-21647\" src=\"https:\/\/wp-assets.highcharts.com\/www-highcharts-com\/blog\/wp-content\/uploads\/2021\/11\/30142348\/image3.gif\" alt=\"\" width=\"950\" height=\"687\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A step-by-step tutorial to create interactive charts with Angular, Nestjs, Highchart, and MongoDB.<\/p>\n","protected":false},"author":249,"featured_media":21762,"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-21634","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\/21634","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=21634"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/21634\/revisions"}],"predecessor-version":[{"id":29358,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/21634\/revisions\/29358"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/21762"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=21634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=21634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=21634"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=21634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}