SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Having fun with graphs
An introduction to d3.js

Michael Hackstein
@mchacki

August 13, 2013

1 / 15
d3.js

Short Description
Take javascript objects or a JSON file
Create SVGs out of them
Synchronize objects and SVG
API quite similar to jQuery
⇒ “The jQuery for SVG”

2 / 15
Examples

3 / 15
Examples

3 / 15
Examples

3 / 15
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
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
. attr ( " y1 " , function ( d ) { return
. attr ( " x2 " , function ( d ) { return
. attr ( " y2 " , function ( d ) { return

{

d . source . x ;
d . source . y ;
d . target . x ;
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
Tree Layout Datastructure

var treeData = {
id : 1 ,
children : [
{ id : 2} ,
{
id : 3 ,
children : [
{ id : 4} ,{ id : 5} ,{ id : 6}
]
},
]
};

6 / 15
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
on

lsi

on

pu
re

re
pu
l si

←

→

Force-Based Layout Idea

←

→

→ attraction ←

Figure : Forces in Force-Directed Layout

Attention
Runtime: O(n2 ) ⇒ Only small number of nodes

8 / 15
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
Force-Based Layout Update Position

force . on ( " tick " , function ()
svg . selectAll ( " . edge " )
. attr ( " x1 " , function ( d )
. attr ( " y1 " , function ( d )
. attr ( " x2 " , function ( d )
. attr ( " y2 " , function ( d )

{
{ return
{ return
{ return
{ return

d . source . x ;})
d . source . y ;})
d . target . x ;})
d . target . y ;}) ;

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

10 / 15
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
Difficulties

Often details are important
Show everything always is not useful
Many nodes
Layout-algorithms get slow
Not enough pixels for nodes

12 / 15
Fisheye-Distortion

Figure : Fish-Eye Distortion

Mouse-pointer is the focus
Magnifies close objects
Minifies far away objects

13 / 15
Group Nodes to smaller parts

Figure : Group similar nodes

Group nodes (f.e. by similar attributes)
Layout each group separately
Treat each group as one large node
Then layout group-nodes

14 / 15
A complete GUI for a Graph-Database

DEMO

15 / 15

Contenu connexe

Tendances

d3jsではじめるデータビジュアライゼーション入門
d3jsではじめるデータビジュアライゼーション入門d3jsではじめるデータビジュアライゼーション入門
d3jsではじめるデータビジュアライゼーション入門
Kohei Kadowaki
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
Kevin Hoyt
 
Human-powered Javascript Compression for Fun and Gummy Bears
Human-powered Javascript Compression for Fun and Gummy BearsHuman-powered Javascript Compression for Fun and Gummy Bears
Human-powered Javascript Compression for Fun and Gummy Bears
Rui Lopes
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentation
sharp-blade
 

Tendances (20)

Scrollytelling
ScrollytellingScrollytelling
Scrollytelling
 
d3jsではじめるデータビジュアライゼーション入門
d3jsではじめるデータビジュアライゼーション入門d3jsではじめるデータビジュアライゼーション入門
d3jsではじめるデータビジュアライゼーション入門
 
D3.js - A picture is worth a thousand words
D3.js - A picture is worth a thousand wordsD3.js - A picture is worth a thousand words
D3.js - A picture is worth a thousand words
 
HTML5 Canvas
HTML5 CanvasHTML5 Canvas
HTML5 Canvas
 
Supstat nyc subway
Supstat nyc subwaySupstat nyc subway
Supstat nyc subway
 
Зависимые типы в GHC 8. Максим Талдыкин
Зависимые типы в GHC 8. Максим ТалдыкинЗависимые типы в GHC 8. Максим Талдыкин
Зависимые типы в GHC 8. Максим Талдыкин
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
Human-powered Javascript Compression for Fun and Gummy Bears
Human-powered Javascript Compression for Fun and Gummy BearsHuman-powered Javascript Compression for Fun and Gummy Bears
Human-powered Javascript Compression for Fun and Gummy Bears
 
Cocos2dを使ったゲーム作成の事例
Cocos2dを使ったゲーム作成の事例Cocos2dを使ったゲーム作成の事例
Cocos2dを使ったゲーム作成の事例
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 
Real life XNA
Real life XNAReal life XNA
Real life XNA
 
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.
 
My favorite slides
My favorite slidesMy favorite slides
My favorite slides
 
Scala.io
Scala.ioScala.io
Scala.io
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentation
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
 
Introduction to D3
Introduction to D3 Introduction to D3
Introduction to D3
 
Introduction to programming class 13
Introduction to programming   class 13Introduction to programming   class 13
Introduction to programming class 13
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 

Similaire à Having fun with graphs, a short introduction to D3.js

Similaire à Having fun with graphs, a short introduction to D3.js (20)

Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)
 
Visualization of Big Data in Web Apps
Visualization of Big Data in Web AppsVisualization of Big Data in Web Apps
Visualization of Big Data in Web Apps
 
D3 svg & angular
D3 svg & angularD3 svg & angular
D3 svg & angular
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tdd
 
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
SenchaCon 2016: Add Magic to Your Ext JS Apps with D3 Visualizations - Vitaly...
 
D3.js 30-minute intro
D3.js   30-minute introD3.js   30-minute intro
D3.js 30-minute intro
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
 
Visual Exploration of Large Data sets with D3, crossfilter and dc.js
Visual Exploration of Large Data sets with D3, crossfilter and dc.jsVisual Exploration of Large Data sets with D3, crossfilter and dc.js
Visual Exploration of Large Data sets with D3, crossfilter and dc.js
 
Dynamic Data Visualization With Chartkick
Dynamic Data Visualization With ChartkickDynamic Data Visualization With Chartkick
Dynamic Data Visualization With Chartkick
 
The Web map stack on Django
The Web map stack on DjangoThe Web map stack on Django
The Web map stack on Django
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
D3 data visualization
D3 data visualizationD3 data visualization
D3 data visualization
 
Micro and moblile: Java on the Raspberry Pi
Micro and moblile: Java on the Raspberry PiMicro and moblile: Java on the Raspberry Pi
Micro and moblile: Java on the Raspberry Pi
 
JQuery Flot
JQuery FlotJQuery Flot
JQuery Flot
 
Learn D3.js in 90 minutes
Learn D3.js in 90 minutesLearn D3.js in 90 minutes
Learn D3.js in 90 minutes
 
SVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generationSVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generation
 
D3
D3D3
D3
 
SVGo workshop
SVGo workshopSVGo workshop
SVGo workshop
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

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

  • 1. Having fun with graphs An introduction to d3.js Michael Hackstein @mchacki August 13, 2013 1 / 15
  • 2. d3.js Short Description Take javascript objects or a JSON file Create SVGs out of them Synchronize objects and SVG API quite similar to jQuery ⇒ “The jQuery for SVG” 2 / 15
  • 6. 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
  • 7. 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 . attr ( " y1 " , function ( d ) { return . attr ( " x2 " , function ( d ) { return . attr ( " y2 " , function ( d ) { return { d . source . x ; d . source . y ; d . target . x ; 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
  • 8. Tree Layout Datastructure var treeData = { id : 1 , children : [ { id : 2} , { id : 3 , children : [ { id : 4} ,{ id : 5} ,{ id : 6} ] }, ] }; 6 / 15
  • 9. 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
  • 10. on lsi on pu re re pu l si ← → Force-Based Layout Idea ← → → attraction ← Figure : Forces in Force-Directed Layout Attention Runtime: O(n2 ) ⇒ Only small number of nodes 8 / 15
  • 11. 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
  • 12. Force-Based Layout Update Position force . on ( " tick " , function () svg . selectAll ( " . edge " ) . attr ( " x1 " , function ( d ) . attr ( " y1 " , function ( d ) . attr ( " x2 " , function ( d ) . attr ( " y2 " , function ( d ) { { return { return { return { return d . source . x ;}) d . source . y ;}) d . target . x ;}) d . target . y ;}) ; svg . selectAll ( " . node " ) . attr ( " cx " , function ( d ) { return d . x ; }) . attr ( " cy " , function ( d ) { return d . y ; }) ; }) ; 10 / 15
  • 13. 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
  • 14. Difficulties Often details are important Show everything always is not useful Many nodes Layout-algorithms get slow Not enough pixels for nodes 12 / 15
  • 15. Fisheye-Distortion Figure : Fish-Eye Distortion Mouse-pointer is the focus Magnifies close objects Minifies far away objects 13 / 15
  • 16. Group Nodes to smaller parts Figure : Group similar nodes Group nodes (f.e. by similar attributes) Layout each group separately Treat each group as one large node Then layout group-nodes 14 / 15
  • 17. A complete GUI for a Graph-Database DEMO 15 / 15