Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Donut chart in Angular 2

I have very limited skill set in SVG,Canvas and CSS3. I have been trying to create a donut chart in angular 2 with no luck. There are some ready to use libraries available but we are not allowed to pull in 3rd party libraries into the project.Can someone point me in the right direction or help me with it.Basically i played around with SVG but there seems to be no 'arc' element in SVG

like image 497
user3113816 Avatar asked Jun 26 '16 03:06

user3113816


People also ask

How to create doughnut chart in angular 11/12 using ng2-charts?

We simply need to follow the given steps for implementing the Doughnut Chart using ng2-charts in the Angular 11/12 app: First of all, open your terminal and execute the given command on it to install an angular app: Next, just install the NPM package called ng2-charts chart.js -save for implementing the doughnut chart in the Angular app.

How to create a chart app in angular 11?

Create components to show charts example in Angular. Install Bootstrap to manage the layout of Angular 11 chart app, however you can skip this step if you don’t want to use Bootstrap. Register the bootstrap.min.css file in the angular.json file. Next, install ng2-charts and Chart Js libraries via npm in Angular project.

How to make a doughnut chart in HTML?

Go to bar-chart.component.html file and add the given below code. Check out the example in your browser, your result must look like this: Doughnut charts are used to prove a “part-to-whole” association, and In doughnut charts, all the section together express 100%.

What is ng2-charts module in Angular 2?

It allows us to build dynamic as well as static charts, and it comes with full animation support for the various charts. It takes data in the JSON form, so it is merely simple to use it with any programming language. The ng2-charts module is an open-source JavaScript library, and it is exclusively built for Angular 2+ and available via npm.


1 Answers

This can be easily done using SVG with stroke-dasharray and stroke-dashoffset. In your donut component html include the following code :-

  <svg height="100%" width="100%" viewBox="0 0 120 120">       
     <circle  *ngFor="let item of items;let i=index" cx="60" cy="60" r="50" fill="transparent" stroke-width="20"
     [attr.stroke-dasharray]="getPerimeter(50)" [attr.stroke-dashoffset]="getOffset(50,i)" [attr.stroke]="getColor(i)"/>             
  </svg>

Basically perimiter of the circle is determined by 2πr where r is the radius. Stroke-dash offset will specify at what point should the new segment start.This can be calculated by subtracting the percentage of perimeter occupied by the previous segments from the total perimeter.In your TS file :-

export class DonutComponent{

 items:Array<{name:string,count:number,color:string}>=[
    {name:'Orange',count:50,color:'orange'},
    {name:'Apple',count:25,color:'red'},
    {name:'Pear',count:15,color:'green'}
  ];
 private _total:number =0;
 constructor() {
   if(this.items.length>0)
   {
     this._total = this.items.map(a=>a.count).reduce((x,y)=>x+y);
   }

 }

  getPerimeter(radius:number):number
  {
    return Math.PI*2*radius;
  }

  getColor(index:number):string
  {
    return this.items[index].color;
  }

  getOffset(radius:number,index:number):number
  {   
    var percent=0;
    for(var i=0;i<index;i++)
    {
      percent+=((this.items[i].count)/this._total);
    }
    var perimeter = Math.PI*2*radius;
    return perimeter*percent;
  }
}

The items can be made as input to the component if you want to make the donut generic

Added the source code for a more generic one in github .

like image 184
Krishnanunni Jeevan Avatar answered Oct 02 '22 02:10

Krishnanunni Jeevan