Categories

Thursday, November 21, 2024
#919814419350 therichposts@gmail.com
Angular 8Laravel 6Php

Angular 8 Grouped Bar chart using data Laravel 6 REST API

Angular 9 Laravel 7 Auth Login working tutorial Part 2

Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 8 Grouped Bar chart using data Laravel 6 REST API.

Today, I am going to show Chartjs Bar Chart in Angular 8 and data is coming from Laravel 6 Rest API.

This is Part 1 because in next part I will show multiple data working with arrays.


Angular 8 Grouped Bar chart using data Laravel 6 REST API
Angular 8 Grouped Bar chart using data Laravel 6 REST API

Here is the working code snipped please use this carefully:


1. Here are the basics commands, you need to use into your terminal or command prompt to install Angular 8 fresh set up:

$ npm install -g @angular/cli //Setup Angular8 atmosphere

$ ng new angular8chartjs //Install New Angular App

/**You need to update your Nodejs also for this verison**/

$ cd angular8chartjs //Go inside the Angular 8 Project

2. After Install Angular 8 fresh setup and go inside the Angular 8 setup, run below command into your terminal to install chartjs module:

npm install --save chart.js

3. After all above setup, here is code, you need to add into your app.component.ts file:

I have used Angular HTTPCLIENT to get data from third party or other site.

import { Component } from '@angular/core';
import * as Chart from 'chart.js';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular8chartjs';
  canvas: any;
  ctx: any;
  data = [];
  constructor(private http: HttpClient) {
    this.http.get('http://localhost/blog/public/api/sample-restful-apis').subscribe(data => {
    this.data.push(data);
    console.log(this.data);
    this.canvas = document.getElementById('myChart');
    this.ctx = this.canvas.getContext('2d');
    let myChart = new Chart(this.ctx, {
      type: 'bar',
      data: {
          labels: [this.data[0]['month']],
          datasets: [{
              label: '# of Votes',
              data: [this.data[0]['sale']],
              backgroundColor: [
                  'rgba(255, 99, 132, 1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)'
              ],
              borderWidth: 1
          }]
      },
      options: {
        responsive: false,
        display:true
      }
    });
    }, error => console.error(error));
  }
}

4. Now, here is code, you need to add into your app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

5. Now, add below code into your app.component.html file:

<canvas id="myChart" width="700" height="400"></canvas>

6. Here is laravel API code in routes/api.php file , from where I am getting the data:

Route::get('sample-restful-apis', function()
{
    return response()->json(
        ['month' => 'january', 'sale' => '100']
    );
});

In the last, don’t forget to run ng serve command and don’t forget to turn on xampp server.

This is it and if you have any query related to this post then please do comment below.

Jassa Jatt

Thank you

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

2 Comments

Leave a Reply

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