{"id":2649,"date":"2013-03-17T09:48:52","date_gmt":"2013-03-17T00:48:52","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2649"},"modified":"2014-07-21T16:18:49","modified_gmt":"2014-07-21T07:18:49","slug":"post-2649","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3-js\/2649","title":{"rendered":"\u3010D3.js\u3011\u4f5c\u3063\u3066\u304a\u304f\u3068\u4fbf\u5229\u306a\u95a2\u6570\u9054"},"content":{"rendered":"<h2>\u30b7\u30e7\u30fc\u30c8\u30ab\u30c3\u30c8<\/h2>\n<p>D3\u306f\u3069\u3046\u3057\u3066\u3082\u7121\u540d\u95a2\u6570\u3092\u591a\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001\u30b7\u30e7\u30fc\u30c8\u30ab\u30c3\u30c8\u3092\u4f5c\u3063\u3066\u304a\u304f\u3068\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >function D(params){\r\n    return function(d, i){\r\n        if(typeof params ==='function') { return params(d) }\r\n        else if( typeof params ==='string'){ return (new Function( 'return (' + d + params + ')' )()) }\r\n        else { return d};\r\n    }\r\n}\r\n\r\nfunction I(params){\r\n    return function(d, i){\r\n        if(typeof params ==='function') { return params(i) }\r\n        else if( typeof params ==='string'){ return (new Function( 'return (' + i + params + ')' )()) }\r\n        else { return i};\r\n    }\r\n}\r\n\r\nfunction F(name){\r\n  var params=Array.prototype.slice.call(arguments,1);  \r\n    return function(d){\r\n        if(typeof params[0] ==='function') { return params[0](d[name]) }\r\n        else if( typeof params[0] ==='string'){ return (new Function( 'return (' + d[name] + params[0] + ')' )()) }\r\n        else if( typeof name === 'object' ){ return name }\r\n        else { return d[name]};\r\n    }\r\n}\r\n<\/pre>\n<p>\u4f7f\u3044\u65b9<\/p>\n<pre class=\"lang:js decode:true \" title=\"sample\" >var data1 = [10, 20 30];\r\n\r\n\/\/before \r\n\r\nd3.selectAll('rect')\r\n  .data(data1)\r\n  .enter()\r\n  .append('rect')\r\n  .attr({\r\n     x:function(d){ return d },\r\n     y:function(d){ return d * 10 + 100 }\r\n  });\r\n\r\n\/\/ after \r\n\r\nd3.selectAll('rect')\r\n  .data(data1)\r\n  .enter()\r\n  .append('rect')\r\n  .attr({\r\n     x:D(),\r\n     y:D('* 10 + 100')\r\n  });\r\n\r\n\/\/ other\r\n\r\nvar data2 = [{x:10, y:20, color:10}, {x:10, y:20, color:20}, {x:10, y:20, color:30}}\r\nvar colorScale = d3.scale.category20();\r\n\r\nd3.selectAll('circle')\r\n  .data(data1)\r\n  .enter()\r\n  .append('circle')\r\n  .attr({\r\n     r:I(),\r\n     x:F('X'),\r\n     y:F('y', '* 10 + 100'),\r\n     color:F('color', colorScale),\r\n  });<\/pre>\n<h2>\u90e8\u5206\u9069\u7528\u3001\u30e9\u30f3\u30c0\u30e0\u3001\u30c7\u30fc\u30bf\u751f\u6210<\/h2>\n<p>curry\u306f\u53b3\u5bc6\u306b\u306f\u30ab\u30ea\u30fc\u5316\u3067\u306f\u306a\u304f\u90e8\u5206\u9069\u7528\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u899a\u3048\u3084\u3059\u3044\u306e\u3067\u3053\u306e\u540d\u524d\u3092\u4f7f\u3063\u3066\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" >if (!Function.prototype.curry){\r\n    (function(){\r\n\tvar slice = Array.prototype.slice;\r\n\t\r\n\tFunction.prototype.curry = function(){\r\n\t    var target = this;\r\n\t    var args = slice.call(arguments);\r\n\t    \r\n\t    return function(){\r\n\t\tvar allArgs = args;\r\n\t\tif (arguments.length &amp;gt; 0){\r\n\t\t    allArgs = args.concat(slice.call(arguments));\r\n\t\t}\r\n\t\treturn target.apply(this, allArgs);\r\n\t    };\r\n\t};\r\n    }());\r\n}\r\n\r\nfunction R(s){ return Math.floor(Math.random() * s) }; \r\n\r\nfunction createData(obj, length){\r\n    var data = [];\r\n    for(var i=0; i < length; i++){\r\n        if(typeof obj === 'function'){ data.push(obj())} \r\n        else{data.push(obj)};   \r\n    }\r\n    return data;\r\n}\r\n<\/pre>\n<p>\u4f7f\u3044\u65b9<\/p>\n<pre class=\"lang:js decode:true \" title=\"sample\" >var rmdObj = function() { return {x:R(100), y:R(100)} };\r\nvar data = createData.curry(rmdObj);\r\ndata(4) \/\/ -> [{\"x\":93,\"y\":57},{\"x\":10,\"y\":26},{\"x\":26,\"y\":46},{\"x\":5,\"y\":50}]\r\ndata(4) \/\/ -> [{\"x\":17,\"y\":38},{\"x\":88,\"y\":77},{\"x\":48,\"y\":92},{\"x\":71,\"y\":26}]\"\r\n<\/pre>\n<p>\u30e9\u30f3\u30c0\u30e0\u306a\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3057\u305f\u3044\u3068\u304d\u306b\u4fbf\u5229<\/p>\n<h2>\u30c8\u30b0\u30eb<\/h2>\n<p>\u30c8\u30b0\u30eb\u306b\u3057\u305f\u3044\u95a2\u6570\u3092\u4e8c\u3064\u6e21\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >function toggle(){\r\n    var fn = arguments;\r\n    var l = arguments.length;\r\n    var i = 0;\r\n    return function(){\r\n        if(l <= i) i=0;\r\n        fn[i++]();            \r\n    }\r\n}\r\n<\/pre>\n<p>\u4f7f\u3044\u65b9<\/p>\n<pre class=\"lang:js decode:true \" title=\"sample\" >    function a1(){\r\n          alert('Hello');  \r\n    }\r\n    function a2(){\r\n        alert('world');    \r\n    }\r\nsetInterval(toggle(a1, a2), 1000)\r\n<\/pre>\n<h2>D3 Helper Function<\/h2>\n<p>jsdo.it\u3067\u8a66\u305b\u307e\u3059\u3002<br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/qliL\/js\"><\/script><br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n<h2>D3utility\u3092\u4f7f\u3063\u305f\u30b5\u30f3\u30d7\u30eb<\/h2>\n<hr>\n<p><script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/hhMQ\/js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/aKGu\/js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/tZNX\/js\"><\/script><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/uOE3\/js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b7\u30e7\u30fc\u30c8\u30ab\u30c3\u30c8 D3\u306f\u3069\u3046\u3057\u3066\u3082\u7121\u540d\u95a2\u6570\u3092\u591a\u7528\u3059\u308b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2649","post","type-post","status-publish","format-standard","hentry","category-d3-js"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2649","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=2649"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2649\/revisions"}],"predecessor-version":[{"id":2651,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2649\/revisions\/2651"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}