{"id":2755,"date":"2013-04-19T17:00:48","date_gmt":"2013-04-19T08:00:48","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2755"},"modified":"2013-12-30T19:46:38","modified_gmt":"2013-12-30T10:46:38","slug":"post-2755","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3-js\/2755","title":{"rendered":"\u3010D3.js\u3011\u53c2\u7167\u5143\u5225\u306e\u8a2a\u554f\u8005\u6570\u3092\u30c4\u30ea\u30fc \u30de\u30c3\u30d7\u3067\u8868\u793a\u3059\u308b"},"content":{"rendered":"<p><a href=\"http:\/\/anond.hatelabo.jp\/20130418163746\">\u6d41\u884c\u308b\u8a18\u4e8b\u3001\u6d41\u884c\u3089\u306a\u3044\u8a18\u4e8b[\u8ffd\u8a18\u3042\u308a]<\/a><\/p>\n<p>\u3053\u308c\u306f\u8eab\u306b\u3064\u307e\u3055\u308c\u307e\u3059\u3002<br \/>\n\u4f53\u611f\u3067\u306f\u3001\u6642\u9593\u3092\u304b\u3051\u305f\u30a8\u30f3\u30c8\u30ea\u30fc\u307b\u3069\u306f\u3066\u30d6\u3055\u308c\u306a\u3044\u6c17\u304c\u3002<br \/>\n\u305f\u3060\u3001\u30d6\u30ed\u30b0\u306e\u30b8\u30e3\u30f3\u30eb\u306b\u3082\u3088\u308a\u307e\u3059\u304c\u3001\u9577\u3044\u76ee\u3067\u307f\u308b\u3068\u30bd\u30fc\u30b7\u30e3\u30eb\u30e1\u30c7\u30a3\u30a2\u304b\u3089\u306e\u6d41\u5165\u3088\u308a\u691c\u7d22\u30a8\u30f3\u30b8\u30f3\u304b\u3089\u306e\u6d41\u5165\u306e\u65b9\u304c\u30d6\u30ed\u30b0\u5168\u4f53\u306e\u30a2\u30af\u30bb\u30b9\u30a2\u30c3\u30d7\u306b\u306f\u8ca2\u732e\u3057\u3066\u304f\u308c\u305f\u308a\u3082\u3059\u308b\u306e\u3067\u3001\u6c17\u306b\u3057\u3059\u304e\u306a\u3044\u65b9\u304c\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002(\u305f\u307e\u306b\u306f\u3066\u30d6\u3055\u308c\u305f\u308a\u3059\u308b\u3068\u3001\u4e00\u6642\u7684\u306bPV\u304c\u8df3\u306d\u4e0a\u304c\u308b\u306e\u3067\u3069\u3046\u3057\u3066\u3082\u6c17\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3051\u3069\u306d)<\/p>\n<p>\u305d\u3053\u3067\u3001\u53c2\u7167\u5143\u5225\u8a2a\u554f\u8005\u6570\u306e\u6bd4\u7387\u3092\u518d\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306bTreeMap\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/shimz.me\/\/example\/d3js\/treemap\/01\/\">\u8a2a\u554f\u8005\u6570\uff0f\u65b0\u898f\u8a2a\u554f\u306e\u5272\u5408\uff08TreeMap\uff09<\/a><\/p>\n<h2>\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u4f5c\u6210<\/h2>\n<p>Google\u30a2\u30ca\u30ea\u30c6\u30a3\u30af\u30b9\u300c\u30c8\u30e9\u30d5\u30a3\u30c3\u30af\u2192\u53c2\u7167\u5143\u2192\u3059\u3079\u3066\u306e\u30c8\u30e9\u30d5\u30a3\u30c3\u30af\u300d\u306e\u30c7\u30fc\u30bf\u3092CSV\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/WS000010.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/WS000010-300x122.jpg\" alt=\"WS000010\" width=\"300\" height=\"122\" class=\"alignnone size-medium wp-image-2756\" \/><\/a><br \/>\n\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u305fCSV\u3092\u30c6\u30ad\u30b9\u30c8\u30a8\u30c7\u30a3\u30bf\u7b49\u3067\u6574\u5f62\u3057\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/shimz.me\/\/example\/d3js\/treemap\/01\/trafficSources.csv\">trafficSources.csv(\u6574\u5f62\u6e08)<\/a><\/p>\n<h2>TreeMap\u3068\u3057\u3066\u8868\u793a<\/h2>\n<p>D3.js\u306eTreeMap\u30ec\u30a4\u30a2\u30a6\u30c8\u3092\u4f7f\u7528\u3057\u3066\u8868\u793a\u3057\u307e\u3059\u3002<br \/>\n\u30d8\u30eb\u30d1\u30fc\u95a2\u6570\u3068\u3057\u3066\u3001<a href=\"https:\/\/gunmagisgeek.com\/blog\/d3-js\/2649\">\u3053\u3061\u3089\u306e\u8a18\u4e8b<\/a>\u3067\u4f5c\u6210\u3057\u305f\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >d3.csv('trafficSources.csv', function(csv){\n    csv = csv.slice(0, 15); \/\/\u8868\u793a\u3059\u308b\u53c2\u7167\u5143\u309215\u4ef6\u306b\u7d5e\u308a\u8fbc\u307f\n\n    var data = { children:csv }; \/\/csv\u304b\u3089\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u5168\u3066root\u306e\u914d\u4e0b(children)\u306b\u8ffd\u52a0\u3059\u308b\n    \n    var svg = d3.select('svg');  \/\/TreeMap\u3092\u63cf\u753b\u3059\u308b\u30b9\u30c6\u30fc\u30b8\u3092\u9078\u629e\n                        \n    \/\/Treemap\u306ebox\u30b5\u30a4\u30ba\u306b\u5bfe\u5fdc\u3055\u305b\u308b\u8981\u7d20\u3092\u6307\u5b9a\n    var retVisits = function(d){ return parseInt(d.Visits) };\n    var retNew_Visits = function(d){ return parseInt(d.New_Visits.replace('%', '')) };\u3000\/\/\u300c\u65b0\u898f\u8a2a\u554f\u8005\u306e\u5272\u5408\u300d\u306f%\u3092\u524a\u9664\u3057\u3066\u6570\u5024\u306b\u5909\u63db\u3059\u308b\n\n    var w = 1000;   \n    var h = 600; \n\n    var treemap = d3.layout.treemap().size([w, h]); \/\/Treemap\u30ec\u30a4\u30a2\u30a6\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3002Treemap\u306e\u30b5\u30a4\u30ba(\u7e26\u6a2a)\u3092\u6307\u5b9a\n    var TreemapData = treemap.value(retVisits).nodes(data); \/\/Visits\u306e\u5024\u3092\u57fa\u6e96\u306bTreemap\u7528\u306e\u30c7\u30fc\u30bf\u306b\u5909\u63db\u3059\u308b\n\n    var boxStyle = { \/\/box\u30b9\u30bf\u30a4\u30eb\u6307\u5b9a\n        x:F('x'),\n        y:F('y'),\n        width:F('dx'),\n        height:F('dy')  \n    }\n    \n    var boxColorStyle = function(d){\u3000\/\/\u53c2\u7167\u5143\u306e\u30b8\u30e3\u30f3\u30eb\u5225\u306bfillcolor\u3092\u6307\u5b9a\n        var color = \"blue\";\n        switch(d.Source){\n            case 'yahoo':\n            case 'google.co.jp':\n            case 'google':color = 'green';break;\n            case 'facebook.com':\n            case 'm.facebook.com':\n            case 't.co': \n            case 'b.hatena.ne.jp':color = 'red';break;\n        }\n        return color\n    }\n    \n    \n    var boxGroup = svg.selectAll(\"g\") \/\/Box\u30b0\u30eb\u30fc\u30d7\u3092\u8ffd\u52a0\n        .data(TreemapData) \n        .enter()\n        .append(\"g\"); \n\n    var box =  boxGroup.append('rect') \/\/\u5404Box\u3092Box\u30b0\u30eb\u30fc\u30d7\u306b\u8ffd\u52a0\n        .attr({\n            class:F('Source'),\n            fill:boxColorStyle, \n            stroke: \"black\",\n            \"fill-opacity\": 0.5\n        })\n        .attr(boxStyle)\n\n    var textStyle = { \/\/\u30e9\u30d9\u30eb\u30b9\u30bf\u30a4\u30eb\u6307\u5b9a\n        x:F('x', ' + 5'),\n        y:F('y', ' + 20')\n    }\n\n    var text = boxGroup.append('text') \/\/\u30e9\u30d9\u30eb\u8ffd\u52a0\n        .attr({\n            fill:\"white\",\n            \"font-size\": 12\n        })\n        .attr(textStyle)\n        .text(F('Source')); \/\/\u53c2\u7167\u5143(Source)\u3092\u8868\u793a\n\n\n    var style1 = function(){ \/\/\u8a2a\u554f\u8005\u6570(Visits)\u3092\u57fa\u6e96\u3068\u3057\u305f\u30b9\u30bf\u30a4\u30eb\n        boxGroup.data(treemap.value(retVisits).nodes(data));\n        box.transition().attr(boxStyle).duration(1000);\n        text.transition().attr(textStyle).duration(1000);    \n    }\n    var style2 = function(){ \/\/\u65b0\u898f\u8a2a\u554f\u8005\u6570\u306e\u5272\u5408(New_Visits)\u3092\u57fa\u6e96\u3068\u3057\u305f\u30b9\u30bf\u30a4\u30eb\n        boxGroup.data(treemap.value(retNew_Visits).nodes(data));\n        box.transition().attr(boxStyle).duration(1000);\n        text.transition().attr(textStyle).duration(1000);    \n    }\n\n    \/\/\u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af\u30a4\u30d9\u30f3\u30c8\u306e\u8a2d\u5b9a\n    d3.select('#Visits_btn').on('click', style1)\n    d3.select('#New_Visits_btn').on('click', style2)\n});<\/pre>\n<p><a href=\"http:\/\/shimz.me\/\/example\/d3js\/treemap\/01\/\">example<\/a><\/p>\n<h2>\u7d50\u679c<\/h2>\n<p>\u8a2a\u554f\u8005\u6570\u306e\u591a\u3044\u53c2\u7167\u5143\u307b\u3069\u5927\u304d\u306aBOX\u3067\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\n\u8d64\u3000\uff1d\u3000\u306f\u3066\u30d6\uff0fTwitter\uff0fFacebook\u306a\u3069\u30bd\u30fc\u30b7\u30e3\u30eb\u30e1\u30c7\u30a3\u30a2<br \/>\n\u7dd1\u3000\uff1d\u3000\u691c\u7d22\u30a8\u30f3\u30b8\u30f3<br \/>\n\u9752\u3000\uff1d\u3000\u305d\u308c\u4ee5\u5916<br \/>\n<a href=\"http:\/\/shimz.me\/\/example\/d3js\/treemap\/01\/\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/WS000011.jpg\" alt=\"WS000011\" width=\"590\" height=\"347\" class=\"alignnone size-full wp-image-2758\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/WS000011.jpg 590w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/04\/WS000011-255x150.jpg 255w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><br \/>\n<\/a><br \/>\n\u56f3\u306b\u3057\u3066\u307f\u308b\u3068\u3001\u6570\u5b57\u3067\u307f\u308b\u3088\u308a\u308f\u304b\u308a\u3084\u3059\u3044\u6c17\u304c\u3057\u307e\u3059\u306d\u3002<br \/>\n\u4eca\u56de\u306f\u3001\u76f4\u8fd1\uff11\u30f6\u6708\u5206\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3044\u307e\u3057\u305f\u304c\u3001\u5e74\u672b\u3042\u305f\u308a\u306b\uff11\u5e74\u9593\u5206\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u3082\u3063\u3068\u7d30\u304b\u3044\u5185\u5bb9\u306e\u30c4\u30ea\u30fc\u30de\u30c3\u30d7\u3092\u4f5c\u3063\u3066\u307f\u3088\u3046\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d41\u884c\u308b\u8a18\u4e8b\u3001\u6d41\u884c\u3089\u306a\u3044\u8a18\u4e8b[\u8ffd\u8a18\u3042\u308a] \u3053\u308c\u306f\u8eab\u306b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2758,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-d3-js","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2755"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2755\/revisions"}],"predecessor-version":[{"id":2940,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2755\/revisions\/2940"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/2758"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}