Core Functions and Knowledge Flashcards

1
Q

What is the .select(…) method?

A

Takes a CSS3-type selector or a node.

Returns the first element in the DOM that matches the selector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the .selectAll(…) method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the .style(…) method?

A

Takes two arguments: (“attribute”,”value”) and operates on a selection.

When “value” is omitted, returns the current value of the given attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a selection?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you select by tag?

A

Pass the element tag to the select function.

E.g., d3.select(“div”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you select by element id?

A

Pass the id to the select function.

E.g., d3.select(“myId”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you select by class?

A

Pass the class to the select function.

E.g., d3.select(“.myClass”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you select by attribute?

A

Pass the attribute to the select function with the proper syntax.

E.g., d3.select(“[color=red]”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you select by an element’s containment?

A

Pass the correct order of containment to the select function.

E.g., d3.select(“parent child”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you put logic into selection statements?

A

Selectors can be intersected (“.this.that” for logical AND) or unioned (“.this, .that” for logical OR)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you get / set an attribute?

A

.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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you get / set a class?

A

.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}).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s the syntax for an anonymous function passed to a selection operator?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you get / set style for a selection?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you get / set property for a selection?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you set the text inside a selection?

A

selection.text([value])

If value is specified, replaces all child elements of selection.

Otherwise, returns text of first non-null element in selection.

17
Q

How do you set the html inside a selection?

A

selection.html([value])

If value is specified, replaces all child elements of selection with given html.

18
Q

How do you sub-select elements?

A

Method chain selection calls.

E.g., d3.select(“body”).selectAll(“div”)

19
Q

How do you append an element to a selection?

A

selection.append(value)

E.g.,

d3.select(“body”).append(“header”)

20
Q

How do you insert an element above another element?

A

selection.append(to_insert, inserting_above)

E.g., d3.select(“body”).insert(“div”,”footer”)

places a div above the footer

21
Q

How do you remove a selection?

A

.remove()