Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7712099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:32:36+00:00 2026-06-01T01:32:36+00:00

I am trying to populate a javascript array that will be used to pass

  • 0

I am trying to populate a javascript array that will be used to pass into the Google Visualizations API

I have multiple tables containing simple stock data, and each element of the array will hold some piece of information from one table. So far, this is how I populate the array

function drawMapOfPortfolio(symbols, chart_div) {
var gdata = new google.visualization.DataTable();
gdata.addColumn('string', 'Symbol');
gdata.addColumn('string', 'Sector');
gdata.addColumn('number', 'Volume');
gdata.addColumn('number', 'Percent Change');
gdata.addRow(["Portfolio", null, 0, 0]);
gdata.addRow(["Information Technology", "Portfolio", 0, 0]);
gdata.addRow(["Financials", "Portfolio", 0, 0]);
gdata.addRow(["Consumer Staples", "Portfolio", 0, 0]);
gdata.addRow(["Consumer Discretionary", "Portfolio", 0, 0]);

//setup arrays to store the XHR objects to resolve
//and to store the data returned from the server for each request
var jqXHRs = [],
data = [];

//your loop will run faster if you cache the length of the array
for(i=0, len = symbols.length; i < len; i++) {
    //create an IIFE to save the state of the `i` variable for each callback function
    //IIFE = Immediately-Invoked-Function-Expression
    (function (i) {
        var symbol = symbols[i];
        jqXHRs[i] = $.ajax({
            url: "mysql_api.php", 
            type: "GET",
            data: { sqlquery: "SELECT volume, day_change_percent, sector FROM " + symbol + " LEFT JOIN sector USING(symbol) ORDER BY unixTS DESC LIMIT 1" },
            dataType: "json",
            success: function(serverResponse) {
                var treemapData = [symbol, serverResponse[0]["sector"], parseFloat(serverResponse[0]["volume"]), parseFloat(serverResponse[0]["day_change_percent"])];
                data[i] = treemapData;
            }
        });
    })(i);
}

//when all the XHR objects resolve, add their server responses to your `gdata` object
$.when(jqXHRs).then(function () {
    gdata.addRows(data);
    alert(data);
    var chart = new google.visualization.TreeMap(document.getElementById(chart_div));
    chart.draw(gdata, {
    minColor: '#f00',
    midColor: '#ddd',
    maxColor: '#0d0',
    headerHeight: 15,
    fontColor: 'black',
    showScale: true
    });
});

}

but in order to do so, I have to set async to false. By setting async to true, the queries are sent out and google visualizations tries to draw the chart before the array is populated. Are there any ways around this? I think what I am looking for is

Use async $.ajax call to populate the array, but then wait for everything to sync before calling Google Visualization’s draw function.

Updated: changed javascript code but “data” array is empty inside $.when

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:32:37+00:00Added an answer on June 1, 2026 at 1:32 am
    //setup arrays to store the XHR objects to resolve
    //and to store the data returned from the server for each request
    var jqXHRs = [],
        data   = [];
    
    //your loop will run faster if you cache the length of the array
    for(i=0, len = symbols.length; i < len; i++) {
    
        //create an IIFE to save the state of the `i` variable for each callback function
        //IIFE = Immediately-Invoked-Function-Expression
        (function (i) {
            var symbol = symbols[i];
            jqXHRs[i] = $.ajax({
                url      : "mysql_api.php", 
                type     : "GET",
                data     : { sqlquery: "SELECT volume, day_change_percent, sector FROM " + symbol + " LEFT JOIN sector USING(symbol) ORDER BY unixTS DESC LIMIT 1" },
                dataType : "json",
                success  : function(serverResponse) {
                    var treemapData = [symbol, serverResponse[0]["sector"], parseFloat(serverResponse[0]["volume"]), parseFloat(serverResponse[0]["day_change_percent"])];
    
                    //add the server response from this request to the same index as the XHR object for this request
                    data[i] = treemapData;
                }
            });
        })(i);
    }
    
    //when all the XHR objects resolve, add their server responses to your `gdata` object
    $.when(jqXHRs).then(function () {
        for (var i = 0, len = data.length; i < len; i++) {
            gdata.addRow(data[i]);
        }
    });
    

    A Stern Word of Warning

    You are passing full SQL queries into your server-side script. This is extremely unsecure as I can just use my developer console to paste in any SQL I want to run. For instance I could delete your whole database very easily…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate a multidimensional array (array with array elements) via Javascript or
Hi i was trying to populate Google Visualization api in jinja template . I
I am trying to use the Google Charts API to create a chart that
Consider I have a table, like below, that I am trying to populate with
I've been trying to make a JavaScript function for a Google Chrome web-application that
I am trying to populate a list of images dynamically using javascript. I have
I am trying to insert rows into an html table with javascript. I have
How can I do this in Javascript/jQuery from PHP? I am trying to populate
I am trying populate H3 with the sum of column E for rows that
I am trying to build a 3D Javascript array, but I am unsure of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.