π Hi!
Today, I suggest we go through the quickest introduction on how a website works. And more importantly, how a graph on a website is built.
β
If you're interested, you can read a long-form version of this on the d3 graph gallery!
If you enjoy charts (I guess you do π), chances are youβve seen a ton of them on the web.
So, itβs worth knowing just a tiny bit about how this magic happens!
Letβs break it down:
1οΈβ£ HTML β The Foundation
Try this:
- Create a file on your laptop and name it index.html
- Add this content in the file
- Double-click the file to open it in your browser.
π Congrats, you just built a minimalist website!
<p> stands for paragraph. HTML has a lot of attributes like this, like </p><h1> for titles, and <img> for images.</h1>
Every webpage you visit is, at its core, just an HTML file. Just like the one you made!
β
2οΈβ£ CSS β The Styling
A website with plain black text? Pretty boring. CSS is here to fix that.
It lets you change the look of your page. For example, you can turn your paragraph red with this rule:
Here the css is included directly in the HTML file, but it is often provided in an external stylesheet.
CSS seems simple at firstβ¦ but trust me, it gets tricky fast. If somebody tells you css is easy, they just don't know much about it. π
β
3οΈβ£ SVG β The Drawing Tool
To make a chart, we need to draw shapes: circles, rectangles, lines... Thatβs where SVG comes in.
SVG lets you create graphics directly in your HTML file.
β
Let's place three circles on our page. That's almost a scatterplot!
The only difference? In a real chart, weβd use data to position the points at specific locations and draw axes (with more SVG!).
β
4οΈβ£ JavaScript β The Brains
JavaScript makes things interactive.
It lets you say for instance: βWhen the user clicks a circle, make it purple!β π΅
β
This is how the javascript code for that looks like, wrapped in a
β
β
You don't need to understand exactly how it works, it's just to give you an idea of what happens when you show a tooltip, click on a button or zoom on a chart.
What really important to understand is the JavaScript runs in your browser.
That means if your site needs to do something complex, like running a regression or plotting thousands of points, itβs your computer doing the work, not some remote server.
So, Why Should You Care?
First, I hope I demystified interactive graphics! Look the interactive scatterplot we've made in 4 minutes!
β
We're not that far from creating a real chart, and d3.js can help us on many of the steps.
β
β
Second, if you use R or Python, youβre already working with these technologies, whether you realize it or not!
- When you use Plotly (R/Python) or Bokeh (Python), these libraries are secretly writing the HTML, CSS, and JavaScript for you.
- When you build reports in Quarto or Jupyter, youβre actually generating an HTML file. Just like the one you made earlier! This is why you can create stunning interactive effects with Quarto.
β
Perhaps this does not sound of any interest for you.
β
But trust me, understanding the basics of dataviz on the web will change the way you think about your tools.
Next week, Iβll show you how this knowledge helps you pick the right tools for your projects. And why Iβm always skeptical when someone says they need an R Shiny app.
See you then!
Yan
β
βPS: Once you have an HTML file like the one we just built, you can host it for free on GitHub and get a public URL. Just like any other website! I cover exactly how to do this in Module 4 of my Productive R Workflow course. π
β
βPPS: I'm a web developer and there are a couple of posts like this I would like to write. Are you interested in web-related stuff?
β
Are you interested in posts like this, explaining how dataviz on the web works? |
|
|
|
π By the way, here is how I can help!
β
- Master R: Join my productive R workflow online course, already helping hundreds to excel in R, Quarto, and GitHub.
- Team Training: Hire me to train your team on Data Visualization and Programming.
- Engaging Talks: Book me for short, impactful talks on Data Visualization and Programming.
β
Check yan-holtz.com or hit reply any time! I love hearing from you.
|
β
β
β
β
β
|
β