Slide 1

Slide 1 text

Philip Roberts d3.js Sunday, 17 February 13

Slide 2

Slide 2 text

Sunday, 17 February 13

Slide 3

Slide 3 text

Visualisations Animated Maps Data Sunday, 17 February 13

Slide 4

Slide 4 text

Demos Sunday, 17 February 13

Slide 5

Slide 5 text

Edinburgh Bus Stops Earthquakes in the last 30 days Nuclear War Tech Meetup Demo Sunday, 17 February 13

Slide 6

Slide 6 text

Key Concepts Sunday, 17 February 13

Slide 7

Slide 7 text

Data Bindings Utilities SVG Sunday, 17 February 13

Slide 8

Slide 8 text

Vectors in the browser Sunday, 17 February 13

Slide 9

Slide 9 text

HTML: D3: svg.append('circle') .attr({cx: 20, cy: 50, r: 10, fill: 'purple'}) svg.append('rect') .attr({ x: 100, y: 200, width: 20, height: 10 }) .attr('stroke', 'green') SVG building svg programmatically Sunday, 17 February 13

Slide 10

Slide 10 text

(cx, cy) r (x1, y1) (x2, y2) (x, y) width height svg.append('circle') .attr('cx', 10) .attr('cy', 20) .attr('r', 50) svg.append('line') .attr('x1', 10) .attr('y2', 10) .attr('x2', 20) .attr('y2', 20) svg.append('rect') .attr('x', 10) .attr('y', 10) .attr('width', 50) .attr('height', 20) Shapes basic svg elements Sunday, 17 February 13

Slide 11

Slide 11 text

svg.append('text') .attr('x', 10) .attr('y', 20) .text('foo') Shapes basic svg elements foo (x, y) } text ... group = svg.append('g') .attr('transform', 'rotate(30)translate(10,20)') group.append('rect')... group.append('text')... foo Sunday, 17 February 13

Slide 12

Slide 12 text

Adding data Databinding Sunday, 17 February 13

Slide 13

Slide 13 text

Scales Data Binding binding data to your visualisation people = [ { name: ‘roy’, age: 60 }, { name: ‘jim’, age: 40 }, { name: ‘bob’, age: 25 }, ] roy jim bob Sunday, 17 February 13

Slide 14

Slide 14 text

Scales Data Binding binding data to your visualisation people = [ { name: ‘roy’, age: 60 }, { name: ‘jon’, age: 40 }, { name: ‘bob’, age: 25 }, { name: ‘son’, age: 16 }, ] roy jon bob son Sunday, 17 February 13

Slide 15

Slide 15 text

Scales Data Binding enter(): new data being added data = [ {x: 10, y: 10}, {x: 20, y: 20} ] points = svg.selectAll(‘circle’) .data(data) points.enter().append(‘circle’) .attr(‘cx’, function(p) { return p.x }) .attr(‘cy’, function(p) { return p.y }) .attr(‘r’, 10) Sunday, 17 February 13

Slide 16

Slide 16 text

Scales Data Binding enter(): new data being added data = [ {x: 10, y: 10}, {x: 20, y: 20} ] data.push({x: 30, y: 30}) points = svg.selectAll(‘circle’) .data(data) points.enter().append(‘circle’) .attr(‘cx’, function(p) { return p.x }) .attr(‘cy’, function(p) { return p.y }) .attr(‘r’, 10) Sunday, 17 February 13

Slide 17

Slide 17 text

Scales Data Binding data being changed data = [ {x: 10, y: 10}, {x: 20, y: 20}, {x: 30, y: 30} ] data[3].y = 10 points = svg.selectAll(‘circle’) .data(data) points.transition().duration(2000) .attr(‘cy’, function(p) { return p.y }) Sunday, 17 February 13

Slide 18

Slide 18 text

Scales Data Binding exit(): data being removed data = [ {x: 10, y: 10}, {x: 20, y: 20}, {x: 30, y: 30} ] data.pop() //removes the last one points = svg.selectAll(‘circle’) .data(data) points.exit().remove() Sunday, 17 February 13

Slide 19

Slide 19 text

Making life easier Utilities Sunday, 17 February 13

Slide 20

Slide 20 text

Scales d3.scale.linear() .domain([20, 100]) .range([0, 500]) Utilities scales: converting data to pixels I have a list of adult’s ages that I want to draw on my diagram The domain is the extent of their ages, in this case 20 – 100 The range is the extent of the axis on which I want to draw (0 – 500px) 0px 500px ages = [20, 30, 60, 75, 100] Sunday, 17 February 13

Slide 21

Slide 21 text

9% 11% 12% 31% 38% Pie Force Histogram Utilities layouts: common visualisations Sunday, 17 February 13

Slide 22

Slide 22 text

Utilities geo: projections var width = 960, height = 500; var projection = d3.geo.orthographic() .scale(245) .clipAngle(90); var path = d3.geo.path() .projection(projection); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); svg.append("path") .datum({type: "Sphere"}) .attr("class", "background") .attr("d", path); svg.append("path") .datum({type: "Sphere"}) .attr("class", "foreground") .attr("d", path); d3.json("/data/world-countries.json", function(error, world) { svg.insert("path", ".graticule") .datum(topojson.object(world, world.objects.land)) .attr("class", "land") .attr("d", path); }); Sunday, 17 February 13

Slide 23

Slide 23 text

Resources Sunday, 17 February 13

Slide 24

Slide 24 text

D3 Homepage & Examples D3 Full Documentation NVD3: Simple D3 based charts Sunday, 17 February 13