How to animate data visualization in Processing? - processing

I'm trying to create an animated data visualization and currently only know how to do the following "static" code, which puts a string of dots in a straight line. How do I make them jump up and down? Also, if there is anyone in Dublin, Ireland, who wouldn't mind giving a few tutorial sessions to a couple of college students working on a data visualization project in Processing, we have a small budget to compensate you for your time. Thanks very much!
For now, here's the code I was talking about...
SimpleSpreadsheetManager sm;
String sUrl = "t6mq_WLV5c5uj6mUNSryBIA";
String googleUser = "USERNAME";
String googlePass = "PASSWORD";
void setup() {
//This code happens once, right when our sketch is launched
size(800,800);
background(0);
smooth();
//Ask for the list of numbers
int[] numbers = getNumbers();
fill(255,40);
noStroke();
for (int i = 0; i < numbers.length; i++) {
ellipse(numbers[i] * 8, width/2, 8,8);
};
};
void draw() {
//This code happens once every frame.
};

Essentially the x position you use for drawing the ellipse is a number value you get from external data. You need a variable that changes value. Redrawing the ellipse at the updated value should get things animated.
Take this basic example:
float x,y;//position variables for the ellipse
void setup(){
size(800,800);
smooth();
fill(255,40);
noStroke();
y = 400;
}
void draw(){
//update values
x = mouseX + (sin(frameCount * .05) * 30);//update x to an arbitrary value
//draw
background(0);//clear the screen for the new frame
ellipse(x,y,8,8);//draw the ellipse at the new position
}
the x variable is a bit like numbers[i] - just a value that chances.
Nothing special happens in draw() other than clearing the screen(by calling background()) and drawing. The example above uses an arbitrary value, but with your setup that might be something else, maybe a certain value in a data set that changes in time (like the population of a country, etc.)
Another handy thing to keep in mind is the separation between the data and visuals code wise. If values in the data set are higher than what can be displayed on screen as raw values, you can map() values so they can be viewer, add some sort of navigation controls, etc. The values analysed will not be affected by what's being displayed. In programming terms this separation between the data/model, the visuals/view (how the data is rendered) and the controls/controller is known as the Model-View-Controller pattern.
This might be a bit much for people just starting with code, but just being aware of the separation without worrying to much on the specific implementation can be helpful.
Here's a very basic example having the width of the sketch mapped to the size of the data (numbers)
SimpleSpreadsheetManager sm;
String sUrl = "t6mq_WLV5c5uj6mUNSryBIA";
String googleUser = "USERNAME";
String googlePass = "PASSWORD";
int[] numbers;//data used for visualisation
void setup() {
//sketch setup
size(800,800);
smooth();
fill(255,40);
noStroke();
//retrieve data
numbers = getNumbers();
}
void draw() {
background(0);
int numId = int(map(mouseX,0,width,0,numbers.length));//map mouse x position based on sketch width and data size
ellipse(numbers[numId] * 8, height/2, 8,8);
}

For an animation you need one or more parameters that their values changed in time, currently there is a library for processing which do such things: Ani
For more information see the site and provided examples.

Related

Processing: Using rollover with text from table

I have a CSV file that was created from a plain text file. In column A there is a list of unique words and in column B it lists their frequency within that text.
I am using Processing and loadTable to draw a list of the words. I would like to use rollover so that when the mouse hovers over them, an ellipse appears that has a size relative to the integer associated with that word's frequency.
I am having a hard time finding a good example of the syntax for using rollover() while in a loop that includes data from a CSV file.
Any help is appreciated!
void setup() {
table = loadTable("tabletest.csv", "header");
size(600,1000);
}
void draw() {
background(252, 245, 224);
for (int i = 0; i < table.getRowCount(); i++) {
TableRow row = table.getRow(i);
String w = row.getString("Word");
int f = row.getInt("Frequency");
textSize(10);
text(w, width/2, 15*i);
fill(8, 114, 105);
textAlign(CENTER);
}
}
There is no built-in rollover() function that I know of. You're going to have to write this functionality yourself.
You can check whether the cursor is inside a given rectangle. See this guide for more info, but basically you check whether mouseX is between the left and right and whether mouseY is between the top and bottom of the rectangle.
If so, then you know the mouse is inside that table cell and you can take the appropriate action. I recommend breaking your problem down into smaller steps and taking those steps on one at a time. For example, start over with a basic sketch that shows a single rectangle that changes color when the mouse is inside it. Get that working perfectly before trying to make it work with multiple rectangles.
Then if you get stuck, you can post a MCVE along with a more specific technical question. Good luck.

How to make a menu with this effect in unity3d?

