programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Scott Selikoff
Bartenders:
• Piet Souris
• Jj Roberts
• fred rosenberger

# drawing a graph.

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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.

Bartender
Posts: 9626
16
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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);
g.setColor(Color.black);
Color brush=new Color(240,240,255);
paintBackground(g,brush);
g.drawRect(x0,y0,x1,y1);
Font f=new Font("Helvetica",Font.PLAIN,30);
g.setFont(f);
g.drawString("",190,70);
Font subf0=new Font("Helvetica",Font.BOLD,15);
g.setFont(subf0);
g.drawString(dateFormatter.format(date),220,95);
Font subf1=new Font("Helvetica",Font.ITALIC,15);
g.setFont(subf1);
g.setColor(Color.black);
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);
g.setFont(fy);
int ycint = 0;

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

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

if ((i + 1) <= 4) {
fymin = bp[i];
fymax = bp[i+1];
}
day1=cal.get(cal.DAY_OF_MONTH);
month1=cal.get(cal.MONTH);
year1=cal.get(cal.YEAR);
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;
// 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_);
g.fillRect(x0,y0,x1,y1);
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);
fr.show();
}
}

Ranch Hand
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:
If it's not for homework, then you could use JFreeChart:
http://www.jfree.org/jfreechart/index.html
Regards,
Dave Gilbert

Ranch Hand
Posts: 61
• Number of slices to send:
Optional 'thank-you' note:
David,
Is there any user/developer guide which details how to use JFreeChart APIs. I looked around in the site but with little success.
Thanks
Ashish

David Gilbert
Ranch Hand
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:
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...
Regards,
Dave Gilbert