We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Charting Library - New to Processing, Components
Page Index Toggle Pages: 1
Charting Library - New to Processing, Components (Read 717 times)
Charting Library - New to Processing, Components
Jan 15th, 2009, 1:08am
 
So, I'm trying to create a data analysis tool and one of the things I needed was a rather unoriginal line chart.  Because most of my data is coming from Java, I wrote the chart code in Java.  The code is here :
http://pastebin.com/m1fa2d835

It'll only last a month, but hopefully I'll have this cleaned up and publicly available before then.

The meat of my question is, what's the best way to create reusable components in Processing?  I was looking for something like a "sub-canvas" on which I could paint my chart, and then move that around inside of Processing, but I didn't find an easy way to do that.

I tried, using PGraphics, to draw the chart and then place it, but the placement didn't seem to work (The PGraphics API page lists apparently defunct constructors?  Specifically one that takes an PApplet).  I don't know enough about PGraphics, but this doesn't seem to be a good fit.

Anyway, looking at other code, people appear to do most of the shifting/translation on a single canvas (http://www.openprocessing.org/visuals/?visualID=431), but that would make my code more complicated (I think, if I'm wrong, I'd love to hear it) as a second round of translation would screw up the original chart.

Any help is appreciated.  I hope I'm explaining this well; if not, I'll clarify.
Re: Charting Library - New to Processing, Componen
Reply #1 - Jan 15th, 2009, 1:25am
 
Actually, I think I figured this out shortly after my post.

Essentially, what I changed was to add placement/size variables to the constructor, then translate first to those coords, draw, and the retranslate back from the initial translation.  This should leave the canvas in the original state, while allowing the chart a small piece of the world in which to work.

I'll probably need to do the same thing with scale.

Is it SOP to use translate/scale to do work of a sub-component then reset them after use?
Re: Charting Library - New to Processing, Componen
Reply #2 - Jan 15th, 2009, 2:39am
 
I'm not sure what the exact problem is, but you may want to use push/popMatrix instead of translating back and forth.

I recall there was a sizable chunk of the book dedicated to charts on Fry's Visualizing Data
Re: Charting Library - New to Processing, Componen
Reply #3 - Jan 15th, 2009, 2:43am
 
Thanks, I'll check that out.  I haven't been able to find a PDF version of the book to buy, and I haven't been to the bookstore in ages.

The main problem is with using scale/translate and not undoing that work.  If I use scale/translate to simply the drawing process without resetting the scale/translation, the 2nd chart ends up pre-scaled/pre-translated.  Hence the unrolling of those operations after printing the chart.
Re: Charting Library - New to Processing, Componen
Reply #4 - Jan 15th, 2009, 2:53am
 
well, what I meant is:

Code:

pushMatrix();
scale(2);
translate(5, 6);
chartobject1.draw();
popMatrix();

pushMatrix();
scale(5);
translate(2, 1);
chartobject2.draw();
popMatrix();



the second object drawn is not influenced by the scaling and translation of the first object.


Maybe I'm not getting the problem at hand.  Maybe I need sleep Smiley
Re: Charting Library - New to Processing, Componen
Reply #5 - Jan 15th, 2009, 2:59am
 
Heh, I haven't tried that yet, so I'll see more about it in a sec.

Hopefully a quick explanation of my though process will make this more clear :
* I need to chart data, I have a series of points
* I can't just print line(Ax, Ay, A(x+1), A(y+1)0 because the Y points are inverted.  Also, this isn't useful because it eats up the whole canvas, and goes outside the bounds of the canvas
* I want to simply chart the data and then scale it back into a particular area in my application
* I "translate" to the area on the canvas where I want my chart
* I scale the canvas so that all my data points fit within the area I've selected on the canvas
* I chart the points literally, just line(prevX, prevY, x, y)

At this point, any further operations by any other code are going to write directly on top of my graph, and generally will produce unexpected behavior because what were absolute operations for my canvas (say, rect(0,0,200,200)) are now being translated/scaled on top of my chart.

Basically, after I write my chart, using translate/scale, all operations are still using that same context (till draw() is called again, of course), and are wonky.

Did I explain that better?  If matrix will do that better, I'll try that.
Re: Charting Library - New to Processing, Componen
Reply #6 - Jan 15th, 2009, 3:11am
 
Well, several people pointed out how to invert Y axis, which is:

Code:

scale(1,-1,1);


Also note that you can nest push/popMatrix, so you can move the entire thing like:

Code:

pushMatrix();

translate(chartPosX, chartPosY);


scale(1, -1);

scale(chartScale);

pushMatrix();
scale(2);
translate(5, 6);
chartobject1.draw();
popMatrix();

pushMatrix();
scale(5);
translate(2, 1);
chartobject2.draw();
popMatrix();


popMatrix();


I usually use curlybrace to keep track of nested push/popMatrix operations.
Re: Charting Library - New to Processing, Componen
Reply #7 - Jan 15th, 2009, 3:14am
 
Yes, I'm using that trick to plot my points (hence plotting "normally" and then scaling to "right" the points visually).

I'll have to look into the matrix functions then.

EDIT: Having looked at Matrix, it's not clear how it interacts with scale().  Is that also reset via push/pop?  It looks that way via your example.

Actually, this works exactly right!  Thanks!

I'll use this for any future work I do.
Re: Charting Library - New to Processing, Componen
Reply #8 - Jan 15th, 2009, 3:37am
 
Yup, they work on scaling, rotation, and translation.  I was wondering what matrix was when I started using Processing (the naming is not too specific on what they do), now I can't code without it Smiley

Good luck with your project!
Page Index Toggle Pages: 1