Sorry, I'll try to explain better.
I want to make a sliding menu where you can select a character. I want the character that is at the center increases in size to know that this is the current character. The effect can be seen in the game of Crossy Road when you want to select a character.
Sorry but I can't upload a imagen because i am new in the forum
I think I might be able to help you without needing too much borrowed code. There are two possibilities here:
You have a perspective camera so the "selected" item can just be closer to the camera.
You have an orthographic camera so you will have to scale things.
For perspective:
List<GameObject> characters; //contains all character.
int selectedIndex = 0; //index to the selected character.
float spacing = 10; //space between chars
void Update()
{
if(Input.GetKeyDown("RightArrow"))
{
selectIndex++;
ApplyChanges();
}
if(Input.GetKeyDown("LeftArrow"))
{
selectIndex--;
ApplyChanges();
}
}
void ApllyChanges()
{
// Make sure the selected index is within range and then space the characters.
selectedIndex = selectedIndex % character.Count();
SpaceCharacters();
}
void SpaceCharacters()
{
for(int i = 0; i < characters.Count(); ++i)
{
// characters on the left will have a negative spacing and so will be placed to the left and vice versa for characters on the right.
int offset = i - selectedIndex;
characters[i].transform.position = new Vector3(offset * spacing, 0, 0);
}
// Move the selected character closer.
characters[selectedIndex].transform.position = new Vector3(0,0,spacing);
}
For orthographic camera you will need to set the select characters transform.scale to a larger vector.
This won't animate anything or look cool. This code will just snap your characters into position.
The solution I adopted was to attach objects to the transparent buttons in a scrool rect, so as to manage 3d objects with the convenient of scrool rect interface.
Here you can find the official documentation for use scrool rect: http://docs.unity3d.com/Manual/script-ScrollRect.html
Maybe my assets can serve you ;)
https://www.assetstore.unity3d.com/en/#!/content/60233

In Processing, how can I save part of the window as an image?

I am using Processing under Fedora 20, and I want to display an image of the extending tracks of objects moving across part of the screen, with each object displayed at its current position at the end of the track. To avoid having to record all the co-ordinates of the tracks, I usesave("image.png"); to save the tracks so far, then draw the objects. In the next frame I use img = loadImage("image.png"); to restore the tracks made so far, without the objects, which would still be in their previous positions.. I extend the tracks to their new positions, then usesave("image.png"); to save the extended tracks, still without the objects, ready for the next loop round. Then I draw the objects in their new positions at the end of their extended tracks. In this way successive loops show the objects advancing, with their previous positions as tracks behind them.
This has worked well in tests where the image is the whole frame, but now I need to put that display in a corner of the whole frame, and leave the rest unchanged. I expect that createImage(...) will be the answer, but I cannot find any details of how to to so.
A similar question asked here has this recommendation: "The PImage class contains a save() function that exports to file. The API should be your first stop for questions like this." Of course I've looked at that API, but I don't think it helps here, unless I have to create the image to save pixel by pixel, in which case I would expect it to slow things down a lot.
So my question is: in Processing can I save and restore just part of the frame as an image, without affecting the rest of the frame?
I have continued to research this. It seems strange to me that I can find oodles of sketch references, tutorials, and examples, that save and load the entire frame, but no easy way of saving and restoring just part of the frame as an image. I could probably do it using Pimage but that appears to require an awful lot of image. in front of everything to be drawn there.
I have got round it with a kludge: I created a mask image (see this Processing reference) the size of the whole frame. The mask is defined as grey areas representing opacity, so that white, zero opacity (0), is transparent and black, fully opaque (255) completely conceals the background image, thus:
{ size (1280,800);
background(0); // whole frame is transparent..
fill(255); // ..and..
rect(680,0,600,600); // ..smaller image area is now opaque
save("[path to sketch]/mask01.jpg");
}
void draw(){}
Then in my main code I use:
PImage img, mimg;
img = loadImage("image4.png"); // The image I want to see ..
// .. including the rest of the frame which would obscure previous work
mimg = loadImage("mask01.jpg"); // create the mask
//apply the mask, allowing previous work to show though
img.mask(mimg);
// display the masked image
image(img, 0, 0);
I will accept this as an answer if no better suggestion is made.
void setup(){
size(640, 480);
background(0);
noStroke();
fill(255);
rect(40, 150, 200, 100);
}
void draw(){
}
void mousePressed(){
PImage img =get(40, 150, 200, 100);
img.save("test.jpg");
}
Old news, but here's an answer: you can use the pixel array and math.
Let's say that this is your viewport:
You can use loadPixels(); to fill the pixels[] array with the current content of the viewport, then fish the pixels you want from this array.
In the given example, here's a way to filter the unwanted pixels:
void exportImage() {
// creating the image to the "desired size"
PImage img = createImage(600, 900, RGB);
loadPixels();
int index = 0;
for(int i=0; i<pixels.length; i++) {
// filtering the unwanted first 200 pixels on every row
// remember that the pixels[] array is 1 dimensional, so some math are unavoidable. For this simple example I use the modulo operator.
if (i % width >= 200) { // "magic numbers" are bad, remember. This is only a simplification.
img.pixels[index] = pixels[i];
index++;
}
}
img.updatePixels();
img.save("test.png");
}
It may be too late to help you, but maybe someone else will need this. Either way, have fun!

