SlideShare a Scribd company logo
1 of 42
Download to read offline
CHARTS FROM SCRATCH IN
REACT
CHRISTINA HOLLAND
ABOUT ME
CHRISTINA HSU HOLLAND
▸ BS in Molecular and Cell Bio
▸ Animator for 10 years
▸ Bootcamp (Hack Reactor)
▸ Front-end Engineer at Pepperdata
INTRO
TODAY’S TALK PLAN
▸ Why?
▸ Overview of browser drawing APIs (with examples).
▸ Demos with code.
INTRO
WHY BUILD CHARTS FROM SCRATCH?
▸ What are we, animals?
▸ We don’t build our own cars!
▸ Okay but seriously
INTRO
THE CHART USE CASE SPECTRUM
custom standard
Roll Your OwnLow Level Libraries High Level Libraries
INTRO
THE CHART USE CASE SPECTRUM
custom standard
Roll Your Own
Low Level
Libraries
High Level Libraries
INTRO
Stephanie Yee & Tony Chu http://www.r2d3.us/
MIND-BLOWING SUPER BESPOKE CUSTOM CHART (D3)
INTRO
THE CHART USE CASE SPECTRUM
custom standard
Roll Your OwnLow Level Libraries
High Level
Libraries
INTRO
GOOD OLD LINE AND BAR CHARTS AND ALL THAT
HighchartsVictory NVD3
C3
INTRO
THE CHART USE CASE SPECTRUM
custom standard
Roll Your Own
Low Level Libraries High Level Libraries
INTRO
WHEN TO ROLL YOUR OWN CHARTS
▸ Concept not standard enough for a high-level library
▸ Don’t need power of D3 (animation, math helpers)
INTRO
EXAMPLE: ROLL YOUR OWN SVG CHART
▸ Blue = running tasks, purple = tasks waiting to run
▸ “Tasks now” is not a standard chart library format but also
isn’t very complex to build manually.
SVG (MANUAL)SVG (D3)
INTRO
WHEN TO ROLL YOUR OWN CHARTS (CONT.)
▸ Add functionality on the fly.
▸ Especially for interacting with other parts of the page.
▸ To minimize dependencies (therefore bundle size &
maintenance worries)
▸ Learn some web graphics fundamentals!
HOW
HOW TO ROLL YOUR OWN CHARTS
▸ Get to know the browser drawing APIs
▸ (HTML, SVG, Canvas, WebGL)
▸ Think about what your chart needs
▸ Interactivity? Large volume of data? Wacky shapes?
Groundbreaking 3D special effects?
▸ Choose the browser drawing API that will truly make your
chart happy.
API OPTIONS
BROWSER DRAWING APIS
regular html
SVG
canvas
WebGL
(divs and such)
(vector-based DOM elements)
(raster-based)
(super-low-level)
declarative
imperative
hello
HTML
BASIC HTML ELEMENTS
Mostly divs
HTML
HTML CHART EXAMPLE 1
▸ Great when your chart is just rectangles.
▸ Tip: X/Y axes are annoying as hell, you may want to use a
library instead of reinventing the wheel badly (as I did here).
HTML
BASIC HTML ELEMENTS: STRENGTHS & WEAKNESSES
▸ First-class support in React!
▸ Interactivity and accessibility are much simpler.
▸ HTML was born to do text.
▸ You can only draw rectangles (maybe circles and triangles
if you’re creative)!
▸ Thousands of DOM elements will choke performance.
HTML
BASIC HTML TIPS
▸ Use <div>s as parent element, to group, to draw.
▸ Use % widths for easy scaling.
▸ Good for bar charts, tables, heat maps, histograms.
▸ Could do scatter plots and bubble charts.
HTML
HTML CHART EXAMPLE 2 (BARS)
DIVS
HTML
HTML CHART EXAMPLE 3 (TIMELINE OF SPARK STAGES)
DIVS
SVG
SVG (SCALABLE VECTOR GRAPHICS)
A bunch of shapes that are DOM elements
SVG
SVG (SCALABLE VECTOR GRAPHICS)
https://www.w3schools.com/graphics/svg_examples.asp
SVG
SVG: STRENGTHS & WEAKNESSES
▸ Each item is a DOM element, can be recognized by React
and tracked to optimize updates.
▸ Selection and mouse events are easy. Accessibility is
doable, but may take some extra effort.
▸ Text is more bothersome than with divs (positioning),

