From 3c6f3a5338652b9eeaa515658e91948f75bf5b34 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Tue, 23 Aug 2016 21:28:00 -0400 Subject: [PATCH] data binding --- D3.md | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/D3.md b/D3.md index 073a04d..0e3d41d 100644 --- a/D3.md +++ b/D3.md @@ -65,16 +65,31 @@ d3.text('path', function(error, data){}); ## Data binding -- make a "ghost call" to all circles, even if there are none already - - `d3.selectAll('circle').data(dataArray).enter().append('circle');` - - `.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 - - `.append()` - 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 - - `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 +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 + .enter() //returns the sub section of dataArray that has not been matched with DOM 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: + +```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