Click to See Complete Forum and Search --> : Java Graphs


PSLoh
02-24-2005, 07:51 PM
Hi All, I need help in getting a logic for drawing a graph using Java Graphics.

The height and width of the graph panel and the actual bar chart need to be in ratio right? How do achieve this?

How to use user input to draw a graph, since a Graphics object is used?

Sorry to ask all this, as I am really not familiar with JAVA. If there are any examples out there, please let me know.

Thanks in advance.

buntine
02-24-2005, 11:50 PM
There are a few open source solutions:

- www.jgraph.com
- http://openjgraph.sourceforge.net/

Regards.

PSLoh
02-25-2005, 12:05 AM
Thanks man, but I think my level of JAVA is tool low to comprehend the codes. Are there any suggested topics that I should read up first to ease my understandng?

buntine
02-25-2005, 12:11 AM
Do you have knowledge of the fundamental concepts of Java and Object Oriented (OO) Programming? These will be very important for any Java project.

Buying a Java book is the best way, but here are some links:

- http://java.sun.com/docs/books/tutorial/
- http://java.sun.com
- http://javaboutique.internet.com/
- http://java.sun.com/docs/books/tutorial/java/concepts/
- http://www.well.com/user/ritchie/oo.html

Regards.

PSLoh
03-06-2005, 07:07 PM
Hi,

Thanks for the references provided...I have came up with a simple one, with some online examples...However, i cannot get the graph to be displayed on the content panel...can u pls help me take a look??

I don't seem to be able to place the graph onto the displayGUI class such that it can be displayed. Here are the extract of codes relevant to the 2 classes. ( I wish to place the predefined graph in Graph1.java onto the displayGUI.java file...)

Thanks in advance.

// displayGUI.java file


// Graph Panel

Border graphBorder;

JPanel graphPanel = new JPanel(new GridLayout(30,1,5,5));

Graph1 graphP = new Graph1();
graphPanel.setLayout(null);
graphP.setSize(50, 50);
graphP.setLocation(50, 70);
graphPanel.add(graphP);
//setVisible(true);

// this border creates an embedded effect
graphBorder = BorderFactory.createLoweredBevelBorder();
TitledBorder title;
title = BorderFactory.createTitledBorder(graphBorder, "Graph of Comparison");

title.setTitlePosition(TitledBorder.ABOVE_TOP); // sets title above graph space

graphPanel.setBorder(title);

//Set graph panel to Center (middle) of Bn_Graph_Advice_Panel
Bn_Graph_Advice_Panel.add(graphPanel, BorderLayout.CENTER);


//And the Graph1.java looks sth like this:


import java.awt.*;
import javax.swing.*;

public class Graph1 extends JPanel
{
public static final int WIDTH = 800, HEIGHT = 500;
private final int NSELLERS = 5;
private final int MAXSALES = 6000; //max quarter sales amount
private final double SCALE = 0.75; //percentage of height for max sales

private double quarter1[] = {1099.85, 2105.86, 3105.25, 987.20, 5000.45};
private double quarter2[] = {2199.85, 3105.86, 2805.25, 1500.20, 6250.95};

public Graph1()
{
setSize(WIDTH, HEIGHT);
}

public void paintComponent(Graphics g)
{
Dimension size = this.getSize();
g.setColor(getBackground());
g.fillRect(0, 0, size.width, size.height); //Clear the panel
g.translate(size.height / 20, size.height - size.height / 20);
drawBarChart(g);
int barWidth = size.width / 40;
} //paintComponent()

public void drawBarChart(Graphics g)
{
Dimension size = this.getSize();
int maxBar = (int)(SCALE * size.height); //size of tallest bar in histogram
int barWidth = size.width / 40; //width of bars
int hGap = size.width / 60; //gaps between bars
g.drawLine(0, 0, NSELLERS * (2 * barWidth + hGap), 0); //draw x-axis

int href = 0;
for (int k = 0; k < NSELLERS; k++) //for each seller
{
int hgt = (int)(maxBar * quarter1[k] / MAXSALES); //height of quarter1 sales
g.setColor(Color.blue);
g.drawString(k + 1 + "", href + 5, 13); //label the chart
g.fillRect(href, -hgt, barWidth, hgt);
hgt = (int)(maxBar * quarter2[k] / MAXSALES); //height of quarter2 sales
href += barWidth; //move reference point
g.setColor(Color.red); //use 2nd color
g.fillRect(href, -hgt, barWidth, hgt); //draw the bar
href += barWidth + hGap; //move reference point
} //for
} //drawBarChart()

public double total(double sales[])
{
double sum = 0;
for (int k = 0; k < NSELLERS; k++)
sum += sales[k];
return sum;
} //total()

public static void main(String args[])
{
JFrame f = new JFrame();

Graph1 chart = new Graph1();
f.getContentPane().add(chart);
f.setSize(chart.WIDTH, chart.HEIGHT);
f.setVisible(true);
}
}

buntine
03-06-2005, 09:21 PM
I cannot see any obvious errors at first glance. What is actually happening what you run the program? Are you receiving an error? Or is the graph just not showing?

From memory, I think the paintComponent() method is called when a component is added to a panel/frame.

Regards.