less bothersome than with canvas.
▸ They are DOM elements, so large numbers = performance
issues.
SVG
BASIC SVG TIPS
▸ Enclosing chart container is <svg>.
▸ Declare shapes with <rect>, <circle>, <path>, etc.
▸ For complex shapes, <path> will do a lot of the heavy
lifting. Most of the complexity goes into formatting a
string for <path>’s “d” attribute.
▸ Coordinates start at top left! Y=0 is the top of the box.
This is the opposite of most charts!
SVG
SVG CHART EXAMPLE
▸ Some ideas: Complex shapes you
fill pieces of using a clip path.
▸ Simple arithmetic, doesn’t really
need D3.
▸ Easy to draw shapes in any vector
program and copy them in.
▸ Demo: Rocket
SVG
SVG
5 minutes

to render
CANVAS
CANVAS
A JavaScript-driven Microsoft Paint window
~10 seconds

to render
CANVAS
CANVAS
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API#Example
CANVAS
CANVAS: STRENGTHS & WEAKNESSES
▸ You can draw as many “items” as you like - they are not
stored as individual objects in memory but just pixels.
▸ Canvas has no awareness of different “elements” after
they’re drawn, which makes interaction tricky.
▸ Canvas draw commands are imperative, nothing inside a
<canvas> is recognized by React. You need to manually
track its state and handle when to render/update.
▸ Text is just pixels, so no font optimization.
CANVAS
BASIC CANVAS TIPS
▸ (Coordinates also start at top left!)
▸ No XML tags! So no JSX.
▸ Use lifecycles to run canvas draw commands. Can use
components to totally abstract it so anything consuming
the component can treat it like a regular HTML/SVG
component.
▸ Or if you want to go whole hog, create a custom renderer!
CANVAS
DEMO: CANVAS MINIMAP
CANVAS
▸ How do I know when the mouse is clicking on the
selection window?
▸ Mouse event triggers anywhere on canvas element
▸ Have to do some manual math.
DEMO: CANVAS MINIMAP
WEBGL
WEBGL
Web GOOD LUCK (Web Graphics Library) - low-level power
PhiloGL
LIBRARY
Luz Caballero
AUTHORS
Nicolas Garcia Belmonte
WEBGL
WEBGL: STRENGTHS & WEAKNESSES
▸ You can pretty much draw anything you can imagine, 2D
or 3D.
▸ Best performance.
▸ Same drawbacks as canvas.
▸ It can be very challenging to work with. Even its
abstractions (Three.js) tend to be relatively low level.
▸ Overkill for most common cases.
DEMO
BARS DEMO & CODE: HTML VS SVG VS CANVAS
DEMO
BARS DEMO & CODE: HTML VS SVG VS CANVAS
▸ This top level component doesn’t need to change whether
Chart or Bar are using SVG or Canvas or what.
DEMO
BARS DEMO & CODE: HTML VS SVG VS CANVAS
HTML SVG Canvas
<Bar> render methods
DEMO
PATHS DEMO & CODE: SVG VS CANVAS
DEMO
SVG VS CANVAS PATHS
▸ Series of (x, y) coords: [(1, 2), (2, 3), (3, 1)]
▸ HTML: Don’t
▸ SVG: <path d=“M1,2L2,3L3,1” />
▸ Canvas:
ctx.beginPath()
ctx.moveTo(1, 2)
ctx.lineTo(2, 3)
ctx.lineTo(3, 1)
ctx.stroke()
CHART DRAWING APPROACHES
CHART DRAWING APPROACHES BY API
HTML SVG Canvas WebGL
Root <div> <svg> <canvas> <canvas>
Items <div>
<rect>,
<circle>,
<path>, etc
track in state,
draw with
commands
track in state,
draw with
commands
Great at rectangles
interactive
shapes
many items perf, 3D
Working w/
React
plugs right
in, works w/
JSX
plugs right
in, works w/
JSX
lifecycle hooks,
or custom
renderer
lifecycle hooks,
or custom
renderer
CONCLUSION
THANK YOU
▸ Twitter: @americanwombat
▸ Email: hsubox@gmail.com

