Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One helpful way of interacting with data is with what's called slicing. Now there's a few different versions of this, but the version of slicing that we're going to be using is basically a lateral only hover. That is, you're driving a line over data points from left to right, and when you get to them, it will bring up the information that you want. It will do the interactivity at that point. Now a couple of chapters ago, we did a movie that featured some slicing and that was the one on the line plot, and what I'm doing is I'm bringing up the code on that one again, but I want to show you a little bit more about how we did the slicing, how it functioned and some of the choices that went into it.
So, let me first show you exactly what it looks like again. So I'm going to just hit the Play button and here we bring up this Georges Braque Google search data. You see I've got a cursor here. As I bring it over a couple of things happen. Most significantly, the cursor disappears. Also I have a reference line, the tall tan line that goes behind all of the red lines and that serves sort of as my slicing cursor, and then I have some information that pops up on the top. I have the month that's been depicted and the actual Google search value for that particular month.
It's a positive or negative number and I've got three decimal places. Now I want to explain a couple of choices here, number one, why I turn the cursor off. That's going to be pretty easy if I just show you what it's like with the cursor on. All I'm going to do is come back to the drawing and right here where I have noCursor, I'm just going to comment that out and run it over again, and now what you see is that the cursor really is intrusive. It takes a big amount of space and it becomes the dominant visual element. It has nothing to do with the data and so it just shouldn't do that. It just needs to be out of the way, and in fact, it turns out that since we're doing just a horizontal slicing anyhow the line source as a great cursor all on its own, so I'm going to take the cursor out again. And see, there's no ambiguity about what's being controlled and where it is and that works well.
The next thing I want to show you is that I made a point of putting the tan line behind all the other lines, because I still want the emphasis on the lines, and I also make sure that the reference line, the slicing line, was no thicker then. It's actually the same thickness as the lines and it's a much lighter color, because I want to draw the attention to the data itself and because the slicing line is moving, it's very easy to find it even if it were much lighter color. Now a couple of things that deserve some explanation; this line right here and this is the "if" loop that brings up the information, and so what I'm saying is if the line is right next to a data point than bring up the information posted on the top.
So you see for instance when I'm in between I don't have anything, but when I get close to one, the data pops up. Now originally, what might seen like a logical thing is to just say when the mouse is on the data point, but it turns out that sometimes that's what this one is. It just says if mx which is the mouse x is equal than it needs to be equal to X. If it is just equal to X, two equal signs, then run what we're looking for.
You see sometimes it's a little hard. It doesn't register. Part of that is because the red lines are not necessarily drawn on the pixels themselves. They're done with the floating point, they could have decimal calculations, and so it turns out that by making it that has to be exactly the same. It's useless, in fact I'm not getting anything. And so by introducing a little bit of wiggle room, what I did, let me comment that one back off and turn this one back on, was I said as long as it's within two pixels to either side, because I know that the bars are about 5 pixels away from each other. It's actually more like 5.47 or something like that, but as long as I'm within two pixels then it's close enough. I don't need to snap to the line, I just need the information to pop up obviously for one or the other, so I'm going to bring that back up, and now you see as long as I'm close to one, I get this information and there's enough wiggle room that that works well.
The last little detail I want to show you for the slicing here you has to do with the numbers that are coming up, that looks like a flag on a flag pole. The first one is the string for the date and that just sort of is what it is, but the one beneath it is the actual value. There are some formatting going on here and I want to take a look at the code directly beneath. What we have here is this is the code that produces that text on the flag pole. This does the dates that brings in the string variable, and places it next to the line and this brings in the actual numbers from Google. Now this function right here is the one that deserves a little bit of attention.
I'm using the processing function nfp and you can think of that as number format positive negative, and what it does is it takes a floating- point number and it turns it into a string variable, and the reasons I would want to do that is because this makes it so that the NFP version, you can remember there are several versions of NF, NFS, there is bunch of them, but the NFP makes it so that every number has either a positive or negative in front of it and if you're using a font that has numbers of uniform spacing than that lines things up very nicely, so the numbers aren't bouncing back and forth as you move across.
Also you get to specify exactly how many decimal places. In the data set, I know for instance there are some that only have two decimal places, because the third one is zero, so we include them, and again that would introduce some jumping into the numbers. That would be distracting from looking at the data itself. It would be looking as sort of these little apparitions that are coming up and let me show you what it looks like without it. It's a small difference, but it's enough I think to matter. So I'm going to just put the popularity number without the formatting and the NFP, and when I run it now, see for instance, now there is a positive one.
It just shows up and then I go to positive and there is a negative, and so those are changing around a little. Okay, that one actually does have the third decimal place. It must have included that one in the text file, but I still think that the appearance and disappearance of the positive or negative is a little bit distracting and you can also imagine situations where if you had a dataset that have seven decimal places, and some of them had one, some of them had two and some them had seven. It'll be bouncing all over the place. It would become the visually dominant aspect, the changing of the number of decimal places, so I'm going to come back here.
I'm going to comment that one off and put this one back on, save that and run it, and now you can see as we go from the positives to negative, I think it's a smoother position because the initial digits don't change their position as much. A slicer is a good way of moving through a one dimensional plane in terms of getting extra information about what you're looking for and these are some of the details that can make the slicer work a little bit better in your own visualizations.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 100996 Viewers
61 Video lessons · 87785 Viewers
71 Video lessons · 71662 Viewers
56 Video lessons · 103551 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.