SlideShare a Scribd company logo
1 of 14
Data Driven Documents
Document Object Model
The Document Object Model (DOM) is a cross-
platform and language-independent application
programming interface that treats an HTML,
XHTML, or XML document as a tree structure
wherein each node is an object representing a part
of the document.
Wikipedia
Scalable Vector Graphics
<svg width="720" height="120">
<circle cx="40" cy="60" r="10"></circle>
<circle cx="80" cy="60" r="10"></circle>
<circle cx="120" cy="60" r="10"></circle>
</svg>
Data join
radii = [5,
10,
15]
<svg width="720" height="120">
⟶ <circle cx="40" cy="60" r="10"></circle>
⟶ <circle cx="80" cy="60" r="10"></circle>
⟶ <circle cx="120" cy="60" r="10"></circle>
</svg>
Data join
Here’s the deal. Instead of telling D3 how to do
something, tell D3 what you want. You want the
circle elements to correspond to data. You want
one circle per datum. Instead of instructing D3 to
create circles, then, tell D3 that the selection
"circle" should correspond to data. This concept
is called the data join.
Mike Bostock
svg.selectAll("circle")
.data(radii)
.enter().append("circle")
.attr(“cx", 60)
.attr("cy", function(d, i) { return return i * 100 + 30; })
.attr(“r”, function(d, i) { return Math.sqrt(d)); })
Force-Directed Graph
Velocity Verlet
D3 uses a Velocity Verlet numerical
integrator for simulating physical forces on
particles:
Constant unit time step: Δt = 1
Constant unit mass: m = 1
Constant acceleration: a
Force F = ma
Velocity Vn = Vn-1 + aΔt
Position Pn = Pn-1 + VΔt
Forces types
• Many-Body
• Centering
• Collision
• Links
• Positioning
D3 Force simulation
Node properties:
• index - the node’s zero-based index into nodes
• x, y - the node’s current x- and y-positions
• vx, vy - the node’s current x- and y- velocities
velocityDecay - “friction” that slows down a node
alpha - “cooling parameter” reduces the effect of forces
tick - iteration for calculation new position, increments alpha by
alphaDecay and decrements velocity by velocityDecay
Minimal Working Examples
• Force-Directed Graph by Mike Bostock -
https://bl.ocks.org/mbostock/4062045
• D3-Force Testing Playground by Steve Haroz -
https://bl.ocks.org/steveharoz/8c3e2524079a8c440df60c1
ab72b5d03
Closure = function + outer content
Closure is a record storing a function together with an
environment: a mapping associating each free variable of the
function (variables that are used locally, but defined in an
enclosing scope) with the value or reference to which the
name was bound when the closure was created.
JavaScript Closures
function draw_network(json, svg){
var nodes_data = json[“nodes"];
...
function splitting_force() {
for (var i = 0, n = nodes_data.length; i < n; i++) {
node = nodes_data[i];
if(node.group == "tf"){
node.y += 10;
} else if(node.group == "kinase"){
node.y -= 10;
}
}
}
...
}
JavaScript Closures
function draw_network(json, svg){
var nodes_data = json[“nodes"];
...
function splitting_force() {
for (var i = 0, n = nodes_data.length; i < n; i++) {
(function(i){
node = nodes_data[i];
if(node.group == "tf"){
node.y += 10;
} else if(node.group == "kinase"){
node.y -= 10;
}
})(i);
}
}
...
}

More Related Content

What's hot

Get started with Microsoft SQL Polybase
Get started with Microsoft SQL PolybaseGet started with Microsoft SQL Polybase
Get started with Microsoft SQL Polybase
Henk van der Valk
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
Wojciech Sznapka
 
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
Neo4j
 

What's hot (20)

NoSQL in SQL - Lior Altarescu
NoSQL in SQL - Lior Altarescu NoSQL in SQL - Lior Altarescu
NoSQL in SQL - Lior Altarescu
 
Couchbase Data Platform | Big Data Demystified
Couchbase Data Platform | Big Data DemystifiedCouchbase Data Platform | Big Data Demystified
Couchbase Data Platform | Big Data Demystified
 
Get started with Microsoft SQL Polybase
Get started with Microsoft SQL PolybaseGet started with Microsoft SQL Polybase
Get started with Microsoft SQL Polybase
 
Cassandra Data Maintenance with Spark
Cassandra Data Maintenance with SparkCassandra Data Maintenance with Spark
Cassandra Data Maintenance with Spark
 
D3.js 30-minute intro
D3.js   30-minute introD3.js   30-minute intro
D3.js 30-minute intro
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
Azure Table Storage: The Good, the Bad, the Ugly (15 min. lightning talk)
Azure Table Storage: The Good, the Bad, the Ugly (15 min. lightning talk)Azure Table Storage: The Good, the Bad, the Ugly (15 min. lightning talk)
Azure Table Storage: The Good, the Bad, the Ugly (15 min. lightning talk)
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop Connector
 
To SQL or NoSQL, That is the Question
To SQL or NoSQL, That is the QuestionTo SQL or NoSQL, That is the Question
To SQL or NoSQL, That is the Question
 
Cubes – pluggable model explained
Cubes – pluggable model explainedCubes – pluggable model explained
Cubes – pluggable model explained
 
Elastic Search
Elastic SearchElastic Search
Elastic Search
 
Level ofdetail
Level ofdetailLevel ofdetail
Level ofdetail
 
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
GraphConnect Europe 2016 - Building Spring Data Neo4j 4.1 Applications Like A...
 
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
 
Munich March 2015 - Cassandra + Spark Overview
Munich March 2015 -  Cassandra + Spark OverviewMunich March 2015 -  Cassandra + Spark Overview
Munich March 2015 - Cassandra + Spark Overview
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
 
RedisConf18 - Redis and Elasticsearch
RedisConf18 - Redis and ElasticsearchRedisConf18 - Redis and Elasticsearch
RedisConf18 - Redis and Elasticsearch
 
Yes, Sql!
Yes, Sql!Yes, Sql!
Yes, Sql!
 
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
 
Starting with MongoDB
Starting with MongoDBStarting with MongoDB
Starting with MongoDB
 

Similar to D3 Force-Directed Graphs

Similar to D3 Force-Directed Graphs (20)

D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
D3 data visualization
D3 data visualizationD3 data visualization
D3 data visualization
 
OSCON 2011 CouchApps
OSCON 2011 CouchAppsOSCON 2011 CouchApps
OSCON 2011 CouchApps
 
D3.js and SVG
D3.js and SVGD3.js and SVG
D3.js and SVG
 
Thats Not Flash?
Thats Not Flash?Thats Not Flash?
Thats Not Flash?
 
Couchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemCouchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problem
 
Data science at the command line
Data science at the command lineData science at the command line
Data science at the command line
 
D3.js
D3.jsD3.js
D3.js
 
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
 
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)
 
Introduction to D3
Introduction to D3 Introduction to D3
Introduction to D3
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
Couchbas for dummies
Couchbas for dummiesCouchbas for dummies
Couchbas for dummies
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
 
Applied Machine learning using H2O, python and R Workshop
Applied Machine learning using H2O, python and R WorkshopApplied Machine learning using H2O, python and R Workshop
Applied Machine learning using H2O, python and R Workshop
 
Introduction to data visualisation with D3
Introduction to data visualisation with D3Introduction to data visualisation with D3
Introduction to data visualisation with D3
 
3 Dundee-Spark Overview for C* developers
3 Dundee-Spark Overview for C* developers3 Dundee-Spark Overview for C* developers
3 Dundee-Spark Overview for C* developers
 
HTML5 - A Whirlwind tour
HTML5 - A Whirlwind tourHTML5 - A Whirlwind tour
HTML5 - A Whirlwind tour
 
Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)
 

