Data that changes dynamically on a chart

When making a rest call to fetch data, I aim to populate the pieChartData with the obtained information. However, I am facing difficulties in achieving this task. Can someone guide me on how to accomplish this?

import { Component, OnInit} from '@angular/core';
import config from '../../config.json';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
  selector: 'app-analytics',
  templateUrl: './analytics.component.html',
  styleUrls: ['./analytics.component.css']
})
export class AnalyticsComponent  {
  public clickData;
  public dataExtracted=[];
  //public pieChartData=[0,0,0,0,0,0,0,0,0,0];
  private readonly CLICK_DATA_GET = config["SettingsInfo"]['clickDataFetchEndpoint'];
  public pieChartLabels:string[] = ['SC', 'CONF', 'ISS','KB','LOC','APP','PORTAL','UD','ANA','WATCH'];
  public pieChartData=this.dataExtracted;
  public pieChartType:string = 'pie';


  constructor(private httpClient: HttpClient) { 
     this.getClickData();
    //  console.log("Array "+this.dataExtracted);

  }
  getClickData()  {
    var pieChartData=[0,0,0,0,0,0,0,0,0,0];
    this.httpClient.get(this.CLICK_DATA_GET).subscribe(
        (data) => {
          this.clickData=data;
          console.log("DATA COMING "+JSON.stringify(this.clickData));
          for(var i=0;i<this.clickData.length;i++){
            var type=this.clickData[i].u_type;
            switch(type){
              case "SC": pieChartData[0]+=1; console.log(pieChartData[0]); break;
              case "CONF": pieChartData[1]+=1; console.log(pieChartData[1]); break;
              case "ISS": pieChartData[2]+=1; console.log(pieChartData[2]); break;
              case "KB": pieChartData[3]+=1; console.log(pieChartData[3]); break;
              case "LOC": pieChartData[4]+=1; console.log(pieChartData[4]); break;
              case "APP": pieChartData[5]+=1; console.log(pieChartData[5]); break;
              case "PORTAL": pieChartData[6]+=1; console.log(pieChartData[6]); break;
              case "UD": pieChartData[7]+=1; console.log(pieChartData[7]); break;
              case "ANA": pieChartData[8]+=1; console.log(pieChartData[8]); break;
              case "WATCH": pieChartData[9]+=1; console.log(pieChartData[9]); break;
              default:
                    console.log("type:" + type + " not handled");
                    break;
            }
          }
          console.log("ARRAY BEING RETURNED "+pieChartData);
          this.dataExtracted=pieChartData;
        }

    )




    // public pieChartData:number[] 
    // public pieChartType:string = 'pie';    
}}

// getClickDataCallback(data: object) {
//   this.setSettings(data);
//   this.settingsUpdate.emit();
// }

Essentially, the problem lies in the pieChartData being either empty or consisting of zeros. My objective is to have the pieChartData filled with the local pieChartData within the getClickData() function.

Answer №1

In my opinion, making the change from:

console.log("ARRAY BEING RETURNED "+pieChartData);
this.dataExtracted = pieChartData;

to:

console.log("ARRAY BEING RETURNED "+pieChartData);
this.pieChartData = this.dataExtracted = pieChartData;

should do the trick.

It's important to note that when you set

this.pieChartData = this.dataExtracted
, it doesn't mean that if you later assign a new value to this.dataExtracted, this.pieChartData will automatically update. Instead, it will still reference the original object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Storing dates using Angular 2 and JSON for efficient data management

I've encountered a challenging issue with my Angular 2 application. I'm attempting to organize my API (MongoDB) in such a way that each new "post" added by the admin can be retrieved by date (not time) on the front end. Here's an example of ...

Tips for setting up a system where PHP serves as the backend and Angular acts as the

I am working on a project that utilizes Angular as the front end and PHP as the back end. Both are installed in separate domains, with the PHP project fully completed and operational. I have created an API in PHP which I plan to call from Angular. My ques ...

Angular ERROR: Trying to access rating property of an undefined value

I'm encountering an issue on a website where users can vote for their favorite animal. Whenever I try to select an animal to vote for, I receive an unclear error message that has been difficult to resolve even after searching online for a solution. A ...

Is lazy loading Angular modules always the best approach?

My team and I are working on a small application, which is essentially just a login page that redirects to another location. Right now, we are considering creating one Core module and another module for the rest of the page content. However, it seems like ...

Creating a personalized installation pathway for NPM, Angular, and TypeScript on Windows operating systems

