Core Functions and Knowledge Flashcards
What is the .select(…) method?
Takes a CSS3-type selector or a node.
Returns the first element in the DOM that matches the selector.
What is the .selectAll(…) method?
Takes a CSS3-type selector or a node.
As an example of the latter, consider d3.selectAll(this.childNodes), which might be inside an event listener.
Returns the all elements in the DOM that match the selector.
What is the .style(…) method?
Takes two arguments: (“attribute”,”value”) and operates on a selection.
When “value” is omitted, returns the current value of the given attribute.
What is a selection?
A selection is an array (technically, a sub-class of an Array) of elements pulled from the current document. D3 uses CSS3 to select elements.
One nuance is that selections are grouped: rather than a one-dimensional array, each selection is an array of arrays of elements.
How do you select by tag?
Pass the element tag to the select function.
E.g., d3.select(“div”)
How do you select by element id?
Pass the id to the select function.
E.g., d3.select(“myId”)
How do you select by class?
Pass the class to the select function.
E.g., d3.select(“.myClass”)
How do you select by attribute?
Pass the attribute to the select function with the proper syntax.
E.g., d3.select(“[color=red]”)
How do you select by an element’s containment?
Pass the correct order of containment to the select function.
E.g., d3.select(“parent child”)
How do you put logic into selection statements?
Selectors can be intersected (“.this.that” for logical AND) or unioned (“.this, .that” for logical OR)
How do you get / set an attribute?
.attr(name [,value])
If value is specified, sets the attribute with the specified name to the specified value on all selected elements. If value is a constant, then all elements are given the same attribute value; otherwise, function syntax applies.
If value is not specified, returns the 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.
How do you get / set a class?
.classed(name[,value])
If value is specified, sets whether or not the specified class is associated with the selected elements.
If value is a constant and truthy, then all elements are assigned the specified class, if not already assigned; if falsey, then the class is removed from all selected elements, if assigned.
If value is a function, uses the typical function rules
If you want to set several classes at once, use an object literal like so: selection.classed({‘foo’: true, ‘bar’: false}).
What’s the syntax for an anonymous function passed to a selection operator?
If “value” is a function, then the function is evaluated for each selected element (in order), being passed the current datum d and the current index i, with the this context as the current DOM element. The function’s return value is then used to set each element’s attribute.
How do you get / set style for a selection?
selection.style(attr[, value[, priority]])
If value is specified, sets the CSS style property with the specified name to the specified value on all selected elements. If value is a constant, then all elements are given the same style value; otherwise, accepts a function.
An optional priority may also be specified, either as null or the string “important” (without the exclamation point).
If you want to set several style properties at once, use an object literal like so: selection.style({‘stroke’: ‘black’, ‘stroke-width’: 2})
How do you get / set property for a selection?
selection.property(name[, value])
Some HTML elements have special properties that are not addressable using standard attributes or styles. For example, form text fields have a value string property.
If value is specified, sets the property with the specified name to the specified value on all selected elements.
If you want to set several properties at once, use an object literal like so: selection.property({‘foo’: ‘bar’, ‘baz’: ‘qux’}).
If value is not specified, returns the value of the specified property for the first non-null element in the selection.