Latest / CyberSecurity Summary / Developing A D3.Js Edge: Constructing Reusable D3 Components And Charts
Transcript
- 0:00Speaker 1: You know that feeling right when you've just finished building
- 0:03Speaker 1: a data visualization.
- 0:04Speaker 2: Oh, I know it all too well.
- 0:05Speaker 1: Right, it looks pristine. I mean, the axes aligned perfectly,
- 0:09Speaker 1: the color palette is accessible, and the data is just
- 0:12Speaker 1: it tells this beautifully clear story.
- 0:14Speaker 2: It's a masterpiece exactly.
- 0:16Speaker 1: So you share your screen with a team and someone
- 0:19Speaker 1: leans in, squints and says, hey, this is fantastic. Could
- 0:24Speaker 1: we just make one tiny tweak.
- 0:26Speaker 2: It's never a tiny tweak.
- 0:28Speaker 1: Never, they say, maybe add a second version of that
- 0:31Speaker 1: exact chart right next to it, but you know, just
- 0:33Speaker 1: filtered for a different region. And suddenly your stomach just drops.
- 0:36Speaker 2: Because you know exactly what's about to happen.
- 0:38Speaker 1: Yeah, because to make that one quote unquote tiny tweak,
- 0:42Speaker 1: you are about to get totally lost in like five
- 0:45Speaker 1: hundred lines of fragile, unreadable spaghetti code. You pull on
- 0:49Speaker 1: one thread and the whole layout collapses.
- 0:52Speaker 2: Is the ultimate developer's curse. I mean, it's a request
- 0:54Speaker 2: that sounds conceptually so simple to the user, but structurally
- 0:58Speaker 2: it basically requires tearing down the entire foundation of the
- 1:01Speaker 2: application to execute.
- 1:03Speaker 1: Well, we are tackling that exact curse today. Our mission
- 1:07Speaker 1: for this deep dive is to rescue you from the
- 1:09Speaker 1: dreaded one off visualization trap. We are looking at a
- 1:12Speaker 1: book called Developing a D three JS edge.
- 1:16Speaker 2: By Chris Viao and uh Andrew Thornton ger Hobbled and
- 1:21Speaker 2: Rolling done.
- 1:22Speaker 1: Right, that's the one, and we're pulling apart the architecture
- 1:25Speaker 1: used by Mike Bostock, the creator of D three himself,
- 1:28Speaker 1: to transform static, you know, messy code into a scalable
- 1:32Speaker 1: modular visualization engine, which is.
- 1:34Speaker 2: So critical because before we can build anything scalable, we
- 1:38Speaker 2: really need to look at why the normal way people
- 1:41Speaker 2: learn to code these charts eventually breaks down.
- 1:44Speaker 1: Yeah, let's start there. The book outlines this classic scenario, right,
- 1:47Speaker 1: a standard bar chart showing the frequency of letters in
- 1:49Speaker 1: the English alphabet, so A shows up eight point one
- 1:51Speaker 1: percent of the time, E is at twelve point seven percent,
- 1:53Speaker 1: and so on.
- 1:54Speaker 2: And for a beginner, the typical flow for building that
- 1:56Speaker 2: chart is honestly, it's completely.
- 1:58Speaker 1: Logical, right, it makes sense.
- 1:59Speaker 2: At first, you established some conventional margins for the kansas,
- 2:03Speaker 2: You define an ordinal scale for your X axis, which
- 2:06Speaker 2: discreetly spaces out those twenty six letters, and.
- 2:09Speaker 1: Then you set a linear scale for your y axis
- 2:11Speaker 1: right to dynamically map those percentage of values to actual
- 2:15Speaker 1: pixel heights on the screen.
- 2:17Speaker 2: Yeah, and then you run into the core mechanic of
- 2:19Speaker 2: D three, which is the enter, update and exit.
- 2:22Speaker 1: Pattern, the famous pattern.
- 2:24Speaker 2: Right, you bind your alphabet array to SVG rectangle elements.
- 2:28Speaker 2: The interphase looks for data points that don't have a
- 2:32Speaker 2: matching rectangle yet, and it creates a placeholder.
- 2:35Speaker 1: You style those rectangles, set the width and height, and
- 2:37Speaker 1: boom you have a beautiful bar chart right there on
- 2:40Speaker 1: the screen.
- 2:40Speaker 2: And it works flawlessly, I mean for a single instance.
- 2:44Speaker 2: But the critical flaw emerges the exact second you attempt
- 2:48Speaker 2: to introduce that second bar chart to the page.
- 2:50Speaker 1: Which is what they always asked for. And are my instinct,
- 2:53Speaker 1: and I think honestly the instinct of like ninety percent
- 2:55Speaker 1: of developers working on tight deadlines is to just highlight
- 2:59Speaker 1: that entire block of code, hit copy and paste it
- 3:01Speaker 1: right below the original block.
- 3:03Speaker 2: Oh, absolutely, we've all done it.
- 3:04Speaker 1: Yeah, you just quickly renamed some variables, like charting with
- 3:07Speaker 1: becomes chart two with. But if you need, say ten
- 3:10Speaker 1: charts on a dashboard, you now have ten identical, massive
- 3:13Speaker 1: code blocks.
- 3:15Speaker 2: And consider what happens when a layout requirement changes, because
- 3:18Speaker 2: it always does. The client reviews the dashboard and decides, actually,
- 3:24Speaker 2: the padding between the bars is too tight. Oh yeah, right.
- 3:28Speaker 2: You can't just change a global variable anymore because each
- 3:31Speaker 2: chart has slightly different constraints. Now you have to hunt
- 3:34Speaker 2: down the padding logic in ten different places within the
- 3:37Speaker 2: code bases.
- 3:38Speaker 1: You've introduced massive redundancy.
- 3:40Speaker 2: And mathematically, you have draftically increased the probability of human error.
- 3:44Speaker 1: So imagine it like this. You need to go to
- 3:46Speaker 1: the grocery store, right, So you spend a week building
- 3:50Speaker 1: a custom car from scratch in your.
- 3:51Speaker 2: Driveway, as one does, right.
- 3:53Speaker 1: Naturally, you drive to the store and it works perfectly.
- 3:56Speaker 1: But the next day you need to go to the
- 3:58Speaker 1: hardware store, and instead of getting back the car you
- 4:00Speaker 1: already built, you build a second, completely identical car from scratch,
- 4:05Speaker 1: specifically to drive to the hardware store.
- 4:07Speaker 2: That is exactly what standard D three scripting is. Like,
- 4:10Speaker 2: it functions, but it is a horrific way to operate
- 4:13Speaker 2: a transportation system.
- 4:14Speaker 1: Exactly. It's so wildly inefficient.
- 4:17Speaker 2: But you know there is a reason tutorials are written
- 4:19Speaker 2: this way. D three tutorials are structured to teach core
- 4:22Speaker 2: functionality right, like how SVG coordinates work or how data
- 4:27Speaker 2: joins function.
- 4:28Speaker 1: They aren't meant to be enterprise architecture guides exactly.
- 4:31Speaker 2: They are not designed to teach large scale software architecture.
- 4:35Speaker 2: D three gives you extreme low level freedom. You aren't
- 4:39Speaker 2: restricted to basic configuration menus like you are in say
- 4:44Speaker 2: Excel or Tableau. But that freedom dictates that you are
- 4:48Speaker 2: entirely responsible for architecting your own application state. It's on
- 4:52Speaker 2: you to keep your own house organized.
- 4:54Speaker 1: Okay, so we need to stop building single use cars
- 4:56Speaker 1: in the driveway. We need to stop copy pasting code blocks.
- 5:00Speaker 1: The source material argues that the solution is moving away
- 5:02Speaker 1: from building a chart and moving toward building an API.
- 5:05Speaker 2: An application programming interface. Yes, the fundamental goal has to
- 5:08Speaker 2: shift here. Instead of writing a script that executes sequentially
- 5:11Speaker 2: to draw a single chart, we want to write a
- 5:13Speaker 2: chart generator. A generator, right, We want a standalone function
- 5:17Speaker 2: that we can call with different parameters which then returns
- 5:20Speaker 2: a brand new chart object that is perfectly isolated from
- 5:23Speaker 2: any other chart on the screen.
- 5:25Speaker 1: And to achieve that, the authors lean into a very
- 5:27Speaker 1: specific programming philosophy, which is composition over inheritance.
- 5:33Speaker 2: Yes, composition over inheritance. In traditional object oriented programming, developers
- 5:39Speaker 2: often rely heavily.
- 5:40Speaker 1: On inheritance, like having a parent class.
- 5:43Speaker 2: Exactly, you define a base chart class with a title
- 5:46Speaker 2: and an access and then a bar chart class inherits
- 5:49Speaker 2: those properties, yea, and maybe a stacked bar chart inherits
- 5:52Speaker 2: from that.
- 5:52Speaker 1: But D three doesn't really like that, does it. No.
- 5:54Speaker 2: D three's ecosystem strongly prefers composition. You don't inherit a
- 5:58Speaker 2: monolithic chart. You assemble one by composing smaller independent modules.
- 6:02Speaker 1: So you take a rectangle module, an access module, and
- 6:05Speaker 1: maybe a tooltip module, and you just snap them together
- 6:07Speaker 1: like legos.
- 6:08Speaker 2: Exactly like legos, the text.
- 6:09Speaker 1: Outlines a Hello World pattern for this. To start building
- 6:12Speaker 1: our modular API, we basically wrap all of our chart
- 6:15Speaker 1: generating code inside a single outer function.
- 6:18Speaker 2: Which creates what's called a closure in JavaScript.
- 6:20Speaker 1: Right, a closure, And from what I understand, this closure
- 6:23Speaker 1: effectively builds a wall around our internal variables, things like
- 6:27Speaker 1: the width of the chart or the font color. Those
- 6:30Speaker 1: variables become entirely private, yes, like nothing else running on
- 6:34Speaker 1: the web page can access.
- 6:35Speaker 2: Them, and keeping those variables private prevents the name space
- 6:39Speaker 2: pollution that causes so many bugs in complex dashboards. If
- 6:43Speaker 2: a separate script on the page happens to use a
- 6:46Speaker 2: variable named width, it won't accidentally overwrite the width of
- 6:49Speaker 2: your specific chart.
- 6:50Speaker 1: Okay. I have to push back on this a little
- 6:52Speaker 1: bit because wrapping functions inside functions, creating these artificial walls
- 6:57Speaker 1: just to draw some bars on a screen, it feels
- 7:00Speaker 1: like we are adding a massive layer of complexity for
- 7:02Speaker 1: no reason. I mean, why not just pass a configuration
- 7:06Speaker 1: object into a standard function.
- 7:07Speaker 2: Like a static object?
- 7:08Speaker 1: Yeah, just hand the chart an object that says, you know,
- 7:11Speaker 1: with five hundred color blue and be done with it.
- 7:15Speaker 1: Why all the inception style functions, It's a fair question.
- 7:18Speaker 2: Passing a static configuration object works fine if the chart
- 7:21Speaker 2: never needs to change after it is drawn. Okay, but
- 7:24Speaker 2: data visualizations in modern applications are rarely static. They need
- 7:28Speaker 2: to respond to window resizing, data filtering, user inputs. If
- 7:34Speaker 2: you just pass a static object, updating the chart requires
- 7:37Speaker 2: tearing it down entirely and passing a whole new object.
- 7:40Speaker 1: Oh. I see.
- 7:41Speaker 2: The closure pattern allows the chart to maintain its own
- 7:44Speaker 2: internal state, and to interact with that secure internal state,
- 7:48Speaker 2: we use a very specific mechanism getter and setter functions.
- 7:52Speaker 1: Right. Okay, so if the closure is a wall, the
- 7:55Speaker 1: getters and setters are the windows.
- 7:56Speaker 2: That's a great way to look at it, But the.
- 7:58Speaker 1: Way they implement it here is pretty unique. The getter
- 8:00Speaker 1: and the setter are actually the exact same function.
- 8:03Speaker 2: Yes they are.
- 8:04Speaker 1: So if I want to control the width, I call
- 8:06Speaker 1: a function named dot and if I pass an argument
- 8:09Speaker 1: into it, say the number five hundred, it acts as
- 8:11Speaker 1: a setter. It updates the internal private variable to five hundred.
- 8:15Speaker 2: What does that setter function return after it updates the variable?
- 8:19Speaker 1: It returns this, like literally the word this. It returns
- 8:22Speaker 1: the execution context of the chart instance itself exactly. And
- 8:25Speaker 1: because it returns itself, you can chain the functions together.
- 8:28Speaker 1: I don't have to write three separate lines of code
- 8:30Speaker 1: to update the chart. I can just type dot width
- 8:32Speaker 1: five hundred, dot height three hundred, dot color blue, all
- 8:36Speaker 1: in one continuous chain.
- 8:38Speaker 2: That chainability is the absolute hallmark of the D three
- 8:41Speaker 2: API pattern. And if you call that exact same dot
- 8:45Speaker 2: width function without passing an argument into it, the fund
- 8:48Speaker 2: can detect that the argument is missing and it acts
- 8:50Speaker 2: as a getter.
- 8:51Speaker 1: Oh so it just tells you the current width.
- 8:53Speaker 2: It simply returns the current internal value. Yeah, you have
- 8:56Speaker 2: an elegant, unified way to query or my notify the
- 9:00Speaker 2: state of the chart at any point in its life cycle.
- 9:02Speaker 1: That is super clean. Now, while we are looking at
- 9:04Speaker 1: the code for these modules, I have to bring up
- 9:06Speaker 1: a moment where I completely geeked out over the source
- 9:09Speaker 1: material which part I noticed a very strange syntax choice.
- 9:12Speaker 1: When they were generating random data to test the charts,
- 9:16Speaker 1: instead of using standard math functions to round numbers, they
- 9:19Speaker 1: placed a double tilled operator like two little squiggly lines
- 9:23Speaker 1: in front of the variables like till the variable.
- 9:27Speaker 2: Ah. Yes, the bit wise not operator applied twice.
- 9:32Speaker 1: Okay, but why do that if you want to drop
- 9:34Speaker 1: the decimal from a random number like four point nine?
- 9:36Speaker 1: Almost every developer on Earth just types math dot floor.
- 9:40Speaker 1: Using a double bitwise knot. Seems like writing code in
- 9:43Speaker 1: a secret language just to show off.
- 9:45Speaker 2: It does look cryptic. I'll give you that, but it
- 9:47Speaker 2: is actually a profound performance optimization at the hardware level.
- 9:51Speaker 1: Wait, really, just dropping a decimal?
- 9:53Speaker 2: Yeah? In JavaScript, all numbers are inherently sixty four bit
- 9:57Speaker 2: floating point numbers. They consume memory and processing psych to
- 10:00Speaker 2: handle decimals even when you only need a whole number.
- 10:03Speaker 2: When you run math dot floor, the JavaScript engine has
- 10:05Speaker 2: to spit up a function execution, check if the input
- 10:08Speaker 2: is a valid number, handle edge cases, and then return
- 10:11Speaker 2: the value.
- 10:11Speaker 1: That sounds like a lot of overhead for rounding a number.
- 10:14Speaker 2: It is, but the bitwise operator bypasses all of that
- 10:17Speaker 2: function overhead completely. When the JavaScript engine sees a bitwise
- 10:22Speaker 2: operator like a tilled, it temporarily forces that sixty four
- 10:25Speaker 2: bit float into a thirty two bit signed integer. The
- 10:29Speaker 2: decimal portion isn't mathematically rounded off, it is literally truncated
- 10:33Speaker 2: and discarded at the bit level.
- 10:35Speaker 1: Wow.
- 10:35Speaker 2: The first tiled flips all the binary bits, and the
- 10:38Speaker 2: second till flips them back to their original state. The
- 10:40Speaker 2: result is the exact same as math dot floor, but
- 10:43Speaker 2: it executes significantly faster because it operates directly on the
- 10:47Speaker 2: CPU's registers. It bypasses the language's math library entirely.
- 10:51Speaker 1: That is wildly efficient. It just chops off the decimal
- 10:54Speaker 1: at the bit level and connecting that optimization back to
- 10:57Speaker 1: our broader architecture. You know this API pattern and the closures,
- 11:00Speaker 1: the getters and setters. This isn't an arbitrary choice by
- 11:03Speaker 1: the authors. This is the genetic makeup of D three itself.
- 11:06Speaker 2: Exactly when a developer calls a core function like D
- 11:09Speaker 2: three do spg dot axis, they are interacting with this
- 11:12Speaker 2: exact closure pattern. That consistency is why third party plugins
- 11:15Speaker 2: in the D three ecosystem mesh together so flawlessly.
- 11:19Speaker 1: Okay, So, moving from unstructured scripts to this intentional API
- 11:23Speaker 1: architecture it really dictates a shift in how we verify
- 11:27Speaker 1: our code. Because when you write a massive, monolithic script,
- 11:31Speaker 1: the only way to test it is to load the browser,
- 11:33Speaker 1: stare at the screen and ask does this chart look correct?
- 11:37Speaker 2: Which is subjective, entirely manual, and honestly impossible to scale
- 11:40Speaker 2: across a team.
- 11:41Speaker 1: Right, But building these isolated modules unlocks the ability to
- 11:45Speaker 1: do test driven development or TDD. Yes, the authors utilize
- 11:50Speaker 1: a behavior driven framework called Jasmine to build a test suite,
- 11:53Speaker 1: and they make a crucial point here. A comprehensive test
- 11:57Speaker 1: suite is actually the best documentation a code based on
- 12:00Speaker 1: can have.
- 12:00Speaker 2: I completely agree with that. It physically verifies the contract
- 12:03Speaker 2: of your public API. Yeah, a written read me file
- 12:06Speaker 2: will inevitably fall out a date someone will forget to
- 12:08Speaker 2: update it, but a test week it will fail the
- 12:11Speaker 2: moment the code diverges from its promised behavior.
- 12:14Speaker 1: But testing a visual medium like an SPG chart requires
- 12:17Speaker 1: a shift in perspective. Right, I mean you cannot write
- 12:19Speaker 1: a unit test to check if a visualization is esthetically pleasing.
- 12:23Speaker 2: No, you're not testing for pretty. You are testing structural mechanisms.
- 12:26Speaker 1: Right. You write an automated test to see if the
- 12:29Speaker 1: module successfully appends SVG groups to the page. You write
- 12:33Speaker 1: a test to assert that when you call the dot
- 12:35Speaker 1: with five hundred setter, the internal variable actually updates to
- 12:39Speaker 1: five hundred, And.
- 12:40Speaker 2: Most crucially, you test the data binding. The DOM binding
- 12:44Speaker 2: is the core mechanic of.
- 12:45Speaker 1: D three, So you aren't just testing if a rectangle exists.
- 12:49Speaker 2: No, you physically query the document object model, locate the rectangle,
- 12:53Speaker 2: and inspect its hidden data property.
- 12:55Speaker 1: The underscore underscore data field exactly.
- 12:58Speaker 2: You check that feel to guarantee that the JSON or
- 13:01Speaker 2: CSV row was successfully attached to that specific DOM node
- 13:05Speaker 2: by the module.
- 13:06Speaker 1: But visualizations are highly interactive. How do you test something
- 13:09Speaker 1: like a user hovering their mouse over a specific bar
- 13:11Speaker 1: to trigger a tooltip the text mentions using spies in Jasmine.
- 13:15Speaker 1: But how does a test suite actually fake a mouse click?
- 13:18Speaker 2: So a spy is basically a function proxy. If your
- 13:21Speaker 2: module have a custom function that triggers a complex tooltip
- 13:23Speaker 2: animation on hover, running that full animation during a unit
- 13:27Speaker 2: test would slow down your test suite.
- 13:29Speaker 1: It need a full browser rendering engine, right, exactly.
- 13:31Speaker 2: So a Jasmine spy intercepts the call. It essentially replaces
- 13:35Speaker 2: your tooltip function with a dummy wrapper. Okay, when the
- 13:39Speaker 2: test simulates the hover event, the spy catches the execution
- 13:43Speaker 2: records yes, I was invoked, and here are the coordinates
- 13:47Speaker 2: I was passed, and then it stops the execution before
- 13:50Speaker 2: the heavy animation code even runs.
- 13:51Speaker 1: It intercepts the command. It's like, okay, it's like evaluating
- 13:55Speaker 1: the assisiency of a restaurant kitchen. How so, Well, you
- 13:59Speaker 1: don't verify the system by sitting in the dining room
- 14:01Speaker 1: and tasting the final plated meal every five minutes.
- 14:04Speaker 2: Right, That would take forever.
- 14:05Speaker 1: You unit test the kitchen infrastructure. You check that the
- 14:09Speaker 1: ticketing machine successfully routes a piece of paper to the
- 14:12Speaker 1: grill station. You don't need to actually cook the steak
- 14:15Speaker 1: to know the communication system worked.
- 14:17Speaker 2: That clarifies the distinction perfectly. You are verifying the communication
- 14:21Speaker 2: pathways and the state changes, not the final visual output.
- 14:25Speaker 2: And once you have established that bulletproof testable infrastructure, you
- 14:29Speaker 2: are finally equipped to handle a real world scenario where
- 14:33Speaker 2: the data is actively working against you, which brings.
- 14:36Speaker 1: Us to the core case study of the book, the
- 14:38Speaker 1: Urban Data Challenge.
- 14:40Speaker 2: This is where it gets fun right.
- 14:41Speaker 1: The task was to visualize complex public transit networks for
- 14:45Speaker 1: three major cities, Zurich, Geneva, and San Francisco. They had
- 14:50Speaker 1: their testable building blocks, but now they had to feed
- 14:53Speaker 1: real world data into them.
- 14:54Speaker 2: And real world data is rarely clean. Public transit data
- 14:58Speaker 2: especially is notoriously fragmed. The raw source files for this
- 15:02Speaker 2: project amounted to roughly one hundred and eighty megabytes of
- 15:06Speaker 2: dirty csvs one.
- 15:08Speaker 1: Hundred eighty megabytes of text data for San Francisco alone.
- 15:11Speaker 1: The scheduled bus timetables and the real time arrival coordinates
- 15:15Speaker 1: were siloed in entirely different files that needed a relational join.
- 15:19Speaker 2: Yeah, and the data set for Geneva was completely missing.
- 15:22Speaker 2: Its latitude and longitude columns just gone.
- 15:24Speaker 1: Which is kind of important for.
- 15:25Speaker 2: Our map just a bit. Yeah, and if a developer
- 15:28Speaker 2: attempted the load one hundred and eighty megabytes of raw
- 15:30Speaker 2: unjoined text files directly into the browser's memory via AJAX.
- 15:34Speaker 1: Request, oh, the application would freeze right immediately.
- 15:36Speaker 2: The browser tab would likely crash. The client side environment
- 15:39Speaker 2: just cannot handle that volume of raw computation.
- 15:42Speaker 1: So the authors knew the browser couldn't handle it, so
- 15:44Speaker 1: they stepped outside of D three entirely. They used data Mirror,
- 15:48Speaker 1: which is an enterprise big data analytics tool, to ingest, clean, join,
- 15:53Speaker 1: and aggregate the raw files. They managed to reduce the
- 15:57Speaker 1: one hundred and eighty megabytes of noise down to a
- 15:59Speaker 1: highly optimized sixty megabyte zipped payload, which is still a
- 16:03Speaker 1: massive amount of data, but it crosses the threshold of
- 16:06Speaker 1: being manageable for a modern browser exactly.
- 16:09Speaker 2: But to orchestrate the intake of that sixty megabyte file,
- 16:12Speaker 2: the author's engineered a dedicated data manager API.
- 16:15Speaker 1: The Data Manager API, and this is a D three
- 16:18Speaker 1: module built using the exact closure pattern we discussed, but
- 16:21Speaker 1: it doesn't render a single pixel to the screen, none.
- 16:24Speaker 2: Not one pixel. Its sole responsibility is to asecrenously fetch
- 16:28Speaker 2: the compressed CSV files, route the data through specific cleaning
- 16:32Speaker 2: functions depending on whether it's processing Zerich or Geneva, and
- 16:35Speaker 2: manage the state of data load.
- 16:37Speaker 1: I understand the need to load data, but why go
- 16:39Speaker 1: to the trouble of building a complex data manager API
- 16:43Speaker 1: at all? I mean, D three has a built in
- 16:44Speaker 1: D three dot CSV function that loads files perfectly well
- 16:48Speaker 1: it does. Why separate the loading logic from the chart code?
- 16:51Speaker 2: It roots back to the model view controller architecture or MVC.
- 16:55Speaker 2: If you embid a D three dot CSV call directly
- 16:58Speaker 2: inside your bar chart module, the bar chart is permanently
- 17:01Speaker 2: coupled to the network.
- 17:02Speaker 1: OH, meaning it can't do anything else.
- 17:04Speaker 2: Right, it cannot draw itself until it fetches a file.
- 17:07Speaker 2: By separating the data manager, which is the model, from
- 17:10Speaker 2: the visual modules, the views, the charts remain completely agnostic
- 17:13Speaker 2: about where the data comes from.
- 17:15Speaker 1: How do they communicate?
- 17:16Speaker 2: Then?
- 17:17Speaker 1: If the chart doesn't know about the data manager, how
- 17:19Speaker 1: does it know when to actually draw the data?
- 17:22Speaker 2: Ah. The text details their use of D three dot
- 17:25Speaker 2: dispatch dispatch o YE. D three dot dispatch operates on
- 17:28Speaker 2: a publisher subscriber mechanism. The decoupled chart modules load onto
- 17:32Speaker 2: the page and they simply listen. They just listen for
- 17:35Speaker 2: a specific signal.
- 17:36Speaker 1: Meanwhile, the data manager is working in the.
- 17:37Speaker 2: Background exactly crunching the sixty megabytes. When it finishes, the
- 17:41Speaker 2: data manager uses D three Dot dispatch to broadcast a
- 17:44Speaker 2: custom event. It's essentially shouting data ready into the application's
- 17:49Speaker 2: global name space along with the cleaned array of transit stops.
- 17:52Speaker 1: The charts hear the shout, they catch the array, and
- 17:55Speaker 1: they render. The separation of concerns is absolute. It's beautiful,
- 17:59Speaker 1: and once the data is finally loaded and clean, we
- 18:02Speaker 1: arrive at the visual challenge drawing millions of individual transit
- 18:05Speaker 1: stops right.
- 18:06Speaker 2: They utilize D three's geographic capabilities for this, specifically the
- 18:11Speaker 2: D three dot geo dot Mercator projection.
- 18:14Speaker 1: The map API.
- 18:15Speaker 2: Yes, this map API dynamically calculates the mathematical center, the
- 18:19Speaker 2: optimal zoom scale, and the pixel translation for each of
- 18:22Speaker 2: the three city layouts. It maps the raw latitude and
- 18:25Speaker 2: longitude coordinates into x and y pixel positions, and it
- 18:29Speaker 2: pends an SVG circle for every single transit stop.
- 18:32Speaker 1: Rendering the map is a processing challenge for sure, but
- 18:35Speaker 1: the true computational hurdle is interactivity.
- 18:38Speaker 2: Always they needed the.
- 18:39Speaker 1: User to be able to instantly filter millions of transit
- 18:42Speaker 1: coordinates without triggering a massive rerender that would crash the
- 18:45Speaker 1: frame rate. And to solve this, they implemented a library
- 18:48Speaker 1: called crossfilter dot js, which.
- 18:50Speaker 2: Not coincidentally was also developed by Mike Bostock, and it
- 18:53Speaker 2: is specifically engineered to handle lightning fast multidimensional filtering in
- 18:57Speaker 2: the browser.
- 18:58Speaker 1: The underlying mechanic of cross filter in fol defining dimensions.
- 19:02Speaker 1: You instruct cross filter to iterate through the massive data
- 19:04Speaker 1: set once and build a highly optimized index based on
- 19:08Speaker 1: a specific trait like.
- 19:10Speaker 2: A location dimension, utilizing those transit coordinates.
- 19:13Speaker 1: Right, and once that index is built in memory querying
- 19:16Speaker 1: it takes fractions of a millisecond to leverage that speed. Visually,
- 19:20Speaker 1: the authors integrated a D three interaction technique known as brushing.
- 19:24Speaker 2: Brushing is such a cool tool. It's basically the visual
- 19:27Speaker 2: interface for filtering. The user clicks and drags their cursor
- 19:31Speaker 2: across the SVG map, physically drawing a translucent bounding box
- 19:35Speaker 2: over a specific neighborhood.
- 19:37Speaker 1: Any transit stop caught inside the box is instantly selected right.
- 19:41Speaker 2: But the mechanism connecting that visual box to the cross
- 19:43Speaker 2: filter index requires a mathematical translation. When the user draws
- 19:47Speaker 2: the brush, the browser only knows the pixel coordinates, say
- 19:51Speaker 2: x one hundred x three hundred.
- 19:53Speaker 1: It doesn't know the geography exactly.
- 19:54Speaker 2: So the map API takes those pixel boundaries and passes
- 19:57Speaker 2: them through the mercator projections dot v vert method.
- 20:01Speaker 1: So it runs the math in reverse.
- 20:04Speaker 2: Yes, it translates the raws creen pixels back in the
- 20:07Speaker 2: geographic longitudes and latitudes. Those geographic bounds are immediately handed
- 20:13Speaker 2: to cross filter, which slices its pre built index and
- 20:17Speaker 2: returns only the transit stops within that specific neighborhood.
- 20:21Speaker 1: Think of crossfilter as an ultra efficient, high speed mechanical
- 20:24Speaker 1: sorting machine in a factory.
- 20:26Speaker 2: Okay, I like this.
- 20:27Speaker 1: When the data first loads, the machine pre sorts millions
- 20:31Speaker 1: of data points into thousands of individual holding tubes based
- 20:35Speaker 1: on their location in time. Brushing on the map is
- 20:38Speaker 1: the equivalent of pulling a physical lever that instantly opens
- 20:41Speaker 1: the valves on a specific cluster of those holding.
- 20:43Speaker 2: Tubes, and the data just falls out immediately exactly.
- 20:46Speaker 1: There is no searching or scanning required the moment of interaction,
- 20:49Speaker 1: because the mechanical tension is already.
- 20:50Speaker 2: Set that pre calculated structure is exactly what enables a
- 20:54Speaker 2: seamless user interface. The user moves the brush, the dot
- 20:57Speaker 2: invert function calculates the boundaries, cross filter opens the valve,
- 21:02Speaker 2: and the filtered data instantly flows into the final piece
- 21:04Speaker 2: of the architecture, the radial histogram API.
- 21:08Speaker 1: Yes, the radial histogram. It takes all the transit stops
- 21:11Speaker 1: that the user just selected with the brush and groups
- 21:13Speaker 1: them to sure delays. But instead of a standard bar
- 21:16Speaker 1: chart that moves left to right, it's cyclical right. It
- 21:19Speaker 1: is a circle divided into twenty four segments representing the
- 21:22Speaker 1: twenty four hours at the day. The inner radius of
- 21:25Speaker 1: each segment pushes outward based on the density of buses
- 21:28Speaker 1: that were delayed by more than one minute.
- 21:29Speaker 2: And the choice to use a radial layout over a
- 21:32Speaker 2: linear bar chart is highly intentional. Here time is inherently cyclical.
- 21:37Speaker 2: On a flat bar chart, eleven point zero PM is
- 21:40Speaker 2: on the far right and one point zero zero AM
- 21:43Speaker 2: is on the far left.
- 21:44Speaker 1: The visual continuity is broken exactly.
- 21:46Speaker 2: A radial layout naturally connects the end of the evening
- 21:49Speaker 2: to the beginning of the early morning. It acts as
- 21:51Speaker 2: an intuitive twenty four hour clock face for the user.
- 21:53Speaker 1: And the linkage between the map API cross filter and
- 21:56Speaker 1: this radial histogram generates genuine analytical insights. The text notes
- 22:01Speaker 1: that if you look at the histogram for the entirety
- 22:03Speaker 1: of Zurich, it just looks like a mess of delays
- 22:06Speaker 1: spanning uniformly between seven zero AM and one point zero pm.
- 22:09Speaker 2: Because the aggregate data hides the.
- 22:11Speaker 1: Nuance right, But the architecture allows for granular exploration. By
- 22:16Speaker 1: dragging the brush over a very specific cluster of suburban
- 22:20Speaker 1: transit stops on the periphery of the map. Cross filter
- 22:23Speaker 1: instantly isolates those stops and updates the radial.
- 22:26Speaker 2: Histogram without the page ever reloading.
- 22:29Speaker 1: Without a single frame drop, and suddenly the histogram morphs
- 22:33Speaker 1: for that specific suburb. The delays disappear during the midday
- 22:37Speaker 1: hours and spike aggressively strictly during the morning and evening windows.
- 22:41Speaker 2: The visualization instantly reveals that this specific geographic cluster is
- 22:46Speaker 2: a high density commuter zone.
- 22:48Speaker 1: It is an incredible payoff. It justifies every complex pattern
- 22:51Speaker 1: we've discussed today. We started this deep dive looking at
- 22:54Speaker 1: a fragile standard bar chart where copying and pasting code
- 22:57Speaker 1: led to an unmaintainable nightmare.
- 22:59Speaker 2: Yeah, dited spaghetti code.
- 23:01Speaker 1: From there, we explored how wrapping our logic enclosures protects
- 23:04Speaker 1: our variables. We saw how implementing getters and setters that
- 23:07Speaker 1: return this creates a chainable API.
- 23:10Speaker 2: We examine how utilizing jasmine and dollnbinding tests ensures the
- 23:14Speaker 2: integrity of those APIs, making sure they actually append data
- 23:17Speaker 2: to the data attribute.
- 23:18Speaker 1: And we saw it all come together in a decoupled
- 23:21Speaker 1: architecture where a data manager cleans the payload D three
- 23:24Speaker 1: dot dispatch, orchestrates the timing, and cross filter links a
- 23:28Speaker 1: geographic map to a radial histogram for real time analysis.
- 23:32Speaker 2: It really is an elegant system, and looking at this
- 23:36Speaker 2: entire architecture holistically, it pushes the storet to much larger
- 23:39Speaker 2: paradigm shift regarding the future of data systems we need well.
- 23:43Speaker 2: We traditionally view a visualization as a final destination, right,
- 23:47Speaker 2: a static output generated strictly for human consumption. But when
- 23:51Speaker 2: we build modular APIs that communicate via dispatch events and
- 23:55Speaker 2: expose their state through getters and setters, we aren't just
- 23:58Speaker 2: drawing pictures anymore. We are building programmatic interfaces.
- 24:01Speaker 1: Okay, I'm tracking with you.
- 24:02Speaker 2: Imagine an automated AI agent analyzing a city's transit grid.
- 24:07Speaker 2: It wouldn't need to look at the screen or parts
- 24:09Speaker 2: at JPEG give a chart. Because the visualization is built
- 24:12Speaker 2: as a living API. The AI could programmatically trigger the
- 24:15Speaker 2: maps dot invert function itself.
- 24:17Speaker 1: Oh wow.
- 24:18Speaker 2: It could manipulate the cross filtered dimensions, read the setter
- 24:21Speaker 2: outputs in milliseconds, and test millions of hypotheses against the
- 24:25Speaker 2: visual engine without human intervention. By structuring visualizations this way,
- 24:31Speaker 2: they cease to be just tools for human insight. They
- 24:34Speaker 2: become interactive environments for machine learning models to explore context.
- 24:38Speaker 1: A visualization that acts as an engine for both humans
- 24:42Speaker 1: and automated agents. That is a fascinating way to look
- 24:46Speaker 1: at the intersection of data, visualization and AI.
- 24:49Speaker 2: It changes everything, it really does well.
- 24:51Speaker 1: We've covered a tremendous amount of ground today pulling apart
- 24:54Speaker 1: the architecture that makes scalable, resilient code possible. We hope
- 24:58Speaker 1: this deep dive into modular compo bon its changes the
- 25:00Speaker 1: way you think about your next project and gives you
- 25:03Speaker 1: the tools to finally escape the spaghetti code trap. Keep
- 25:06Speaker 1: experimenting with your architecture, keep pushing for cleaner data, and
- 25:09Speaker 1: we will catch you on the next deep dive.