d3js tutorial - univie.ac.atvda.univie.ac.at/teaching/vis/14s/lecturenotes/10_d3... ·...

23
D3js Tutorial Tom Torsney-Weir Michael Trosin

Upload: others

Post on 30-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

D3js Tutorial

Tom Torsney-WeirMichael Trosin

Page 2: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

http://www.washingtonpost.com/wp-srv/special/politics/state-vs-federal-exchanges/

Page 3: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

Contents

● Some important aspects of JavaScript● Introduction to SVG● CSS● D3js● Hands On● Further Reading

Page 4: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

JavaScript - Datatypes

● Variable declaration

● Datatypes are handled automatically

→ may lead to problems!

var a = “Arr”;var b = 23;

var a = 1;var b = 2;var c = a + b; // c = 3

Page 5: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

JavaScript - Datatypes

● Variable declaration

● Datatypes are handled automatically

→ may lead to problems!

var a = “Arr”;var b = 23;

var a = 1;var b = 2;var c = a + b; // c = 3

var a = 1;var b = “2”;var c = a + b; // c = 12 (!)

Page 6: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

JavaScript - Datatypes

● Variable declaration

● Datatypes are handled automatically

→ may lead to problems!

var a = “Arr”;var b = 23;

var a = 1;var b = 2;var c = a + b; // c = 3

var a = 1;var b = “2”;var c = a + +b; // c = 3 (casted b to a number)

Page 7: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

7

JavaScript - Arrays

● declaration

● iterating:

var a = [];var a = new Array();

for(var i = 0; i < a.length; i++){

alert(a[i]);}

var myfunction = function(element){

alert(element);}

a.forEach( myfunction );

Page 8: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

8

JavaScript - Objects

● Everything is a object● Objects can be extended easily

var obj = {}; // we have an empty object now

obj.speed = 10; // obj has now a member!obj[“arr”] = 1; // objects can be used as a map

Page 9: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

9

Scalable Vector Graphics

● Extending HTML with graphics

● New Tags <svg>, <g>, <rect>, <circle>, <text>● http://www.w3schools.com/SVG

Page 10: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

10

SVG

● How to use?<svg width="300" height="300" style="border: 1px solid #CCCCCC"> <circle cx="100" cy="100" r="40" fill="orange"/> <g id="testgroup" opacity="0.3"> <rect x="110" y="110" width="50" height="20" /> <text x="130" y="150">Hello</text> </g></svg>

Page 11: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

11

SVG - transformations

● Translate, Rotate, Scale:

● Take care of the order!

<svg width="300" height="300" style="border: 1px solid #CCCCCC"> <rect width="50" height="20" transform="translate(50 50) scale(2 1) rotate(45 25,5)" /></svg>

Page 12: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

12

CSS● Styling your graphics● Write a css-file containing e.g.:

● Include it in your html-file:

svg { border: 1px solid grey;}

rect { stroke: blue; stroke-dasharray: 2,5; stroke-width: 3; fill: orange;}

<link href="test.css" rel="stylesheet" type="text/css" />

Page 13: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

13

CSSsvg { border: 1px solid grey;}

rect { stroke: blue; stroke-dasharray: 2,5; stroke-width: 3; fill: orange;}

Page 14: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

14

CSS

● What can you manipulate?

field description

stroke: {color} Sets the stroke-color (used to outline the shape)

stroke-width: {in pixel} Sets the width of the stroke

stroke-opacity: {0.0 .. 1.0} Sets the opacity

stroke-linecap: {round, butt or square}

Sets the line-caps

fill: {color} Sets the fill-color

fill-opacity: {0.0 .. 1.0} Sets the fill-opacity of the shape

Page 15: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

15

D3js

● JavaScript-Library like jQuery● http://www.d3js.org● Modify the HTML-DOM easily● Support for animations● Compatibility: only problems with old versions

of IE (< v9) and Android Browser (< v3)

Page 16: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

16

D3js – function chaining

var a = d3.select("#barchart");var b = a.append("g");var c = b.attr("transform", "translate(10,10)");

d3.select("#barchart").append("g").attr("transform", "translate(10,10)");

-or-

d3.select("#barchart") .append("g") .attr("transform", "translate(10,10)");

Page 17: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

17

D3js – create elements

● Automatically adds new elements for the given data● If myData has elements from a previous call new

elements won't be created– enter() only applied onto not-existent

data-elements

var chart = d3.select("chart");

chart.selectAll(".group") .data(myData) .enter().append("rect");

Page 18: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

18

D3js – load data

● Loading CSV/TSV-files

● is loaded asynchronously

→ callback is called, when all data are read

d3.csv(filename, doneCallback);d3.tsv(filename, doneCallback);

d3.csv(filename, function(error, data) { data.forEach(function(d) { d.year = +d.year; } // ... } );

Page 19: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

19

D3js – Mapping Coordinates

● We have data in data-space → how to map them to screen-space?

● D3 offers simple helpers:

● linear, log, ordinal, category10

var y = d3.scale.linear() .range([height, 0]) .domain([minimum, maximum]);

function(d) {return y(d); } will give the correct screen-coordinate for the given value d

Page 20: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

20

Interaction - Drag

function dragged() { chart.selectAll(".bar") .attr("y", d3.event.y) .attr("height", height - d3.event.y); }

// Add a drag handle to change the return bar chart.selectAll(".bar") .call(d3.behavior.drag().on("drag", dragged));

Page 21: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

21

Hands On / Live Demo

Page 22: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

22

Further Information

● Tools and Toolkits– Zurb Foundation

– Twitter Bootstrap

– JQuery

● See also Resources-list on course-site

Page 23: D3js Tutorial - univie.ac.atvda.univie.ac.at/Teaching/Vis/14s/LectureNotes/10_D3... · 2015-02-24 · 14 CSS What can you manipulate? field description stroke: {color} Sets the stroke-color

23

Thank you foryour attention