I have my data stored as JSON in Firebase. I want to use D3.js to create a visualization from that data. My previous experience in D3 has always been to load in specific files:
d3.csv ('csv-file-here.csv', callbackfunction);
OR
d3.json('json-file-here.json', callbackfunction);
But now that I need D3 to "access" or "pull" from Firebase, I'm a little bit lost as to how to do that. Any help would be greatly appreciated.
Edit:
Okay, so I discovered this: https://github.com/mattdodge/D3Fire which can make Firebase and D3 talk to each other. However, I tried following the instructions and still can't get anything to work. That "Hello World" and console.log statement does not print anything out.
d3.select('svg').firebase(
'https://mydatabaseURL.com',
{
createFunc : function(newData) {
// callback when data is added
console.log(newData.val());
d3.select("body").append("span").text("Hello, world!");
return this.append('text').text(newData.val());
},
updateFunc : function(changedData) {
// data was changed, let's change the text
this.text(changedData.val());
}
}
);
Edit 2:
If I did it without the above plugin, nothing is printed out either...
var ref = firebase.database().ref();
ref.on('value', function(dataSnapshot){
console.log(dataSnapshot.val());
});
Edit 3:
My issue was in my database security rules. I reverted that back to default and now I am able to establish a connection with Firebase and get the snapshot to print out my database as an object array. Now, I need to figure out how to actually load that data into D3. I tried d3.json(dataSnapshot.val(), function(rawdata){}) but that didn't load it in.
Edit 4:
Okay, the D3 docs seem to indicate that d3.json() takes a URL for the location where the request for the JSON can be made. So, if it is in a local folder, data.json will suffice. Because I want to get it from Firebase, I need to find out a way to load in that Firebase as a JSON. So I tried
d3.json('https://mydatabasename.firebaseio.com/.json', update);
which still did not work. Printing out the raw data gives me undefined.
console.logstatements everywhere and it seems to run theref.onstatement but doesn't print anything out. - noblerare