0

I'm trying to extract value from this json file , so far I can see my object in console , but how can I extract the target value to be use in d3

d3.json "https://api.github.com/repos/wesm/D3py/commits", (data) -> 
  console.log data[0]
  return

Here's the json https://api.github.com/repos/wesm/D3py/commits

enter image description here

ultimately I want to be able to use it to create svg circle using these element , so I am planning on put the value into the simple list like this ...

svg_w = 800
svg_h = 400
padding = 50
svg = d3.select("body").append("svg").attr("width", svg_w).attr("height", svg_h)


list = [
  { "item": 1, "date": "2012-01-04T01:39:42Z",  "commit_name": "Mike Dewar", "id":1, "message" : "clenaed up typos in README" },
  { "item": 2, "date": "2012-01-04T01:37:33Z",  "commit_name": "Mike Dewar", "id":2, "message" : "updated the README to point people at the v2 branch" },
  { "item": 3, "date": "2011-12-16T03:09:41Z",  "commit_name": "Mike Dewar", "id":2, "message" : "added ignore file" },
  { "item": 4, "date": "2011-10-06T12:05:53Z",  "commit_name": "Mike Dewar", "id":2, "message" : "merging" },
  { "item": 5, "date": "2011-08-16T20:48:02Z",  "commit_name": "Mike Dewar", "id":3, "message" : "added time series" }]


names = (m.item for m in list)
console.log names


nodes = svg.append("g").attr("class", "nodes").selectAll("circle")
        .data(names).enter().append("g")
            .attr("transform", (d, i) ->

                  dx = i * 70 + padding
                  dy = svg_h / 2

                  "translate(" + dx + "," + dy + ")"

                )

nodes.append("circle").attr("class", "node").attr "r", 20

nodes.append("text").attr("text-anchor", "middle").text (d) ->d.name
JPC
  • 5,063
  • 20
  • 71
  • 100

2 Answers2

1

Have you tried something like this?

(JS code)

var list = [],
    dataLength = data.length;

for (var i = 0, item; i < dataLength; i++) {
    var obj = {};

    item = data[i];

    // You may need to check here for every property you're gonna access:
    // item.commit, item.commit.author, item.commit.message, item.commit.author.date, item.author, item.author.name
    if (item && item.commit) {
        obj.item = i;
        obj.date = item.commit.author.date;
        obj['commit_name'] = item.author.name;
        // Populate id here based on your logic...
        //obj.id = someId;
        obj.message = item.commit.message;

        // Push newly created obj to list.
        list[list.length] = obj;
    }
}
Nicolae Olariu
  • 2,487
  • 2
  • 18
  • 30
  • I'm new to JS and coffee, can you show me the full code using d3.json "https://api.github.com/repos/wesm/D3py/commits", (data) -> console.log data[0] , i have no idea what I suppose to do. – JPC Feb 24 '14 at 17:06
1

You can map your data with the specific attributes to a new array. Coffeescipt example:

list = data[0].map (item) ->
   return
      author: 
        name: item.commit.author.name
        date: item.commit.author.date
      ...
defreek
  • 71
  • 3