ååãD3.jsã§ã¬ã¼ãã¼ãã£ã¼ãæ¸ãã¦ã¦ãenter() ã§ä½ãèµ·ãã¦ãã®ãããåãããªãã£ãã®ã§ã½ã¼ã¹ã³ã¼ããèªãã§ã¿ã¾ããããã®ã¨ãã®ã¡ã¢ã§ãã
é·ããªãã¾ãããä¸çªä¸ã®æ¹ã«ã¾ã¨ããããã¾ãã
èªãã D3.jsã®ãã¼ã¸ã§ã³ã¯ã3.4.2ã§ãã
èªãã§ã¿ããenter()ã¯ãã ã®getterã¡ã½ããã§ç¹ã«ä½ã®å¦çããã¦ã¾ããã§ããã代ããã«data()ãããã°ã£ã¦ããã§ããã¼ã
ã½ã¼ã¹ã³ã¼ãèªãã§ã¿ã¦ããã£ã¨D3 - セレクションの仕組みã«æ¸ãã¦ããæå³ãåãã£ãæ°ããã¾ãã
ä¸ã®ãããªåãæãã³ã¼ããå ã«ã½ã¼ã¹ã³ã¼ããèªãã§ã¿ã¾ããã
var svg = d3.select('body') .append('svg') .attr('width', w) .attr('height', h); svg.selectAll('path') .data(dataset) .enter() .append("circle") .attr("cx", function(d, i) { return (i * 50) + 25; }) .attr("cy", h/2) .attr("r", function(d) { return d; });
ã»ã¬ã¯ã·ã§ã³ãåå¾ããé¨åãèªã
ã¾ãã¯ãselect()ãä½ããã¦ããè¦ã¦ã¿ãã
var svg = d3.select('body')
d3.jsã®é¢é£ãã¦ãé¨åãã®ããã
// d3.js // 9è¡ç® var d3_document = document, d3_documentElement = d3_document.documentElement, d3_window = window; // 38è¡ç® var d3_select = function(s, n) { return n.querySelector(s); }, d3_selectAll = function(s, n) { return n.querySelectorAll(s); }, // 534è¡ç® d3.select = function(node) { var group = [ typeof node === "string" ? d3_select(node, d3_document) : node ]; group.parentNode = d3_documentElement; return d3_selection([ group ]); }; d3.selectAll = function(nodes) { var group = d3_array(typeof nodes === "string" ? d3_selectAll(nodes, d3_document) : nodes); group.parentNode = d3_documentElement; return d3_selection([ group ]); };
ã¾ãã¯ãdocument.querySelector
ã使ã£ã¦ãselect
ã®å¼æ°ã§æå®ããDOMã®Elementãªãã¸ã§ã¯ãã®é
åãä½ããä»åã®ä¾ã ã¨bodyè¦ç´ ã®Elementãªãã¸ã§ã¯ããä¿æããé
åããã®é
åãgroup
å¤æ°ã«ä»£å
¥ãã¦ãparentNode
ããããã£ãè¨å®ãã¦ãd3_selection([group])
ã§å¦çãã¦ããd3_selection
é¢æ°ãå¼ã¶ã¨ãã«ã[[element1, element2, ... ]]
ã®ãããª2次å
é
åãå¼æ°ã«ãã¦ããã§ããã
d3_selection
é¢æ°ã¯ä»¥ä¸ã
// d3.js // 478è¡ç® var d3_subclass = {}.__proto__ ? function(object, prototype) { object.__proto__ = prototype; } : function(object, prototype) { for (var property in prototype) object[property] = prototype[property]; }; function d3_selection(groups) { d3_subclass(groups, d3_selectionPrototype); return groups; }
d3_subclass
ã§ãgroups
ãã¤ã¾ãElementãªãã¸ã§ã¯ãã®é
åãè¦ç´ ã«æã¤2次å
é
åï¼[[ element1, element2, ...]]
ï¼ã«ãd3_selectionPrototype
ã®ããããã£ãã³ãã¼ãã¦è¿å´ãã¦ãã
ããã¦ãd3_selectionPrototype
ãªãã¸ã§ã¯ãã«ã¯ä»¥ä¸ã®ãããªããããã£ãè¨å®ããã¦ãã
d3_selectionPrototype.attr = function(name, value) { ... d3_selectionPrototype.append = function(name, value) { ... d3_selectionPrototype.data = function(name, value) { ... // ä»ã«ããããã
ãµããµãã
select
ã§ã¯ã[[element1, element2, ...]]
ã¨ãããããª2次å
é
åãè¿ãã¦ã¦ããã®2次å
é
åã«ã¯d3_selectionPrototype
ããããããã£ãã³ãã¼ããã¦ãããã®æ¡å¼µããã2次å
é
åããã»ã¬ã¯ã·ã§ã³ã£ã¦å¼ãã§ãããã ãªã
ããã§ãã£ã¦ãã»ã¬ã¯ã·ã§ã³ãä¿æãã¦ã[element1, element2, ...]
ã¨ããé
åã«parentNode
ããããã£ã追å ããããªãã¸ã§ã¯ããã°ã«ã¼ãã¨å¼ã¶ã¨ã
ä»åã®ä¾ã®d3.select('body')
ã§åå¾ããã»ã¬ã¯ã·ã§ã³ã¯ä»¥ä¸ã®ãããªã¤ã¡ã¼ã¸ã
ä¸è¬åããã¨ä»¥ä¸ã®ãããªã¤ã¡ã¼ã¸ããã¶ãããã®è¨äºï¼Nested Selectionsï¼ãèªãã¨ããç解ãæ·±ã¾ããããªæ°ãããã
SVGè¦ç´ ãDOMã¨ãã¦è¿½å ããé¨åãèªã
d3.select('body').append('svg')
ã£ã¦ããæãã§ã»ã¬ã¯ã·ã§ã³ã®append
ã¡ã½ãããå¼ãã§ãã
append
ã®ã½ã¼ã¹ã³ã¼ããèªãã ã¨ãããcreateElementNS
ã§Elementãªãã¸ã§ã¯ããçæãã¦appendChild
ãã¦ããappend
ã®è¿ãå¤ã¯appendChild
ããElementãªãã¸ã§ã¯ããä¿æããã»ã¬ã¯ã·ã§ã³ã«ãªãã
ããã¾ã§ãã以ä¸ã®ã³ã¼ãã§bodyè¦ç´ å ã«svgè¦ç´ ãçæãããé¨åã
var svg = d3.select('body') .append('svg') .attr('width', w) .attr('height', h);
ãã¼ã¿ãã¤ã³ããã¦ãé¨åãèªã
ããããã¯ãdatasetã使ã£ã¦ãåãæãé¨åã
svg.selectAll('path') .data(dataset) .enter() .append("circle") .attr("cx", function(d, i) { return (i * 50) + 25; }) .attr("cy", h/2) .attr("r", function(d) { return d; });
selectAll
ã¯ããã£ãã®select('body')
ã§è¦ãã®ã¨ã»ã¨ãã©åãã§ãéãã®ã¯querySelector
ã§ã¯ãªãquerySelectorAll
ã使ããã¦ãç¹ãã¤ã¾ããElementãªãã¸ã§ã¯ã1件ã§ã¯ãªããè¤æ°ä»¶åå¾ããå ´åãããã
ãã ãsvgè¦ç´ ã¯ãã£ãä½ã£ãã°ããã§ãsvgè¦ç´ å
ã¯ç©ºã ãããsvg.selectAll('path')
ã¯Elementãªãã¸ã§ã¯ãã1ã¤ãä¿æãã¦ããªãã»ã¬ã¯ã·ã§ã³ãè¿ããã¨ã«ãªãã
ãã®ã»ã¬ã¯ã·ã§ã³ã®data
ã¡ã½ãããå¼ã³åºããdata
ã¡ã½ããã¯å¤§ããã®é¢æ°ã
// d3.js // 749è¡ç® d3_selectionPrototype.data = function(value, key) { var i = -1, n = this.length, group, node; // 70è¡ããã var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); if (typeof value === "function") { while (++i < n) { bind(group = this[i], value.call(group, group.parentNode.__data__, i)); } } else { while (++i < n) { bind(group = this[i], value); } } update.enter = function() { return enter; }; update.exit = function() { return exit; }; return update; }
i
ã¨n
ã®å¤ãè¦ãã¨ãbind
é¢æ°ã1åã ãå¼ã°ããããbind
é¢æ°ã¯ãdata
ã¡ã½ããã®ä¸ã§å®ç¾©ããã¦ãã
// d3.js // 760è¡ç® function bind(group, groupData) { var i, n = group.length, m = groupData.length, n0 = Math.min(n, m), updateNodes = new Array(m), enterNodes = new Array(m), exitNodes = new Array(n), node, nodeData; if (key) { // çç¥ } else { for (i = -1; ++i < n0; ) { node = group[i]; nodeData = groupData[i]; if (node) { node.__data__ = nodeData; updateNodes[i] = node; } else { enterNodes[i] = d3_selection_dataNode(nodeData); } } for (;i < m; ++i) { enterNodes[i] = d3_selection_dataNode(groupData[i]); } for (;i < n; ++i) { exitNodes[i] = group[i]; } } enterNodes.update = updateNodes; enterNodes.parentNode = updateNodes.parentNode = exitNodes.parentNode = group.parentNode; enter.push(enterNodes); update.push(updateNodes); exit.push(exitNodes); } // 831è¡ç® function d3_selection_dataNode(data) { return { __data__: data }; }
bind
é¢æ°å
ã§ãã»ã¬ã¯ã·ã§ã³ã®Elementãªãã¸ã§ã¯ãã®æ°ã¨dataset
ã®è¦ç´ æ°ã«å¿ãã¦ãupdateNodes
ãenterNodes
ãexitNodes
ãããããã®é
åã«ãªãã¸ã§ã¯ããpush
ãããã
push
ããããªãã¸ã§ã¯ãã«ã¯ã__data__
ããããã£ãè¨å®ããã¦ããããããã¼ã¿ãã¤ã³ãã
ã©ããã風ã«ãªãã¸ã§ã¯ããpush
ããããã¨ããã¨ãElementãªãã¸ã§ã¯ãã®æ°ã®ããdataset
ã®è¦ç´ æ°ã®æ¹ãå¤ãã¨ããä¾ãã°Elementãªãã¸ã§ã¯ãã®æ°ã3ã§ãdataset
ã®è¦ç´ æ°ã7ã®ã¨ã
- updateNodesã«ã¯
- 3ã¤ã®Elementãªãã¸ã§ã¯ãããããã®
__data__
ããããã£ã«ãdataset
ã®æåãã3ã¤ã®å¤ãããããã»ãããã¦ãããpush
- 3ã¤ã®Elementãªãã¸ã§ã¯ãããããã®
- enterNodesã«ã¯
dataset
ã®æ®ãã®4ã¤ã®å¤ã使ã£ã¦ã{__data__: data}
ã¨ãããªãã¸ã§ã¯ããçæããpush
- exitNodes
- ãªã«ã
push
ããªã
- ãªã«ã
éã«ãElementãªãã¸ã§ã¯ãã®æ°ã7ã§ãdataset
ã®è¦ç´ æ°ã3ã®ã¨ã
- updateNodesã«ã¯
- 7ã¤ã®Elementãªãã¸ã§ã¯ãã®ãã¡ãæåãã3ã¤ã®ãªãã¸ã§ã¯ãã«
__data__
ããããã£ãã»ãããã¦push
- 7ã¤ã®Elementãªãã¸ã§ã¯ãã®ãã¡ãæåãã3ã¤ã®ãªãã¸ã§ã¯ãã«
- enterNodesã«ã¯
- ãªã«ã
push
ããªã
- ãªã«ã
- exitNodes
- æ®ãã®4ã¤ã®Elementãªãã¸ã§ã¯ãã
push
- æ®ãã®4ã¤ã®Elementãªãã¸ã§ã¯ãã
ããã3ã¤ã®é
åã¯ãbind
é¢æ°ã§ã¯ãªããå¤å´ã®data
ã¡ã½ããã§å®ç¾©ããã¦ãã3ã¤ã®ã»ã¬ã¯ã·ã§ã³ãenter
ãupdate
ãexit
ã«push
ãããã
data
ã¡ã½ãããããä¸åº¦è¦ã¦ã¿ãã¨ããããã®å¤ãdata
ã¡ã½ããã®è¿ãå¤ã¨ãã¦ä½¿ããã¦ããã¨ãåããã
// d3.js // 749è¡ç® d3_selectionPrototype.data = function(value, key) { // çç¥ var enter = d3_selection_enter([]), update = d3_selection([]), exit = d3_selection([]); // çç¥ update.enter = function() { return enter; }; update.exit = function() { return exit; }; return update; }
data
ã¡ã½ããã§è¿ã£ã¦ããã®ã¯ãã»ã¬ã¯ã·ã§ã³ã§ãä¿æãã¦ãElementã®é
åã¯update
ã«ãªã£ã¦ãããããã«ãdata
ã¡ã½ããã§è¿ã£ã¦ããã»ã¬ã¯ã·ã§ã³ã«ã¯ãenter
ã¨exit
ã®getterã¡ã½ãããenter()
ã¨exit()
ãè¨å®ããã¦ããã
ã¯ãã¼ããªãã»ã©ã¼ãenter()
ã£ã¦æ¢åã®DOMããdataset
ã®æ°ãå¤ãå ´åã«ãä½åãªãã¼ã¿ã®é
åãæã¤ã»ã¬ã¯ã·ã§ã³ãè¿å´ãã¦ãã ããªã®ãã
[[{__data__: 10}, {__data__: 20}, ...]]
ãããªæãã®ã
ããã§ãã£ã¦ãä½åãªãã¼ã¿ã®é
åã¯enter()
ãå¼ãã æãããªãã¦ãdata()
ãå¼ã³åºããæç¹ã§ä½ããã¦ããã ãã
enter()å¾ã®å¦çãèªã
append
ã§ã¯ã°ã«ã¼ããä¿æãããªãã¸ã§ã¯ãã®æ°ã ãappendChild
ãã¦ãããenter()
ã¯ä¸è¨ã®éãenterNodes
ãä¿æããã»ã¬ã¯ã·ã§ã³ãè¿ãã ããã¤ã¾ããenter().append('circle')
ã¨ããã¨enterNodesã®æ°ã ãcircleè¦ç´ ãappendChild
ããããã¨ã«ãªãã
ã¾ã¨ã
- ã»ã¬ã¯ã·ã§ã³ã¯ãElementã®é åã®é åã§ãã2次å é åã«ãè²ã ãªããããã£ã追å ãã¦æ¡å¼µãããã®ã
- ã»ã¬ã¯ã·ã§ã³ãä¿æããElementã®é
åã«ã¯
parentNode
ããããã£ã追å ããã¦ãã¦ããã®é åãã°ã«ã¼ãã¨å¼ã¶ã - ã»ã¬ã¯ã·ã§ã³ã®
data
ã¡ã½ããã¯ä»äºãå¤ããè¶ ãã£ããè¨ãã¨æ°ããªã»ã¬ã¯ã·ã§ã³update
ãçæãè¿å´ãããupdate
ã®ã°ã«ã¼ããä¿æãããªãã¸ã§ã¯ãã«ã¯ã__data__
ããããã£ã追å ããã¦ããããããã¼ã¿ãã¤ã³ãã- å
ã
ã®ã»ã¬ã¯ã·ã§ã³ï¼
data
ã¡ã½ããå ã«ãããthis
ï¼ã®ã°ã«ã¼ãã®è¦ç´ æ°ã¨dataset
ã®è¦ç´ æ°ã«å¿ãã¦ã3ã¤ã®ã»ã¬ã¯ã·ã§ã³ãupdate
ã»enter
ã»exit
ã«ãªãã¸ã§ã¯ãã追å ãã¦ããã update
ã«ã¯ãenter
ã¡ã½ããã¨exit
ã¡ã½ããã追å ããã¦ãã¦ãããããenter
ãexit
ã®getterã¡ã½ããã«ãªã£ã¦ããã
ãªããç解ãå°ãé²ãã æ°ããã¾ãï¼
ä»æ¥ãä¸ã®æ¬ãèªãã§ããã³ã©ã ã«dataãenterå¨ãã«ã¤ãã¦æ¸ãã¦ããã®ãè¦ã¤ãã¾ããï¼
- ä½è : 森è¤å¤§å°,ããã¡ã¹
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2014/02/20
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- ãã®ååãå«ãããã°ãè¦ã