Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load JSON data synchronously with d3.js?

When my site first initializes, it queries a server to get back some data. I can't lay anything out on the page until this data gets back. With d3.js, I can use d3.json() to get my data, but because it's asynchronous, I need to put the entire page logic in the callback function. How do I request the data and wait for it to come back?

like image 412
Uri Laserson Avatar asked Mar 07 '13 01:03

Uri Laserson


People also ask

Is the syntax to read JSON data in D3?

json() function is used to fetch the JSON file. If this function got an init parameter, then this is called along with the fetch operation. Syntax: d3.

What can you do with d3js?

D3. js is a JavaScript library used to manipulate documents based on data. It uses HTML, CSS, and SVG to create visual representations of data which can be viewed on any modern browser. It also provides some awesome features for interactions and animations.

How do I import D3 into JavaScript?

Install D3 by running npm install d3 --save . Import D3 to App. js by adding import * as d3 from d3 . You need to use import * (“import everything”) since D3 has no default exported module.


1 Answers

You're basically doing it the only way. The callback function has to be the one initiating the rest of your code. You don't need all your code in the callback function though, you can introduce indirection. So the callback function will call another function inside which would be what is currently in your callback function.

like image 159
CL22 Avatar answered Sep 22 '22 20:09

CL22