SVA-DSI
H/T Aysegul Yonet
Not a prototyping tool.
Lot's of examples
Select a parent container and child nodes
$("div p").css('color', 'red').addClass('classy')
d3.select("div").selectAll("p").style('color', 'red')
.attr('class', 'classy');
D3 difference: child nodes might not actually exist
Bind array of data to child nodes
d3.selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.style("font-size", function(d) { return d + "px"; });
Append nodes to parent container
d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.enter().append("p")
.text(function(d) { return "I’m number " + d + "!"; });
Using D3’s enter and exit selections, you can create new nodes for incoming data and remove outgoing nodes that are no longer needed.
var bars = d3.select('svg').selectAll('rect');
//if we have more data, add new rectangles for those data items
bars.enter().append('rect')
.attr('class', 'bar');
//if we have less data points, remove the rectangles that no longer have a data pairing.
bars.exit().remove();
if you forget about the enter and exit selections, you will automatically select only the elements for which there exists corresponding data.
// Update…
var p = d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.text(function(d,i){ return "I'm paragraph number" + i; });
// Enter…
p.enter().append("p")
.text(function(d,i){ return "I'm paragraph number" + i; });
// Exit…
p.exit().remove();
You can also use data or index in a callback to define any property.
d3.select("div").selectAll("p").style("color", function(data, index) {
return i % 2 ? "red" : "blue";
});
D3’s interpolators support both primitives, such as numbers and numbers embedded within strings (font sizes, path data, etc.), and compound values.
d3.selectAll("circle").transition()
.duration(750)
.delay(function(d, i) { return i * 10; })
.attr("r", function(d) { return Math.sqrt(d * scale); });
SVG does not support common HTML attributes or styles such as width, height, position, left, top, right, bottom or float.
Unless you set the style of the SVG text, it inherits font-styles from CSS.
Any transformations (positioning, scaling, rotating) that are applied to the group element will also be applied to each of the child elements.
Exercise: http://tinyurl.com/sva-d3-ex
In case you get stuck... Solution
Scales and Domains
Axis
Tooltips
Loading external data
Scales convert between data and visual encodings.
d3.scale.category10() - Constructs a new ordinal scale with a range of ten categorical colors:
d3.svg.axis creates a new axis generator.
Most commonly used functions