D3.js and SVG

41
Welcome!

Transcript of D3.js and SVG

Page 1: D3.js and SVG

Welcome!

Page 2: D3.js and SVG

“Networking”

There is a “networking table” near the entrance to the lecture room, where you can put your business cards and prospects :-)

KAROL

DEPKA

Page 3: D3.js and SVG
Page 4: D3.js and SVG

– Woody Allen

“80% percent of success is showing up.”

Page 5: D3.js and SVG

About the presentation• As interactive as possible

• Questions welcome

• Feel free to interrupt at any time

• We can spend as much time as you want, on any slide

• I assume that the most active audience members are the most interested and therefor should be able to control the presentation flow

KAROL

DEPKA

Page 6: D3.js and SVG

A little poll• On SVG:

• who has used SVG?

• who considers themselves proficient with SVG?

• who has plans to learn / use SVG?

• On D3• who has used D3?

• who considers themselves proficient with D3?

• who has plans to learn / use D3?

Hands upplease!

KAROL

DEPKA

Page 7: D3.js and SVG

D3.js

DATA DRIVEN DOCUMENTS

KAROL

DEPKA

Page 8: D3.js and SVG

Data-to-Documents

DATA DOCUMENT

Page 9: D3.js and SVG

SVG

SCALABLE VECTOR GRAPHICS

KAROL

DEPKA

Page 10: D3.js and SVG

Why should you care about D3 and why is it better?

• Interactive, unlike static images

• Open-source

• Good merits and Eye-candy

• Super flexible

KAROL

DEPKA

Page 11: D3.js and SVG

Why should you care about SVG and why is it better?

• Web standard (unlike Flash)

• Runs in the browser without a plugin (unlike Flash)

• Scales well

KAROL

DEPKA

Page 12: D3.js and SVG

Scaling & Resolutions

KAROL

DEPKA

Page 13: D3.js and SVG

Zoom in

Page 14: D3.js and SVG

The final product that we want to achieve

KAROL

DEPKA

Page 15: D3.js and SVG

The features that we want to achieve• Physics-based automatic layout

• Drag-and-drop

• Automatic layout

• Zoom

• Pan

• Mouse-over highlight

KAROL

DEPKA

Page 16: D3.js and SVG

Our example is on GitHubhttps://github.com/karol-depka/D3SVGTechnologyGraph

KAROL

DEPKA

Page 17: D3.js and SVG

https://bl.ocks.org/mbostock/4062045

Force layout

KAROL

DEPKA

Page 18: D3.js and SVG

D3 modules visualized in D3 :-D

KAROL

DEPKA

http://bl.ocks.org/alisd23/5762cc5912253c4febeb

Page 19: D3.js and SVG

Animated SVG

How do I animate SVG?

KAROL

DEPKA

Page 20: D3.js and SVG

Declaring nodes

// … JavaScript: { id: JavaScript, body: "viewBox=\"0 0 256 256\" version= "<path d=\"M67.311746,213.932292 "</svg>\n", sizeMult: 1.2},

KAROL

DEPKA

Page 21: D3.js and SVG

Declaring nodes, part 2

Inkscape: {id: Inkscape}, Illustrator: {id: Illustrator},AffinityDesigner: {id: AffinityDesigner, html: "Affinity<br/>Designer"},

// …

