0

Referring to the answer to this question:

Parallel asynchronous Ajax requests using jQuery

Can we do something similar and have d3 objects in the body

Example from the question

var done = 4; // number of total requests
var sum = 0;

/* Normal loops don't create a new scope */
$([1,2,3,4,5]).each(function() {
  var number = this;
  $.getJSON("/values/" + number, function(data) {
    sum += data.value;
    done -= 1;
    if(done == 0) $("#mynode").html(sum);
  });
});
Community
  • 1
  • 1

1 Answers1

0

As was mentioned in the comments, d3.json works roughly the same as $.getJSON so you could employ the same approach. However, there's a library Queue.js by the same author as D3 which formalizes the process for you and makes the results easier to process.

Here is the example from the D3 release notes:

queue()
    .defer(d3.json, "us-states.json")
    .defer(d3.tsv, "us-state-populations.tsv")
    .await(ready);

function ready(error, states, statePopulations) {
  // display map here
}

I should also mention that d3 doesn't care how the data arrived, so if you are already planning to use jQuery in your page alongside d3 there's no real benefit to obtaining the data via d3's own methods. The main benefit of d3 having things like d3.json built-in is that you can avoid also loading something like jQuery if you don't otherwise need it.

explunit
  • 18,967
  • 6
  • 69
  • 94