Troubleshooting: "C3.js - Issue with this.chart.load function in angular 2 and Typescript"

When working on generating a chart using c3, I encountered an issue with accessing a Rest API in the c3 chart to load JSON data. I found guidance from this example . The problem arises when attempting to dynamically load the chart using the 'load' function, resulting in a runtime error:

Runtime Error this.chart.load is not a function

private chart: any;
this.chart = c3.generate({
    data: {
        url: '/api/systemvalues',
        type: 'line'
    }
});
setTimeout(function () {
    this.chart.load({
        url: '/api/systemvalues'
    });
}, 1000);

The versions used for this project are:

 @angular/core": "2.4.10", 
 "c3": "^0.4.11", 
 "d3": "^3.5.17", 
 "typescript": "2.2.2"

Answer №1

Utilize the setTimeout method with ()=> instead of function(){

private chart: any;
this.chart = c3.generate({
    data: {
        url: '/api/systemvalues',
        type: 'line'
    }
});
setTimeout(()=>{
    this.chart.load({
        url: '/api/systemvalues'
    });
}, 1000);

OR

Assign this to a variable and utilize that variable within a constructor to execute the setTimeout function.

private chart: any;
this.chart = c3.generate({
    data: {
        url: '/api/systemvalues',
        type: 'line'
    }
});
constructor(){
  var vm = this;

  setTimeout(function () {
    vm.chart.load({
        url: '/api/systemvalues'
    });
  }, 1000);
}  

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

Executing a Typescript function in an MVC5 cshtml file, optimized with webpack bundling

Brand new to webpack, and I'm facing obstacles in merging MVC components with webpack AND typescript. Take a look at my code combinations below: webpack.config.js: var wbConfigEntries = { "jqkendoMain": [ paths.appjs + "main.ts" ] }; ...

Unable to create any diagrams through the Angular CLI generate function

Currently, I am experiencing an issue where I encounter an Error: Option "_name" is not defined when running any generate commands like ng g <any_schematic>. I have already attempted the solutions of performing a npm install and using the --project ...

Is it possible to implement pagination on a material table in Angular 10 without relying on the MatTableDataSource component?

How can I implement material pagination on a table without using MatTableDataSource? Most tutorials and examples I find online recommend the use of MatTableDataSource, but I'm unsure of how to actually utilize it. I am fetching data from a database ta ...

Tips for updating the main route and its components upon re-clicking in Angular

Within my application, I have a Master-Detail setup consisting of a main container component holding two nested components - master and detail. By clicking on the link http://localhost:4200/master, the master component fetches data from the server to displ ...

Using optional chaining along with the methods toLowerCase and indexOf while iterating through an array using the map

I have implemented an autocomplete input that searches for project properties while typing. I am looking to enhance the existing code for better performance. filterProjects(value: string) { return this.projects.filter( project => project.key ...

Encountered an error while loading resource: net::ERR_CONNECTION_REFUSED in Nodejs

I am currently working on a form in my angular 6 app with nodejs, utilizing nodemailer for sending emails. Unfortunately, I am encountering an error that reads: Failed to load resource: net::ERR_CONNECTION_REFUSED : :3000/contact/send:1 Below is the form ...

Discover the way to remotely assess the functionality of an Angular application using chrome-remote-interface

Trying to remotely access an Angular app using the chrome-remote-interface node package. When trying to evaluate an expression that returns a Promise, I'm using the following code snippet: Runtime.evaluate({expression, awaitPromise: true, returnByVa ...

Tips for guaranteeing a Typescript string enum with identical key and value pairs

I am looking for a way to create a generic type that verifies certain criteria on an enum: all fields must be strings all values must be equal to their respective keys For example, the following enums would meet the requirements: enum correct1 { bar ...

after ajax post, enhance original object by merging with the latest server values

After making a call to the server and successfully saving some data, I need to update the JSON array object that I have with new values. Here is a snippet of my code: [ { "id": 1, "uuid": "ce54acea-db20-4716-bceb-2f3deb1b2b86", "name": null, ...

Reading a single line of text synchronously in Node JS

I have a scenario where I need to process a file named data.txt that contains thousands of lines. My objective is to read each line, extract specific data, and store it in an array. To achieve this, I am using the readline and fs.createReadStream functions ...

Utilize Angular Service Worker to prefetch API data efficiently (dataGroups)

I am working on an Angular website that needs to function offline. In order to achieve this, I need some data from the API, which can be considered Stale data. To handle this scenario, my approach is to first attempt to retrieve the data from the API, and ...

Is it possible to dynamically assign class properties from an array in JavaScript/TypeScript?

Greetings for the assistance in advance. Currently, I am working with TypeScript, but I believe any JS solution will suffice. I aim to create something akin to the following: class ExcelData { 'Id 1': items[0].id, 'Quantity 1': item ...

Error code TS2749 is indicating that the identifier 'XXX' is being interpreted as a value instead of a type. Perhaps you intended to use 'typeof XXX' instead

I've encountered a strange issue while running my npm run dev command in my project built with Nuxt.js, which includes Vue.js components. While launching the application, I'm encountering errors related to TypeScript like TS2749: 'About&apos ...

Utilize DomSanitizer to add custom styles to elements using classes in Angular

<div [innerHtml]="someHtml | safeHtml"></div> someHtml = '<span class="fas fa-calendar" style="font-size:15px"></span>' By utilizing the innerHtml property, I am able to inject a html element and apply inline styles thro ...

Invalid sequencing of Nest.js async onModuleInit causing issues

I am dealing with a scenario where ServiceA relies on RedisService. In order for ServiceA to be fully operational, it must wait for RedisService to complete its initialization process (specifically, for RedisService.onModuleInit to be called and awaited). ...

Obtaining the 'Content-Range' in an Angular application with Lotus Notes/Domino

After developing an Angular application, I have encountered an issue while trying to retrieve data from a Notes Domino server using HttpClient. Specifically, I am utilizing Domino Data Services to access information from a database with a URL structure lik ...

What is the best way to access object IDs from Firebase and then save them individually?

I'm currently developing an Ionic application using the Angular framework. In order to remove objects from Firebase's real-time database, I need the ID of each object. Despite being a beginner in this field, I have not been able to find a solutio ...

Angular is expecting data of type string or number for the operands provided

When working in VS Code, I encountered the following warning: "Expected operands to be a string or number type" https://i.stack.imgur.com/lULLQ.png The contentType variable is defined as a string. I noticed that when I remove 'data:', every ...

Managing data from POST requests in Angular using Typescript

I have implemented a simple function in my component TypeScript file to send a post request and retrieve data from a server. The function is working correctly, but I am facing an issue when trying to assign the array of data to a variable named 'json& ...

Executing the serve command in an Angular project necessitates a project definition, which unfortunately could not be located

An issue has arisen with the application I developed (angular2 nodejs). Upon attempting to open it, I encountered the following error message: The serve command needs to be executed within an Angular project, however a project definition could not be lo ...