Updating values in an array for a chart with chartsJS and Angular: A step-by-step guide

I'm attempting to change a specific value within a data array for a chart by using chartsJS and Angular. In this case, I want to update the first data point to 0 when the "update data" button is clicked. Unfortunately, despite correctly identifying the elements in the array, I keep encountering the error

ERROR TypeError: Cannot read property '1' of undefined
.

I've experimented with both the push and pop methods, but they only add or remove different data points – what I really need is to replace them.

Here's the HTML code:

<button (click)="add()">Add Data</button>

And here's how the chart is initialized:

this.NewChart = new Chart('totalJourneys', {
      type: 'bar',
    data: {
      labels: ["Jan", 'Feb', "Mar", "Apr", "May","Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
      datasets: [{
        label: "Number of Items Sold",
        data: [9,7,3,5,2,10,15,16,19,3,1,9],
        fill: false,
        lineTension: 0.2,
        backgroundColor: "light-blue",
      }]
    },
  });

This is the function used to update the chart:

public add() {
    console.log('added');
    this.NewChart.data.datasets.data[1].data[0] = 10;
    this.NewChart.update();
}

Answer №1

this.ChartData.data[0].values[1] = 100;

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

Deactivating a button using intricate conditions in an html document

Apologies for the vague title, I couldn't find help on Google either. I'm trying to implement the following code that doesn't seem to be working: <button [disabled]="condition1 || condition2 || (condition3 && condition4)">Save ...

tips for converting an object in typescript

import { Enseignant } from "./Enseignant"; import { AlreadyExistsError } from "./errors/AlreadyExistsError"; import { Etudiant } from "./Etudiant"; export class Utilisateur { private _id: string; private _first_name: string; private _last_name: stri ...

Determine the appropriate generic type based on a sub class

I'm currently working on deducing the type of an attribute within a generic class. For instance: abstract class A<T> { attr: T; } class B extends A<number> { attr = 1; } type Custom = { value: string; }; class C extends A<Custo ...

Leveraging Global Functions with Angular 5

I am faced with the challenge of integrating video.js into my IonicFramework application built using Angular. I attempted to install it via npm, but encountered issues with TypeScript recognizing any importable module, and was unable to find clear document ...

Understanding the properties of different types in Typescript and how to differentiate them

Imagine I have three different types: typeA, typeB, and typeC. TypeA includes a property called name, while typeB and typeC do not. When attempting to render this using code like: interface propTypes { post: typeA | typeB | typeC } ..... some react cod ...

There was a problem locating the module .../node_modules/next/link' that was imported from ... Were you trying to import .../node_modules/next/link.js instead?

Struggling to package a React components library for my Next.js app, I'm faced with an elusive error that has left me perplexed. Despite experimenting with Rollup configurations, TypeScript compiler settings, and various tweaks to the package.json fil ...

The NgRx EntityState is causing a compiler error stating "TS2769: No overload matches this call"

I encountered an unfamiliar compile error. When I have an EntityState and store Persons in the store, then attempt to check if a specific id exists, I attempted the following code: this.store.select(fromPersonSelectors.selectPersonIds) .pipe(tap(theIds ...

What is the best way to ensure that Interface (or type) Properties always begin with a particular character?

I attempted to tackle this assignment using template literals, but unfortunately, I wasn't successful. Here is the interface that I am working with: interface SomeInterface { '@prop1': string; '@prop2': string; '@ ...

Obtaining a return value from a function in Angular

After just starting to work with Angular, I am attempting to extract a value from a button displayed in the HTML using a function. `<button class="btn" id="btn-gold" (click)="value(9)" name="mybutton" value="9">` 9 I have also inclu ...

What could be causing jQuery to overlook this button?

Having some trouble with my angular, bootstrap, and jQuery setup. I can't get jQuery to select a button and trigger an alert when clicked: $('#some_button').click(function(e) { alert('testing'); }); <button id="some_but ...

Vite is turning a blind eye to compiler errors until the specific file is executed during runtime

There's an odd issue with Vite that allows me to log in to my app and use it freely, until I click on a page that triggers a compilation error. Once that happens, the page won't render and the error is displayed in the console. Strangely enough, ...

Unable to toggle ngx-carousel function by clicking

Currently, I have incorporated ngx-carousel to display a series of images in carousels. While everything appears to be functioning as intended, I am encountering issues with the toggle buttons below. The ngx-carousel library from valor Software works seam ...

Implementation of search filtering pipe function

Currently, I am implementing item filtering using a pipe in my Angular application. The search input field is located in the search.html file while the list of items is displayed in the List.html file. However, I've encountered an issue where change ...

Tips for utilizing react-native-root-toast as a component in TypeScript

I am currently developing a React Native project using Typescript and I'm attempting to implement react-native-root-toast as a component. Below is the code for my component: import React, { memo, useEffect, useState } from "react"; import To ...

Error: Unable to locate binding.gyp file

I'm currently in the process of setting up the required modules for my web application. As I execute $npm install, an error message pops up: john@mylaptop frontend % npm install > <a href="/cdn-cgi/l/email-protection" class="__cf_ ...

In TypeScript, what do we call a property that is accessed dynamically?

I have a reusable function that can be used on various properties of a custom type. Here's an example: interface MyType { prop1: string; prop2: number; } type MyTypeKey = keyof MyType; const testValue = ( obj: MyType, property: MyTypeKey, v ...

Awaiting the outcome of the Typescript map function is non-existent

In order to find the subcategories of an article, I am utilizing a many-to-many relationship with TypeOrm that requires Ids. However, I encountered an issue where the map function is not properly waiting to push the results into the array. Below is the c ...

Is it possible to integrate multiple instances of Mat Paginator within a single component?

After encountering issues with the Mat Table / Mat Paginator due to an ngIf in my HTML file, I had to readjust the component. The standard paginator convention didn't work as expected because of delayed data arrival. Below is the updated typescript th ...

What is the process of refactoring TypeScript and React project expressions?

Currently, I am utilizing TypeScript in a React project but encountering type errors. Please assist me by examining my code. I need help with refactoring this code. I believe the issue lies in the expression that uses the args of the useTabs function. ...

Jasmine was unsuccessful in detecting a exported function being invoked by another function

In my code, I've created 2 helper functions where one is a shortcut to the other. I need to verify in my test that the shortcut function is actually calling the main function. Both functions are located in the same file: export function test1(param1, ...