- Podcasts
- CyberSecurity Summary
- Data Visualization with JavaScript
About this episodeA practical guide for developers to implement effective digital graphics using various code libraries. The text emphasizes that simplicity…
You can listen and download our episodes for free on more than 10 different platforms:
https://linktr.ee/cyber_security_summary
Get the Book now from Amazon:
https://www.amazon.com/Data-Visualization-JavaScript-Stephen-Thomas-ebook/dp/B00V20IFDG?&linkCode=ll2&tag=cvthunderx-20&linkId=a8d31698ffc4b51db32b2e8332ca15a5&language=en_US&ref_=as_li_ss_tl
Discover our free courses in tech and cybersecurity, Start learning today:
https://linktr.ee/cybercode_academy
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Data Visualization with JavaScript
Imagine you just pulled up to a gas station. You put the nozzle in your car, you squeeze the handle, and you pump twenty gallons of fuel. But like there's this massive leak in the hose. Oh no, yeah, so out of those twenty gallons, nineteen point nine gallons just drip directly onto the concrete highway.
And you know what I love about this specific book is that it doesn't just treat charts like magic tricks, right. It acknowledges that effective visualizations are really the only way to transform those abstract, raw numbers into a shape that the human brain can actually process, yeah, making it digestible exactly. When a chart is built perfectly, you free the viewer like they don't have to decisive the math, they just get it. Yeah, they can just focus entirely on the store that the data is trying to tell. But you cannot build that kind of seamless experience unless you understand the mechanics happening under the hood, Which means.
Yes, and the book walks us through a brilliant example of this. Okay, let's unpack this. Let's do We look at the English Mere League wins for the soccer team Manchester City. This is from two thousand and six to twenty twelve, and the data points show their performance shifting pretty dramatically. They started thirteen wins in two thousand and six, they drop to eleven wins in two thousand and seven, and eventually they climb all the way up to twenty eight wins by twenty twelve.
Element right, And for anyone who hasn't built a web page, think of an HTML canvas exactly like it sounds. It is a literal, blank, invisible rectangle that the browser sets aside on your screen, like a literal canvas exactly. It doesn't process texts like a normal web page. It's a dedicated sandbox specifically for a JavaScript library. To draw raw pixels.
It does, But here's the catch. If you just feed those coordinates in and let the library use its default mathematical settings, you run into a massive problem. The default algorithm automatically calculates the vertical axis the y axis based purely on your lowest and highest data points, so it zooms in. Yes, because Manchester City's lowest number of wins was eleven, the bottom of the chart starts.
If I am a computer programmer, I might argue that the computer is doing exactly what it should if the data only exists between the numbers eleven and twenty eight. Isn't starting the axis at eleven mathematically the most precise way to show that specific variance? Technically, yes, right, So why are we overwriting the computer when its math is technically correct?
Well the math is correct visually, it is a total disaster. When we look at a bar chart, our brains don't just read the numbers on the side. We process the proportion of the shapes. Oh, I see, We inherently believe that if one bar is twice as tall as another, the underlying value must be twice as large.
Points, and the book uses this incredible data set from NOAA, the National Oceanic and Atmospheric Administration. It's a great example. It spans over fifty years tracking the carbon dioxide concentration in the atmosphere from Manola, Hawaii alongside the global surface temperature deviations for that exact same time period.
You have CO two, which is measured in parts per million. Those are large absolute numbers in the three hundreds and four hundreds, right, and then you have temperature deviations which are measured in tiny fractions of a single degree celsi's numbers like zero point zero seven or point one.
If you just overlay them, the grid lines clash, the values don't align, and the viewer just gives up. But beyond the formatting, the whole concept of a deviation metric is confusing. It really is the temperature values aren't absolute temperatures, they are deviations from the twentieth century historical average. So how on earth does a developer make that historical average clear to the end user without just slapping a giant paragraph of explanatory text onto the screen.
That's a great way to think about it. And they don't just leave it looking like a normal line of data. What do they do in the code? They style this specific zero line to be only one pixel wide. They color it a dark, subtle gray, and they completely disable its drop shadow, so it blends in exactly. By changing those visual properties, it stops looking like a data set to the human eye and starts looking like a piece of the charts structural framework.
It creates an instant visual anchor. You don't have to read an explanation. Your brain immediately understands that anything above that gray line is warmer than the historical average, and anything below it is cooler. It's so intuitive, And the author does one more subtle thing in the code that I found so clever. When you have multiple lines, you obviously need a legend so the user knows what they're looking.
But once again, if you just hand this raw data to the flat er two library, the default output is pretty claustrophobic, very crammed. The library tries to maximize the space, so the points get crammed right against the very edges of the chart box. The dot for the country with the lowest spending physically touches the left wall, and the highest bending touches the right wall.
It separates the cluster of information from the boundaries of the chart, but the breathing room isn't the only thing the user needs. The x axis represents a percentage, bending as a percentage GDP. But the raw data in the array, it's just the integers five, ten, fifteen, extrail numbers. Yeah. If you leave it like that, the user has to look at the number fifteen, scan their eyes over to the access title, read percentage of GDP, and then remind themselves right that number fifteen means fifteen percent.
So instead of just accepting the default integers, you write a tiny piece of logic that intercepts the label right before the browser draws it on the canvas, and simply a pens a percentage signed to it. Oh nice. The user just sees fifteen percent and instantly understands the value, and you really want them to understand it quickly, because the payoff of this specific data set is amazing. When you step back and look at the scatterplot, you don't see a perfect diagonal line going up.
No, you don't, and that is exactly why you need a scatterplot instead of a bar chart. If you just average the data out into bars, you would miss the reality. You miss the whole point exactly. The scatter reveals a messy cluster. It visually proves that simply spending more money on healthcare does not guarantee a linear, straight line increase in life expectancy.
Well, if we connect this to the bigger picture, it comes down to a fundamental biological flaw in how the human brain processes geometry. Really biology, Yeah, we are actually terrible at judging the relative size of areas. We are very good at comparing lengths, like the height of vertical bars in a bar chart.
It highlights that twenty two point four percent of the world's population was living on less than a dollar and twenty five cents a day. Wow. When you put that single red wedge of twenty two point four percent against a solid, massive circle representing the rest of humanity, you don't need to compare multiple variables. The impact is visceral and instant.
But actually getting that pie chart to render brings us to my absolute favorite part of this deep dive. It is the reality versus ideal world philosophy of the book, which is so relatable because we tend to think of computer programming as this pristine, perfect science where everything is logical. But the reality is that even great widely used open source cools have hilarious, messy corps.
Exactly so the author details this known deliberate and the Flatschow Library. When the library is trying to calculate how big to draw the charts, the size the text, the width of labels, everything, it has to physically create dummy HTML elements in the background of your web browser to measure them.
So what does this all mean? How do the developers hide them? Well, every web page is structured using something called the DOM, the document object model, the blueprint. It's basically the blueprint of the page, and you style that blueprint using CSS. The developers of this library wrote a CSS rule using absolute positioning. They told these dummy text elements to be placed exactly ten thousand pixels above the top of the.
It is such a wonderfully messy reality check. Yeah, which actually brings us to the most important question for you, the listener. We've spent this time talking about arrays, HTML canvases, y axis minimums, and CSS bugs. Why does knowing the JavaScript logic behind these charts actually matter to you?
Absolutely. Yeah, so we want to leave you with this thought. Tom all over today. Now that you know exactly how easily a chart's entire narrative can be radically altered just by tweaking the minimum value of a y axis in the code, or switching from comparative bars to an unreadable pie slice.
