SlideShare a Scribd company logo
1
React with D3:
DOM Manipulation Orchestration
Elden S. Park
Nov 2018
Index
• What	is	SVG and	why	does	it	matter
• Why	D3
• Challenges	in	dealing	with	two	DOM	controlling	libraries
• Ways	to	make	React	and	D3	interoperate
• Data	passing	inbound/outbound
• Synthetic	Events
• Spy	method to	capture	child	data
• Birth	of	Dechart.js
• Retrospective
2
SVG
3
SVG, Scalable Vector Graphics
SVG
XML-based vector image	format for two-dimensional graphics
Supported	by	most	web	browsers
4
SVG, Scalable Vector Graphics
SVG (2)
https://picsvg.com/example/bike-1.svg
5
One	of	the	most	popular	tools	to	create	visual	objects in	web
with	its	declarative nature
Comparisons with Other Drawing APIs
SVG (3)
• Drawn	as	element	in	DOM	just	like	plain	HTML	
• Easy to	manipulate,	event	handlers	can	be	attached	
• Slower than	WebGL-based	Canvas	
• Vector	graphics.	Scalable regardless	of	resolution,	unlike	Canvas	
(raster-based)	
6
Chart Libraries (with or without SVG)
SVG (4)
7
Chart.js Canvas, Easy-to-use,	Hard	to	extend
C3.js SVG, D3	Wrapper,	Abstracted
Recharts.js SVG,	D3-React	binding,	Exclusive	with	React
D3.js SVG,	DOM	low	level	manipulation,	Big	community
...and	more
D3
8
A Brief Introduction to D3.js
D3.js
• Data-DOM	Binder	(not	a	chart	library)
• Features	include,	though	not	limited	to,
• Selection
• Functional	Property	Bindings
• Transition	(animation)
• Various	use	cases:	Chart,	Map,	Diagrams
9
Examples of D3 Objects
D3.js (2)
10
Of	Multiple	Sources	Provided	in,	https://github.com/d3/d3/wiki/gallery
When Should You Consider Using D3?
D3.js (3)
• Visualizing	data	with	specific	features	(e.g.	business-related)
• Creating	charts	not	depending	on	specific	3rd	party	framework
(React,	Angular,	etc)
• Need	an	abundance	of	samples and	QA’s	
• You	are	already	familiar	with	DOM
11
Defining	D3	Objects	in	React	App
12
Challenges in Using Both D3 and React
D3-React
13
• React	handles	DOM	with	its	internal	Virtual	DOM representation.		
D3,	which	also	manipulates	DOM,	conflicts with	React	
• Performance	degrades if	a	great	number	of	SVG	elements	are	
rendered	in	React’s DOM	tree
Two	Suggestions
14
`React in Full Control` Way
D3-React Ways to Interoperate
15
`React in Full Control` Way
D3-React Ways to Interoperate (2)
16
`React in Full Control` Way
D3-React Ways to Interoperate (3)
17
React (DOM)
svg
path
...
D3 (Math)
Path	Calculation
Data
Event Handlers
`React, D3 Governing Each Region` Way
D3-React Ways to Interoperate (4)
18
`React, D3 Governing Each Region` Way
D3-React Ways to Interoperate (5)
19
React (DOM)
div
path
...
D3 (Math + DOM)
Event Handlers
svgAction Triggers
Two Ways to Interoperate D3 and React
D3-React Ways to Interoperate (6)
20
React	Full	Control React,	D3	governing	
each	region
- One-way (natural*) Data	Flow - Complicated
(two	control	towers)
- Maybe	worse Performance - Usual
- D3,	React	linked	tight
(hard	to	reuse	w/o	React)
Independence - D3	remains	agnostic
of	externals
Which Way to Choose, Why?
D3-React Ways to Interoperate (7)
21
`React,	D3	Governing	Each	Region`	can	be	more	appealing	
especially	because,
• It	allows	to	keep	the	pure	D3	code.
=>	It	is	hard	to	debug	the	combined	logic	of	D3	and	React.	
=>	D3	code	can	be	used	easy	without	React.
Now	the	Way	Is	Chosen,	
Let’s	Add	an	Abstraction	to	It
22
D3 Object Encapsulation
D3 Abstraction
23
D3 Object Encapsulation
D3 Abstraction (2)
24
D3 Object Encapsulation
D3 Abstraction (3)
25
D3	abstraction,	hereafter	is	to	be	called	`D3chart`
How	Does	Our	D3chart	Look	Like
at	This	Point?
26
D3 Multi-Line Graph
27
Inward	Data	Passing
From	React	to	D3
28
React May Pass Data Such as Functions
React to D3, Inward Data Traffic
29
Callbacks, for	example,	to	listen	to	events	in	D3
(e:	Event)	=>	{	/*...behavior...*/	}
+	We	can	also	make	use	of	D3’s	own	event	
handling	utilities
React May Pass Data Such as Functions
React to D3, Inward Data Traffic (2)
30
React May Pass Data Such as Functions
React to D3, Inward Data Traffic (3)
31
Too	Many	Handlers	Polluting	D3	Region
32
`handle-()` Methods and Corresponding Calls
React to D3, Inward Data Traffic (4)
33
Various	event	handlers	(of	N	types)	are	passed	in	to	D3chart.
This	leads	D3	to	call	dedicated	handlers for	different	actions.
f1	(e1:	Event)	=>	void
f2	(e2:	Event)	=>	void
fn ...
When
condition1:	f1()
condition2:	f2()
...
Synthetic	Events	to	Rescue
34
Generating Custom Events
React to D3, Inward Data Traffic
35
D3chart	generates	events,	which	React	Components	subscribe	to	listen	
to.	Event	handlers	(call	+	definition)	reside	outside	of	D3chart.
*Synthetic	Event	in	this	context	refers	to	custom	object	D3chart	defines,	
nothing	to	do	with	React’s own	Synthetic	Event.
Generating Custom Events
React to D3, Inward Data Traffic (2)
36
Generating Custom Events
React to D3, Inward Data Traffic (3)
37
React	Components	React	to
Events	on	Its	Own	Land
38
React’s Response to Synthetic Events
React to D3, Inward Data Traffic (4)
39
Adding	Tooltip	to	Our	D3chart
40
React’s Response to Synthetic Events
React to D3, Inward Data Traffic (5)
41
React’s Response to Synthetic Events
React to D3, Inward Data Traffic (6)
42
React’s Response to Synthetic Events
React to D3, Inward Data Traffic (7)
43
The	beauty	of	generating	Synthetic	Events	is	its	keeping	
D3	logic	free	of	any	literals passed	from	external	libraries
Inbound Traffic of Data
React to D3, Inward Data Traffic (8)
44
{	Data,	(Event	Handlers)	}	flow	into	D3	object	from	React	Components.
React (DOM)
div
path
...
D3 (Math + DOM)
Event Handlers
svgData Synthetic Event
How	about	the	Vice-Versa?
45
blackbox
D3 Listening to Events in React
D3, Outward Data Traffic
46
How	can D3	listen	to	events	on	React’s Components	
when	React	does	not	even	know	it	has	a	child?
div
div
svg
path
click
Exposing Action Trigger
D3, Outward Data Traffic (2)
47
Spy Method to Capture Child Data
D3, Outward Data Traffic (3)
48
Spy Method to Capture Child Data
D3, Outward Data Traffic (4)
49
Wait,	Isn’t	It	a	Contradiction	to	
React’s Unidirectional	Data	Flow?
50
Pseudo Two-Way Data Binding
D3-React Binding
51
div
div
svg
path
dispatch()
dispatch()
Event
Handlers
Data
Synthetic
Event
click
Birth	of	Dechart.js
52
Creating a Library of Our Own
Dechart.js
53
Reusable	abstraction	(D3chart)	became	a	library.
- Configurable
- Built-in	chart	type	(Line,	Bar,	etc)
- External-lib	free	D3	wrapper
Dechart.js https://github.com/dechartjs/dechart
Dechart-react.js https://github.com/dechartjs/dechart-react
*Migration	(mirroring)	to	Github in	progress	(Nov	18’)
Other	Considerations	When	
Embedding	D3	into	Web	Application
54
Modularized Render Functions
Dechart.js (2)
55
Chart	is	composed	of	several	elements	so	can	be	render	functions.
It	is	easier	when	D3	cares	nothing	about	other	view	logic	(e.g.	React)
Pure JavaScript Style Injection
Dechart.js (3)
56
We	are	minimizing	the	impact	of	third-party	factors,	style	is	no	exception.
Sample Diagrams
Dechart.js (4)
57
Wrapping Up
Retrospective
58
• Both	React	and	D3	controls	DOM,	but	they	can	operate	together
• If	possible,	define	D3	and	React	separated
• Unidirectional	Data	Flow is	not	powerful	enough	for	all	situations
• Static	typing needs	to	be	considered	building	an	app	(lib)	of	scale
Code Lab
59
Let’s	discover	what	we	discussed	in	hands-on	coding
For	those	who	see	these	slides	afterwards,
Checkout	https://github.com/dechartjs/dechart-react
Thank you
End of Slide
60
Gamsahabnida
Elden	S.	Park
eldeni.github.io

