concept D3 in category data

This is an excerpt from Manning's book Data Wrangling with JavaScript.
Table 1.1 Reasons for using JavaScript for data wrangling
Reason Details You already know JavaScript. Why learn another language for working with data? (Assuming you already know JavaScript.) JavaScript is a capable language. It’s used to build all manner of complex applications. Exploratory coding. Using a prototyping process with live reload (discussed in chapter 5) is a powerful way to write applications using JavaScript. Strong visualization ecosystem. Python programmers often end up in JavaScript to use its many visualization libraries, including D3, possibly the most sophisticated visualization library. We’ll explore visualization in chapters 10 and 13. Generally strong ecosystem. JavaScript has one of the strongest user-driven ecosystems. Throughout the book we’ll use many third-party tools, and I encourage you to explore further to build out your own toolkit. JavaScript is everywhere. JavaScript is in the browser, on the server, on the desktop, on mobile devices, and even on embedded devices. JavaScript is easy to learn. JavaScript is renowned for being easy to get started with. Perhaps it’s hard to master, but that’s also true of any programming language. JavaScript programmers are easy to find. In case you need to hire someone, JavaScript programmers are everywhere. JavaScript is evolving. The language continues to get safer, more reliable, and more convenient. It’s refined with each successive version of the ECMAScript standard. JavaScript and JSON go hand in hand. The JSON data format, the data format of the web, evolved from JavaScript. JavaScript has built-in tools for working with JSON as do many third-party tools and libraries.
I couldn’t end this book without coverage of D3: the preeminent visualization framework for building interactive and animated browser-based visualizations in JavaScript.
D3 (data-driven documents) is a complicated library; that’s why this is called the advanced visualization chapter. D3 has a large API and sophisticated concepts. This is a powerful addition to your toolkit, but unfortunately it comes with a steep learning curve! For most routine charts you’re better off using a simpler API, such as C3, which we covered in chapter 10. When you reach the limitations of C3, however, or you want to create something completely out of the box, that’s when you’ll reach for D3.
I can’t hope to fully teach you D3; that would require a whole book. But I do hope to teach you fundamental concepts and how they relate. For example, we’ll learn one of D3’s core concepts, the data join pattern, and how to use it to translate our data to a visualization. D3 is a large and complex API, so we’ll only scratch the surface.
We can select a single element with D3’s
selectfunction. You can see in figure 13.11 that I’m representing the current D3 selection with a dotted box that has selected ourcircleelement.Figure 13.11 Use the D3
selectfunction to operate on a single existing DOM node.![]()
Assuming we already have an existing element, say our
circleelement, we can use the D3selectfunction to select our circle using a CSS-style selector:
Listing 13.9 shows the code that produces our data join. Calling
selectAllyields the empty selection. Our data set is then joined using thedatafunction. Next, note the use of theenterfunction, which filters the selection for new data records. Theenterfunction allows us to specify what happens when new data is added to our visualization. This is where we call D3’sappendfunction. When we callappendin the context of theenterselection, D3 will replace the placeholder DOM nodes with the elements that we want to add to our visualization.Listing 13.9 Doing a data join and procedurally generating space junk visuals (extract from app.js)
// ... other code here ... var spaceJunk = svgElement.selectAll("g") #1 .data(spaceJunkData); #2 var enterSelection = spaceJunk.enter(); #3 enterSelection.append("g") #4 .attr("class", "junk") #4 .attr("transform", spaceJunkTranslation) #4 .append("circle") #4 .attr("r", 5); #4 #1 Selects all g elements. Because none exist, this yields an empty selection. #2 Creates the data join #3 Creates the enter selection. This is the selection of new data records bound to placeholder DOM nodes. #4 Creates the space junk visual and replaces placeholder DOM nodes with itListing 13.9 defines the core of what I’m calling the visualization recipe. This is the instruction to D3 that says, “please take all my space junk data and create a visual representation.” If you understand this the way that I do, you can see why I like to think of it as a data pipeline. Listing 13.9 is a snippet of code that consumes data and produces a visualization.