having fun with graphs, a short introduction to d3.js

17
Having fun with graphs An introduction to d3.js Michael Hackstein @mchacki August 13, 2013 1 / 15

Upload: michael-hackstein

Post on 10-May-2015

1.067 views

Category:

Technology


1 download

DESCRIPTION

This talk is all about drawing on your webpage. We will have a short introduction to d3.js, a library to easily create SVGs in your webpage. Along the way we will render graphs using different layouting strategies. But what are the problems when displaying a graph? Just think of graphs having more vertices then you have pixels on your screen. Or what if you want a user to manupilate the graph and his changes being persistent? Michael will present his answers to this questions, ending up wit a GUI for a graph database.

TRANSCRIPT

Page 1: Having fun with graphs, a short introduction to D3.js

Having fun with graphsAn introduction to d3.js

Michael Hackstein@mchacki

August 13, 2013

1 / 15

Page 2: Having fun with graphs, a short introduction to D3.js

d3.js

Short DescriptionTake javascript objects or a JSON fileCreate SVGs out of themSynchronize objects and SVGAPI quite similar to jQuery

⇒ “The jQuery for SVG”

2 / 15

Page 3: Having fun with graphs, a short introduction to D3.js

Examples

3 / 15

Page 4: Having fun with graphs, a short introduction to D3.js

Examples

3 / 15

Page 5: Having fun with graphs, a short introduction to D3.js

Examples

3 / 15

Page 6: Having fun with graphs, a short introduction to D3.js

Simple Setup

var nodes = [{id: 0, x: 100, y: 80}, ...];var edges = [{id: 0,

source :nodes [0],target :nodes [1]

}, ...];

var svg = d3. select ("body"). append ("svg").attr("width", 960).attr(" height ", 500);

draw(svg , nodes , edges);

4 / 15

Page 7: Having fun with graphs, a short introduction to D3.js

The draw function

var draw = function (svg , nodes , edges) {var d3Edge = svg. selectAll (".link")

.data(edges)

.enter (). append ("line").attr("class", "link").attr("x1", function (d) { return d. source .x; }).attr("y1", function (d) { return d. source .y; }).attr("x2", function (d) { return d. target .x; }).attr("y2", function (d) { return d. target .y; });

var d3Node = svg. selectAll (".node").data(nodes).enter (). append (" circle ")

.attr("r", 25)

.attr("cx", function (d){ return d.x})

.attr("cy", function (d){ return d.y})

.style("fill", "red");};

5 / 15

Page 8: Having fun with graphs, a short introduction to D3.js

Tree Layout Datastructure

var treeData = {id: 1,children : [

{id: 2},{

id: 3,children : [

{id: 4},{id: 5},{id: 6}]

},]

};

6 / 15

Page 9: Having fun with graphs, a short introduction to D3.js

Tree Layout

var tree = d3. layout .tree ().size ([ width , height ]);

var nodes = tree.nodes( treeData );var edges = tree.links(nodes);draw(svg , nodes , edges);

7 / 15

Page 10: Having fun with graphs, a short introduction to D3.js

Force-Based Layout Idea

→ attraction ←

←rep

ulsion →

←repulsion →

Figure : Forces in Force-Directed Layout

AttentionRuntime: O(n2) ⇒ Only small number of nodes

8 / 15

Page 11: Having fun with graphs, a short introduction to D3.js

Force-Based Layout Setup

var force = d3. layout .force (). charge ( -120). linkDistance (30).size ([ width , height ]).nodes(nodes).links(edges).start ();

draw(svg , nodes , edges);

9 / 15

Page 12: Having fun with graphs, a short introduction to D3.js

Force-Based Layout Update Position

force.on("tick", function () {svg. selectAll (".edge")

.attr("x1", function (d) { return d. source .x;})

.attr("y1", function (d) { return d. source .y;})

.attr("x2", function (d) { return d. target .x;})

.attr("y2", function (d) { return d. target .y;});

svg. selectAll (".node").attr("cx", function (d) { return d.x; }).attr("cy", function (d) { return d.y; });

});

10 / 15

Page 13: Having fun with graphs, a short introduction to D3.js

Adding User-Interaction

Similar to jQuery.Add in the draw function:

d3Node .on("click", function (d) {alert(" Clicked on Node with ID: " + d.id);

}).on(" mouseover ", function (d) {

d3. select (this).style("fill", "green");}).on(" mouseout ", function (d) {

d3. select (this).style("fill", "red");});

11 / 15

Page 14: Having fun with graphs, a short introduction to D3.js

Difficulties

Often details are importantShow everything always is not useful

Many nodesLayout-algorithms get slowNot enough pixels for nodes

12 / 15

Page 15: Having fun with graphs, a short introduction to D3.js

Fisheye-Distortion

Figure : Fish-Eye Distortion

Mouse-pointer is the focusMagnifies close objectsMinifies far away objects

13 / 15

Page 16: Having fun with graphs, a short introduction to D3.js

Group Nodes to smaller parts

Figure : Group similar nodes

Group nodes (f.e. by similar attributes)Layout each group separatelyTreat each group as one large nodeThen layout group-nodes

14 / 15

Page 17: Having fun with graphs, a short introduction to D3.js

A complete GUI for a Graph-Database

DEMO

15 / 15