d3.js is designed to present data
Strong support for web standards
Code snippets are live d3 demos
Edit the code if you are following along in your browser
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('rect')
.style('fill', 'red')
.attr('height', h/2)
.attr('width', w/2)
.attr('x', -w/4)
.attr('y', -h/4);
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('circle')
.style('fill', 'red')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', w/8);
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('ellipse')
.style('fill', 'red')
.attr('cx', 0)
.attr('cy', 0)
.attr('rx', w/4)
.attr('ry', w/8);
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('line')
.style('stroke', 'red')
.attr('x1', -w/8)
.attr('y1', -w/8)
.attr('x2', w/8)
.attr('y2', w/8);
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('polyline')
.style('stroke', 'red')
.style('fill', 'none')
.attr('points', '-100,-100 -50,100 50,100 100,-100');
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('polygon')
.style('stroke', 'red')
.style('fill', 'none')
.attr('points', '-100,-100 -50,100 50,100 100,-100');
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('text')
.style('fill', 'red')
.style('stroke', 'none')
.attr('x', -150)
.attr('y', 10)
.text('Hello, Global Markets!');
Work with sets of elements.
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('text')
.style('fill', 'red')
.style('stroke', 'none')
.attr('x', -175)
.attr('y', 10)
.text(d3.select('.select.sandbox svg').property('namespaceURI'));
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('circle').attr('cx', -100).attr('cy', 0).attr('r', 25);
g.append('circle').attr('cx', 0).attr('cy', 0).attr('r', 25);
g.append('circle').attr('cx', 100).attr('cy', 0).attr('r', 25);
g.append('text')
.attr('x', 150)
.attr('y', 10)
.text('= ' + g.selectAll('circle').size());
// g = an SVG 'g' element, h = height in px, w = width in px
var data = g.selectAll('circle')
.data([-100, 0, 100])
.enter()
.append('circle').attr('cy', 0).attr('r', 25)
.attr('cx', function(d,i) { return d; });
g.append('text').attr('x', 150).attr('y', 10)
.text('= ' + g.selectAll('circle').size());
data() enables powerful data-driven operations:
// data = an array of data
//
// data() contains any of the changed array items
data = [100, 50, 100, 50, 100, 50, 100, 50];
// data = an array of data
//
// enter() contains new elements added to this array
data = [100, 200];
// data = an array of data
//
// exit() contains elements removed from this array
data = [250, 225, 200, 175, 150, 125, 100];
// data = an array of data
//
// this selection
data = [
[250, 225, 200],
[175, 150, 125],
[100]
];
Apply a change to a property or style of a primitive over time.
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('rect')
.style('fill', 'red')
.attr('height', h/2)
.attr('width', w/4)
.attr('x', -w/2)
.attr('y', -h/4)
.transition()
.attr('x', w/4);
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('rect')
.style('fill', 'red')
.attr('height', h/2)
.attr('width', w/2)
.attr('x', -w/4)
.attr('y', -h/4)
.transition()
.style('fill', 'green');
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('rect')
.style('fill', 'red')
.attr('height', h/2)
.attr('width', w/4)
.attr('x', -w/2)
.attr('y', -h/4)
.transition()
.delay(3000)
.attr('x', w/4);
// g = an SVG 'g' element, h = height in px, w = width in px
g.append('rect')
.style('fill', 'red')
.attr('height', h/2)
.attr('width', w/4)
.attr('x', -w/2)
.attr('y', -h/4)
.transition()
.duration(3000)
.attr('x', w/4);
// g = an SVG 'g' element, h = height in px, w = width in px
g.selectAll('rect')
.data([0, 10, 20])
.enter()
.append('rect').style('fill', 'red').attr('height', h/4).attr('width', w/8).attr('x', -w/2)
.attr('y', function(d){ return (10 * d) - h/2; })
.transition()
.delay( function(d){ return 100 * (10 + d); })
.duration( function(d){ return 100 * (10 + d); })
.attr('x', w*3/8);
Control data distribution across the canvas