23

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

2 Answers 2

13

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);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Shawn, thanks for getting back to me on this but I'm still pretty lost. I was looking to change the d3 calendar example from using data read from CSV file to use a JSON: mbostock.github.com/d3/ex/calendar.html
Hi Shawn, it took me a while to figure out(I'm juggling a non-programming day job with night school and teaching myself data viz & programming..) but you were right the key is to format the JSON with the d3.entries. That worked but else where in my code I forgot to match the year of my empty calendar frame with the year of my data.In other words my empty calendar was for 1999 and my JSON data was for 2000. Thanks for all your help & patience.
6

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'},
];

1 Comment

This sounds really hacky

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.