Error in Angular caused by ChartJS: TypeError, inability to access property '_model' because it is null

I am currently working on a project that involves showcasing real-time data in a ChartJS graph. The data is retrieved from an external web server, and I have managed to store the data in 6 arrays (which are constantly changing) with each array containing 10 values:

for (const module of parsedListResults) {
  this._consoAir[module.id - 1].shift();
  this._consoAir[module.id - 1].push(module.consoAir);
}

As a result, every 5 seconds, the data arrays undergo changes:

0: (10) [0, 0, 0, 0, 0, 0, 6.865885, 6.865885, 6.865885, 6.865885]
1: (10) [0, 0, 0, 0, 0, 0, 6.865885, 6.865885, 6.865885, 6.865885]
2: (10) [0, 0, 0, 0, 0, 0, 6.865885, 6.865885, 6.865885, 6.865885]
3: (10) [0, 0, 0, 0, 0, 0, 6.865885, 6.865885, 6.865885, 6.865885]
4: (10) [0, 0, 0, 0, 0, 0, 6.865885, 6.865885, 6.865885, 6.865885]
5: (10) [0, 0, 0, 0, 0, 0, 4.654, 4.654, 4.654, 4.654, _chartjs: {…}, push: ƒ, pop: ƒ, shift: ƒ, splice: ƒ, …]
length: 6

The graph is displayed on a specific page identified by an ID. For example, for page 3, we access it through localhost:4200/lines/3. In order to display the correct data array on the graph for this page, I have implemented the following code snippet:

import {Component, Input, OnDestroy, OnInit} from '@angular/core';
// Other required imports...

@Component({
  selector: 'app-conso-graph',
  templateUrl: './conso-graph.component.html',
  styleUrls: ['./conso-graph.component.scss']
})
export class ConsoGraphComponent implements OnDestroy{
  // Class properties...

  constructor(private lineService: LineService,
              private route: ActivatedRoute) {
    this.chartIt();
    setInterval(() => {
     this.chartIt();
    }, 5000);
  }

  // Other methods...

}

In the HTML file, the graph component is structured as follows:

<div class="doubleTrp">
    <canvas baseChart
            [chartType]="chartType"
            [datasets]="dataType"
            [labels]="this._time"
            [colors]="[{
              backgroundColor: ['rgb(255, 182, 0)'],
              hoverBackgroundColor: ['rgb(185, 30, 30)'],
              borderWidth: 1
            }]"
            [options]="{
              responsive: true,
              cutoutPercentage: 100,
              animation: {
                duration: 0
              },
              title : {
                display: true,
                text: 'Consommation Air',
                fontSize: 25
              }
            }"
            [legend]="true"
            (chartHover)="chartHovered($event)"
            (chartClick)="chartClicked($event)">
    </canvas>
</div>

The issue arises when navigating to a different page while the graph is being displayed. An error message "ERROR TypeError: Cannot read property '_model' of null" occurs, and I am seeking assistance in resolving this problem. Thank you in advance for any valuable insights or solutions provided.

Answer №1

When identifying issues, I write the array of data directly into my HTML template ...

Instead of:

this.dataType = [{
  data: this._consoAir[this.id - 1],
  label: 'Consommation Air'
}];

I now use this in HTML:

<canvas baseChart
[datasets]="[{
data: this._consoAir[0],
this._consoAir[1],
...
this_consoAir[9],
label: 'Consumption Air'
}]"
...

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

What is the reason behind VS Code not showing an error when executing the command line tsc shows an error message?

Deliberately introducing a typo in my code results in an error. Here is the corrected code: declare const State: TwineState; If I remove the last character and then run tsc on the command line, it throws this error: tsc/prod.spec.ts:7:22 - error TS2304: ...

Implement functionality to update the user interface with raw data received through Bluetooth serial communication

I've been utilizing the plugin https://github.com/don/BluetoothSerial to handle my Bluetooth connection with a HC-06 module on an Arduino Leonardo. Everything is functioning well, except for an issue I am encountering with the mobile application. Here ...

Navigating to a new link containing query parameters

I am working on setting up a redirect in Angular 6 The process for the redirect is quite simple as outlined below: Obtain destination URL from parameters: this.returnUrl = this.route.snapshot.queryParams['route'] || '/'; Perform Red ...

