Home Angular 12 Angular 12 D3 Chart Working Functionality

Angular 12 D3 Chart Working Functionality

by therichpost
Angular 12 D3 Chart Working Functionality

Hello friends, welcome back to my blog. Today in this blog post, I am going to tell you, Angular 12 D3 Chart Working Functionality.

Working video

Guy’s here are the more demos related to Angular 12 with Bootstrap 5:

  1. Bootstrap 5 Popover working in Angular 12
  2. Bootstrap 5 Tooltip working in Angular 12
  3. Bootstrap5 Modal with Forms in Angular 12

Angular 12 D3 Chart Working Functionality
Angular 12 D3 Chart Working Functionality

Angular 12 came and Bootstrap 5 also and if you are new then you must check below two links:

  1. Angular12 Tutorials
  2. Angular12 Free Templates
  3. Bootstrap 5

Friends now I proceed onwards and here is the working code for snippet glightbox in angular 12 and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 12 setup and for this we need to run below commands but if you already have angular 12 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli 

ng new angulardemo //Create new Angular Project

cd angulardemo // Go inside the Angular Project Folder

2. Now friends, here we need to run below commands into our project terminal to install d3 modules into our angular application:

npm install d3 --save 

npm install @types/d3 --save-dev

ng serve --o

3. Now friends we just need to add below code into src/app/app.component.html file to get final out on the web browser:

<figure id="bar"></figure>

4. Now friends we just need to add below code into src/app/app.component.ts file :

import * as d3 from 'd3'; 
export class AppComponent {
         private data12 = [
            {"Framework": "Vue", "Stars": "166443", "Released": "2014"},
            {"Framework": "React", "Stars": "150793", "Released": "2013"},
            {"Framework": "Angular", "Stars": "62342", "Released": "2016"},
            {"Framework": "Backbone", "Stars": "27647", "Released": "2010"},
            {"Framework": "Ember", "Stars": "21471", "Released": "2011"},
          private svg;
          private margin = 50;
          private width = 750 - (this.margin * 2);
          private height = 400 - (this.margin * 2);
          private createSvg(): void {
            this.svg = d3.select("figure#bar")
            .attr("width", this.width + (this.margin * 2))
            .attr("height", this.height + (this.margin * 2))
            .attr("transform", "translate(" + this.margin + "," + this.margin + ")");
        private drawBars(data: any[]): void {
          // Create the X-axis band scale
          const x = d3.scaleBand()
          .range([0, this.width])
          .domain(this.data12.map(d => d.Framework))
          // Draw the X-axis on the DOM
          .attr("transform", "translate(0," + this.height + ")")
          .attr("transform", "translate(-10,0)rotate(-45)")
          .style("text-anchor", "end");
          // Create the Y-axis band scale
          const y = d3.scaleLinear()
          .domain([0, 200000])
          .range([this.height, 0]);
          // Draw the Y-axis on the DOM
          .attr("transform", "translate(0,0)")
          // Create and fill the bars
          .attr("x", d => x(d.Framework))
          .attr("y", d => y(d.Stars))
          .attr("width", x.bandwidth())
          .attr("height", (d) => this.height - y(d.Stars))
          .attr("fill", "#d04a35");
               //Draw Chart


Friends in the end must run ng serve command into your terminal to run the angular 12 project.

Now we are done friends. If you have any kind of query, suggestion and new requirement then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.



You may also like


Jhansi March 14, 2022 - 4:44 pm

Hi could u add click event on the individual bar and wen a bar is selected nad checkbox checked then data of the check box should be added to the bar

Ajay Malhotra March 15, 2022 - 4:36 am

Okay I will make post on that. Thanks


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.