SharpGL Animation Questions

So I am writing a program that parses files with xyz points and makes a bunch of connected lines. What I am trying to do is animate each line being drawn. I have tried to use VBO's and Display Lists in order to increase performance (as I am dealing with large amount of data points i.e. 1,000,000 points) but I could not figure out how to use them in SharpGL. So the code I am using to draw right now is as follows:
private void drawInput(OpenGL gl)
{
gl.Begin(OpenGL.GL_LINE_STRIP);
for (int i = 0; i < parser.dataSet.Count; i++)
{
gl.Color((float) i, 3.0f, 0.0f);
gl.Vertex(parser.dataSet[i].X, parser.dataSet[i].Y, parser.dataSet[i].Z);
gl.Flush();
}
gl.End();
}
I know immediate mode is super noobzore5000 of me, but I can't find any SharpGL examples of VBO's or Display Lists. So know what I want to do is to 'redraw' the picture after each line is drawn. I thought when the flush method is called, it draws everything up to that point. But it still 'batches' it, and displays all the data at once, how can I animate this? I am incredibly desperate, I don't think thoroughly learning OpenGL or DirectX is practical for such a simple task.
After lots of tinkering, I chose to go with OpenTK because I did end up figuring out VBO's for SharpGL and the performance is AWFUL compared to OpenTK. I will give an answer as to how to animate in the way that I wanted.
My solution works with Immediate Mode and using VBO's. The main concept is making a member integer (animationCount) that you increase every time your paint function gets called, and paint up to that number.
Immediate Mode:
private void drawInput(OpenGL gl)
{
gl.Begin(OpenGL.GL_LINE_STRIP);
for (int i = 0; i < animationCount; i++)
{
gl.Color((float) i, 3.0f, 0.0f);
gl.Vertex(parser.dataSet[i].X, parser.dataSet[i].Y, parser.dataSet[i].Z);
}
gl.End();
animationCount++;
}
or
VBO:
private void glControl1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
GL.DrawArrays(PrimitiveType.LineStrip, 0, animationCount);
animationCount++;
}

Processing append float[]

Im working on a Processing App wich is supposed to grap data from a serial port and put in into various graphs. I've downloaded the giCentre Utilities libary to draw the graphs.
Based of one of the examples i got it to plot a simple graph, but since it will be grabbing data from the serial port in real-time i need to be able to add data. Im trying to use the Append() function, but without any luck.
import org.gicentre.utils.stat.*; // For chart classes.
float[] test = {1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990};
float[] test2 ={ 19000, 6489, 6401, 7657, 9649, 9767, 12167, 15154, 18200, 23124};
XYChart lineChart;
/** Initialises the sketch, loads data into the chart and customises its appearance.
*/
void setup()
{
size(500,200);
smooth();
noLoop();
PFont font = createFont("Helvetica",11);
textFont(font,10);
// Both x and y data set here.
lineChart = new XYChart(this);
append(test, 2050);
append(test2, 21000);
lineChart.setData(test, test2);
// Axis formatting and labels.
lineChart.showXAxis(true);
lineChart.showYAxis(true);
lineChart.setMinY(0);
lineChart.setYFormat("###");
lineChart.setXFormat("0000");
// Symbol colours
lineChart.setPointColour(color(180,50,50,100));
lineChart.setPointSize(5);
lineChart.setLineWidth(2);
}
/** Draws the chart and a title.
*/
void draw()
{
background(255);
textSize(9);
lineChart.draw(15,15,width-30,height-30);
}
Isn't the line
append(test, 2050);
append(test2, 21000);
supposed to add a new datapoint at (2050, 21000) ? It would be nice to only have to call these every time serial data comes in and then redraw the plot.
Any help or advice is much appreciated.
The append() function returns the appended array. append(test, 2050) doesn't actually change the test array, it returns an array equivalent to test with 2050 appended to it. So you should be able to do the following:
test = append(test, 2050);
test2 = append(test2, 21000);
EDIT:
Here is the documentation on the Processing Reference page: append(). And here is some code which can be run as a simple test (or learning tool):
void setup()
{
int[] a = {1, 2, 3};
int[] b;
b = append(a, 4);
print(a.length + "\n");
print(b.length);
}
which returns
3
4

Resources