Latest / CyberSecurity Summary / D3.js in Action: Data visualization with JavaScript
Transcript
- 0:00Speaker 1: You know, the fundamental architecture of the Internet was uh
- 0:04Speaker 1: never actually designed to draw graphics, right, I mean it
- 0:09Speaker 1: was basically built as a static document sharing network for
- 0:12Speaker 1: physicists at CERN, right, just to pass text files back
- 0:15Speaker 1: and forth. Yet today you as a user, you expect
- 0:19Speaker 1: fluid data driven visualizations, oh absolutely, like interactive election maps,
- 0:23Speaker 1: real time financial dashboards, complex network graphs, and you expect
- 0:27Speaker 1: them to render instantly in your browser. And we kind
- 0:30Speaker 1: of just treat this as magic, like we assume there's
- 0:32Speaker 1: just some massive insert chart command hidden in the browser's
- 0:36Speaker 1: source code.
- 0:37Speaker 2: Yeah, But the reality of how those visuals actually get
- 0:39Speaker 2: onto your screen is it's far more architectural. Creating top
- 0:44Speaker 2: tier interactive graphics requires actively hijacking the native document object
- 0:48Speaker 2: model to DOM and binding raw data directly to the
- 0:51Speaker 2: structural nodes of the web page itself.
- 0:53Speaker 1: And that structural hijacking is our entire focus today. We
- 0:56Speaker 1: are taking a deep dive into the underlying mechanics of
- 0:59Speaker 1: D three J A very deep dive, yeah, using excerpts
- 1:02Speaker 1: from Elijah Meek's book D three JS and Action second
- 1:06Speaker 1: edition as our source material. Our mission is to basically
- 1:09Speaker 1: unpack exactly how raw boring data streams are transformed into
- 1:14Speaker 1: the rich, interactive visualizations that dominate modern top tier websites.
- 1:19Speaker 2: Because it really is a transformation.
- 1:20Speaker 1: It is, and whether you're a developer, a designer, or
- 1:23Speaker 1: just an insanely curious web user, understanding this invisible layer
- 1:28Speaker 1: of the Internet will completely change how you look at
- 1:30Speaker 1: digital charts and maps.
- 1:32Speaker 2: For sure. The paradigm shift with D three is realizing
- 1:35Speaker 2: you aren't working with a prepackaged visualization tool. You're working
- 1:38Speaker 2: with a system of functional abstractions that map data properties
- 1:42Speaker 2: to web standards. You start seeing the underlying geometric math
- 1:46Speaker 2: well in everything you click on.
- 1:48Speaker 1: Okay, let's unpack this because the immediate assumption most people
- 1:51Speaker 1: make when they hear charting library is that they are
- 1:53Speaker 1: getting a standard charting API. Naturally, you feed it some data,
- 1:57Speaker 1: call a pie chart method, and boom, the library does
- 1:59Speaker 1: the rest. But Meeks makes it very clear right away.
- 2:01Speaker 1: D three does not do that.
- 2:02Speaker 2: No, it definitely doesn't.
- 2:04Speaker 1: Like there is no instant pie chart command. If you
- 2:07Speaker 1: want a pie chart, you literally have to run your
- 2:09Speaker 1: data through a function to process it into specific angles
- 2:13Speaker 1: in radians, and then pass that to a separate arc
- 2:16Speaker 1: function just to generate the raw drawing instruction.
- 2:18Speaker 2: Yeah, you are manually constructing the geometry step by step.
- 2:21Speaker 2: The library doesn't draw the chart for you. It just
- 2:24Speaker 2: provides the mathematical utility functions you need to draw it
- 2:27Speaker 2: yourself using standard web.
- 2:29Speaker 1: Elements, which, okay, pushing back on that a bit, why
- 2:32Speaker 1: would anyone use a tool that makes a simple pie
- 2:35Speaker 1: chart so complicated? I mean, if a project manager needs
- 2:38Speaker 1: a standard sales dashboard by Friday, building raw SPG arcs
- 2:42Speaker 1: from scratch seems like a guaranteed way to miss your deadline.
- 2:45Speaker 2: Oh. Absolutely. If your only requirement is a basic static
- 2:48Speaker 2: bar chart or pie chart, D three is absolute overkill.
- 2:51Speaker 2: There are plenty of convenient libraries out there for that,
- 2:54Speaker 2: But this is key. The explicit nature of D three
- 2:57Speaker 2: is actually its greatest strength. How So, because it avoids
- 3:00Speaker 2: proprietary restrictions, a standard charting API locks you into whatever
- 3:06Speaker 2: visualizations the maintainers decided to include. But D three exposes
- 3:10Speaker 2: the full unadulterated capabilities of HTML five, CSS three and SVG,
- 3:16Speaker 2: So the second you hit the ceiling of those convenient libraries,
- 3:19Speaker 2: D three becomes mandatory. It's an open ended laboratory.
- 3:22Speaker 1: And the examples in the source material really highlight that
- 3:25Speaker 1: diverse capability, Like, it's not just for business dashboards, not
- 3:28Speaker 1: at all. They use it to map the ethnic makeup
- 3:30Speaker 1: of US cities, plot undersea communication cables across a manipulatable
- 3:35Speaker 1: three D globe.
- 3:36Speaker 2: Or trees, visualizing musical scores.
- 3:38Speaker 1: Yes, and even mapping the twenty five years social and
- 3:42Speaker 1: co authorship networks of archaeologists. That a dig site, which
- 3:45Speaker 1: is wild. You can't just pick archaeologist network rapt from
- 3:48Speaker 1: a standard charting dropdown.
- 3:49Speaker 2: You really can't. And the aha moment there. The crucial
- 3:52Speaker 2: architectural concept is that D three treats everything on the
- 3:55Speaker 2: web with the exact same high level abstractions. A massive
- 3:59Speaker 2: node in an archael logical network graph is manipulated the
- 4:01Speaker 2: exact same way as a basic text dive in the dom.
- 4:04Speaker 1: Which brings us to the actual magic trick here. If
- 4:07Speaker 1: D three isn't just handing us pre built charts, how
- 4:10Speaker 1: does it actually connect our data to the visuals on
- 4:13Speaker 1: the screen.
- 4:13Speaker 2: That's the binding mechanism.
- 4:14Speaker 1: Yeah, moving from the what to the how. I was
- 4:16Speaker 1: thinking about it like a choreographer. You have a group
- 4:19Speaker 1: of dancers. Those are your web page elements, and you
- 4:22Speaker 1: have a musical beat, that's your data. D three's job
- 4:26Speaker 1: is to pair them up perfectly, so every dancer moves
- 4:28Speaker 1: exactly to their specific beat.
- 4:30Speaker 2: That's a great analogy, and what's fascinating here is how
- 4:33Speaker 2: it actually executes that. Conceptually, it relies on selections and binding.
- 4:38Speaker 1: Break that down for us.
- 4:39Speaker 2: Sure, so you write a selection query something like D
- 4:42Speaker 2: three dot select all circle, dot a until it's a
- 4:45Speaker 2: style of phil is read. D three grabs those elements
- 4:47Speaker 2: and modifies the man mass. But the real power is
- 4:50Speaker 2: when you bind data, like using the dot data command. Exactly,
- 4:53Speaker 2: you write dot data and pass in an array like one, five,
- 4:56Speaker 2: eleven three. D three takes that array and permanently bind
- 5:00Speaker 2: those specific numerical values directly to the DOM elements like
- 5:03Speaker 2: diff containers in the browser's memory.
- 5:05Speaker 1: So what does this all mean. It means we derive
- 5:08Speaker 1: the very appearance of the web page straight from the
- 5:10Speaker 1: data precisely. D three iterates through the elements, and based
- 5:15Speaker 1: on how the data varies, it creates different graphical effects,
- 5:18Speaker 1: making a line longer, or you know, changing a circle
- 5:21Speaker 1: to a specific color. But wait, what if the data
- 5:24Speaker 1: array and the DOM elements don't match up?
- 5:27Speaker 2: That's where the virtual states come in.
- 5:28Speaker 1: Yeah.
- 5:29Speaker 2: D three calculates the intersection between the data and the nodes,
- 5:33Speaker 2: creating three distinct states, the update selection, the enter selection,
- 5:37Speaker 2: and the exit selection.
- 5:39Speaker 1: Right, this part of the reading was super interesting. Let's
- 5:41Speaker 1: say I have an array of five data objects, but
- 5:43Speaker 1: my page only has three graphical elements right now, how
- 5:47Speaker 1: does it handle that?
- 5:48Speaker 2: So the three data objects that match up with the
- 5:50Speaker 2: existing DOM nodes fall into the update selection. You can
- 5:53Speaker 2: just transition those, maybe scale up their width.
- 5:55Speaker 1: Or whatever, leaving two data objects with nowhere to go.
- 5:58Speaker 2: Right, those two fall into the end. D three creates
- 6:02Speaker 2: virtual placeholder nodes for them. You tell it what to append,
- 6:05Speaker 2: like a new SVG circle, and it injects them into
- 6:08Speaker 2: the DOM and.
- 6:09Speaker 1: The exit selection is just the reverse exactly.
- 6:12Speaker 2: If your data shrinks to one object, the two leftover
- 6:15Speaker 2: DOM nodes fall into the exit selection, and D three
- 6:18Speaker 2: smoothly removes.
- 6:19Speaker 1: Them, which honestly sounds like a lot of heavy lifting.
- 6:22Speaker 1: Why not just wipe the screen clean and redraw all
- 6:24Speaker 1: five elements from scratch.
- 6:25Speaker 2: Because wiping the DOM is incredibly expensive for the browser,
- 6:29Speaker 2: and you lose object constancy if you just redraw. The
- 6:33Speaker 2: user sees a jarring flash. Oh right. By using these states,
- 6:37Speaker 2: D three can apply a fluid transition. A node can
- 6:40Speaker 2: smoothly slide down the screen, while deleted nodes independently fade out.
- 6:44Speaker 1: So we've got this amazing binding system. But what exactly
- 6:47Speaker 1: are these elements being drawn on? Because standard HTML dives
- 6:50Speaker 1: are pretty clunky for complex geometry.
- 6:52Speaker 2: Yeah, the box model isn't built.
- 6:53Speaker 1: For that, right, which brings us to the visual canvas
- 6:56Speaker 1: of the modern web. The source material actually brought up
- 6:59Speaker 1: a hilarious memory from the dark ages of the nineteen
- 7:02Speaker 1: nineties Internet dash dpgraph dot com.
- 7:04Speaker 2: Oh wow. Yeah.
- 7:05Speaker 1: Back then, if you wanted to show dynamic math or data,
- 7:08Speaker 1: your only option was animated gifts. You just had these chaotic,
- 7:12Speaker 1: eye bleeding pixelated web pages.
- 7:14Speaker 2: Because it was a closed system, you couldn't interact with
- 7:16Speaker 2: a gift. But if we connect this to the bigger picture.
- 7:19Speaker 2: HTML five revolutionized this by supporting scalable vector graphics or sveg.
- 7:24Speaker 1: Which are basically mathematical instructions right.
- 7:26Speaker 2: Exactly, they're geometric primitives. We're talking about XML tags like
- 7:30Speaker 2: wrecked circle, line, polygon. And what's cool is that even
- 7:35Speaker 2: the most intimidating complex visualizations like a.
- 7:39Speaker 1: Sprawling radial bendrogram.
- 7:41Speaker 2: Yeah, it's really just a compilation of simple circles and lines.
- 7:44Speaker 2: Deconstructing it like that removes the intimidation factor, though.
- 7:49Speaker 1: To be fair, the code for some of those primitives
- 7:51Speaker 1: is a little intimidating. There's this bizarre detail in the
- 7:54Speaker 1: book about the svgpath element.
- 7:56Speaker 2: Oh, the D attribute.
- 7:57Speaker 1: Yes, the D attribute looks literally like the old Logo
- 8:01Speaker 1: Turtle programming language, just a massive string of coordinates like
- 8:05Speaker 1: M teny sixty c to move the pen and draw curves.
- 8:08Speaker 2: Writing those path strings manually is functionally impossible for big
- 8:11Speaker 2: data sets. That's where D three's layout generators come in.
- 8:15Speaker 2: It does the math and generates that massive Turtle graphic string.
- 8:17Speaker 1: For you, thank goodness. But even with D three helping out,
- 8:21Speaker 1: the book makes it clear you still need to understand
- 8:23Speaker 1: how SVG renders. And you got like delightfully nerdy about
- 8:28Speaker 1: the math of SVG borders earlier.
- 8:30Speaker 2: Oh I did. I love this quirk because it's so
- 8:33Speaker 2: different from standard HTML. In HTML, if you add a border,
- 8:37Speaker 2: it goes on the outside of the box, but in
- 8:39Speaker 2: SEG the stroke is drawn exactly centered over the mathematical edge.
- 8:44Speaker 1: Of the geometry right, so it straddles the line.
- 8:46Speaker 2: Yes, so if you have a twenty five x twenty
- 8:48Speaker 2: five pixel rectangle and you give you a two pixel stroke,
- 8:51Speaker 2: it'll actually measure twenty seven by twenty seven pixels on
- 8:54Speaker 2: the screen because.
- 8:55Speaker 1: One pixel bleeds inside and one pixel bleeds outside exactly.
- 8:58Speaker 2: And if you're building a dense pixel perfect matrix and
- 9:01Speaker 2: you don't know that, you will completely break your layout.
- 9:04Speaker 1: That sounds incredibly frustrating. And speaking of breaking things, well,
- 9:06Speaker 1: what happens when you have like a massive data set.
- 9:09Speaker 1: If you put a million SPG elements in the browser,
- 9:11Speaker 1: why don't just crash?
- 9:13Speaker 2: It absolutely will overwhelm the dom. But D three has
- 9:16Speaker 2: alternative methods for extreme scale. It can switch to the
- 9:20Speaker 2: canvas element to draw static bitmaps, or even use WebGL
- 9:24Speaker 2: for three D objects, pushing the work to the GPU.
- 9:26Speaker 1: Okay, so we know how it binds, we know what
- 9:28Speaker 1: it draws on. But practically speaking, how does a creator
- 9:31Speaker 1: actually put all this together and style it without losing
- 9:34Speaker 1: their mind?
- 9:35Speaker 2: It requires a bit of a sandbox approach.
- 9:37Speaker 1: And here's where it gets really interesting. The book talks
- 9:40Speaker 1: about using the browser develop book console as that real
- 9:43Speaker 1: time sandbox.
- 9:44Speaker 2: You don't have to guess no, you can just open
- 9:46Speaker 2: the inspector.
- 9:47Speaker 1: Right, you can literally select a dom element, press delete
- 9:49Speaker 1: and watch your web page get lonely, or just edit
- 9:52Speaker 1: the HTML on the fly. There was this funny kind
- 9:55Speaker 1: of ugly experiment the book suggests.
- 9:57Speaker 2: The styling exercise.
- 9:58Speaker 1: Yeah, taking a pristine SVG rectangle, going into the console
- 10:02Speaker 1: and applying this inline style fill dot purple stroke with
- 10:06Speaker 1: dot five px stroke dot cornflower blue, just to successfully
- 10:09Speaker 1: visualize the abstract concept of ugly.
- 10:12Speaker 2: It is remarkably ugly, but it proves the point. You
- 10:16Speaker 2: can manipulate the live domb instantly, but as you scale
- 10:21Speaker 2: up styling things individually like that gets unmanageable. You need structure,
- 10:26Speaker 2: which brings us to grouping. Right, the crucial G element,
- 10:31Speaker 2: the grouping element. It's totally unique because it has no
- 10:34Speaker 2: physical bounds itself, no width, no color. It's basically invisible exactly.
- 10:39Speaker 2: It acts purely as a logical container. Let's say you
- 10:41Speaker 2: want to move a circle and its text label together.
- 10:43Speaker 2: You don't want to calculate the math for both of
- 10:45Speaker 2: them independently every time they.
- 10:46Speaker 1: Move, because that would be exhausting.
- 10:48Speaker 2: Yeah, so you put them both inside a g element
- 10:50Speaker 2: and then you just apply a transform attribute transform to
- 10:53Speaker 2: the group. You calculate the cordinate's once, and everything inside
- 10:56Speaker 2: it travels together.
- 10:57Speaker 1: It's so efficient. But what about interactivity? Like if I
- 11:01Speaker 1: hover my mouse over that group and I want the
- 11:03Speaker 1: circle to turn red and the text to get bold.
- 11:05Speaker 1: Doing that with raw jobscript sounds messy.
- 11:08Speaker 2: It would be a nightmare. But D three leverages CSS
- 11:12Speaker 2: to handle that interactivity. Instead of manually changing a doesen't
- 11:16Speaker 2: inline styles on hover, D three just uses a method
- 11:19Speaker 2: called D three dot class.
- 11:21Speaker 1: Oh, like assigning a CSS class.
- 11:23Speaker 2: Exactly, You write D three dot class active true and
- 11:27Speaker 2: it instantly applies a whole new style sheet rule to
- 11:30Speaker 2: that element.
- 11:31Speaker 1: That's so much cleaner it is, And.
- 11:33Speaker 2: This raises an important question about modern web development. How
- 11:36Speaker 2: does a team actually collaborate on this?
- 11:38Speaker 1: Right? Because not everyone is a D three wizard exactly.
- 11:41Speaker 2: Because D three utilizes standard cfs, you have a natural
- 11:45Speaker 2: separation of concerns. A developer can write the complex JavaScript,
- 11:48Speaker 2: handle the D three data binding and the.
- 11:50Speaker 1: Math, and then hand it off.
- 11:52Speaker 2: Yes, a designer who doesn't know a single thing about
- 11:54Speaker 2: D three can still completely restyle the visualization just by
- 11:58Speaker 2: updating the external CSS file.
- 12:00Speaker 1: If that is brilliant, it makes the whole thing so
- 12:02Speaker 1: much more approachable for a team.
- 12:03Speaker 2: It really integrates it into the standard webstack.
- 12:05Speaker 1: So as we wrap up this deep dive, I think
- 12:07Speaker 1: the biggest takeaway here is that D three is far
- 12:10Speaker 1: more than just a tool for making bar charts. It
- 12:12Speaker 1: really is an entire philosophy.
- 12:14Speaker 2: A philosophy for treating the web as a dynamic data
- 12:17Speaker 2: driven canvas.
- 12:18Speaker 1: Perfectly said so to you listening. The next time you
- 12:21Speaker 1: are reading a news site and you hover over an
- 12:24Speaker 1: interactive map, or you scrub across a slick timeline, you'll
- 12:27Speaker 1: know exactly what you're looking at exactly. You'll know you're
- 12:29Speaker 1: looking at bound data driving SVG, geometric primitives structured in
- 12:34Speaker 1: logical groups and styled with dynamic CSS. It's not magic,
- 12:38Speaker 1: it's architecture.
- 12:39Speaker 2: It's the visual web firing in real time.
- 12:41Speaker 1: It really is. And we want to leave you with
- 12:43Speaker 1: one final slightly provocative thought to maul Over. We've talked
- 12:48Speaker 1: all about how D three can seamlessly turn any data
- 12:51Speaker 1: point into any visual element, from the length of a
- 12:54Speaker 1: simple line to the color of a three D globe.
- 12:57Speaker 2: The possibilities are endless right.
- 12:59Speaker 1: So, if the web is that fluid and data can
- 13:02Speaker 1: manipulate structure that easily, what invisible data streams are currently
- 13:06Speaker 1: shaping the structure of the websites you visit every day.
- 13:09Speaker 2: Well, it's a good question.
- 13:10Speaker 1: Like what unseen telemetry or behavioral data is constantly rearranging
- 13:15Speaker 1: the digital room while you are actively standing in it.
- 13:18Speaker 2: The architecture is always reacting to you.
- 13:20Speaker 1: It's wild to think about. Keep exploring those invisible layers.
- 13:24Speaker 1: Everyone will catch you on the next deep dive.