utahjs d3

33
D3 & SVG Jason Madsen

Upload: knomedia

Post on 27-Jan-2015

109 views

Category:

Technology


0 download

DESCRIPTION

Getting started presentation on using D3

TRANSCRIPT

Page 1: Utahjs D3

D3 & SVGJason Madsen

Page 2: Utahjs D3

$ whoami@jason_madsen

Page 3: Utahjs D3

What is D3?JS library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG and CSS.

@mbostock

Page 5: Utahjs D3

What is it doing?Create and bind data to DOM elements

Add, remove data, update DOM w/transitions

Map domain data to display data (scales)

Page 6: Utahjs D3

D3 and Me

Page 7: Utahjs D3

Compatibilityhttp://caniuse.com/svg

Page 8: Utahjs D3

Compatibilityhttp://caniuse.com/svg

"You'll need a modern browser to use SVG and CSS3 Transitions. D3 is not a compatibility layer, so if your browser doesn't support standards,

you're out of luck. Sorry!"**

https://github.com/shawnbot/aight

Page 9: Utahjs D3

Getting Startedhttps://github.com/knomedia/utahjs_blank_d3

Page 10: Utahjs D3

Creating SVGsvg  =  d3.select("#chart").append("svg")

Page 11: Utahjs D3

.selectAll(selector).select(selector)

$foo.find(".bar")

Page 12: Utahjs D3

.append(element)add a child tag

Page 13: Utahjs D3

.attr(prop [,value])setter / getter for attribute values

Page 14: Utahjs D3

lots `o chainingsvg  =  d3.select("#chart").append("svg")    .attr("width",  w)    .attr("height",  h);svg  =  svg.append("g")    .attr("transform",  "translate(20,20)");

Page 15: Utahjs D3

Margin Conventionshttp://bl.ocks.org/mbostock/3019563

Page 16: Utahjs D3

.data(array)bind data to the selection

Page 17: Utahjs D3

Joinsseparate into sections: existing, enter, exit

http://mbostock.github.io/d3/tutorial/circle.html

http://bost.ocks.org/mike/join/

Page 18: Utahjs D3

.exit()selection of no longer needed elements

.enter()selection of new elements

generally gets an `append()`

generally gets a `remove()`

Page 19: Utahjs D3

.enter()selection of new elements

generally gets an `append()`

data  =  [50,  50,  50];

?

Page 20: Utahjs D3

.enter()selection of new elements

generally gets an `append()`

data  =  [50,  50,  50];

element in enter selection

Page 21: Utahjs D3

data  =  [50,  50];

.exit()selection of no longer needed elements

generally gets a `remove()`

Page 22: Utahjs D3

data  =  [50,  50];

element in exit selection

.exit()selection of no longer needed elements

generally gets a `remove()`

Page 23: Utahjs D3

simple barssvg.selectAll(".bars")    .data(dataset)    .enter().append("svg:rect")        .attr("class",  "bars  bright")        .attr("height",  function(d,i){  return  d})        .attr("width",  50)        .attr("y",  function(d,i){  return  h  -­‐  d})        .attr("x",  function(d,i){  return  i  *  100  })

Page 24: Utahjs D3

scalesyScale  =  d3.scale.linear()                    .domain([  0,  d3.max(dataset)  ])                    .range([  0,  h  ]);

domain 890

range 5800

Page 25: Utahjs D3

bar height.attr("width",  (w  /  dataset.length)  -­‐  3).attr("height",  function(d,i){  return  yScale(d)})

Page 26: Utahjs D3

scales for color

colorScale  =  d3.scale.category20c();

https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors

colorScale  =  d3.scale.linear()                            .domain([0,  d3.max(dataset)])                            .range(["blue",  "green"]);

or

Page 27: Utahjs D3

transition.transition()    .delay(  ms  )    .ease("cubic-­‐in-­‐out")    .duration(  ms  )        .attr(“prop”,  “value”);

** could also use CSS3 transitions

https://github.com/mbostock/d3/wiki/Transitions#wiki-d3_ease

Page 28: Utahjs D3

transitions.transition()    .delay(  function(d,i){  return  i  *  250  })    .ease("cubic-­‐in-­‐out")    .duration(  300  )        .attr("height",  function(d,i){  return  yScale(d)})        .attr("y",  function(d,i){  return  h  -­‐  yScale(d)})

Page 29: Utahjs D3

axisyAxis  =  d3.svg.axis()                    .scale(yScale)                    .orient("left")                    .ticks(5)                    .tickSize(1);

function  drawAxis  ()  {    svg.append("g")        .attr("class",  "axis")        .call(yAxis);}

Page 30: Utahjs D3

axis

Rethink height and y for bars.

Swap yScale range

.attr("height",  function(d,i){  return  h  -­‐  yScale(d)})

.attr("y",  function(d,i){  return    h  -­‐(h  -­‐  yScale(d))})

.range([  h,  0  ]);

Page 31: Utahjs D3

Plus lots moreCircles Arcs Lines Paths

Page 33: Utahjs D3

Thanks@jason_madsen

knomedia