Data Visualization for React Developers

Breaking Down D3 API

Shirley Wu

Shirley Wu

Data Sketches
Data Visualization for React Developers

Check out a free preview of the full Data Visualization for React Developers course

The "Breaking Down D3 API" Lesson is part of the full, Data Visualization for React Developers course featured in this preview video. Here's what you'd learn in this lesson:

Shirley reviews the data preparation, and the layout calculation portions of D3 API. In particular, why D3 is so powerful, tools to interact with the D3 Documentation, and several D3 libraries are introduced.


Transcript from the "Breaking Down D3 API" Lesson

>> Shirley Wu: I want to show you the D3 API again. I want to break it down so that you know which parts of the D3 API might be relevant to you, and which parts you can replace it with other things. So this is kind of what we were talking about before.

These are some of the, I call it the, so the first section, this first column, I call it data preparation. And this is how I think of like all of the utility kind of like utility functions, where you could just use native JavaScript or low dash functions. This is like the D3 dot map.

I'm sorry, this is like the map functions, filter, reduce Is I think in a lot of these array ones, I think in the transformations. The D3 min and max and extend is under statistics. So each of the bolded things are D3 modules. So, this is how you can find it in the documentation.

So like for D3 coords for example, you can judy say D3 dash coord. And that's how you find the documentation for it.
>> Shirley Wu: And then, each of these are subsections within that module. So each of these little bolded sections are modules of D3. And so these two sections, I mostly ignore because I end up just using native array functions or lodash but it really depends on what you feel comfortable with.

This and the next part are what I think is really the most powerful things about D3 and what makes it a unique selling point of D3 for me. These are functions that are helpful for getting the data ready for D3's scale/shape/layout functions. So you can see like this coord you pass in raw data to this code, function and then that will return the data that is necessary for this code, will then function that will then give you the path.

String, and similar things for a nessen hierarchy will get you data that you can then pass into the hierarchy section to get you back x-y positions. And pies are really helpful for getting like this one, if you pass in an array of numbers, it will give you back the start an angle.

Radius, et cetera for the arcs. Which is what we just used. So, this, and I mispoke earlier, so this is really the unique selling point of d3 for me. Because this is where d3 will help turn your raw data into what SVG needs to draw. This is how you can get XY positions.

This is how you can get path strings, et cetera. That otherwise, would be really hard for you to do it yourself, or it would just take a lot of calculations to do it for yourself. And I kind of want to show you some of the things that are available.

So, this whole time we've been using D3 Shape and D3 Shape will help you draw pie charts, so that's using arcs. It will help you create line charts.
>> Shirley Wu: Area charts if you want to do anything stacked.
>> Shirley Wu: And then, we'll also help you do a stacked bar chart and this stream graph thing, which I never fully understood as a visualization.

I think this is another one of those like very controversial visualizations where some people are like, this is great! And then there's other people like me that are like I don't get it. But it's possible with D3. And for each one of these, the way that you read the documentation is it will always, always tell you what it expects as an input.

So for example it's saying, this one is telling you D3 expects the data to be in this format and then it expects you to give this as kind of the settings so this one in particular. Saying like to create stack bar chart, you need to give me this kind of data.

And then, you need to tell me what the keys are to make the stacks out of each of the levels of stacks. And then, it will basically then give you this array. Of arrays, and this is what then you plug in to an xScale or yScale to figure out how to draw this stack.

So that's how you can go through the documentation. If you get for work handed an area chart, you come over here, find the area chart. And then figure out what it expects for it's input, and then what kind of settings you need to give it, the default settings you need to give it.

And then it will tell you what it returns somewhere. Somewhere here will tell you what it returns, or you can console log what it returns. So that's one of them. This helps you calculate histograms. So this is chord diagram, this one is for showing flows, source to target flow.

And so for this one I think you can just take a look, this is how it expects the input. And then it will give you as output these things that you can then plug into this ribbon function. So this is how you expect its input, this is how you expects its output.

