Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 - reading JSON data instead of CSV file

Tags:

I'm trying to read data into my calendar visualisation using JSON. At the moment it works great using a CSV file:

d3.csv("RSAtest.csv", function(csv) {   var data = d3.nest()     .key(function(d) { return d.date; })     .rollup(function(d) { return d[0].total; })     .map(csv);   rect.filter(function(d) { return d in data; })       .attr("class", function(d) { return "day q" + color(data[d]) + "-9"; })       .select("title")       .text(function(d) { return d + ": " + data[d]; });  }); 

It reads the following CSV data:

date,total 2000-01-01,11 2000-01-02,13 . . .etc 

Any pointers on how I can read the following JSON data instead: {"2000-01-01":19,"2000-01-02":11......etc}

I tried the following but it not working for me (datareadCal.php spits out the JSON for me):

d3.json("datareadCal.php", function(json) {   var data = d3.nest()     .key(function(d) { return d.Key; })     .rollup(function(d) { return d[0].Value; })     .map(json);  

thanks

like image 974
eoin Avatar asked Apr 09 '12 17:04

eoin


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.

Why is JSON preferred over CSV?

CSV does not support data hierarchies. In JSON, it is significantly easier to work within and mainly used for the programming languages, but JSON file becomes twice the CSV file when a lot of data is required or maintained in files. JSON is also having APIs, which automatically converts JSON into the native structure.

How do I import a CSV file into d3 JS?

D3 provides the following methods to load different types of data from external files. Sends http request to the specified url to load . csv file or data and executes callback function with parsed csv data objects. Sends http request to the specified url to load .

What does d3 JSON return?

The function d3. json() is an asynchronous function that directly returns (with an undefined value I assume). Only when the data is received from the backend, the callback function you passed to it will be called.


2 Answers

You can use d3.entries() to turn an object literal into an array of key/value pairs:

var countsByDate = {'2000-01-01': 10, ...}; var dateCounts = d3.entries(countsByDate); console.log(JSON.stringify(dateCounts[0])); // {"key": "2000-01-01", "value": 10} 

One thing you'll notice, though, is that the resulting array isn't properly sorted. You can sort them by key ascending like so:

dateCounts = dateCounts.sort(function(a, b) {     return d3.ascending(a.key, b.key); }); 
like image 115
Shawn Allen Avatar answered Sep 25 '22 03:09

Shawn Allen


Turn your .json file into a .js file that is included in your html file. Inside your .js file have:

var countsByDate = {'2000-01-01':10,...}; 

Then you can reference countsByDate....no need to read from a file per se.

And you can read it with:

var data = d3.nest() .key(function(d) { return d.Key; })           .entries(json);        

As an aside....d3.js says it's better to set your json up as:

var countsByDate = [   {Date: '2000-01-01', Total: '10'},   {Date: '2000-01-02', Total: '11'}, ]; 
like image 44
Ginny Avatar answered Sep 24 '22 03:09

Ginny