Is there a way to prevent NPM and Angular from using the folder C:\Users\XXXXXX\AppData\Roaming\npm in Windows? I am trying to globally install Node.Js and Angular on a different disk (using the command NPM i --prefix D:\MyF ...

Having trouble retrieving values from the getEntry method in Contentful

How can I retrieve an entry from contentful using Contentful v10 with Node.js 18? I am having trouble accessing the value in getEntry(). interface Example { contentTypeId: 'item' fields:{ title: EntryFeildTypes.Text rate: EntryFeildType ...

Slim API receives a request from Ionic 5

Seeking assistance with making a GET request from my Ionic application to an API constructed using the Slim Framework. Below is the code snippet of the API: <?php header('Access-Control-Allow-Origin: *'); header('Content-Type: applicati ...

What is the best way to retrieve the previous URL in Angular after the current URL has been refreshed or changed

Imagine being on the current URL of http://localhost:4200/#/transactions/overview/5?tab=2 and then navigating to http://localhost:4200/#/deals/detail/ If I refresh the deals/detail page, I want to return to the previous URL which could be something like h ...

Angular 2 experiencing issues with the authorization header

Hello there! I am currently working with the Ionic 2 framework alongside Angular, and I'm facing an issue while trying to make an HTTP request with the authorization header. It seems like the header is not being sent properly. Can someone help me iden ...

What is the best way to find information in a multi-page table?

I've implemented a table with pagination and search functionality to look up data within the table. However, currently the search only works within the current page of the table rather than searching the entire dataset. Is there a way to modify the se ...

Is there a way to modify a suffix snippet and substitute the variable in VS Code?

While working on my Java project in VS Code, I stumbled upon some really helpful code snippets: suffix code snippets Once I type in a variable name and add .sysout, .cast, or similar, the snippet suggestion appears. Upon insertion, it translates to: res ...

Displaying a Router Component within an Angular 4 Interface

Is there a way to display a router component without explicitly calling the router.navigate method? I have a method that currently navigates like this: public onClickEdit(id: number): void { this.router.navigate(['...', '...', &ap ...

What is the best method for inserting a hyperlink into the JSON string obtained from a subreddit?

const allowedPosts = body.data.children.filter((post: { data: { over_18: any; }; }) => !post.data.over_18); if (!allowedPosts.length) return message.channel.send('It seems we are out of fresh memes!, Try again later.'); const randomInd ...

Issue with Prettier AutoFormatting in a project that combines TypeScript and JavaScript codebases

Recently, I've started incorporating TypeScript into an existing JavaScript project. The project is quite large, so I've decided to transition it to TypeScript gradually. Below is a snippet from my eslintrc.js file: module.exports = { parser: ...

"Silently update the value of an Rxjs Observable without triggering notifications to subscribers

I'm currently working on updating an observable without alerting the subscribers to the next value change. In my project, I am utilizing Angular Reactive Forms and subscribing to the form control's value changes Observable in the following manner ...

Include TypeScript in a single component within an already established Vue project

I've been working on a large Vue project and I want to integrate TypeScript into it. However, every time I try to do so, I run into a lot of errors that take weeks to fix. Instead of going through that, I'd like to find a way to add TypeScript to ...

What is causing the dysfunction of angular2 form when used with the HTML <form> tag?

Can someone explain the strange behavior of the <form> element in ng2 to me? I have noticed something odd and need some clarification :) To demonstrate, I have created a simple Plunker example at this link: https://plnkr.co/edit/vdrHJBNdd26y6YhPXTHD ...

Avoid passing the ngrxStore object between separate custom elements

I am working with an Angular custom element that is exported as a web component. This custom element dispatches actions to a search state in order to perform a search functionality. The handling of these search state actions is carried out within an angula ...

The Angular directive ng-if does not function properly when trying to evaluate if array[0] is equal to the string value 'Value'

In my code, I want to ensure that the icon is only visible if the value at array index 0 is equal to 'Value': HTML <ion-icon *ngIf="allFamily[0] === 'Value'" class="checkas" name="checkmark"></ion-icon> TS allFamily = [ ...

Exploring the process of retrieving token expiration in TypeScript using the 'jsonwebtoken' library

Currently utilizing jsonwebtoken for token decoding purposes and attempting to retrieve the expiration date. Encountering TypeScript errors related to the exp property, unsure of the solution: import jwt from 'jsonwebtoken' const tokenBase64 = ...