source: node_modules/d3-selection/README.md

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 58.9 KB
Line 
1# d3-selection
2
3Selections allow powerful data-driven transformation of the document object model (DOM): set [attributes](#selection_attr), [styles](#selection_style), [properties](#selection_property), [HTML](#selection_html) or [text](#selection_text) content, and more. Using the [data join](#joining-data)’s [enter](#selection_enter) and [exit](#selection_enter) selections, you can also [add](#selection_append) or [remove](#selection_remove) elements to correspond to data.
4
5Selection methods typically return the current selection, or a new selection, allowing the concise application of multiple operations on a given selection via method chaining. For example, to set the class and color style of all paragraph elements in the current document:
6
7```js
8d3.selectAll("p")
9 .attr("class", "graf")
10 .style("color", "red");
11```
12
13This is equivalent to:
14
15```js
16const p = d3.selectAll("p");
17p.attr("class", "graf");
18p.style("color", "red");
19```
20
21By convention, selection methods that return the current selection use *four* spaces of indent, while methods that return a new selection use only *two*. This helps reveal changes of context by making them stick out of the chain:
22
23```js
24d3.select("body")
25 .append("svg")
26 .attr("width", 960)
27 .attr("height", 500)
28 .append("g")
29 .attr("transform", "translate(20,20)")
30 .append("rect")
31 .attr("width", 920)
32 .attr("height", 460);
33```
34
35Selections are immutable. All selection methods that affect which elements are selected (or their order) return a new selection rather than modifying the current selection. However, note that elements are necessarily mutable, as selections drive transformations of the document!
36
37For more, see [the d3-selection collection on Observable](https://observablehq.com/collection/@d3/d3-selection).
38
39## Installing
40
41If you use npm, `npm install d3-selection`. You can also download the [latest release on GitHub](https://github.com/d3/d3-selection/releases/latest). For vanilla HTML in modern browsers, import d3-selection from Skypack:
42
43```html
44<script type="module">
45
46import {selectAll} from "https://cdn.skypack.dev/d3-selection@3";
47
48const div = selectAll("div");
49
50</script>
51```
52
53For legacy environments, you can load d3-selection’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:
54
55```html
56<script src="https://cdn.jsdelivr.net/npm/d3-selection@3"></script>
57<script>
58
59const div = d3.selectAll("div");
60
61</script>
62```
63
64[Try d3-selection in your browser.](https://observablehq.com/collection/@d3/d3-selection)
65
66## API Reference
67
68* [Selecting Elements](#selecting-elements)
69* [Modifying Elements](#modifying-elements)
70* [Joining Data](#joining-data)
71* [Handling Events](#handling-events)
72* [Control Flow](#control-flow)
73* [Local Variables](#local-variables)
74* [Namespaces](#namespaces)
75
76### Selecting Elements
77
78Selection methods accept [W3C selector strings](http://www.w3.org/TR/selectors-api/) such as `.fancy` to select elements with the class *fancy*, or `div` to select DIV elements. Selection methods come in two forms: select and selectAll: the former selects only the first matching element, while the latter selects all matching elements in document order. The top-level selection methods, [d3.select](#select) and [d3.selectAll](#selectAll), query the entire document; the subselection methods, [*selection*.select](#selection_select) and [*selection*.selectAll](#selection_selectAll), restrict selection to descendants of the selected elements.
79
80<a name="selection" href="#selection">#</a> d3.<b>selection</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/index.js)
81
82[Selects](#select) the root element, `document.documentElement`. This function can also be used to test for selections (`instanceof d3.selection`) or to extend the selection prototype. For example, to add a method to check checkboxes:
83
84```js
85d3.selection.prototype.checked = function(value) {
86 return arguments.length < 1
87 ? this.property("checked")
88 : this.property("checked", !!value);
89};
90```
91
92And then to use:
93
94```js
95d3.selectAll("input[type=checkbox]").checked(true);
96```
97
98<a name="select" href="#select">#</a> d3.<b>select</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/select.js)
99
100Selects the first element that matches the specified *selector* string. If no elements match the *selector*, returns an empty selection. If multiple elements match the *selector*, only the first matching element (in document order) will be selected. For example, to select the first anchor element:
101
102```js
103const anchor = d3.select("a");
104```
105
106If the *selector* is not a string, instead selects the specified node; this is useful if you already have a reference to a node, such as `this` within an event listener or a global such as `document.body`. For example, to make a clicked paragraph red:
107
108```js
109d3.selectAll("p").on("click", function(event) {
110 d3.select(this).style("color", "red");
111});
112```
113
114<a name="selectAll" href="#selectAll">#</a> d3.<b>selectAll</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selectAll.js)
115
116Selects all elements that match the specified *selector* string. The elements will be selected in document order (top-to-bottom). If no elements in the document match the *selector*, or if the *selector* is null or undefined, returns an empty selection. For example, to select all paragraphs:
117
118```js
119const paragraph = d3.selectAll("p");
120```
121
122If the *selector* is not a string, instead selects the specified array of nodes; this is useful if you already have a reference to nodes, such as `this.childNodes` within an event listener or a global such as `document.links`. The nodes may instead be an iterable, or a pseudo-array such as a NodeList. For example, to color all links red:
123
124```js
125d3.selectAll(document.links).style("color", "red");
126```
127
128<a name="selection_select" href="#selection_select">#</a> <i>selection</i>.<b>select</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/select.js)
129
130For each selected element, selects the first descendant element that matches the specified *selector* string. If no element matches the specified selector for the current element, the element at the current index will be null in the returned selection. (If the *selector* is null, every element in the returned selection will be null, resulting in an empty selection.) If the current element has associated data, this data is propagated to the corresponding selected element. If multiple elements match the selector, only the first matching element in document order is selected. For example, to select the first bold element in every paragraph:
131
132```js
133const b = d3.selectAll("p").select("b");
134```
135
136If the *selector* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). It must return an element, or null if there is no matching element. For example, to select the previous sibling of each paragraph:
137
138```js
139const previous = d3.selectAll("p").select(function() {
140 return this.previousElementSibling;
141});
142```
143
144Unlike [*selection*.selectAll](#selection_selectAll), *selection*.select does not affect grouping: it preserves the existing group structure and indexes, and propagates data (if any) to selected children. Grouping plays an important role in the [data join](#joining-data). See [Nested Selections](http://bost.ocks.org/mike/nest/) and [How Selections Work](http://bost.ocks.org/mike/selection/) for more on this topic.
145
146<a name="selection_selectAll" href="#selection_selectAll">#</a> <i>selection</i>.<b>selectAll</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/selectAll.js)
147
148For each selected element, selects the descendant elements that match the specified *selector* string. The elements in the returned selection are grouped by their corresponding parent node in this selection. If no element matches the specified selector for the current element, or if the *selector* is null, the group at the current index will be empty. The selected elements do not inherit data from this selection; use [*selection*.data](#selection_data) to propagate data to children. For example, to select the bold elements in every paragraph:
149
150```js
151const b = d3.selectAll("p").selectAll("b");
152```
153
154If the *selector* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). It must return an array of elements (or an iterable, or a pseudo-array such as a NodeList), or the empty array if there are no matching elements. For example, to select the previous and next siblings of each paragraph:
155
156```js
157const sibling = d3.selectAll("p").selectAll(function() {
158 return [
159 this.previousElementSibling,
160 this.nextElementSibling
161 ];
162});
163```
164
165Unlike [*selection*.select](#selection_select), *selection*.selectAll does affect grouping: each selected descendant is grouped by the parent element in the originating selection. Grouping plays an important role in the [data join](#joining-data). See [Nested Selections](http://bost.ocks.org/mike/nest/) and [How Selections Work](http://bost.ocks.org/mike/selection/) for more on this topic.
166
167<a name="selection_filter" href="#selection_filter">#</a> <i>selection</i>.<b>filter</b>(<i>filter</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/filter.js)
168
169Filters the selection, returning a new selection that contains only the elements for which the specified *filter* is true. The *filter* may be specified either as a selector string or a function. If the *filter* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]).
170
171For example, to filter a selection of table rows to contain only even rows:
172
173```js
174const even = d3.selectAll("tr").filter(":nth-child(even)");
175```
176
177This is approximately equivalent to using [d3.selectAll](#selectAll) directly, although the indexes may be different:
178
179```js
180const even = d3.selectAll("tr:nth-child(even)");
181```
182
183Similarly, using a function:
184
185```js
186const even = d3.selectAll("tr").filter((d, i) => i & 1);
187```
188
189Or using [*selection*.select](#selection_select) (and avoiding an arrow function, since *this* is needed to refer to the current element):
190
191```js
192const even = d3.selectAll("tr").select(function(d, i) { return i & 1 ? this : null; });
193```
194
195Note that the `:nth-child` pseudo-class is a one-based index rather than a zero-based index. Also, the above filter functions do not have precisely the same meaning as `:nth-child`; they rely on the selection index rather than the number of preceding sibling elements in the DOM.
196
197The returned filtered selection preserves the parents of this selection, but like [*array*.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), it does not preserve indexes as some elements may be removed; use [*selection*.select](#selection_select) to preserve the index, if needed.
198
199<a name="selection_merge" href="#selection_merge">#</a> <i>selection</i>.<b>merge</b>(<i>other</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/merge.js)
200
201Returns a new selection merging this selection with the specified *other* selection or transition. The returned selection has the same number of groups and the same parents as this selection. Any missing (null) elements in this selection are filled with the corresponding element, if present (not null), from the specified *selection*. (If the *other* selection has additional groups or parents, they are ignored.)
202
203This method is used internally by [*selection*.join](#selection_join) to merge the [enter](#selection_enter) and [update](#selection_data) selections after [binding data](#joining-data). You can also merge explicitly, although note that since merging is based on element index, you should use operations that preserve index, such as [*selection*.select](#selection_select) instead of [*selection*.filter](#selection_filter). For example:
204
205```js
206const odd = selection.select(function(d, i) { return i & 1 ? this : null; ));
207const even = selection.select(function(d, i) { return i & 1 ? null : this; ));
208const merged = odd.merge(even);
209```
210
211See [*selection*.data](#selection_data) for more.
212
213This method is not intended for concatenating arbitrary selections, however: if both this selection and the specified *other* selection have (non-null) elements at the same index, this selection’s element is returned in the merge and the *other* selection’s element is ignored.
214
215<a name="selection_selectChild" href="#selection_selectChild">#</a> <i>selection</i>.<b>selectChild</b>([<i>selector</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/selectChild.js)
216
217Returns a new selection with the (first) child of each element of the current selection matching the *selector*. If no *selector* is specified, selects the first child (if any). If the *selector* is specified as a string, selects the first child that matches (if any). If the *selector* is a function, it is evaluated for each of the children nodes, in order, being passed the child (*child*), the child’s index (*i*), and the list of children (*children*); the method selects the first child for which the selector return truthy, if any.
218
219<a name="selection_selectChildren" href="#selection_selectChildren">#</a> <i>selection</i>.<b>selectChildren</b>([<i>selector</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/selectChildren.js)
220
221Returns a new selection with the children of each element of the current selection matching the *selector*. If no *selector* is specified, selects all the children. If the *selector* is specified as a string, selects the children that match (if any). If the *selector* is a function, it is evaluated for each of the children nodes, in order, being passed the child (*child*), the child’s index (*i*), and the list of children (*children*); the method selects all children for which the selector return truthy.
222
223<a name="selection_selection" href="#selection_selection">#</a> <i>selection</i>.<b>selection</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/index.js)
224
225Returns the selection (for symmetry with [<i>transition</i>.selection](https://github.com/d3/d3-transition/blob/master/README.md#transition_selection)).
226
227<a name="matcher" href="#matcher">#</a> d3.<b>matcher</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/matcher.js)
228
229Given the specified *selector*, returns a function which returns true if `this` element [matches](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches) the specified selector. This method is used internally by [*selection*.filter](#selection_filter). For example, this:
230
231```js
232const div = selection.filter("div");
233```
234
235Is equivalent to:
236
237```js
238const div = selection.filter(d3.matcher("div"));
239```
240
241(Although D3 is not a compatibility layer, this implementation does support vendor-prefixed implementations due to the recent standardization of *element*.matches.)
242
243<a name="selector" href="#selector">#</a> d3.<b>selector</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selector.js)
244
245Given the specified *selector*, returns a function which returns the first descendant of `this` element that matches the specified selector. This method is used internally by [*selection*.select](#selection_select). For example, this:
246
247```js
248const div = selection.select("div");
249```
250
251Is equivalent to:
252
253```js
254const div = selection.select(d3.selector("div"));
255```
256
257<a name="selectorAll" href="#selectorAll">#</a> d3.<b>selectorAll</b>(<i>selector</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selectAll.js)
258
259Given the specified *selector*, returns a function which returns all descendants of `this` element that match the specified selector. This method is used internally by [*selection*.selectAll](#selection_selectAll). For example, this:
260
261```js
262const div = selection.selectAll("div");
263```
264
265Is equivalent to:
266
267```js
268const div = selection.selectAll(d3.selectorAll("div"));
269```
270
271<a name="window" href="#window">#</a> d3.<b>window</b>(<i>node</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/window.js)
272
273Returns the owner window for the specified *node*. If *node* is a node, returns the owner document’s default view; if *node* is a document, returns its default view; otherwise returns the *node*.
274
275<a name="style" href="#style">#</a> d3.<b>style</b>(<i>node</i>, <i>name</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/style.js)
276
277Returns the value of the style property with the specified *name* for the specified *node*. If the *node* has an inline style with the specified *name*, its value is returned; otherwise, the [computed property value](https://developer.mozilla.org/en-US/docs/Web/CSS/computed_value) is returned. See also [*selection*.style](#selection_style).
278
279### Modifying Elements
280
281After selecting elements, use the selection’s transformation methods to affect document content. For example, to set the name attribute and color style of an anchor element:
282
283```js
284d3.select("a")
285 .attr("name", "fred")
286 .style("color", "red");
287```
288
289To experiment with selections, visit [d3js.org](https://d3js.org) and open your browser’s developer console! (In Chrome, open the console with ⌥⌘J.) Select elements and then inspect the returned selection to see which elements are selected and how they are grouped. Call selection methods and see how the page content changes.
290
291<a name="selection_attr" href="#selection_attr">#</a> <i>selection</i>.<b>attr</b>(<i>name</i>[, <i>value</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/attr.js)
292
293If a *value* is specified, sets the attribute with the specified *name* to the specified value on the selected elements and returns this selection. If the *value* is a constant, all elements are given the same attribute value; otherwise, if the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function’s return value is then used to set each element’s attribute. A null value will remove the specified attribute.
294
295If a *value* is not specified, returns the current value of the specified attribute for the first (non-null) element in the selection. This is generally useful only if you know that the selection contains exactly one element.
296
297The specified *name* may have a namespace prefix, such as `xlink:href` to specify the `href` attribute in the XLink namespace. See [namespaces](#namespaces) for the map of supported namespaces; additional namespaces can be registered by adding to the map.
298
299<a name="selection_classed" href="#selection_classed">#</a> <i>selection</i>.<b>classed</b>(<i>names</i>[, <i>value</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/classed.js)
300
301If a *value* is specified, assigns or unassigns the specified CSS class *names* on the selected elements by setting the `class` attribute or modifying the `classList` property and returns this selection. The specified *names* is a string of space-separated class names. For example, to assign the classes `foo` and `bar` to the selected elements:
302
303```js
304selection.classed("foo bar", true);
305```
306
307If the *value* is truthy, then all elements are assigned the specified classes; otherwise, the classes are unassigned. If the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function’s return value is then used to assign or unassign classes on each element. For example, to randomly associate the class *foo* with on average half the selected elements:
308
309```js
310selection.classed("foo", () => Math.random() > 0.5);
311```
312
313If a *value* is not specified, returns true if and only if the first (non-null) selected element has the specified *classes*. This is generally useful only if you know the selection contains exactly one element.
314
315<a name="selection_style" href="#selection_style">#</a> <i>selection</i>.<b>style</b>(<i>name</i>[, <i>value</i>[, <i>priority</i>]]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/style.js)
316
317If a *value* is specified, sets the style property with the specified *name* to the specified value on the selected elements and returns this selection. If the *value* is a constant, then all elements are given the same style property value; otherwise, if the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function’s return value is then used to set each element’s style property. A null value will remove the style property. An optional *priority* may also be specified, either as null or the string `important` (without the exclamation point).
318
319If a *value* is not specified, returns the current value of the specified style property for the first (non-null) element in the selection. The current value is defined as the element’s inline value, if present, and otherwise its [computed value](https://developer.mozilla.org/en-US/docs/Web/CSS/computed_value). Accessing the current style value is generally useful only if you know the selection contains exactly one element.
320
321Caution: unlike many SVG attributes, CSS styles typically have associated units. For example, `3px` is a valid stroke-width property value, while `3` is not. Some browsers implicitly assign the `px` (pixel) unit to numeric values, but not all browsers do: IE, for example, throws an “invalid arguments” error!
322
323<a name="selection_property" href="#selection_property">#</a> <i>selection</i>.<b>property</b>(<i>name</i>[, <i>value</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/property.js)
324
325Some HTML elements have special properties that are not addressable using attributes or styles, such as a form field’s text `value` and a checkbox’s `checked` boolean. Use this method to get or set these properties.
326
327If a *value* is specified, sets the property with the specified *name* to the specified value on selected elements. If the *value* is a constant, then all elements are given the same property value; otherwise, if the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function’s return value is then used to set each element’s property. A null value will delete the specified property.
328
329If a *value* is not specified, returns the value of the specified property for the first (non-null) element in the selection. This is generally useful only if you know the selection contains exactly one element.
330
331<a name="selection_text" href="#selection_text">#</a> <i>selection</i>.<b>text</b>([<i>value</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/text.js)
332
333If a *value* is specified, sets the [text content](http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent) to the specified value on all selected elements, replacing any existing child elements. If the *value* is a constant, then all elements are given the same text content; otherwise, if the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function’s return value is then used to set each element’s text content. A null value will clear the content.
334
335If a *value* is not specified, returns the text content for the first (non-null) element in the selection. This is generally useful only if you know the selection contains exactly one element.
336
337<a name="selection_html" href="#selection_html">#</a> <i>selection</i>.<b>html</b>([<i>value</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/html.js)
338
339If a *value* is specified, sets the [inner HTML](http://dev.w3.org/html5/spec-LC/apis-in-html-documents.html#innerhtml) to the specified value on all selected elements, replacing any existing child elements. If the *value* is a constant, then all elements are given the same inner HTML; otherwise, if the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function’s return value is then used to set each element’s inner HTML. A null value will clear the content.
340
341If a *value* is not specified, returns the inner HTML for the first (non-null) element in the selection. This is generally useful only if you know the selection contains exactly one element.
342
343Use [*selection*.append](#selection_append) or [*selection*.insert](#selection_insert) instead to create data-driven content; this method is intended for when you want a little bit of HTML, say for rich formatting. Also, *selection*.html is only supported on HTML elements. SVG elements and other non-HTML elements do not support the innerHTML property, and thus are incompatible with *selection*.html. Consider using [XMLSerializer](https://developer.mozilla.org/en-US/docs/XMLSerializer) to convert a DOM subtree to text. See also the [innersvg polyfill](https://code.google.com/p/innersvg/), which provides a shim to support the innerHTML property on SVG elements.
344
345<a name="selection_append" href="#selection_append">#</a> <i>selection</i>.<b>append</b>(<i>type</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/append.js)
346
347If the specified *type* is a string, appends a new element of this type (tag name) as the last child of each selected element, or before the next following sibling in the update selection if this is an [enter selection](#selection_enter). The latter behavior for enter selections allows you to insert elements into the DOM in an order consistent with the new bound data; however, note that [*selection*.order](#selection_order) may still be required if updating elements change order (*i.e.*, if the order of new data is inconsistent with old data).
348
349If the specified *type* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). This function should return an element to be appended. (The function typically creates a new element, but it may instead return an existing element.) For example, to append a paragraph to each DIV element:
350
351```js
352d3.selectAll("div").append("p");
353```
354
355This is equivalent to:
356
357```js
358d3.selectAll("div").append(() => document.createElement("p"));
359```
360
361Which is equivalent to:
362
363```js
364d3.selectAll("div").select(function() {
365 return this.appendChild(document.createElement("p"));
366});
367```
368
369In both cases, this method returns a new selection containing the appended elements. Each new element inherits the data of the current elements, if any, in the same manner as [*selection*.select](#selection_select).
370
371The specified *name* may have a namespace prefix, such as `svg:text` to specify a `text` attribute in the SVG namespace. See [namespaces](#namespaces) for the map of supported namespaces; additional namespaces can be registered by adding to the map. If no namespace is specified, the namespace will be inherited from the parent element; or, if the name is one of the known prefixes, the corresponding namespace will be used (for example, `svg` implies `svg:svg`).
372
373<a name="selection_insert" href="#selection_insert">#</a> <i>selection</i>.<b>insert</b>(<i>type</i>[, <i>before</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/insert.js)
374
375If the specified *type* is a string, inserts a new element of this type (tag name) before the first element matching the specified *before* selector for each selected element. For example, a *before* selector `:first-child` will prepend nodes before the first child. If *before* is not specified, it defaults to null. (To append elements in an order consistent with [bound data](#joining-data), use [*selection*.append](#selection_append).)
376
377Both *type* and *before* may instead be specified as functions which are evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The *type* function should return an element to be inserted; the *before* function should return the child element before which the element should be inserted. For example, to append a paragraph to each DIV element:
378
379```js
380d3.selectAll("div").insert("p");
381```
382
383This is equivalent to:
384
385```js
386d3.selectAll("div").insert(() => document.createElement("p"));
387```
388
389Which is equivalent to:
390
391```js
392d3.selectAll("div").select(function() {
393 return this.insertBefore(document.createElement("p"), null);
394});
395```
396
397In both cases, this method returns a new selection containing the appended elements. Each new element inherits the data of the current elements, if any, in the same manner as [*selection*.select](#selection_select).
398
399The specified *name* may have a namespace prefix, such as `svg:text` to specify a `text` attribute in the SVG namespace. See [namespaces](#namespaces) for the map of supported namespaces; additional namespaces can be registered by adding to the map. If no namespace is specified, the namespace will be inherited from the parent element; or, if the name is one of the known prefixes, the corresponding namespace will be used (for example, `svg` implies `svg:svg`).
400
401<a name="selection_remove" href="#selection_remove">#</a> <i>selection</i>.<b>remove</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/remove.js)
402
403Removes the selected elements from the document. Returns this selection (the removed elements) which are now detached from the DOM. There is not currently a dedicated API to add removed elements back to the document; however, you can pass a function to [*selection*.append](#selection_append) or [*selection*.insert](#selection_insert) to re-add elements.
404
405<a name="selection_clone" href="#selection_clone">#</a> <i>selection</i>.<b>clone</b>([<i>deep</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/clone.js)
406
407Inserts clones of the selected elements immediately following the selected elements and returns a selection of the newly added clones. If *deep* is truthy, the descendant nodes of the selected elements will be cloned as well. Otherwise, only the elements themselves will be cloned. Equivalent to:
408
409```js
410selection.select(function() {
411 return this.parentNode.insertBefore(this.cloneNode(deep), this.nextSibling);
412});
413```
414
415<a name="selection_sort" href="#selection_sort">#</a> <i>selection</i>.<b>sort</b>(<i>compare</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/sort.js)
416
417Returns a new selection that contains a copy of each group in this selection sorted according to the *compare* function. After sorting, re-inserts elements to match the resulting order (per [*selection*.order](#selection_order)).
418
419The compare function, which defaults to [ascending](https://github.com/d3/d3-array#ascending), is passed two elements’ data *a* and *b* to compare. It should return either a negative, positive, or zero value. If negative, then *a* should be before *b*; if positive, then *a* should be after *b*; otherwise, *a* and *b* are considered equal and the order is arbitrary.
420
421Note that sorting is not guaranteed to be stable; however, it is guaranteed to have the same behavior as your browser’s built-in [sort](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort) method on arrays.
422
423<a name="selection_order" href="#selection_order">#</a> <i>selection</i>.<b>order</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/order.js)
424
425Re-inserts elements into the document such that the document order of each group matches the selection order. This is equivalent to calling [*selection*.sort](#selection_sort) if the data is already sorted, but much faster.
426
427<a name="selection_raise" href="#selection_raise">#</a> <i>selection</i>.<b>raise</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/raise.js)
428
429Re-inserts each selected element, in order, as the last child of its parent. Equivalent to:
430
431```js
432selection.each(function() {
433 this.parentNode.appendChild(this);
434});
435```
436
437<a name="selection_lower" href="#selection_lower">#</a> <i>selection</i>.<b>lower</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/lower.js)
438
439Re-inserts each selected element, in order, as the first child of its parent. Equivalent to:
440
441```js
442selection.each(function() {
443 this.parentNode.insertBefore(this, this.parentNode.firstChild);
444});
445```
446
447<a name="create" href="#create">#</a> d3.<b>create</b>(<i>name</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/create.js)
448
449Given the specified element *name*, returns a single-element selection containing a detached element of the given name in the current document. This method assumes the HTML namespace, so you must specify a namespace explicitly when creating SVG or other non-HTML elements; see [namespace](#namespace) for details on supported namespace prefixes.
450
451```js
452d3.create("svg") // equivalent to svg:svg
453d3.create("svg:svg") // more explicitly
454d3.create("svg:g") // an SVG G element
455d3.create("g") // an HTML G (unknown) element
456```
457
458<a name="creator" href="#creator">#</a> d3.<b>creator</b>(<i>name</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/creator.js)
459
460Given the specified element *name*, returns a function which creates an element of the given name, assuming that `this` is the parent element. This method is used internally by [*selection*.append](#selection_append) and [*selection*.insert](#selection_insert) to create new elements. For example, this:
461
462```js
463selection.append("div");
464```
465
466Is equivalent to:
467
468```js
469selection.append(d3.creator("div"));
470```
471
472See [namespace](#namespace) for details on supported namespace prefixes, such as for SVG elements.
473
474### Joining Data
475
476For an introduction to D3’s data joins, see the [*selection*.join notebook](https://observablehq.com/@d3/selection-join). Also see [Thinking With Joins](http://bost.ocks.org/mike/join/).
477
478<a name="selection_data" href="#selection_data">#</a> <i>selection</i>.<b>data</b>([<i>data</i>[, <i>key</i>]]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/data.js), [Examples](https://observablehq.com/@d3/brushable-scatterplot)
479
480Binds the specified array of *data* with the selected elements, returning a new selection that represents the *update* selection: the elements successfully bound to data. Also defines the [enter](#selection_enter) and [exit](#selection_exit) selections on the returned selection, which can be used to add or remove elements to correspond to the new data. The specified *data* is an array of arbitrary values (*e.g.*, numbers or objects), or a function that returns an array of values for each group. When data is assigned to an element, it is stored in the property `__data__`, thus making the data “sticky” and available on re-selection.
481
482The *data* is specified **for each group** in the selection. If the selection has multiple groups (such as [d3.selectAll](#selectAll) followed by [*selection*.selectAll](#selection_selectAll)), then *data* should typically be specified as a function. This function will be evaluated for each group in order, being passed the group’s parent datum (*d*, which may be undefined), the group index (*i*), and the selection’s parent nodes (*nodes*), with *this* as the group’s parent element.
483
484In conjunction with [*selection*.join](#selection_join) (or more explicitly with [*selection*.enter](#selection_enter), [*selection*.exit](#selection_exit), [*selection*.append](#selection_append) and [*selection*.remove](#selection_remove)), *selection*.data can be used to enter, update and exit elements to match data. For example, to create an HTML table from a matrix of numbers:
485
486```js
487const matrix = [
488 [11975, 5871, 8916, 2868],
489 [ 1951, 10048, 2060, 6171],
490 [ 8010, 16145, 8090, 8045],
491 [ 1013, 990, 940, 6907]
492];
493
494d3.select("body")
495 .append("table")
496 .selectAll("tr")
497 .data(matrix)
498 .join("tr")
499 .selectAll("td")
500 .data(d => d)
501 .join("td")
502 .text(d => d);
503```
504
505In this example the *data* function is the identity function: for each table row, it returns the corresponding row from the data matrix.
506
507If a *key* function is not specified, then the first datum in *data* is assigned to the first selected element, the second datum to the second selected element, and so on. A *key* function may be specified to control which datum is assigned to which element, replacing the default join-by-index, by computing a string identifier for each datum and element. This key function is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]); the returned string is the element’s key. The key function is then also evaluated for each new datum in *data*, being passed the current datum (*d*), the current index (*i*), and the group’s new *data*, with *this* as the group’s parent DOM element; the returned string is the datum’s key. The datum for a given key is assigned to the element with the matching key. If multiple elements have the same key, the duplicate elements are put into the exit selection; if multiple data have the same key, the duplicate data are put into the enter selection.
508
509For example, given this document:
510
511```html
512<div id="Ford"></div>
513<div id="Jarrah"></div>
514<div id="Kwon"></div>
515<div id="Locke"></div>
516<div id="Reyes"></div>
517<div id="Shephard"></div>
518```
519
520You could join data by key as follows:
521
522
523```js
524const data = [
525 {name: "Locke", number: 4},
526 {name: "Reyes", number: 8},
527 {name: "Ford", number: 15},
528 {name: "Jarrah", number: 16},
529 {name: "Shephard", number: 23},
530 {name: "Kwon", number: 42}
531];
532
533d3.selectAll("div")
534 .data(data, function(d) { return d ? d.name : this.id; })
535 .text(d => d.number);
536```
537
538This example key function uses the datum *d* if present, and otherwise falls back to the element’s id property. Since these elements were not previously bound to data, the datum *d* is null when the key function is evaluated on selected elements, and non-null when the key function is evaluated on the new data.
539
540The *update* and *enter* selections are returned in data order, while the *exit* selection preserves the selection order prior to the join. If a key function is specified, the order of elements in the selection may not match their order in the document; use [*selection*.order](#selection_order) or [*selection*.sort](#selection_sort) as needed. For more on how the key function affects the join, see [A Bar Chart, Part 2](http://bost.ocks.org/mike/bar/2/) and [Object Constancy](http://bost.ocks.org/mike/constancy/).
541
542If *data* is not specified, this method returns the array of data for the selected elements.
543
544This method cannot be used to clear bound data; use [*selection*.datum](#selection_datum) instead.
545
546<a name="selection_join" href="#selection_join">#</a> <i>selection</i>.<b>join</b>(<i>enter</i>[, <i>update</i>][, <i>exit</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/join.js)
547
548Appends, removes and reorders elements as necessary to match the data that was previously bound by [*selection*.data](#selection_data), returning the [merged](#selection_merge) enter and update selection. This method is a convenient alternative to the explicit [general update pattern](https://bl.ocks.org/mbostock/3808218), replacing [*selection*.enter](#selection_enter), [*selection*.exit](#selection_exit), [*selection*.append](#selection_append), [*selection*.remove](#selection_remove), and [*selection*.order](#selection_order). For example:
549
550```js
551svg.selectAll("circle")
552 .data(data)
553 .join("circle")
554 .attr("fill", "none")
555 .attr("stroke", "black");
556```
557
558The *enter* function may be specified as a string shorthand, as above, which is equivalent to [*selection*.append](#selection_append) with the given element name. Likewise, optional *update* and *exit* functions may be specified, which default to the identity function and calling [*selection*.remove](#selection_remove), respectively. The shorthand above is thus equivalent to:
559
560```js
561svg.selectAll("circle")
562 .data(data)
563 .join(
564 enter => enter.append("circle"),
565 update => update,
566 exit => exit.remove()
567 )
568 .attr("fill", "none")
569 .attr("stroke", "black");
570````
571
572By passing separate functions on enter, update and exit, you have greater control over what happens. And by specifying a key function to [*selection*.data](#selection_data), you can minimize changes to the DOM to optimize performance. For example, to set different fill colors for enter and update:
573
574```js
575svg.selectAll("circle")
576 .data(data)
577 .join(
578 enter => enter.append("circle").attr("fill", "green"),
579 update => update.attr("fill", "blue")
580 )
581 .attr("stroke", "black");
582```
583
584The selections returned by the *enter* and *update* functions are merged and then returned by *selection*.join.
585
586You can animate enter, update and exit by creating transitions inside the *enter*, *update* and *exit* functions. If the *enter* and *update* functions return transitions, their underlying selections are merged and then returned by *selection*.join. The return value of the *exit* function is not used.
587
588For more, see the [*selection*.join notebook](https://observablehq.com/@d3/selection-join).
589
590<a name="selection_enter" href="#selection_enter">#</a> <i>selection</i>.<b>enter</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/enter.js)
591
592Returns the enter selection: placeholder nodes for each datum that had no corresponding DOM element in the selection. (The enter selection is empty for selections not returned by [*selection*.data](#selection_data).)
593
594The enter selection is typically used to create “missing” elements corresponding to new data. For example, to create DIV elements from an array of numbers:
595
596```js
597const div = d3.select("body")
598 .selectAll("div")
599 .data([4, 8, 15, 16, 23, 42])
600 .enter().append("div")
601 .text(d => d);
602```
603
604If the body is initially empty, the above code will create six new DIV elements, append them to the body in-order, and assign their text content as the associated (string-coerced) number:
605
606```html
607<div>4</div>
608<div>8</div>
609<div>15</div>
610<div>16</div>
611<div>23</div>
612<div>42</div>
613```
614
615Conceptually, the enter selection’s placeholders are pointers to the parent element (in this example, the document body). The enter selection is typically only used transiently to append elements, and is often [merged](#selection_merge) with the update selection after appending, such that modifications can be applied to both entering and updating elements.
616
617<a name="selection_exit" href="#selection_exit">#</a> <i>selection</i>.<b>exit</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/exit.js)
618
619Returns the exit selection: existing DOM elements in the selection for which no new datum was found. (The exit selection is empty for selections not returned by [*selection*.data](#selection_data).)
620
621The exit selection is typically used to remove “superfluous” elements corresponding to old data. For example, to update the DIV elements created previously with a new array of numbers:
622
623```js
624div = div.data([1, 2, 4, 8, 16, 32], d => d);
625```
626
627Since a key function was specified (as the identity function), and the new data contains the numbers [4, 8, 16] which match existing elements in the document, the update selection contains three DIV elements. Leaving those elements as-is, we can append new elements for [1, 2, 32] using the enter selection:
628
629```js
630div.enter().append("div").text(d => d);
631```
632
633Likewise, to remove the exiting elements [15, 23, 42]:
634
635```js
636div.exit().remove();
637```
638
639Now the document body looks like this:
640
641```html
642<div>1</div>
643<div>2</div>
644<div>4</div>
645<div>8</div>
646<div>16</div>
647<div>32</div>
648```
649
650The order of the DOM elements matches the order of the data because the old data’s order and the new data’s order were consistent. If the new data’s order is different, use [*selection*.order](#selection_order) to reorder the elements in the DOM. See the [General Update Pattern](http://bl.ocks.org/mbostock/3808218) example thread for more on data joins.
651
652<a name="selection_datum" href="#selection_datum">#</a> <i>selection</i>.<b>datum</b>([<i>value</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/datum.js)
653
654Gets or sets the bound data for each selected element. Unlike [*selection*.data](#selection_data), this method does not compute a join and does not affect indexes or the enter and exit selections.
655
656If a *value* is specified, sets the element’s bound data to the specified value on all selected elements. If the *value* is a constant, all elements are given the same datum; otherwise, if the *value* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). The function is then used to set each element’s new data. A null value will delete the bound data.
657
658If a *value* is not specified, returns the bound datum for the first (non-null) element in the selection. This is generally useful only if you know the selection contains exactly one element.
659
660This method is useful for accessing HTML5 [custom data attributes](http://www.w3.org/TR/html5/dom.html#custom-data-attribute). For example, given the following elements:
661
662```html
663<ul id="list">
664 <li data-username="shawnbot">Shawn Allen</li>
665 <li data-username="mbostock">Mike Bostock</li>
666</ul>
667```
668
669You can expose the custom data attributes by setting each element’s data as the built-in [dataset](http://www.w3.org/TR/html5/dom.html#dom-dataset) property:
670
671```js
672selection.datum(function() { return this.dataset; })
673```
674
675### Handling Events
676
677For interaction, selections allow listening for and dispatching of events.
678
679<a name="selection_on" href="#selection_on">#</a> <i>selection</i>.<b>on</b>(<i>typenames</i>[, <i>listener</i>[, <i>options</i>]]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/on.js)
680
681Adds or removes a *listener* to each selected element for the specified event *typenames*. The *typenames* is a string event type, such as `click`, `mouseover`, or `submit`; any [DOM event type](https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events) supported by your browser may be used. The type may be optionally followed by a period (`.`) and a name; the optional name allows multiple callbacks to be registered to receive events of the same type, such as `click.foo` and `click.bar`. To specify multiple typenames, separate typenames with spaces, such as `input change` or `click.foo click.bar`.
682
683When a specified event is dispatched on a selected element, the specified *listener* will be evaluated for the element, being passed the current event (*event*) and the current datum (*d*), with *this* as the current DOM element (*event*.currentTarget). Listeners always see the latest datum for their element. Note: while you can use [*event*.pageX](https://developer.mozilla.org/en/DOM/event.pageX) and [*event*.pageY](https://developer.mozilla.org/en/DOM/event.pageY) directly, it is often convenient to transform the event position to the local coordinate system of the element that received the event using [d3.pointer](#pointer).
684
685If an event listener was previously registered for the same *typename* on a selected element, the old listener is removed before the new listener is added. To remove a listener, pass null as the *listener*. To remove all listeners for a given name, pass null as the *listener* and `.foo` as the *typename*, where `foo` is the name; to remove all listeners with no name, specify `.` as the *typename*.
686
687An optional *options* object may specify characteristics about the event listener, such as whether it is capturing or passive; see [*element*.addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener).
688
689If a *listener* is not specified, returns the currently-assigned listener for the specified event *typename* on the first (non-null) selected element, if any. If multiple typenames are specified, the first matching listener is returned.
690
691<a name="selection_dispatch" href="#selection_dispatch">#</a> <i>selection</i>.<b>dispatch</b>(<i>type</i>[, <i>parameters</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/dispatch.js)
692
693Dispatches a [custom event](http://www.w3.org/TR/dom/#interface-customevent) of the specified *type* to each selected element, in order. An optional *parameters* map may be specified to set additional properties of the event. It may contain the following fields:
694
695* [`bubbles`](https://www.w3.org/TR/dom/#dom-event-bubbles) - if true, the event is dispatched to ancestors in reverse tree order.
696* [`cancelable`](https://www.w3.org/TR/dom/#dom-event-cancelable) - if true, *event*.preventDefault is allowed.
697* [`detail`](https://www.w3.org/TR/dom/#dom-customevent-detail) - any custom data associated with the event.
698
699If *parameters* is a function, it is evaluated for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). It must return the parameters map for the current element.
700
701<a name="pointer" href="#pointer">#</a> d3.<b>pointer</b>(<i>event</i>[, <i>target</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/pointer.js)
702
703Returns a two-element array of numbers [*x*, *y*] representing the coordinates of the specified *event* relative to the specified *target*. *event* can be a [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent), a [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent), a [Touch](https://www.w3.org/TR/touch-events/#touch-interface), or a custom event holding a UIEvent as *event*.sourceEvent.
704
705If *target* is not specified, it defaults to the source event’s currentTarget property, if available. If the *target* is an SVG element, the event’s coordinates are transformed using the [inverse](https://www.w3.org/TR/geometry-1/#dom-dommatrixreadonly-inverse) of the [screen coordinate transformation matrix](https://www.w3.org/TR/SVG/types.html#__svg__SVGGraphicsElement__getScreenCTM). If the *target* is an HTML element, the event’s coordinates are translated relative to the top-left corner of the *target*’s [bounding client rectangle](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect). (As such, the coordinate system can only be translated relative to the client coordinates. See also [GeometryUtils](https://www.w3.org/TR/cssom-view-1/#the-geometryutils-interface).) Otherwise, [*event*.pageX, *event*.pageY] is returned.
706
707<a name="pointers" href="#pointers">#</a> d3.<b>pointers</b>(<i>event</i>[, <i>target</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/pointers.js)
708
709Returns an array [[*x0*, *y0*], [*x1*, *y1*]…] of coordinates of the specified *event*’s pointer locations relative to the specified *target*. For touch events, the returned array of positions corresponds to the *event*.touches array; for other events, returns a single-element array.
710
711If *target* is not specified, it defaults to the source event’s currentTarget property, if any.
712
713### Control Flow
714
715For advanced usage, selections provide methods for custom control flow.
716
717<a name="selection_each" href="#selection_each">#</a> <i>selection</i>.<b>each</b>(<i>function</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/each.js)
718
719Invokes the specified *function* for each selected element, in order, being passed the current datum (*d*), the current index (*i*), and the current group (*nodes*), with *this* as the current DOM element (*nodes*[*i*]). This method can be used to invoke arbitrary code for each selected element, and is useful for creating a context to access parent and child data simultaneously, such as:
720
721```js
722parent.each(function(p, j) {
723 d3.select(this)
724 .selectAll(".child")
725 .text(d => `child ${d.name} of ${p.name}`);
726});
727```
728
729See [Sized Donut Multiples](http://bl.ocks.org/mbostock/4c5fad723c87d2fd8273) for an example.
730
731<a name="selection_call" href="#selection_call">#</a> <i>selection</i>.<b>call</b>(<i>function</i>[, <i>arguments…</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/call.js)
732
733Invokes the specified *function* exactly once, passing in this selection along with any optional *arguments*. Returns this selection. This is equivalent to invoking the function by hand but facilitates method chaining. For example, to set several styles in a reusable function:
734
735```js
736function name(selection, first, last) {
737 selection
738 .attr("first-name", first)
739 .attr("last-name", last);
740}
741```
742
743Now say:
744
745```js
746d3.selectAll("div").call(name, "John", "Snow");
747```
748
749This is roughly equivalent to:
750
751```js
752name(d3.selectAll("div"), "John", "Snow");
753```
754
755The only difference is that *selection*.call always returns the *selection* and not the return value of the called *function*, `name`.
756
757<a name="selection_empty" href="#selection_empty">#</a> <i>selection</i>.<b>empty</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/empty.js)
758
759Returns true if this selection contains no (non-null) elements.
760
761<a name="selection_nodes" href="#selection_nodes">#</a> <i>selection</i>.<b>nodes</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/nodes.js)
762
763Returns an array of all (non-null) elements in this selection. Equivalent to:
764
765```js
766const elements = Array.from(selection);
767````
768
769See also [*selection*[Symbol.iterator]](#selection_iterator).
770
771<a name="selection_node" href="#selection_node">#</a> <i>selection</i>.<b>node</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/node.js)
772
773Returns the first (non-null) element in this selection. If the selection is empty, returns null.
774
775<a name="selection_size" href="#selection_size">#</a> <i>selection</i>.<b>size</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/size.js)
776
777Returns the total number of (non-null) elements in this selection.
778
779<a name="selection_iterator" href="#selection_iterator">#</a> <i>selection</i>\[<b>Symbol.iterator</b>\]() · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/iterator.js)
780
781Returns an iterator over the selected (non-null) elements. For example, to iterate over the selected elements:
782
783```js
784for (const element of selection) {
785 console.log(element);
786}
787```
788
789To flatten the selection to an array:
790
791```js
792const elements = [...selection];
793````
794
795### Local Variables
796
797D3 locals allow you to define local state independent of data. For instance, when rendering [small multiples](http://bl.ocks.org/mbostock/e1192fe405703d8321a5187350910e08) of time-series data, you might want the same *x*-scale for all charts but distinct *y*-scales to compare the relative performance of each metric. D3 locals are scoped by DOM elements: on set, the value is stored on the given element; on get, the value is retrieved from given element or the nearest ancestor that defines it.
798
799<a name="local" href="#local">#</a> d3.<b>local</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/local.js)
800
801Declares a new local variable. For example:
802
803```js
804const foo = d3.local();
805```
806
807Like `var`, each local is a distinct symbolic reference; unlike `var`, the value of each local is also scoped by the DOM.
808
809<a name="local_set" href="#local_set">#</a> <i>local</i>.<b>set</b>(<i>node</i>, <i>value</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/local.js)
810
811Sets the value of this local on the specified *node* to the *value*, and returns the specified *value*. This is often performed using [*selection*.each](#selection_each):
812
813```js
814selection.each(function(d) { foo.set(this, d.value); });
815```
816
817If you are just setting a single variable, consider using [*selection*.property](#selection_property):
818
819```js
820selection.property(foo, d => d.value);
821```
822
823<a name="local_get" href="#local_get">#</a> <i>local</i>.<b>get</b>(<i>node</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/local.js)
824
825Returns the value of this local on the specified *node*. If the *node* does not define this local, returns the value from the nearest ancestor that defines it. Returns undefined if no ancestor defines this local.
826
827<a name="local_remove" href="#local_remove">#</a> <i>local</i>.<b>remove</b>(<i>node</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/local.js)
828
829Deletes this local’s value from the specified *node*. Returns true if the *node* defined this local prior to removal, and false otherwise. If ancestors also define this local, those definitions are unaffected, and thus [*local*.get](#local_get) will still return the inherited value.
830
831<a name="local_toString" href="#local_toString">#</a> <i>local</i>.<b>toString</b>() · [Source](https://github.com/d3/d3-selection/blob/master/src/local.js)
832
833Returns the automatically-generated identifier for this local. This is the name of the property that is used to store the local’s value on elements, and thus you can also set or get the local’s value using *element*[*local*] or by using [*selection*.property](#selection_property).
834
835### Namespaces
836
837XML namespaces are fun! Right? Fortunately you can mostly ignore them.
838
839<a name="namespace" href="#namespace">#</a> d3.<b>namespace</b>(<i>name</i>) · [Source](https://github.com/d3/d3-selection/blob/master/src/namespace.js)
840
841Qualifies the specified *name*, which may or may not have a namespace prefix. If the name contains a colon (`:`), the substring before the colon is interpreted as the namespace prefix, which must be registered in [d3.namespaces](#namespaces). Returns an object `space` and `local` attributes describing the full namespace URL and the local name. For example:
842
843```js
844d3.namespace("svg:text"); // {space: "http://www.w3.org/2000/svg", local: "text"}
845```
846
847If the name does not contain a colon, this function merely returns the input name.
848
849<a name="namespaces" href="#namespaces">#</a> d3.<b>namespaces</b> · [Source](https://github.com/d3/d3-selection/blob/master/src/namespaces.js)
850
851The map of registered namespace prefixes. The initial value is:
852
853```js
854{
855 svg: "http://www.w3.org/2000/svg",
856 xhtml: "http://www.w3.org/1999/xhtml",
857 xlink: "http://www.w3.org/1999/xlink",
858 xml: "http://www.w3.org/XML/1998/namespace",
859 xmlns: "http://www.w3.org/2000/xmlns/"
860}
861```
862
863Additional prefixes may be assigned as needed to create elements or attributes in other namespaces.
Note: See TracBrowser for help on using the repository browser.