Advanced D3 Charting

23
Advanced D3 Charting Blake Dietz Daniel Ryan

description

A presentation on creating reusable components with D3.js

Transcript of Advanced D3 Charting

Page 1: Advanced D3 Charting

Advanced D3 Charting

Blake Dietz Daniel Ryan

Page 2: Advanced D3 Charting

What is ?

Page 3: Advanced D3 Charting

What is D3?• http://d3js.org/

• D3.js is a JavaScript library for manipulating for binding data to your documents.

• D3 helps you bring data to life using HTML, canvas, SVG and CSS.

Page 4: Advanced D3 Charting

Why am I here?

Page 5: Advanced D3 Charting

Why am I here?!

• Learn how to make your D3 code cleaner

• How to build reusable components

• Get a better grasp of D3 by understanding the pattern it's built upon

Page 6: Advanced D3 Charting

Getting Started

Page 7: Advanced D3 Charting

Getting Started• SVG Conventions

• Coordinate system

• Selecting elements in the D.O.M.

• d3.select()

• Binding the data to elements

• .selectAll('<element>').data(<your data>).enter()

• CSS

Page 8: Advanced D3 Charting

Margin Conventions

Page 9: Advanced D3 Charting

Coordinate System Quirksx axis

y ax

is

Page 10: Advanced D3 Charting

Scales

Page 11: Advanced D3 Charting

Linear vs Ordinal

Range Interval

.rangePoints()

.rangeBands()

.rangeBand()

Page 12: Advanced D3 Charting

CSS for SVG/* typical CSS */ div { background: blue; } !/* SVG CSS */ rects { fill: blue; shape-rendering: crisp-edges; }

SVG elements use unique CSS

styles

Page 13: Advanced D3 Charting

Selections

Page 14: Advanced D3 Charting

Selections are the basic data type in D3 !// select all <rect> elements var rects = d3.selectAll("rect"); !// set some attributes and styles rects.attr("height", 50); rects.attr("width", 20); rects.style("fill", “blue");

Applies the attributes and styles as a whole!(similar to jQuery)

Page 15: Advanced D3 Charting

Data

Page 16: Advanced D3 Charting

Data are Arrays (that's not a mistake data is the plural form of datum)

Selections are arrays, data are arrays. Coincidence? Nope.

Page 17: Advanced D3 Charting

Data Binding !Datum Element

Page 18: Advanced D3 Charting

Data Joins

Enter Update Exit

Page 19: Advanced D3 Charting

!

Boiler Plate: http://jsfiddle.net/blakedietz/72aFH/2/

!

Final Product: http://jsfiddle.net/blakedietz/zxX98/1/

!

!

Page 20: Advanced D3 Charting

Divide Your Chart into Groupings

(charts are like onions)

Page 21: Advanced D3 Charting

Layers

Axes

Data

TextInterface

<svg> <g id="Axes"></g> <g id="Data"></g> <g id="Text"></g> <g id="Interface"></g> </svg>

Page 22: Advanced D3 Charting

Under the Covers (getting intimate with d3)

Page 23: Advanced D3 Charting

Thanks!@dietztweetz @dcryan22