Join Barton Poulson for an in-depth discussion in this video Working with appended text data, part of Interactive Data Visualization with Processing.
- View Offline
One very common form of data in most visualizations is the use of text data, and in Processing the easiest way to use text data is not by entering it into the sketch itself, but by referring to an external text file. And what I want to show you in this movie is how to access that text and be able to integrate it into a Processing sketch. And begin by just putting a little comment here with the name. And the most important thing I need to do is I need to help Processing locate the file.
The first thing I am going to do is I am going to go up to the folder that has the file. In this particular case, it's called presidents and it's in the Exercise folder, but it's not in the sketch folder. What I'm going to do is I am going to grab that and drag it into the Processing window, and now you see down at the bottom it says One file added to the sketch. In fact, if we double-click on the folder, we now see that we have the data folder, and it's got the presidents file in it. What I need to do now is I just need to create my window that I am going to be dealing with, and I need to load the file.
The way I do that is as an array of strings. And what happens is Processing looks at each line as a separate string of text, and so the document is a collection of lines; it's a collection of strings. So do String and then the open and closed square brackets to indicate that it's an array. I'll simply call this one presidents. And then the command we want to use is loadStrings. And then in parentheses and in quotations, we put the name of the file and then close that off.
And then the easiest way to check that we have this, if the file isn't too long, is just do a print line, which I'll do right now. And I have an empty display window pop up, but you see that the contents of the file have printed down here below. Now, if I want to put some of this text into a sketch, the easy way to do it is by calling on the string by its index number and placing it within the sketch itself. Before I do that, I'm going to do just a few of the parameters of the sketch. I'm going to put a background color on. In fact, I want to bring in a color palette, which I'll do by going back to here, and I am going to use this one number, 2, crowds. Go back to the sketch and place that at the top.
Then I'll just use the first color in the palette. That's the background color. Next, what I want to do is I am going to want to use a fill color for the text, so I'll do fill. That'll also be palette. I'll just use index number 1 for that one. And then I can start placing the text. So in this case, I just use the text function, and I type in "presidents" and then the line that I want.
So for instance, let's say I want to put Jimmy Carter in there. He is on index 40 in the array, and then I need to put where it gets positioned. Let's go 50 pixels in and 50 pixels down. When I press run on this one, you could see that it shows up. It's a very light font. We've shown before that we have the ability to change fonts by creating a font within Processing; in fact, I'll do that right now. Simply go to Tools, to the first one, Create Font. And all I need here is, oh, that's a good a font as any, so Aharoni-Bold 48.
I'll copy that text right there, press OK, and this will add the font into the data folder. Now, there's a number of variables I need to add to make it use the font, so what I am going to do is I am going to actually comment that one out. I've got this fill here, so I've got the background. I am going to do PFont. That's going to declare a font. I am just going to call it font. And then I need to load into that variable.
I use loadFont. And then in the parentheses and quotes, I put the name of the font. vlw is going to be its extension. And then I say that I am going to use that font for this particular text, textFont (font). And let's just change the color of the font, because we can. I'll go to the next item in the array. I'm going to save that and run it. And now you see we have a much larger version of the name that has gone in there.
Now I have some other options, in terms of placing these. One of the more interesting ones can be placing them in a dynamic nature. I need to make this a dynamic drawing to do that, though. All I need to do is come up here and type in "void setup" and place this text in curly brackets. I should probably put the font information up there as well. There we go. And then down here, I can put this in void draw.
And then if all goes well, I can get the text actually follow the mouse around. I'm going to save that. And I see I need to low the presidents down here, it appears. there we go. And now I can get the information to follow me around as I do it. I could even make it so that it brought in each of the presidents, about one every second as I went through, and I get a sort of a shuffling- through-history effect if we wanted to do that as well.
But mostly I want to let you know that in terms of working with text, the easiest way is to bring it in as a text file and then you set up Processing so that it reads each line of text as a string and the entire file as an array of strings.
- Exploring the need for creative data visualization
- Drawing basic lines and shapes
- Introducing variables, strings, and arrays
- Modifying drawing attributes such as color
- Making drawings more dynamic with animation loops and spirals
- Creating keyboard- and mouse-based interactions
- Adding images, video, and sound
- Reading in text or XML data
- Creating plots and charts
- Publishing and sharing your work