More Related Content

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Charts From Scratch In React

  • 1. CHARTS FROM SCRATCH IN REACT CHRISTINA HOLLAND
  • 2. ABOUT ME CHRISTINA HSU HOLLAND ▸ BS in Molecular and Cell Bio ▸ Animator for 10 years ▸ Bootcamp (Hack Reactor) ▸ Front-end Engineer at Pepperdata
  • 3. INTRO TODAY’S TALK PLAN ▸ Why? ▸ Overview of browser drawing APIs (with examples). ▸ Demos with code.
  • 4. INTRO WHY BUILD CHARTS FROM SCRATCH? ▸ What are we, animals? ▸ We don’t build our own cars! ▸ Okay but seriously
  • 5. INTRO THE CHART USE CASE SPECTRUM custom standard Roll Your OwnLow Level Libraries High Level Libraries
  • 6. INTRO THE CHART USE CASE SPECTRUM custom standard Roll Your Own Low Level Libraries High Level Libraries
  • 7. INTRO Stephanie Yee & Tony Chu http://www.r2d3.us/ MIND-BLOWING SUPER BESPOKE CUSTOM CHART (D3)
  • 8. INTRO THE CHART USE CASE SPECTRUM custom standard Roll Your OwnLow Level Libraries High Level Libraries
  • 9. INTRO GOOD OLD LINE AND BAR CHARTS AND ALL THAT HighchartsVictory NVD3 C3
  • 10. INTRO THE CHART USE CASE SPECTRUM custom standard Roll Your Own Low Level Libraries High Level Libraries
  • 11. INTRO WHEN TO ROLL YOUR OWN CHARTS ▸ Concept not standard enough for a high-level library ▸ Don’t need power of D3 (animation, math helpers)
  • 12. INTRO EXAMPLE: ROLL YOUR OWN SVG CHART ▸ Blue = running tasks, purple = tasks waiting to run ▸ “Tasks now” is not a standard chart library format but also isn’t very complex to build manually. SVG (MANUAL)SVG (D3)
  • 13. INTRO WHEN TO ROLL YOUR OWN CHARTS (CONT.) ▸ Add functionality on the fly. ▸ Especially for interacting with other parts of the page. ▸ To minimize dependencies (therefore bundle size & maintenance worries) ▸ Learn some web graphics fundamentals!
  • 14. HOW HOW TO ROLL YOUR OWN CHARTS ▸ Get to know the browser drawing APIs ▸ (HTML, SVG, Canvas, WebGL) ▸ Think about what your chart needs ▸ Interactivity? Large volume of data? Wacky shapes? Groundbreaking 3D special effects? ▸ Choose the browser drawing API that will truly make your chart happy.
  • 15. API OPTIONS BROWSER DRAWING APIS regular html SVG canvas WebGL (divs and such) (vector-based DOM elements) (raster-based) (super-low-level) declarative imperative hello
  • 17. HTML HTML CHART EXAMPLE 1 ▸ Great when your chart is just rectangles. ▸ Tip: X/Y axes are annoying as hell, you may want to use a library instead of reinventing the wheel badly (as I did here).
  • 18. HTML BASIC HTML ELEMENTS: STRENGTHS & WEAKNESSES ▸ First-class support in React! ▸ Interactivity and accessibility are much simpler. ▸ HTML was born to do text. ▸ You can only draw rectangles (maybe circles and triangles if you’re creative)! ▸ Thousands of DOM elements will choke performance.
  • 19. HTML BASIC HTML TIPS ▸ Use <div>s as parent element, to group, to draw. ▸ Use % widths for easy scaling. ▸ Good for bar charts, tables, heat maps, histograms. ▸ Could do scatter plots and bubble charts.
  • 20. HTML HTML CHART EXAMPLE 2 (BARS) DIVS
  • 21. HTML HTML CHART EXAMPLE 3 (TIMELINE OF SPARK STAGES) DIVS
  • 22. SVG SVG (SCALABLE VECTOR GRAPHICS) A bunch of shapes that are DOM elements
  • 23. SVG SVG (SCALABLE VECTOR GRAPHICS) https://www.w3schools.com/graphics/svg_examples.asp
  • 24. SVG SVG: STRENGTHS & WEAKNESSES ▸ Each item is a DOM element, can be recognized by React and tracked to optimize updates. ▸ Selection and mouse events are easy. Accessibility is doable, but may take some extra effort. ▸ Text is more bothersome than with divs (positioning),
 less bothersome than with canvas. ▸ They are DOM elements, so large numbers = performance issues.
  • 25. SVG BASIC SVG TIPS ▸ Enclosing chart container is <svg>. ▸ Declare shapes with <rect>, <circle>, <path>, etc. ▸ For complex shapes, <path> will do a lot of the heavy lifting. Most of the complexity goes into formatting a string for <path>’s “d” attribute. ▸ Coordinates start at top left! Y=0 is the top of the box. This is the opposite of most charts!
  • 26. SVG SVG CHART EXAMPLE ▸ Some ideas: Complex shapes you fill pieces of using a clip path. ▸ Simple arithmetic, doesn’t really need D3. ▸ Easy to draw shapes in any vector program and copy them in. ▸ Demo: Rocket
  • 28. CANVAS CANVAS A JavaScript-driven Microsoft Paint window ~10 seconds
 to render
  • 30. CANVAS CANVAS: STRENGTHS & WEAKNESSES ▸ You can draw as many “items” as you like - they are not stored as individual objects in memory but just pixels. ▸ Canvas has no awareness of different “elements” after they’re drawn, which makes interaction tricky. ▸ Canvas draw commands are imperative, nothing inside a <canvas> is recognized by React. You need to manually track its state and handle when to render/update. ▸ Text is just pixels, so no font optimization.
  • 31. CANVAS BASIC CANVAS TIPS ▸ (Coordinates also start at top left!) ▸ No XML tags! So no JSX. ▸ Use lifecycles to run canvas draw commands. Can use components to totally abstract it so anything consuming the component can treat it like a regular HTML/SVG component. ▸ Or if you want to go whole hog, create a custom renderer!
  • 33. CANVAS ▸ How do I know when the mouse is clicking on the selection window? ▸ Mouse event triggers anywhere on canvas element ▸ Have to do some manual math. DEMO: CANVAS MINIMAP
  • 34. WEBGL WEBGL Web GOOD LUCK (Web Graphics Library) - low-level power PhiloGL LIBRARY Luz Caballero AUTHORS Nicolas Garcia Belmonte
  • 35. WEBGL WEBGL: STRENGTHS & WEAKNESSES ▸ You can pretty much draw anything you can imagine, 2D or 3D. ▸ Best performance. ▸ Same drawbacks as canvas. ▸ It can be very challenging to work with. Even its abstractions (Three.js) tend to be relatively low level. ▸ Overkill for most common cases.
  • 36. DEMO BARS DEMO & CODE: HTML VS SVG VS CANVAS
  • 37. DEMO BARS DEMO & CODE: HTML VS SVG VS CANVAS ▸ This top level component doesn’t need to change whether Chart or Bar are using SVG or Canvas or what.
  • 38. DEMO BARS DEMO & CODE: HTML VS SVG VS CANVAS HTML SVG Canvas <Bar> render methods
  • 39. DEMO PATHS DEMO & CODE: SVG VS CANVAS
  • 40. DEMO SVG VS CANVAS PATHS ▸ Series of (x, y) coords: [(1, 2), (2, 3), (3, 1)] ▸ HTML: Don’t ▸ SVG: <path d=“M1,2L2,3L3,1” /> ▸ Canvas: ctx.beginPath() ctx.moveTo(1, 2) ctx.lineTo(2, 3) ctx.lineTo(3, 1) ctx.stroke()
  • 41. CHART DRAWING APPROACHES CHART DRAWING APPROACHES BY API HTML SVG Canvas WebGL Root <div> <svg> <canvas> <canvas> Items <div> <rect>, <circle>, <path>, etc track in state, draw with commands track in state, draw with commands Great at rectangles interactive shapes many items perf, 3D Working w/ React plugs right in, works w/ JSX plugs right in, works w/ JSX lifecycle hooks, or custom renderer lifecycle hooks, or custom renderer
  • 42. CONCLUSION THANK YOU ▸ Twitter: @americanwombat ▸ Email: hsubox@gmail.com