Closed Thread
Results 1 to 3 of 3

Thread: Making Scatter Plot Graphs in Java

  1. #1
    Snuffles is offline Newbie
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Making Scatter Plot Graphs in Java

    Hello CodeCall,

    I'm trying to find a way to make a simple scatter plot in Java. I've Googled around and found lots of things like jgraph and jchart which all claim to be able to do what I want, but the stuff they are doing is a bit too far over my head for me to be able to use them.

    I was hoping someone could give me a starter code for one of these to make my aforementioned scatter plot. I have the data i want to graph saved in an ArrayList, and I just want to graph the ArrayList index on the x and the value on the y.

    I searched the forums for this but didn't find anything that quite got me what I needed.

    Anyways, thanks in advance.

    Regards,
    ~Snuffles

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Making Scatter Plot Graphs in Java

    It sounds like you just need a drawing space and to plot some points on it. (no, I don't know the details of how to do that... yet)
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Snuffles is offline Newbie
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Re: Making Scatter Plot Graphs in Java

    Ok I actually found something for it, from more intense googling.

    heres the code if you want it, comes with axis lines and labels (made by myself i might add, jsut the getting it on a graph part was taken from somewhere else)
    Code:
    import javax.swing.*;
    import java.awt.geom.*;
    import java.awt.Graphics;
    import java.util.*;
    
    public class Scatterplot extends JFrame {
    
        private List points = new ArrayList();
    
        public Scatterplot() {
            super("Scatterplot");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //adding points
            for(int a = 0; a < c.myList.size(); a++)
            {
                points.add(new Point2D.Float(a, c.myList.get(a).getSteps()));
            }
            //end adding points
            
            
            JPanel panel = new JPanel() { 
                public void paintComponent(Graphics g) {
                    for(Iterator i=points.iterator(); i.hasNext(); ) {
                        Point2D.Float pt = (Point2D.Float)i.next();
                        g.drawString("*", (int)(pt.x)+40, 
                            (int)(-pt.y+getHeight())- 40);
                    }
                    int width = getWidth();
                    int height = getHeight();
                    setVisible(true);
                    //axises (axes?)
                    g.drawLine(0, height - 40, width, height-40);
                    g.drawLine(40, height - 270, 40, height);
                    
                    //y-axis labels below
                    for (int a = 1; a < 5; a++)
                    {
                        String temp = 20*a + "--";
                        g.drawString(temp, 20, height - (36 + 20*(a)));
                    }
                    for (int a = 5; a < 11; a++)
                    {
                        String temp = 20*a + "--";
                        g.drawString(temp, 11, height - (36 + 20*(a)));
                    }
                    //y-axis labels above
                    
                    //x-axis labels below
                    for (int a = 1; a < 21; a++)
                    {
                        g.drawString("|", 40 + 50*a, height - 30);
                        int x = 50*a;
                        String temp = x + " ";
                        g.drawString(temp, 30 + 50*a, height - 18);
                    }
                    g.drawString("The Collatz Conjecture: Number vs. Stopping Time", 400, 60);
                    //x-axis labels above
    
                }
            };
    
            setContentPane(panel);
            //last two numbers below change the initial size of the graph.
            setBounds(20, 20, 1100, 400);
            setVisible(true);       
        }
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java:Tutorial - Making A Window
    By John in forum Java Tutorials
    Replies: 7
    Last Post: 08-31-2011, 04:26 PM
  2. Plot data
    By nedsana in forum General Programming
    Replies: 4
    Last Post: 08-12-2010, 08:03 AM
  3. Need help w making graphs in PHP using JPGraph
    By Darkone85 in forum PHP Development
    Replies: 0
    Last Post: 05-18-2010, 03:18 AM
  4. Line Graphs in java script
    By Diana86 in forum JavaScript and CSS
    Replies: 4
    Last Post: 02-01-2009, 06:19 PM
  5. Making images with Java
    By Sionofdarkness in forum Java Help
    Replies: 4
    Last Post: 08-23-2006, 05:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts