Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes drawing a graph. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "drawing a graph." Watch "drawing a graph." New topic

drawing a graph.

daudi jjingo

Joined: Nov 26, 2003
Posts: 3
Hello there, iam fairly new to java and still grappling with the problem of drawing a graph. If i have say points 1, 2,3,4,5,6,7,8 for the x-axis and points 10, 20,30,40,50,60,70,80 for the y_axis, can someone please give me the code for this? I have looked through a few tutorials recommended on this platform, but still can't quite find my way around. Thanks.
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Since many people asking for help in these forums are students looking for someone to do their assignments, you probably won't have any luck asking for code. If you've already looked at the Swing Tutorial, especially the part about 2D Graphics, you'd get better results making some sort of effort and asking specific questions about the code you've written.

[How To Ask Questions On JavaRanch]
Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
i have this graph assignment from my high school just copy and paste and then run it. hope this helps
here .........
import java.awt.Graphics;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.text.*;
import java.awt.event.*;
//import java.awt.Frame;
public class LineGraph extends Frame
String col1, title, dateStamp;
int line_number;
String y0val, y1val, x0val, x1val;
String yr0, yr1;
int x0 = 55;
int x1 = 475;
int y0 = 100;
int y1 = 220;
int ystep = 0;
String ycstg;
String xcstg;
float fxmin, fxmax, fymin, fymax;
int xmin, xmax, ymin, ymax, fxprev;
String hour[] = new String[24];
String hourvar;
int hourint;
int bpint;
int day1, month1, year1;
DateFormat dateFormatter;
float avgs[] = new float[24];
//input values are here

float hrs[] = new float[24];
int daysint;

public void paint(Graphics g) {

float bp[] = {2000, 0 , 5100, 4200, 8990};

Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
dateFormatter = DateFormat.getDateInstance(DateFormat.FULL);
Color brush=new Color(240,240,255);
Font f=new Font("Helvetica",Font.PLAIN,30);
Font subf0=new Font("Helvetica",Font.BOLD,15);
Font subf1=new Font("Helvetica",Font.ITALIC,15);
g.drawString("X-axis - Date",190,400);
g.drawString("Y-axis - Number of Sites",190,420);
// write y coordinates
Font fy=new Font("Helvetica",Font.PLAIN,10);
int ycadv = 320 ;
int ycint = 0;

for (int j=0; j<=14; j++) {
ycstg = String.valueOf(ycint);
ycint = ycint + 1000;
ycadv = ycadv - 15 ;
// write x coordinates
Font fx=new Font("Helvetica",Font.PLAIN,10);
g.setFont(fx );
int xcadv = 50 ;
float fxmin = 50;
String s_month;
String s_year;
int daysint = -7;// -2 align middle
/* day1 = day1 - daysint;*/
cal.add(cal.DAY_OF_MONTH, daysint);

for (int i=0; i<=4; i++) {

if ((i + 1) <= 4) {
fymin = bp[i];
fymax = bp[i+1];
cal.add(cal.DAY_OF_MONTH, 1);
s_month = String.valueOf(month1+1);
s_year = String.valueOf(year1);
xcstg = String.valueOf(day1+1);
daysint = daysint + 1;

if ((i + 1) <= 4){
fymin = 320 - fymin * 15/1000;
fymax = 320 - fymax * 15/1000;
xcadv = xcadv + 75 ;
fxmax = xcadv;
// draw graph
/* Plot the lines */
g.drawLine((int) java.lang.Math.round(fxmin),
(int) java.lang.Math.round(fymin),
(int) java.lang.Math.round(fxmax),
(int) java.lang.Math.round(fymax));
fxmin = fxmax;

public void paintBackground(Graphics g, Color c_) {
Color c=g.getColor();
g.setColor(c); // Returns to original color

public static void main(String args[]){
Frame fr = new LineGraph();

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e){ System.exit(0); }};
fr.setSize(600, 450);;

if u can't impress people with ur intelligence , confuse them with ur bullshit
David Gilbert
Ranch Hand

Joined: Jun 01, 2002
Posts: 42
If it's not for homework, then you could use JFreeChart:
Dave Gilbert
JFreeChart Project Leader
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
Is there any user/developer guide which details how to use JFreeChart APIs. I looked around in the site but with little success.
David Gilbert
Ranch Hand

Joined: Jun 01, 2002
Posts: 42
My belief is that open source projects need funding in order to survive and prosper so although JFreeChart is completely free (GNU LGPL) you have to pay for support and documentation(*) if you want it.
I have published a free installation guide (jfreechart-0.9.15-install.pdf) to get people started, the Javadocs are online (and can be regenerated directly from the source code) and there are 100+ demos included in the distribution. So you can get by easily if you are unable or unwilling to pay for support and documentation.
It's not easy to make a living from free software, but I'm scraping by...
Dave Gilbert
JFreeChart Project Leader
(*) If you are working on an open source project that uses JFreeChart, you qualify for a free copy of the documentation. E-mail me the URL for your project and I'll send you the details.
I agree. Here's the link:
subject: drawing a graph.
jQuery in Action, 3rd edition