wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to plot a graph in Java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to plot a graph in Java ?" Watch "How to plot a graph in Java ?" New topic
Author

How to plot a graph in Java ?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Ok I think this question falls into Advanced java because information is impossible to obtain on it and all the othet cataegories could not answer it.
So does anyone out there know how to plot a simple 2D graph for an Application ( not an APPLET). Lets assume that you have simply 10 X and Y cordinates...
How do you do this.. I've tried everywhere to get info on this.. even other forums ...
Any info would be greatly appreciated... Thanks for even looking at it... yours respectufully Avin Sinanan


yours respectfully<br />Avin Sinanan
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
If you know how to display graphics, I'd assume you could also write a method to analyze a set of data and display a graph of the data any way you'd like.
So, is your question: "How do I display graphics in an application?"
Please clarify.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hi Dirk,
Well to tell you the truth I don't know that much about graphics.. all I know is how to draw a line or paint a JPanel background. So now that you mention it I guess I am asking a graphics question to.
Ok so i assume you know how to write a simple program of how to analyze data and display it in a graphical form.
Am suppose to write an application to simulate a celluar network and plot the amount of bytes on the network overa period of time.
So I guess my newtork will be generating a certain amount of integer byte values at a certain time and I am to plot these interger values a then generate a graph with it. I am also to generate (its optional to the user) a best fit curve to fit the plot. Am also to single out the maximum value and minimum value in plot.
So I guess thats my question. I have like a week to do this. An am kinda new to java. I didn't ask for the project it was knida dropped on to me.
So could you give me some help with this please. Do you have any code on this. Or could you tutor me over the forum for a few days please. I promise I'll work hard. I really need to know how to do this.
Thanks for responding .. Sorry if I was a bother.... yours respectufully Avin Sinanan
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Do you need to create a graphing engine, or are you just trying to create a graph in java of some data?
Depending on the complexity of what you need, creating a graphing program will take you anywhere from 3 to 12 months. It's certainly not something you can just whip out in a week, no matter how hard you study :roll:
However, there are quite a few 3rd party java graphing tools. Check out Perspective for Java from Three D Graphics:
http://www.threedgraphics.com/download.html
Good Luck!


Rob
SCJP 1.4
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hey Rob Ross, I don't think that the complexity is that great. AM not looking for any kinda of facny 3D grpahs ( but thanks anyway).
All I require my graph to do is plot the X and Y cordonate values given to it. Where X is time and Y is the amount of bytes.
And then it is to point out the maximum Y value and the corresonding X value. It is to do the same for the minimum value.
Can you help me with this?
Thanks for all teh help..
Yours respectufully Avin Sinanan
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
As this is in the advanced forum, I'm not sure that I'm understanding the level of your question. If I am understanding it correctly, it would seem to be rather basic.
It sounds like what you might want to do is read up on the AWT or Swing - just the basics of how displaying graphics works. Then create your display of whatever dimension you'd like and create an algorithm to scale your data to the display.
So, if you're using a display of 500 pixels in height and you're data ranges from 0.0 to 6.0, your scaling algorithm might be

yCoordinate = 500 - (int)(yDataValue * 500 / 6.0)

There are a number of things to consider when displaying data - things that are specific to your data set. I don't know your data set. You are going to have to figure out the details of it's display.
With regards to analyzing the data, are you having trouble writing algorithms to do this as well? It would seem that you had a question about how to find the greatest value in a set of data. If this is the case and your data is in an array, something like this could do the job:

Well, good luck.
-Dirk Schreckmann
[ February 19, 2002: Message edited by: Dirk Schreckmann ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Try this:

(Code tags added by sean)
This takes a very naive approach but you should get the general idea from the code and from the JAVA 2 API docs.
[ February 20, 2002: Message edited by: Sean MacLean ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
To Dirk and Morris -
Thanks for the help guys. I used the info you all gave me and I got my first application graph running. Thanks a million.
I have another question though. I've included what I've done so far.
So far my graph is very simple. I just plots x and y cordinates. It just draws lines connecting the cordinates. However have you all ever used Microsoft EXCEL? Well if you all do then you may notive then when you are plotting a graph in microsft EXCEL you can plot a simple graph just joing all the cordinates ( Which is what I have now ) of you can obtain the best exponential fit or even a best ploynimial fit or even a RMS fit for a line graph. Can you assist me in expanding my code to do this please.? I also got piece of the code of the internet.
Any help would be greatly appreciated. And thanks again guys.. Here is the code of what I did so far ->

(code tags added by sean)
[ February 20, 2002: Message edited by: Sean MacLean ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
How about using the [ code ] and [ /code ] tags?
Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Hello Dirk,
I got the best linear fit to work by using Regression equations found in my maths book. Are you good at maths cause am wondering if you know where I can find the formulee for best exponetial fit and polynoimail fit and RMS fit?
Thnaks
Yours respectfully Avin Sinanan
[ February 19, 2002: Message edited by: Avin Sinanan ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
No promises, but if I run across something I'll post a link to it for you. Don't hold your breath.
Good Luck,
-Dirk Schreckmann
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
It occurred to me that when you were asking about how to plot a graph in Java, you might have been asking just that very literal question.
This might not have been what you were getting at, though...
To plot a simple graph, maybe y = sin(x), there are no doubt libraries available somewhere to make the process easy, but "by hand" one method would be to break it up into little lines and draw all of the lines. So, if you were plotting the graph from x = 0 to x = 10 you might evaluate it every tenth of a unit and draw a line from evaluated-point to evaluated-point.
To make some sense of this...
If you were using a display area of 300 pixels wide, you'd have 30 pixels horizontally for every x-unit on your graph and 3 pixels horizontally for every tenth of a x-unit. Then just evaluate the graph at x = 0 and x = 0.1 and connect the points with a line. Then evaluate at x = 0.2 and connect that point with the evaluated point from x = 0.1 and so on until x = 10 (I'd recommend a loop - unless you just like typing a lot).
Whatever you do finally put together, with the data analysis, equation fitting, and discrete event handling, I'm certain would be interesting and I'd appreciate some code to ponder.
Good Luck,
-Dirk Schreckmann
[ February 25, 2002: Message edited by: Dirk Schreckmann ]
[ February 25, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
One more thing to mention...
A nice method of the Graphics class is the translate(int x, int y) method. You might want to read up on it in the API Specification.
[ February 25, 2002: Message edited by: Dirk Schreckmann ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to plot a graph in Java ?
 
Similar Threads
Socket communication
Graph Plotting using java under linux?
Graph Plotting
Graphs in Java Standalone Project
please critique the code