树映射d3 JSON数组子对象的子对象

树映射d3 JSON数组子对象的子对象,json,d3.js,children,treemaps,Json,D3.js,Children,Treemaps,我从HTTP get请求返回了这个JSON对象 { "statuses": [ { "metadata": { "result_type": "popular", "iso_language_code": "en" }, "created_at": "Sun Feb 16 19:38:00 +0000 2014", "id": 435135996314000000, "id_str": "43

我从HTTP get请求返回了这个JSON对象

{
  "statuses": [
    {
      "metadata": {
        "result_type": "popular",
        "iso_language_code": "en"
      },
      "created_at": "Sun Feb 16 19:38:00 +0000 2014",
      "id": 435135996314000000,
      "id_str": "435135996314001408",
      "text": "While #stackoverflow is down, probably best just to dm @jonskeet directly ;p (just kidding: DO NOT DO THIS!!!)",
      "source": "<a href=\"https://chrome.google.com/extensions/detail/encaiiljifbdbjlphpgpiimidegddhic\" rel=\"nofollow\">Silver Bird</a>",
      "truncated": false,
      "in_reply_to_status_id": null,
      "in_reply_to_status_id_str": null,
      "in_reply_to_user_id": null,
      "in_reply_to_user_id_str": null,
      "in_reply_to_screen_name": null,
      "user": {
        "id": 27306000,
        "id_str": "27306000",
        "name": "Marc Gravell",
        "screen_name": "marcgravell",
        "location": "UK",
        "description": "Code geek working for stackoverflow; C# MVP and author of protobuf-net; dad, husband and school governor",
        "url": "http://t.co/pXdKmxWNCw",
        "entities": {
          "url": {
            "urls": [
              {
                "url": "http://t.co/pXdKmxWNCw",
                "expanded_url": "http://marcgravell.blogspot.com/",
                "display_url": "marcgravell.blogspot.com",
                "indices": [
                  0,
                  22
                ]
              }
            ]
          },
          "description": {
            "urls": []
          }
        },
        "protected": false,
        "followers_count": 5315,
        "friends_count": 327,
        "listed_count": 257,
        "created_at": "Sat Mar 28 20:52:41 +0000 2009",
        "favourites_count": 7,
        "utc_offset": 0,
        "time_zone": "London",
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 5743,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "C0DEED",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/976862617/mgravell_normal.jpeg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/976862617/mgravell_normal.jpeg",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "C0DEED",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": true,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": null,
      "coordinates": null,
      "place": null,
      "contributors": null,
      "retweet_count": 58,
      "favorite_count": 29,
      "entities": {
        "hashtags": [
          {
            "text": "stackoverflow",
            "indices": [
              6,
              20
            ]
          }
        ],
        "symbols": [],
        "urls": [],
        "user_mentions": [
          {
            "screen_name": "jonskeet",
            "name": "Jon Skeet",
            "id": 18193572,
            "id_str": "18193572",
            "indices": [
              55,
              64
            ]
          }
        ]
      },
      "favorited": false,
      "retweeted": false,
      "lang": "en"
    },
    {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "created_at": "Tue Feb 18 10:50:10 +0000 2014",
      "id": 435727938411950000,
      "id_str": "435727938411954176",
      "text": "http://t.co/Gfxq6WXiDi Stack Overflow Goes Down, Programmers Around The World Panic (It’s Back Up Now) #stackoverflow #running #backup #pa",
      "source": "<a href=\"http://www.heystartup.com\" rel=\"nofollow\">Heystartup</a>",
      "truncated": false,
      "in_reply_to_status_id": null,
      "in_reply_to_status_id_str": null,
      "in_reply_to_user_id": null,
      "in_reply_to_user_id_str": null,
      "in_reply_to_screen_name": null,
      "user": {
        "id": 448458813,
        "id_str": "448458813",
        "name": "heystartup",
        "screen_name": "heystartup",
        "location": "Sydney, Australia",
        "description": "HeyStartup is an online pitching platform for entrepreneurs and startups to receive feedback from others.",
        "url": "http://t.co/8E9Y7cb9aB",
        "entities": {
          "url": {
            "urls": [
              {
                "url": "http://t.co/8E9Y7cb9aB",
                "expanded_url": "http://heystartup.com",
                "display_url": "heystartup.com",
                "indices": [
                  0,
                  22
                ]
              }
            ]
          },
          "description": {
            "urls": []
          }
        },
        "protected": false,
        "followers_count": 94,
        "friends_count": 11,
        "listed_count": 1,
        "created_at": "Wed Dec 28 01:19:13 +0000 2011",
        "favourites_count": 0,
        "utc_offset": 39600,
        "time_zone": "Sydney",
        "geo_enabled": true,
        "verified": false,
        "statuses_count": 4708,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "C0DEED",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/2547017608/leay8yfmekftmi69znye_normal.png",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/2547017608/leay8yfmekftmi69znye_normal.png",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "C0DEED",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": true,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": null,
      "coordinates": null,
      "place": null,
      "contributors": null,
      "retweet_count": 0,
      "favorite_count": 0,
      "entities": {
        "hashtags": [
          {
            "text": "stackoverflow",
            "indices": [
              103,
              117
            ]
          },
          {
            "text": "running",
            "indices": [
              118,
              126
            ]
          },
          {
            "text": "backup",
            "indices": [
              127,
              134
            ]
          },
          {
            "text": "pa",
            "indices": [
              135,
              138
            ]
          }
        ],
        "symbols": [],
        "urls": [
          {
            "url": "http://t.co/Gfxq6WXiDi",
            "expanded_url": "http://bit.ly/1eLJdo2",
            "display_url": "bit.ly/1eLJdo2",
            "indices": [
              0,
              22
            ]
          }
        ],
        "user_mentions": []
      },
      "favorited": false,
      "retweeted": false,
      "possibly_sensitive": false,
      "lang": "en"
    },
    {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "created_at": "Tue Feb 18 10:50:08 +0000 2014",
      "id": 435727927515560000,
      "id_str": "435727927515561984",
      "text": "Using @TextExpander is a good start at being more polite when closing/flagging #StackOverflow questions/answers.",
      "source": "<a href=\"http://tapbots.com/software/tweetbot/mac\" rel=\"nofollow\">Tweetbot for Mac</a>",
      "truncated": false,
      "in_reply_to_status_id": null,
      "in_reply_to_status_id_str": null,
      "in_reply_to_user_id": null,
      "in_reply_to_user_id_str": null,
      "in_reply_to_screen_name": null,
      "user": {
        "id": 59375148,
        "id_str": "59375148",
        "name": "David Rönnqvist",
        "screen_name": "davidronnqvist",
        "location": "Stockholm, Sweden",
        "description": "If you animate your app using an NSTimer, I will find you. Writing @SceneKitBook",
        "url": "http://t.co/zLGdRWJxX8",
        "entities": {
          "url": {
            "urls": [
              {
                "url": "http://t.co/zLGdRWJxX8",
                "expanded_url": "http://ronnqvi.st",
                "display_url": "ronnqvi.st",
                "indices": [
                  0,
                  22
                ]
              }
            ]
          },
          "description": {
            "urls": []
          }
        },
        "protected": false,
        "followers_count": 387,
        "friends_count": 275,
        "listed_count": 18,
        "created_at": "Thu Jul 23 05:42:40 +0000 2009",
        "favourites_count": 120,
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": true,
        "verified": false,
        "statuses_count": 1525,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "C0DEED",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_tile": false,
        "profile_image_url": "http://pbs.twimg.com/profile_images/378800000304436351/ce5a22f824ff1b3ae4aab042b6d8db2a_normal.jpeg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/378800000304436351/ce5a22f824ff1b3ae4aab042b6d8db2a_normal.jpeg",
        "profile_banner_url": "https://pbs.twimg.com/profile_banners/59375148/1376604305",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "C0DEED",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": true,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": null,
      "coordinates": null,
      "place": {
        "id": "d56c5babcffde8ef",
        "url": "https://api.twitter.com/1.1/geo/id/d56c5babcffde8ef.json",
        "place_type": "city",
        "name": "Stockholm",
        "full_name": "Stockholm, Stockholm",
        "country_code": "SE",
        "country": "Sweden",
        "contained_within": [],
        "bounding_box": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                17.7601322,
                59.2271383
              ],
              [
                18.199914,
                59.2271383
              ],
              [
                18.199914,
                59.4402037
              ],
              [
                17.7601322,
                59.4402037
              ]
            ]
          ]
        },
        "attributes": []
      },
      "contributors": null,
      "retweet_count": 0,
      "favorite_count": 0,
      "entities": {
        "hashtags": [
          {
            "text": "StackOverflow",
            "indices": [
              79,
              93
            ]
          }
        ],
        "symbols": [],
        "urls": [],
        "user_mentions": [
          {
            "screen_name": "TextExpander",
            "name": "TextExpander",
            "id": 25610613,
            "id_str": "25610613",
            "indices": [
              6,
              19
            ]
          }
        ]
      },
      "favorited": false,
      "retweeted": false,
      "lang": "en"
    },
    {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "created_at": "Tue Feb 18 10:00:35 +0000 2014",
      "id": 435715461074800000,
      "id_str": "435715461074804736",
      "text": "#Bing Code Search for Visual Studio 2013 helps you find samples from #StackOverflow, MSDN and more http://t.co/4iXXDuo7Vd",
      "source": "web",
      "truncated": false,
      "in_reply_to_status_id": null,
      "in_reply_to_status_id_str": null,
      "in_reply_to_user_id": null,
      "in_reply_to_user_id_str": null,
      "in_reply_to_screen_name": null,
      "user": {
        "id": 481029248,
        "id_str": "481029248",
        "name": "JigsLetsNurture",
        "screen_name": "JigsThanki1987",
        "location": "#Ahmedabad, #Gujarat",
        "description": "Like #traveling, watching #movies:) #SEO, #socialmedia expert at @letsnurture. Get up. Be awesome. Go back to bed.. #Bloodmonk (O-Negative)",
        "url": "http://t.co/wMDh95OiPB",
        "entities": {
          "url": {
            "urls": [
              {
                "url": "http://t.co/wMDh95OiPB",
                "expanded_url": "http://www.letsnurture.com",
                "display_url": "letsnurture.com",
                "indices": [
                  0,
                  22
                ]
              }
            ]
          },
          "description": {
            "urls": []
          }
        },
        "protected": false,
        "followers_count": 651,
        "friends_count": 1946,
        "listed_count": 21,
        "created_at": "Thu Feb 02 06:37:56 +0000 2012",
        "favourites_count": 143,
        "utc_offset": 19800,
        "time_zone": "Chennai",
        "geo_enabled": false,
        "verified": false,
        "statuses_count": 22819,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "is_translation_enabled": false,
        "profile_background_color": "FCFCFC",
        "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/378800000076905957/74717a41d51ccc0a88b6bf2379f3c6d9.png",
        "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/378800000076905957/74717a41d51ccc0a88b6bf2379f3c6d9.png",
        "profile_background_tile": true,
        "profile_image_url": "http://pbs.twimg.com/profile_images/1798499460/jigs_normal.jpg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/1798499460/jigs_normal.jpg",
        "profile_link_color": "0084B4",
        "profile_sidebar_border_color": "FFFFFF",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": false,
        "follow_request_sent": false,
        "notifications": false
      },
      "geo": null,
      "coordinates": null,
      "place": null,
      "contributors": null,
      "retweet_count": 0,
      "favorite_count": 1,
      "entities": {
        "hashtags": [
          {
            "text": "Bing",
            "indices": [
              0,
              5
            ]
          },
          {
            "text": "StackOverflow",
            "indices": [
              69,
              83
            ]
          }
        ],
        "symbols": [],
        "urls": [
          {
            "url": "http://t.co/4iXXDuo7Vd",
            "expanded_url": "http://tinyurl.com/nenrx3z",
            "display_url": "tinyurl.com/nenrx3z",
            "indices": [
              99,
              121
            ]
          }
        ],
        "user_mentions": []
      },
      "favorited": false,
      "retweeted": false,
      "possibly_sensitive": false,
      "lang": "en"
    }
  ],
  "search_metadata": {
    "completed_in": 0.02,
    "max_id": 435727938411950000,
    "max_id_str": "435727938411954176",
    "next_results": "?max_id=435135996314001407&q=%23stackoverflow&count=3&include_entities=1&result_type=mixed",
    "query": "%23stackoverflow",
    "refresh_url": "?since_id=435727938411954176&q=%23stackoverflow&result_type=mixed&include_entities=1",
    "count": 3,
    "since_id": 0,
    "since_id_str": "0"
  }
}
{
“状态”:[
{
“元数据”:{
“结果类型”:“流行”,
“iso语言代码”:“en”
},
“创建时间”:“2014年2月16日星期日19:38:00+0000”,
“id”:435135996314000000,
“id_街”:“435135996314001408”,
“文本”:“当stackoverflow关闭时,最好直接到dm@jonskeet;p(开玩笑:不要这样!!!)”,
“来源”:“,
“截断”:false,
“在对状态id的回复中”:null,
“在对状态的回复中”id“str”:空,
“in_reply_to_user_id”:null,
“在对用户id的回复中”:null,
“回复至屏幕名称”为空,
“用户”:{
“id”:27306000,
“id_街”:“27306000”,
“名称”:“Marc Gravell”,
“屏幕名称”:“MarcGravel”,
“地点”:“英国”,
“描述”:“为stackoverflow工作的代码极客;C#MVP和protobuf net的作者;父亲、丈夫和学校管理者”,
“url”:”http://t.co/pXdKmxWNCw",
“实体”:{
“url”:{
“URL”:[
{
“url”:”http://t.co/pXdKmxWNCw",
“扩展url”:http://marcgravell.blogspot.com/",
“显示url”:“marcgravel.blogspot.com”,
“指数”:[
0,
22
]
}
]
},
“说明”:{
“URL”:[]
}
},
“受保护”:错误,
“粉丝数”:5315,
“朋友数”:327,
“列出的数量”:257,
“创建时间”:“2009年3月28日星期六20:52:41+0000”,
“最受欢迎项”统计:7,
“utc_偏移量”:0,
“时区”:“伦敦”,
“已启用地理位置”:false,
“已验证”:错误,
“状态计数”:5743,
“郎”:“恩”,
“已启用贡献者”:false,
“is_translator”:错误,
“是否已启用翻译”:false,
“配置文件\背景\颜色”:“C0”,
“配置文件\背景\图像\ url”:http://abs.twimg.com/images/themes/theme1/bg.png",
“配置文件\u背景\u图像\u url\u https”:https://abs.twimg.com/images/themes/theme1/bg.png",
“配置文件\u背景\u平铺”:false,
“配置文件\图像\ url”:http://pbs.twimg.com/profile_images/976862617/mgravell_normal.jpeg",
“配置文件\u图像\u url\u https”:https://pbs.twimg.com/profile_images/976862617/mgravell_normal.jpeg",
“配置文件链接颜色”:“0084B4”,
“配置文件\侧边栏\边框\颜色”:“C0”,
“配置文件\侧边栏\填充\颜色”:“DDEEF6”,
“配置文件\文本\颜色”:“333333”,
“profile\u use\u background\u image”:真,
“默认_配置文件”:true,
“默认_配置文件_图像”:false,
"跟随":假,,
“跟踪请求发送”:false,
“通知”:错误
},
“geo”:空,
“坐标”:空,
“地点”:空,
“贡献者”:空,
“转发次数”:58,
“最喜欢的”计数:29,
“实体”:{
“hashtags”:[
{
“文本”:“堆栈溢出”,
“指数”:[
6.
20
]
}
],
“符号”:[],
“URL”:[],
“用户_提到”:[
{
“屏幕名称”:“jonskeet”,
“姓名”:“Jon Skeet”,
“id”:18193572,
“id_str”:“18193572”,
“指数”:[
55,
64
]
}
]
},
“偏爱”:错误,
“转发”:错误,
“郎”:“恩”
},
{
“元数据”:{
“结果类型”:“最近”,
“iso语言代码”:“en”
},
“创建时间”:“2014年2月18日星期二10:50:10+0000”,
“id”:435727938411950000,
“id_街”:“435727938411954176”,
“文本”:http://t.co/Gfxq6WXiDi 堆栈溢出下降,全世界的程序员都惊慌失措(它现在恢复了)#堆栈溢出#运行#备份#pa#,
“来源”:“,
“截断”:false,
“在对状态id的回复中”:null,
“在对状态的回复中”id“str”:空,
“in_reply_to_user_id”:null,
“在对用户id的回复中”:null,
“回复至屏幕名称”为空,
“用户”:{
“id”:448458813,
“id_str”:“448458813”,
“名称”:“heystartup”,
“屏幕名称”:“heystartup”,
“地点”:“澳大利亚悉尼”,
“说明”:“HeyStartup是一个在线投手平台,供创业者和初创公司从他人那里获得反馈。”,
“url”:”http://t.co/8E9Y7cb9aB",
“实体”:{
“url”:{
“URL”:[
{
“url”:”http://t.co/8E9Y7cb9aB",
“扩展url”:http://heystartup.com",
“显示url”:“heystartup.com”,
“指数”:[
0,
22
]
}
]
},
“说明”:{
“URL”:[]
}
},
“受保护”:错误,
“粉丝数”:94,
“朋友数”:11,
“列出的计数”:1,
“创建时间”:“Wed Dec 28 01:19:13+0000 2011”,
“收藏夹计数”:0,
“utc_偏移量”:39600,
“时区”:“悉尼”,
“geo_enabled”:正确,
“已验证”:错误,
“状态计数”:4708,
“郎”:“恩”,
“已启用贡献者”:false,
“is_translator”:错误,
“是否已启用翻译”:false,
“配置文件\背景\颜色”:“C0”,
“配置文件\背景\图像\ url”:http://abs.twimg.com/images/themes/theme1/bg.png",
“配置文件\u背景\u图像\u url\u https”:https://abs.twimg.com/images/themes/theme1/bg.png",
“配置文件\u背景\u平铺”:false,
“配置文件\图像\ url”:http://pbs.twimg.com/profile_images/2547017608/leay8yfmekftmi69znye_normal.png",
“配置文件\u图像\u url\u https”:https://pbs.twimg.com/profile_images/2547017608/l
<!DOCTYPE html>
<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
    <title>TREE MAP</title>

</head>
<body>

<script src="http://d3js.org/d3.v3.min.js"></script>


<script>


var color = d3.scale.category10();

console.log('fetching data');

var canvas = d3.select("body").append("svg")
        .attr("width",1000)
        .attr("height",800)

d3.json("searchresults.json", function(error, dataset) {


    organizedata(dataset);
    console.log('done fetching data');
    console.log('Printing data');




function organizedata (dataset){

var tweets = (dataset.statuses);
var tweetinfo = (dataset.search_metadata);

    console.log(dataset);
    console.log(tweets); //creates an JSON array with Tweet Objects
    console.log(tweetinfo); //creates a seperate JSON for the search details

    drawtreemap (dataset);



//console.log(tweets);

function drawtreemap (dataset){

     var margin = {top: 0, right: 10, bottom: 20, left: 10}, width = 1000, height = 800;

    var treemap = d3.layout.treemap()
        //.children(function(d) { return d.text})
        //.children(function (d) {return d.array})
        .children(function (d) {return d.statuses})
        .size([width,height])
        .value(function (d) {return d.id})
        .nodes(dataset)


    var cells = canvas.selectAll(".cell")
            .data(treemap)
            .enter()
            .append("g")
            .attr("clas", "cell")


        cells.append("rect")
            .attr("x",       function (d)  {return d.x; })
            .attr("y",       function (d)  {return d.y; })
            .attr("width",   function (d)  {return d.dx; })
            .attr("height",  function (d)  {return d.dy;})
            .attr("fill",    function (d)  {return d.children ? null : color(d.id/10);})


        cells.append("text")

            .attr("x",       function (d)  {return d.x + d.dx/2 })
            .attr("y",       function (d)  {return d.y + d.dy/2  })
            //.attr("x",       function (d)  {return d.x + d.dx / 2 })
            //.attr("y",         function (d)  {return d.y + d.dy / 2  })
            .text(           function (d) {return d.text; } )
            .style("text-anchor", "middle")



console.log(treemap);

}}  
})

</script>
</body> 
</html>