Recently uploaded

Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
vexqp
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
ptikerjasaptiker
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
wsppdmt
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
gajnagarg
 

Recently uploaded (20)

Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATIONCapstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
 
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangePredicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 

D3 Force-Directed Graphs

  • 2.
  • 3. Document Object Model The Document Object Model (DOM) is a cross- platform and language-independent application programming interface that treats an HTML, XHTML, or XML document as a tree structure wherein each node is an object representing a part of the document. Wikipedia
  • 4. Scalable Vector Graphics <svg width="720" height="120"> <circle cx="40" cy="60" r="10"></circle> <circle cx="80" cy="60" r="10"></circle> <circle cx="120" cy="60" r="10"></circle> </svg>
  • 5. Data join radii = [5, 10, 15] <svg width="720" height="120"> ⟶ <circle cx="40" cy="60" r="10"></circle> ⟶ <circle cx="80" cy="60" r="10"></circle> ⟶ <circle cx="120" cy="60" r="10"></circle> </svg>
  • 6. Data join Here’s the deal. Instead of telling D3 how to do something, tell D3 what you want. You want the circle elements to correspond to data. You want one circle per datum. Instead of instructing D3 to create circles, then, tell D3 that the selection "circle" should correspond to data. This concept is called the data join. Mike Bostock svg.selectAll("circle") .data(radii) .enter().append("circle") .attr(“cx", 60) .attr("cy", function(d, i) { return return i * 100 + 30; }) .attr(“r”, function(d, i) { return Math.sqrt(d)); })
  • 8. Velocity Verlet D3 uses a Velocity Verlet numerical integrator for simulating physical forces on particles: Constant unit time step: Δt = 1 Constant unit mass: m = 1 Constant acceleration: a Force F = ma Velocity Vn = Vn-1 + aΔt Position Pn = Pn-1 + VΔt
  • 9. Forces types • Many-Body • Centering • Collision • Links • Positioning
  • 10. D3 Force simulation Node properties: • index - the node’s zero-based index into nodes • x, y - the node’s current x- and y-positions • vx, vy - the node’s current x- and y- velocities velocityDecay - “friction” that slows down a node alpha - “cooling parameter” reduces the effect of forces tick - iteration for calculation new position, increments alpha by alphaDecay and decrements velocity by velocityDecay
  • 11. Minimal Working Examples • Force-Directed Graph by Mike Bostock - https://bl.ocks.org/mbostock/4062045 • D3-Force Testing Playground by Steve Haroz - https://bl.ocks.org/steveharoz/8c3e2524079a8c440df60c1 ab72b5d03
  • 12. Closure = function + outer content Closure is a record storing a function together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created.
  • 13. JavaScript Closures function draw_network(json, svg){ var nodes_data = json[“nodes"]; ... function splitting_force() { for (var i = 0, n = nodes_data.length; i < n; i++) { node = nodes_data[i]; if(node.group == "tf"){ node.y += 10; } else if(node.group == "kinase"){ node.y -= 10; } } } ... }
  • 14. JavaScript Closures function draw_network(json, svg){ var nodes_data = json[“nodes"]; ... function splitting_force() { for (var i = 0, n = nodes_data.length; i < n; i++) { (function(i){ node = nodes_data[i]; if(node.group == "tf"){ node.y += 10; } else if(node.group == "kinase"){ node.y -= 10; } })(i); } } ... }

Editor's Notes

  1. Today I'm gonna tell you about D3. I'll give you superficial introduction, explain the cornerstone concept and show a few examples including force-directed graph layout. D3 stands for Data Driven Documents and it sounds a little bit misleading because it's a JavaScript library used for producing dynamic, interactive, online data visualizations.
  2. Mike Bostock started developing data visualization library called Protovis at 2009 when he was a PhD student at Stanford. In those days there were only a few libraries for interactive online data visualization libraries, although demand was high. At 2011 Protovis team finished developing it and started developing D3 which inherited And at 2015 Mike Bostock left New York Times and full-time on D3. The last important thing in D3 timeline is release of d3.v4 year ago. I recommend to use this version as it is more convenient and has better performance.
  3. D3 operates on DOM elements. Every element of webpage is a DOM element and DOM considers every element as a node of a tree. In case of D3 we're talking about SVG - Scalable Vector Graphics.
  4. It's an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. For example, this code will generate three circles with a radius of 10 pixels. (0) 'circle' specifies the shape 'cx' and 'cy' specifies x and y positions on canvas, and 'r' is a radius of a circle. Using D3 we can modify those circles. Let's change their radius. (1) This is very cute but what if we had four numbers to display, rather than three? We wouldn’t have enough circles, and we would need to create more elements to represent our data. We can append new nodes manually, but a more powerful way is the enter selection computed by a data join.
  5. Data join is cornerstone concept of D3. It's somewhat similar to SQL joins. In SQL you have two tables and when you join then, you merge elements that have same ids. In D3 on one side you have data. On the other side you have SVG elements. So when we do this in code, we append our data to SVG elements. One datum per element and datum and element become an object. (1 again) How does D3 understand which datum it should relate to which element? D3 has 'key function' which you can rewrite if you wish. By default as you data is array and your elements are nodes of DOM tree, it takes first datum, first node and creates first element. And so on.
  6. If we go back to hypothetical situation with four data points and three objects, we'll append three data points to three elements and one left element will be hanging somewhere there. In D3 this 'somewhere there' is called 'enter' selection. Whenever code is run, it recomputes the data join and maintains the correspondence between elements and data. If the new dataset is smaller than the old one, the surplus elements end up in the exit selection and get removed. If the new dataset is larger, the surplus data ends up in the enter selection and new nodes are added. If the new dataset is exactly the same size, then all the elements are simply updated with new positions, and no elements are added or removed. Questions?
  7. Force layouts are visually interesting. They are expanding and collapsing, you can drag them around. It’s fun and engaging. Also implementation is interesting on a back-end (in terms of underlying principles) and easy (in terms of code). And as we do network analysis in the lab this is useful as it is the most obvious way to visualize networks.
  8. And force is one of the places where performance really matters in terms of doing stuff in the browser. With naive approach and N nodes it will be N-squared calculations. So D3 uses technique that comes from astrophysical simulation of n-body problem where they use quad-tree that allows you to do N*logN calculations instead N-squared.
  9. D3 implements three primary forces upon the nodes at every iteration: The sum of the forces acting on each node by all other nodes. Many-Body simulates gravity (attraction) or electrostatic charge (repulsion). Centering force translates nodes uniformly so that the the center of mass is at the given position. Collision treats nodes as circus with certain radius. The force pushing and pulling between two linked nodes The force pulling each node to a focal point, usually the center of the user-defined space
  10. At each tick, the nodes data array is directly manipulated with the calculated x- and y-positions. D3 triggers a “tick” event at each of these iterations, and an “end” event when the simulation ends. On top of the three primary forces, there are two more concepts that affect the placement of the nodes at each tick: “friction”, that slows down the rate at which the node travels from its original position to its newly calculated position, and alpha, or the “cooling parameter”, that decrements at each tick and reduces the effect each of the forces play on the position of the nodes. These concepts exist because, if we were to position the nodes based on the above three forces at each tick and render them, the nodes would fly everywhere. To prevent this from happening, friction slows the nodes down at each tick, and alpha slows them down between each tick. After a certain threshold is reached for alpha, the force layout stops calculating, freezing the graph into what is hopefully an optimal layout.
  11. Let's say you wrote your beautiful visualization and now you want to integrate it into a webpage. So you wrap your code into one main function. User inputs data, presses a button, data is sent to your function and the function draws a graph. If you have functions in your main function, and if those functions contain loops, at this point you'll be disappointed and confused because your function isn't gonna work properly. The problem happened when you wrapped your code into the main function. Because it created closures. Closure is a function than contains references to variables declared outside the body of this function and not being its parameters. A closure does not merely pass the value of a variable or even a reference to the variable. A closure captures the variable itself!
  12. So when you write something like this which is totally normal, instead of doing what you expect, JavaScript will share variable i across functions PLUS the current function/scope/context. Think of it as a sort of private global variable that only the functions involved can see. What we want is an instance of that variable or at least a simple reference to the variable instead of the variable itself. Fortunately JavaScript already has a mechanism for passing a reference (for objects) or value (for strings and numbers): function arguments!