var nodesWebOnly = [ nodes.SVG, // … nodes.NodeJS, nodes.JavaScript, ];

KAROL

DEPKA

Page 22: D3.js and SVG

Declaring links between nodesvar linksWebOnly = [ {source: HTML5, target: Angular2}, {source: Ionic, target: Angular2}, {source: HTML5, target: Angular2}, {source: D3, target: SVG}, {source: JavaScript, target: HTML5}, {source: JavaScript, target: TypeScript}, {source: JavaScript, target: jQuery}, // … {source: Angular2, target: TypeScript}, {source: jQuery, target: HTML5}, {source: HTML5, target: CSS, thick: 10}, {source: SVG, target: HTML5, thick: 10, distance: 1.5}, ];

Page 23: D3.js and SVG

Setting up the D3 force simulation

/* Base Example: Force-Directed Graph: https://bl.ocks.org/mbostock/4062045 */ var simulation = d3.forceSimulation() // .force("gravity", 3) .force("link", d3.forceLink().id(function(d) { return d.id; }) .strength(function(d) { return 3; })) .force("charge", d3.forceManyBody().strength(-1000)) .force("center", d3.forceCenter(width / 2, height / 2));

KAROL

DEPKA

Page 24: D3.js and SVG

Where to handle mouse events

Layer for drag&drop and mouse-over

KAROL

DEPKA

Page 25: D3.js and SVG

Add drag&drop

https://github.com/d3/d3-drag

KAROL

DEPKA

Page 26: D3.js and SVG

Add drag&dropfunction dragStarted(d) { isDragging = true; if (!d3.event.active) { simulation.alphaTarget(0.3).restart(); } d.fx = d.x; d.fy = d.y; } function dragged(d) { isDragging = true; d.fx = d3.event.x; d.fy = d3.event.y; } function dragEnded(d) { isDragging = false; unHighlightHover() if (!d3.event.active) { simulation.alphaTarget(0); } d.fx = null; d.fy = null; }

Page 27: D3.js and SVG

Add drag&drop

nodeCircleOverlay.call( d3.drag() .on("start", dragStarted) .on("drag", dragged) .on("end", dragEnded));

KAROL

DEPKA

Page 28: D3.js and SVG

Add SVG icons

perNodeMainGroup.append("g").html(function(d) { var bodyText = d.body || ""; var size = d.sizeMult ? d.sizeMult * defaultSize : defaultSize; if (bodyText.trim().endsWith("</svg>")) { const htmlContent = '<svg ' + 'width=\"' + size + 'px\" ' + 'height=\"' + size + 'px\" ' + 'x="' + (-size/2) + '" ' + 'y="' + (-size/2) + '" ' + bodyText /* also contains </svg> */; return htmlContent; } else { return ""; }});

Page 29: D3.js and SVG

Add HTML foreignObjectperNodeMainGroup.append("foreignObject") .attr("style", "pointer-events:none;") .attr("width", foreignObjectW) .attr("height", foreignObjectH) .attr("height", foreignObjectH) .attr("x", -foreignObjectW/2) .attr("y", -foreignObjectH/2) .style("font", "9px 'Helvetica Neue'") .html(function(d) { if ( d.body ) { return ""; // has icon: no need for text } var bodyText = d.html || d.id; return "<div style='display: table;" + "text-align:center;" + "height:100%; width:100%'>" + "<p style='display: table-cell; " + "vertical-align: middle'>" + bodyText + "</p></div>"; });

Page 30: D3.js and SVG

ticked() function function ticked() { 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; }); nodeG1.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); }

KAROL

DEPKA

Page 31: D3.js and SVG

Tested on browsers• Chrome

• Firefox

• Safari

• Opera

• Edge

• Internet Explorer

Page 32: D3.js and SVG
Page 33: D3.js and SVG

Browsers, Browsers, Browsers!

• Who here uses IE as their main browser?

• Who here uses IE as their main browser and is not afraid to admit it?

• Who here uses Microsoft Edge as their main browser?

Hands upplease!

Page 34: D3.js and SVG

https://github.com/eisman/neo4jd3

KAROL

DEPKA

Page 35: D3.js and SVG

Cool D3 Exampleshttps://bl.ocks.org/mbostock/

2990a882e007f8384b04827617752738

Page 36: D3.js and SVG

Cool D3 Examples• http://bl.ocks.org/rkirsling/5001347 - directed graph

editor

KAROL

DEPKA

Page 37: D3.js and SVG

Gallery of D3 ExamplesURL:

Page 38: D3.js and SVG

Alternatives to SVG

• Font Awesome (only monochrome?)

• Canvas? (not scalable?)

• Adobe Flash? ;-)

• PDF

KAROL

DEPKA

Page 39: D3.js and SVG

A little poll - after the presentation

• On SVG: • who has plans to learn / use SVG?

• On D3• who has plans to learn / use D3?

Hands upplease!

KAROL

DEPKA

Page 40: D3.js and SVG

Qund A

Page 41: D3.js and SVG

THANKS TO

Ewa, Rafa

Antonio, Guillermo, Abigail

Ania, Greg