树形图d3 JSON数组儿童

时间:2014-02-18 11:06:51

标签: json d3.js children treemaps

我有从HTTP get Request

返回的这个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"
  }
}

我打算使用这个json数据创建一个树形图,我面临的问题是,我想基于“hashtags”嵌套()数据,这是“子节点的子节点。我尝试使用”状态“作为子节点,但是hashtag值是实体数组中的一个数组,

My JSON来自发送http get请求的PHP文件,之后将其保存到.json文件中。 我从以下文件访问此.json。

有没有一种方法可以根据hashtag值对json进行分类?

以下是我的代码:

<!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>

这将返回以下输出,但是我想使用“hashtags”打印在文本上,并且还能够使用hastags作为nest()json的“key”值。请协助。

0 个答案:

没有答案