And so the documentation follows this sort of pattern for every single thing that it does. So this one in particular is heirarchy. This one is the one that helps you draw trees. So draws trees, draws these tree maps, and this is kinda showing hierarchy. It's basically I think categorical hierarchy and it's showing you everything here is a child of this.

And then I think it shows you the size. It's laid out based on the size of that particular child relative to everything else. And then this partition, I'm in a circle packs.
>> Speaker 2: So when you're reading through the documentation and you see some code and you just want to run it to see how it works, is there a website or a sandbox or something that you can-

>> Shirley Wu: Again, a great question. [LAUGH] I wish I was smart enough to plant these questions. So a lot of times, if you just click on the picture, it will take you to, so this is kind of the example. So before Mike Bostock, the creator of D3 went and made observable networks, he actually hosted a lot of his examples on blocks, B,L and this is where like a lot of the examples and code.

And then what you can do is going here and then instead of I'm replace that with And this is something that Ian Johnson, who's one of the primary organizers of the D3 user group built. So if you just replace the part with, then it will pop you into this online editor where you can kinda play around with the code.

Like, maybe if I was like, what does this do? And I just delete this whole thing. Look, it live updates to show you I've deleted all the rectangles. So that's what I would recommend. So, like for each of these. Usually if you click on the picture sometimes it's a little disappointing and it just takes you to the picture itself, but most of the time it actually takes you to the code.

And, you just replace a blocks part with and it takes you to a live editor. I think these days. A lot of the most popular examples I think are being replaced and redone in Observable. So I think if you go to observable dot, so it's, but the notebooks that we've been working in and if you just look for Any of the modules that you want to try, like the cord one earlier.

So somebody will have done an example with it. So, look at that. So I highly recommend you checking out. Any of these. So either on Observable search whatever you're looking for and go to someone's notebook and then you can actually like this. As you know, because we've been using it, we can live edit and see what happens.

Or in block builder. I think this one I'm really a fan of This one let's you search by the API function so then I can do d3.chord. And this will have basically have every single block ever that uses D3.cord and then you can edit those. So, when you're trying to find that layout to implement whichever design.

Yeah, just try clicking on the image and then if not, try going to observable HQ and try and find it. In one of the examples, or Block-Build our search, quite a few options, yeah.
>> Speaker 3: You said that d3 is going to be the tool to put the data in the format it needs to.

How does it get the data? Can it go to a database?
>> Shirley Wu: No. Okay, so that's the part it doesn't do. It does have a little bit of functionality for helping you with requests. So it will have things like It helps you to get request, I think post request?

Json, it has one like D3.json so helps you like fetch json files, csv files, etcetera but it doesn't ever try to talk to any database that you need something else too. D3 is really for once you've gotten the data. From wherever your data source is then translating that into what you need to draw it onto the screen.

>> Speaker 3: So it's always like a flat file type thing? I'm not clear when you say already gotten the data, is it like XML string? What does it look like?
>> Shirley Wu: So, usually, you want to get whatever your data is, wherever it's from, to I think for most of the calculations, most of the D3 functions, it would want it in a JSON format.

Yeah. And then, so just arrays and objects. And then, you can go from there to do whatever you need with D3. Yeah. That's a great question also. D3 contour. This is one of the new things that got added in in Version 5.
>> Shirley Wu: I think it helps with, this says density contour, I haven't really quite read into exactly what it's for, but it's one of the options.

And the force layout, that helps you layout network diagrams or bee swarm plots, sometimes physics simulations. And then, I'm of course, D3 geo, that helps you with mapping. And it has all of these different projections that you can choose from. And unfortunately I think most people go with mercader.

I'm glad isn't even the first thing on here. And then this is the U.S.A., so a lot of different projections to get geodata onto the screen. And this is how we do that we saw earlier. Now there's directions and write ups on how to get geodata onto the screen too.

So yeah, so that's the, this whole part of the layout calculations. And this is where, what I use a lot for getting data into what SVG needs to draw and this to me is, personally I think the best part of D3.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now