# Creating And Plotting Graphs

Discussion in 'Java' started by morc, Feb 23, 2006.

1. ### morcGuest

Hey,
Does anybody know of a tutrial or soemthing i can read that would teach
me how to make a graph and to plot it as well? Simple charts will do
for know but eventualy I need to make stock market related charts.

And please don't refer me to JFreeChart i know its good but it doesn't
help me. I would like to be able to make one on my own. So if anyone
would like to teach me or point me to a tutorial it would be greatly
appreciated.

thanks
-morc

morc, Feb 23, 2006

2. ### RhinoGuest

"morc" <> wrote in message
news:...
> Hey,
> Does anybody know of a tutrial or soemthing i can read that would teach
> me how to make a graph and to plot it as well? Simple charts will do
> for know but eventualy I need to make stock market related charts.
>
> And please don't refer me to JFreeChart i know its good but it doesn't
> help me. I would like to be able to make one on my own. So if anyone
> would like to teach me or point me to a tutorial it would be greatly
> appreciated.
>

I think you'll find drawing graphs is pretty simple. A very basic example of
a graph can be seen at
http://java.sun.com/applets/jdk/1.4/demo/applets/SimpleGraph/example1.html.
If you look at the source code (linked below the applet), you'll see that it
is only a very small number of lines of code to draw this sine wave. If you
want to add axes to the graph, that is basically a matter of drawing two
more straight lines via the g.drawLine() method.

If you want to put tick marks on the axes and label the marks, that's a bit
more work but still no big deal: you simply compute the length of the axis
and the range of values you want to show on the axis, then determine where
the tick marks need to be to represent the desired values. For instance, if
the X axis represents 100 units and is 8 cm long and you want tick marks for
the values 25, 50, 75 and 100, you simply put them at 2 cm, 4 cm, 6 cm, and
8 cm from the origin and label appropriately via drawString().

Naturally, you can add as many bells and whistles as you like, such as the
ability to zoom in or out on the graph, show multiple curves, etc. etc.

I think you'll find everything you need in the Graphics and/or Graphics2D
classes in the Java API. Assuming you are using Swing, simply paint the
various lines, curves, and Strings on a JPanel and you should be off to the
races.

--
Rhino

Rhino, Feb 23, 2006

3. ### IchBinGuest

morc wrote:
> Hey,
> Does anybody know of a tutrial or soemthing i can read that would teach
> me how to make a graph and to plot it as well? Simple charts will do
> for know but eventualy I need to make stock market related charts.
>
> And please don't refer me to JFreeChart i know its good but it doesn't
> help me. I would like to be able to make one on my own. So if anyone
> would like to teach me or point me to a tutorial it would be greatly
> appreciated.
>
> thanks
> -morc
>

There is a demo under your JDK install at
...\Java\jdk1.5.0_06\demo\applets\SimpleGraph

So
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

Examples can be found here in the "The Java Developers Almanac 1.4"

http://java.ittoolbox.com/topics/t.asp?t=440&p=440&h1=440

Google is an amazing tool when used..

--

IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

IchBin, Feb 23, 2006