More Related Content

PDF
Giovanni Lanzani – SQL & NoSQL databases for data driven applications - NoSQL...
PDF
Siddhi - cloud-native stream processor
PPTX
Relational cloud, A Database-as-a-Service for the Cloud
PDF
The Rise of Streaming SQL
PDF
WSO2 Stream Processor: Graphical Editor, HTTP & Message Trace Analytics and m...
PDF
PDF
A head start on cloud native event driven applications - bigdatadays
PPTX
Extended Events 101 : Japan SQL Server User Group
Giovanni Lanzani – SQL & NoSQL databases for data driven applications - NoSQL...
Siddhi - cloud-native stream processor
Relational cloud, A Database-as-a-Service for the Cloud
The Rise of Streaming SQL
WSO2 Stream Processor: Graphical Editor, HTTP & Message Trace Analytics and m...
A head start on cloud native event driven applications - bigdatadays
Extended Events 101 : Japan SQL Server User Group

Similar to React with D3: DOM Manipulation Orchestration (20)

PDF
React, D3 and the dataviz ecosystem
PDF
React and d3
PDF
React with D3 - who’s in control?
PPTX
Dreamforce 2014 - Introduction to d3.js
PDF
D3 js in Action Data visualization with JavaScript Second Edition Elijah Meeks
PDF
Design engineering with d3+react with-speaker-notes
PPTX
Data visualisation with D3
PPTX
Dazzing Data Depiction with D3.JS
PDF
D3.js in Action: Data visualization with JavaScript 2nd Edition Elijah Meeks
PDF
Introduction to data visualisation with D3
PPTX
Visdjango presentation django_boston_oct_2014
PDF
Learn D3.js in 90 minutes
PDF
React + Redux + d3.js
PPTX
Data Visualization with D3
PPTX
Introduction to D3.js
PPTX
The simplest guide to d3
PPTX
Academy PRO: D3, part 1
PPTX
SVGD3Angular2React
PDF
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
PPTX
D3 : Data driven documents with Data visualization principles .
React, D3 and the dataviz ecosystem
React and d3
React with D3 - who’s in control?
Dreamforce 2014 - Introduction to d3.js
D3 js in Action Data visualization with JavaScript Second Edition Elijah Meeks
Design engineering with d3+react with-speaker-notes
Data visualisation with D3
Dazzing Data Depiction with D3.JS
D3.js in Action: Data visualization with JavaScript 2nd Edition Elijah Meeks
Introduction to data visualisation with D3
Visdjango presentation django_boston_oct_2014
Learn D3.js in 90 minutes
React + Redux + d3.js
Data Visualization with D3
Introduction to D3.js
The simplest guide to d3
Academy PRO: D3, part 1
SVGD3Angular2React
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
D3 : Data driven documents with Data visualization principles .
Ad

Recently uploaded (20)

PPTX
assetexplorer- product-overview - presentation
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Nekopoi APK 2025 free lastest update
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Understanding Forklifts - TECH EHS Solution
PPT
Introduction Database Management System for Course Database
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
assetexplorer- product-overview - presentation
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Odoo Companies in India – Driving Business Transformation.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Nekopoi APK 2025 free lastest update
wealthsignaloriginal-com-DS-text-... (1).pdf
L1 - Introduction to python Backend.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
history of c programming in notes for students .pptx
Design an Analysis of Algorithms I-SECS-1021-03
Understanding Forklifts - TECH EHS Solution
Introduction Database Management System for Course Database
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Ad

React with D3: DOM Manipulation Orchestration