What is the best way to disconnect an ngFor loop from its original context and reconnect it to a wrapping component's context when utilized as projected content?

In my project, I've developed a custom wrapping component that accepts <div *ngFor='let item of items> as part of its projected content. My goal is to replace the immediate bindings within the projected content with those defined in the wr ...

Ways to customize the placeholder for Material input fields in Angular 5

Is it possible to customize the placeholder color in an Angular Material input field? <mat-form-field> <input matInput placeholder="Name"> </mat-form-field> ...

Having difficulty testing an Angular/NGXS action that triggers after an unsuccessful API request

Struggling with writing tests for an NGXS action that calls an http request? Want to add tests for both successful and failed requests? Here is the code for my Action: @Action(SearchChuckNorrisJokes) searchChuckNorrisJokes({ getState, setState }: StateCo ...

Is there a method for verifying the application signature in Ionic?

For the past 2 days, I've been on a quest to find information about app certificate validation libraries/functions in Ionic. After discovering SignatureCheck.java for Android (link: enter link description here), I wonder if there is a similar solution ...

Node-server hosted in Azure experiencing difficulties with websockets connectivity

I have a simple example that works fine on my local machine but fails to work properly when deployed on Azure. The original issue I had related to a CORS error, which I have since resolved by editing it out. However, I am still struggling to get WebSockets ...

Angular SwitchMap is a powerful operator that allows you

I am currently utilizing the mat-table component from angular material, in conjunction with pagination features. Whenever a user inputs text into the search field, a filter method is activated to send the text, page size, and current page to the backend f ...

Encountering issues when launching Node.js application using PM2 and ts-node in production mode

I've run into an issue while trying to use PM2 with ts-node. Whenever I attempt to start my application using PM2, I receive an error message stating that the ts-node interpreter is not found in the PATH. I've experimented with installing ts-nod ...

Eliminate any excess white space on the edges of the Angular Chart

How can I eliminate the highlighted white space? I would like the chart to appear in this format I am working with the ionic framework, please see my code below <div class = "row responsive-sm"> <div class = "item col col-50&quo ...

Exploring Angular 6's nested routes and corresponding components for child navigation

I'm currently diving into the concept of lazy loading in Angular 6. Here's a visual representation of the structure of my application: ─src ├───app │ ├───components │ │ ├───about │ │ ├─── ...

Guide to inheriting functions from a parent component in Angular 2

Hello, I am a beginner in the realm of angular2 and would appreciate any assistance in refining my vocabulary and terminology. Currently, I have a class that consists of two directives structured as follows: In parent.component.ts, the Parent component i ...

The module 'AppModule' has unexpectedly declared a value of 'Component' that was not anticipated

Recently, I've been working on transitioning my application to the new angular2 webpack rc5 setup. I have successfully generated the app module using ng cli migration. However, I am facing an issue while trying to integrate a component from my own li ...

What methods can I utilize to manage the output generated by the C# backend project?

I'm new here and I'm thrilled to be asking my first question! :) Currently, I am working on a car rental project using C# for the backend and Angular for the frontend. I have encountered an issue while trying to register a new user with existing ...

Choose a date range from the date picker

I am currently attempting to combine two dates using a rangepicker. Below is the command used to select the date: Cypress.Commands.add('setDatePickerDate', (selector, date) => { const monthsShort = [ 'janv.', 'févr.& ...

What is the best way to ensure that a global variable is populated after subscribing to an asynchronous event?

Recently, I encountered an async firebase call that looks something like this: this.fbProvider.fbGetProfile() .subscribe(p => { this.profile = p; ...

Guide to creating a universal interface using the generic class concept

I am in the process of developing a new augmented, generic interface that is based on an existing interface. The original interface sets out the properties that the object will possess (root). The enhanced type should also have these properties, but instea ...

Numerous calls are made to the Ionic 3 component, causing the query to continuously run without stopping

I am working on a project where I need to retrieve unique dates from a table and then use each date as a parameter to query another table for records to display in an HTML format. To achieve this, I have implemented a component, but I am facing issues with ...

Tips for managing nested data in Angular 4 using a Bootstrap 4 data-table

I am currently using the Data Table from a GitHub project found at: https://github.com/afermon/angular-4-data-table-bootstrap-4-demo. It works perfectly with data structured in a key-value format like the sample provided. However, I am facing challenges wh ...