The chart is failing to update with the data it obtained through Jquery

Scenario: I want to populate a chart using data fetched by Jquery.

$.getJSON("/dashboard/", function(data, status)
        {

            var test_data=data
            console.log(test_data)
            chart.data.datasets[0].data=test_data;
            chart.update();
        }

Output of console.log(test_data)

 data: Array(3)
    0: 500
    1: 200
    2: 50
    length: 3

However, the chart is not updating as expected.

The chart is displaying no values and there are no errors.

https://i.sstatic.net/E8WzC.jpg

When I manually input the values as shown below, the chart updates successfully.

$.getJSON("/dashboard/", function(data, status)
        {

            var test_data=data
            console.log(test_data)
            chart.data.datasets[0].data=[500,200,50];
            chart.update();
        } 

After setting hard-coded values https://i.sstatic.net/PhdlT.jpg What am I missing here?

Update

The issue was that I was not utilizing the Ajax response in my function. I have updated my code as follows and now it works:

$.getJSON("/dashboard/", function(response, status)
        {

            chart.data.datasets[0].data=response.data;
            chart.update();
        }
        )

Answer №1

The API response you received contains an object with a data property, rather than just an array of results.

When using $.getJSON to fetch data from "/dashboard/", make sure to access the response's data property to update your chart:
chart.data.datasets[0].data = response.data;
chart.update();

Answer №2

The current format of your test_data is as an object. To access the required array, you should use:

var test_data = data.data;

This will allow you to access the necessary array.

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

Guide on extracting HTML content from JSON and displaying it in a UIWebView (utilizing Swift 3.0)

Can anyone guide me on how to use JSON2HTML to parse HTML data from JSON and display it in an UIWebView using Swift 3.0? Your help is much appreciated! This is what I have attempted so far: let jsfile1 = try!String(contentsOfFile: Bundle.main.path(forRes ...

Issue with facebook button in reactJS where onClick() event is not being triggered

I'm facing an issue where my onClick handler is not working with the Facebook button from https://developers.facebook.com/docs/facebook-login/web/login-button/. However, it works fine with a regular div element. Any thoughts on why the onClick event ...

Retrieve only the final number within the sequence using JavaScript

I am receiving a series of numbers from an API. Here is how they look: 1,2,3,4,5,6 My goal is to only display the last digit instead of all of them. How can I achieve this? I know that I need to add .slice at the end, but I'm unsure about what to p ...

I'm getting a "module not found" error - what's the solution?

const { getIo } = require('services/socketio'); const restful = require('utils/restful'); const publicApiService = require('services/publicApi'); const accessTokenMiddleware = require('middleware/accessToken'); const ...

Triggering click events in jQuery/AJAX to replace previous appends

Having some issues with page refreshes after jquery click events. This content is loaded dynamically via ajax, so it's not initially present on the page. I'm using ajax to fetch a json array and add it to select dropdowns. When one button is c ...

Modify the `value` of the `<input type=color>` element

Hello there! I have been working on a feature where I need users to select a color and have that choice reflected in the value field. Initially, I assumed this could be achieved easily through Bootstrap's features since my project is based on Bootstr ...

Displaying asynchronous promises with function components

Apologies if this post appears duplicated, I am simply searching for examples related to class components. Here is the code snippet I am working with: export const getUniPrice = async () => { const pair = await Uniswap.Fetcher.fetchPairDat ...

Unable to display HTML content on a page using the foreach callback in JavaScript

I have a function that iterates through each element of an array and generates HTML content on the page while updating some properties using elements from the array. I am utilizing forEach to iterate over the elements of the array and innerHTML to display ...

Tips for updating form tag details within the same blade file upon reloading

I have set up a payment page that displays the total amount to be paid. To facilitate payments through a 3rd party gateway, I have utilized a form tag on the page. Now, I am looking to integrate a promo code feature on this payment page so that the total a ...

Jest snapshot tests using react-test-renderer encounter null references in refs

Having an issue with manually initializing the Quill editor in componentDidMount and encountering Jest test failures. It seems that the ref value I am receiving is null in jsdom. There is a related issue here: https://github.com/facebook/react/issues/7371, ...

Executing a series of HTTP requests sequentially using Angular 5

I need some guidance on sending an array of HTTP requests in sequential order within my application. Here are the details: Application Entities : Location - an entity with attributes: FanZone fanZone, and List<LocationAdministrator> locationAdmins ...

Gathering information from database for the purpose of making updates

I am having trouble retrieving data from my database for user editing, and I can't figure out what's going wrong... The following codes are meant to fetch information from the database in json format and populate existing textboxes for user modi ...

Loading a Vuetify component dynamically within a Vue 3 environment

In my Vue 3 project, I am attempting to dynamically load Vuetify components using the code below: <template> <v-chip>try</v-chip> <component :is="object.tag">{{ object.content }}</component> </template> & ...

Apache conf file configured with CSP not functioning properly when serving PHP files

While configuring the Apache CSP lockdown for a site, I encountered an unusual behavior when opening the same file as a PHP script compared to opening it as an HTML file. The HTML file looks like this: <html> <head> <meta http-equiv= ...

Pass information from JavaScript to PHP without using a form

I am trying to pass data from JavaScript to PHP without using an HTML form. The scenario involves the user clicking on a specific HTML div, sending its ID to a JavaScript file, and then transferring it back to a PHP file. How can I achieve this? Below is ...

Modifying the CSS properties of elements with identical IDs

Encountering an issue while attempting to modify the CSS property of elements with similar IDs using JavaScript. Currently, running a PHP loop through hidden elements with IDs such as imgstat_1, imgstat_2, and so forth. Here is the code snippet being emp ...

AJAX call using jQuery not returning the expected callback result

My goal is rather straightforward: I want to store the object retrieved from Instagram's API in a variable so that I can manipulate it as needed. Here is my current progress: $(document).ready(function() { // declare variable var instagram_infos; fu ...

Troubleshooting problems with a JavaScript game that involves guessing numbers

I have been given a challenging Javascript assignment that involves using loops to create a counting betting game. The concept is simple: the User inputs a number, and the computer randomly selects a number between 1 and 10. The User can then bet up to 10 ...

Django, the Like button remains the same and does not transform into a Dislike button

A problem arises when a user likes a post but the reverse dislike button is not displayed. Removing the "if" condition allows likes and dislikes to work independently, data is successfully written and deleted from the database. However, the issue lies with ...

Collaborate and apply coding principles across both Android and web platforms

Currently, I am developing a web version for my Android app. Within the app, there are numerous utility files such as a class that formats strings in a specific manner. I am wondering if there is a way to write this functionality once and use it on both ...