data binding

master
Matt Huntington 9 years ago
parent 6f98575c7e
commit 3c6f3a5338

35
D3.md

@ -65,16 +65,31 @@ d3.text('path', function(error, data){});
## Data binding ## Data binding
- make a "ghost call" to all circles, even if there are none already ```javascript
- `d3.selectAll('circle').data(dataArray).enter().append('circle');` d3.selectAll('circle')//make a "ghost call" to all circles, even if there are none already
- `.data(dataArray)` joins each element in dataArray to an element in the selection .data(dataArray) //joins each element in dataArray to an element in the selection
- `.enter()` returns the sub section of dataArray that has not been matched with DOM elements .enter() //returns the sub section of dataArray that has not been matched with DOM elements
- `.append()` - creates a DOM element for each of the remaining dataArray elements .append('circle'); //creates a DOM element for each of the remaining dataArray elements
- once data has been bound to elements, you can call something like `selection.attr('r', function(d,i){ })` or `selection.style('fill', function(d,i){ })` ```
- callback will be executed for each DOM element
- `d` is data for the current element once data has been bound to elements, you can call something like:
- `i` is the index of that element in the array
- `.exit().remove()` examines visual elements that are not bound to data and removes them ```javascript
selection.attr('r', function(d,i){ //d is data for the current element, i is the index of that element in the array
//callback will be executed for each DOM element
//return value is how each value will be set
return d.value * 2 //takes value property of d (data), multiplies it by two and sets the radius to that
})
```
Can remove elements:
```javascript
d3.selectAll('circle')//make a "ghost call" to all circles, even if there are none already
.data(dataArray) //joins each element in dataArray to an element in the selection
.exit() //returns the sub section of DOM elements that has not been matched with dataArray elements
.remove(); //removes those elements
```
## Linear Scale ## Linear Scale

Loading…
Cancel
Save