score:0
Since you said you have jQuery available (*), we can use it's Deferred feature to manage the two asynchronous operations you are looking at.
We are doing this by converting D3's callback-based approach into a promise-based approach.
For that, we set up two helper functions that wrap D3's .csv
and .json
helpers and return jQuery promises:
d3.csvAsync = function (url, accessor) {
var result = $.Deferred();
this.csv(url, accessor, function (data) {
if (data) {
result.resolve(data);
} else {
result.reject("failed to load " + url);
}
});
return result.promise();
};
d3.jsonAsync = function (url) {
var result = $.Deferred();
this.json(url, function (error, data) {
if (error) {
result.reject("failed to load " + url + ", " + error);
} else {
result.resolve(data);
}
});
return result.promise();
};
Now we can invoke the requests in parallel and store them in variables. We can use .then()
to transform the results on the fly, as well:
var colDataReq = d3.jsonAsync("colData.json");
var xyDataReq = d3.csvAsync("xyData.csv").then(function (data) {
data.forEach(function (d) {
d.x = +d.x;
d.y = +d.y;
});
return data;
});
Finally, we use the $.when()
utility function to wait on both resources and have them handled by a single callback.
$.when(xyDataReq, colDataReq).done(function (xyData, colData) {
var combinedData = d3.zip(colData, xyData);
// now do something with combinedData
}).fail(function (error) {
console.warn(error);
});
This way we can avoid nesting (and therefore needlessly serializing) the two requests.
Also, since the requests are stored in variables, we can simply re-use them without having to change our existing functions. For example, if you wanted to log the contents of one of the requests, you could do this anywhere in your code:
xyDataReq.done(function (data) {
console.log(data);
});
and it would run as soon as xyDataReq
has returned.
Another consequence of this approach is that — since we have decoupled loading a resource from using it — we can perform the loading very early, even before the rest of the page has rendered. This can save additional time.
score:0
D3.js can actually process a JavaScript object instead of a file. If you replace the file name with the variable name of the object storing (let's say, a JSON array of data) with D3.json(myData){...}, it will have access to that data.
Let's say we are using jQuery and we also include a helper library called Papa Parse (it makes life easier).
Step 1. Turn your CSV data into JSON data and store it in a variable A:
var A = Papa.parse(yourCSV);
Step 2. Read your JSON data and store it in a variable called B
var B;
$(document).ready(function() {
$.getJSON('yourJSON.json', function(json){
B = json;
});
});
Step 3. Combine datasets A and B into variable C IMPORTANT: You might need to format the CSV json stored in A to look how you expect it to look before we give it to D3 later
var C={};
$.extend(C, A, B);
Step 4. Give C to D3
d3.json(C, function(error, jsonData) {
// Use data here to do stuff
});
I've used the above as a work around in my own projects.
You might be able to try calling D3.json within D3.csv, but I haven't tried this before:
d3.csv("A.csv", function(errorA, dataA) {
d3.json("B.json", function(errorB, dataB) {
// Use data to do stuff
});
});
Source: stackoverflow.com
Related Query
- How to load a CSV and a JSON file and combine both datsets to one dataset using d3.json, d3.csv and d3.zip
- How to change json file in d3.js and update chart using select options
- How can I load a local json file using d3?
- Django: create csv file and load it in view using Javascript
- How can we load a CSV file into JupyterLab using d3?
- how to load json file using the latest d3?
- how to load csv file with vue.js and d3.js
- How do I select one group of values from dataset to plot a linechart using D3.js and not the whole dataset
- How to import a json file using angular and typescript
- How to merge several JSON objects in one and put values from both of them in a tooltip
- How to load data from a CSV file in D3 v5
- how to load a json object instead of json file
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- Javascript: Load csv file and print it to page
- How to divide a map into zipcodes using d3, javascript, and a json file?
- Bundle layout using d3.js and given json file structure
- How to load very large CSV dataset into d3
- How do I read a CSV file into an array using d3js v5
- How to loop through JSON file using jQuery
- How would I import a single column CSV file into a pie chart using Javascript D3?
- JS- how to remove duplicate JSON nodes and add one link for all nodes that get merged
- How to load CSV File in Angular JS to do charts
- how to update data form file json using d3.js (zoomable circle pack)
- d3: how can I load external CSV file into data?
- How to fetch data from json file to draw a dynamic graph by using d3.js
- Javascript D3 How to access and manipulate dataset from CSV
- How to iterate over data in JSON file using D3 javascript
- How to display d3 bubbles in different colors for a dataset with one branch and many children?
- How to load a csv file from static in django into javascript function
- How to parse a csv into a dataTable quickly and efficiently using D3 v6
More Query from same tag
- Animating shapes with text anchor in d3.js
- Get width of text node in d3 inside .attrs method
- Open all Children at once under a parent on click in D3 Js Vertical Tree
- d3 json - Color a state manually in a map
- D3.js horizontal bar chart and data from json file
- D3, Typescript, and GraphNodes (Optional fields in a typescript interface)
- Same X-axis Multiple Line Charts using d3
- How to access variables declared outside d3.json request within it
- d3.js v4 - nodes are stuck in the top left corner
- d3.js external json file
- Draw special line with js framework
- D3 linechart string domain x-axis
- How to use Meteor and D3 to draw circles?
- Keep SVG centred while using media queries
- converting a d3.csv method to d3.csv.parse method
- d3.js - attribute d: Expected number
- how to calculate max value of a data
- Capture XAxis value NVD3 Stacked Area Chart
- d3.js force collapsible graph - input data is object array
- cannot read property 'neighbours' of undefined
- Bar-chart toggling between 3 variables in an array
- d3.js get JSON from url
- How to use d3.time.scale() to generate an array of evenly spaced dates?
- How to give a better Y axis label / scale in d3 chart?
- Reading JSON data into the correct data structure
- Update graphs automatically in d3.js
- Bars not hidding properly
- Math.random() - set a range of minimum to maximum
- How to make a bar bigger on mouseover in D3?
- Can D3.js fetch in data using "whitespace" separated values?