Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The joy of d3.js

Makoto Inoue
January 30, 2013
3.7k

The joy of d3.js

Makoto Inoue

January 30, 2013
Tweet

Transcript

  1. GOAL “Understand enough basic so that you can tweak examples

    by yourself” Wednesday, 30 January 13
  2. TOPICS 1.Introduction 2.Drawing Circles 3.Debugging 4.Basic Transition 5.Scaling 6.Array, Group

    And Event 7.Loading Multiple Data files 8.Geo https://github.com/makoto/the_joy_of_d3js/wiki Wednesday, 30 January 13
  3. DRAWING CIRCLES var width = 500,height = 400, data =

    [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, {id:3,x:490,y:270} ] var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) var circle = svg.selectAll("circle") .data(data); circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) Wednesday, 30 January 13
  4. DRAWING CIRCLES var width = 500,height = 400, data =

    [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, {id:3,x:490,y:270} ] var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) var circle = svg.selectAll("circle") .data(data); circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) Wednesday, 30 January 13
  5. DRAWING CIRCLES var width = 500,height = 400, data =

    [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, {id:3,x:490,y:270} ] var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) var circle = svg.selectAll("circle") .data(data); circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) Wednesday, 30 January 13
  6. DRAWING CIRCLES var width = 500,height = 400, data =

    [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, {id:3,x:490,y:270} ] var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) var circle = svg.selectAll("circle") .data(data); circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) Wednesday, 30 January 13
  7. DRAWING CIRCLES var width = 500,height = 400, data =

    [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, {id:3,x:490,y:270} ] var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) var circle = svg.selectAll("circle") .data(data); circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) Wednesday, 30 January 13
  8. DRAWING CIRCLES var width = 500,height = 400, data =

    [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, {id:3,x:490,y:270} ] var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) var circle = svg.selectAll("circle") .data(data); circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) Wednesday, 30 January 13
  9. BASIC TRANSITION var width = 500, height = 400, data1

    = [ {id:1,x:163,y:10}, {id:2,x:326,y:140}, ], data2 = [ {id:2,x:400,y:340}, {id:3,x:290,y:280} ]; var svg = d3.select("body").append("svg") .attr({ class:"svg", width:width, height:height }) Wednesday, 30 January 13
  10. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  11. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  12. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  13. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  14. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  15. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  16. BASIC TRANSITION var chart = function(data){ var circle = svg.selectAll("circle")

    .data(data, function(d){ return d.id }) circle.enter().append("circle") .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 0) circle.transition().duration(2000) .attr("cx", function(d){return d.x}) .attr("cy", function(d){return d.y}) .attr("r", 5) circle.exit() .remove() } chart(data1) Wednesday, 30 January 13
  17. SCALING year1 = [ { id:1, region:'africa', population: 10000,lifeExpectancy:163,income:10}, {

    id:2, region:'asia', population: 20000,lifeExpectancy:326,income:140 }, ] year2 = [......] Wednesday, 30 January 13
  18. SCALING var x = d3.scale.linear() .domain([0, 400]) .range([0, width -

    100]); var y = d3.scale.linear() .domain([0,200]) .range([0, width - 100]) circle.enter().append("circle") .attr("cx", function(d){ return x(d.lifeExpectancy) }) .attr("cy", function(d){ return y(d.income) }) Wednesday, 30 January 13
  19. SCALING var x = d3.scale.linear() .domain([0, 400]) .range([0, width -

    100]); var y = d3.scale.linear() .domain([0,200]) .range([0, width - 100]) circle.enter().append("circle") .attr("cx", function(d){ return x(d.lifeExpectancy) }) .attr("cy", function(d){ return y(d.income) }) Wednesday, 30 January 13
  20. SCALING var x = d3.scale.linear() .domain([0, 400]) .range([0, width -

    100]); var y = d3.scale.linear() .domain([0,200]) .range([0, width - 100]) circle.enter().append("circle") .attr("cx", function(d){ return x(d.lifeExpectancy) }) .attr("cy", function(d){ return y(d.income) }) Wednesday, 30 January 13
  21. SCALING var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickSize(1) svg.append("g") .attr("class",

    "y axis"); d3.select(".y.axis") .call(yAxis); svg.append("text") .attr("class", "y label") .attr("text-anchor", "end") .attr("y", -45) .attr("x", -120) .attr("dy", ".75em") .attr("transform", "rotate(-90)") .text("life expectancy (years)"); Wednesday, 30 January 13
  22. SCALING var yAxis = d3.svg.axis() .scale(y) .orient("left") .tickSize(1) svg.append("g") .attr("class",

    "y axis"); d3.select(".y.axis") .call(yAxis); svg.append("text") .attr("class", "y label") .attr("text-anchor", "end") .attr("y", -45) .attr("x", -120) .attr("dy", ".75em") .attr("transform", "rotate(-90)") .text("life expectancy (years)"); Wednesday, 30 January 13
  23. ARRAY, GROUP, EVENT var group = svg.selectAll(".group") .data(data, function(d){return d.country});

    var groupEnter = group.enter().append('g') .attr('class', 'group') var groupUpdate = group.transition().duration(2000) var groupExit = d3.transition(group.exit()) .remove() groupEnter.append("circle") .attr("cx", function(d){return x(d.income)}) .attr("cy", function(d){return y(d.lifeExpectancy)}) .attr("r", 2) .style("fill", function(d){return color(d.country)}) .on('mouseover', function(d){ d3.select(this.parentElement).select('text') .style('opacity', 1) }) Wednesday, 30 January 13
  24. ARRAY, GROUP, EVENT var group = svg.selectAll(".group") .data(data, function(d){return d.country});

    var groupEnter = group.enter().append('g') .attr('class', 'group') var groupUpdate = group.transition().duration(2000) var groupExit = d3.transition(group.exit()) .remove() groupEnter.append("circle") .attr("cx", function(d){return x(d.income)}) .attr("cy", function(d){return y(d.lifeExpectancy)}) .attr("r", 2) .style("fill", function(d){return color(d.country)}) .on('mouseover', function(d){ d3.select(this.parentElement).select('text') .style('opacity', 1) }) Wednesday, 30 January 13
  25. ARRAY, GROUP, EVENT var group = svg.selectAll(".group") .data(data, function(d){return d.country});

    var groupEnter = group.enter().append('g') .attr('class', 'group') var groupUpdate = group.transition().duration(2000) var groupExit = d3.transition(group.exit()) .remove() groupEnter.append("circle") .attr("cx", function(d){return x(d.income)}) .attr("cy", function(d){return y(d.lifeExpectancy)}) .attr("r", 2) .style("fill", function(d){return color(d.country)}) .on('mouseover', function(d){ d3.select(this.parentElement).select('text') .style('opacity', 1) }) Wednesday, 30 January 13