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
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)
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); } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks