Loading data
Quite often, you won't have the benefit of being able to create data using the Math random number generator functions, you'll need to load it from an external source. While sometimes it's easier to have your code generate your dataset, most of the time you'll be mapping real data to what you create with D3.
You can get data in a number of ways. I'll cover the main ones here:
- Make some sort of manual HTTP request: We already did this in earlier chapters. We supply a URL to a function that causes the browser to make a request. BothÂ
XMLHttpRequest
andFetch
 fall into this category. To import JSON, the server needs to have Cross-Origin Resource Sharing (CORS) enabled, meaning that it sends a header resemblingAccess-Control-Allow-Origin: *.
This is due to the browser's security model, but it doesn't apply if we're loading data off the same domain, so we're able to do that without any extra work. - Import it as a module: Some datasets are available as modules via npm. A good example of...