# Thread: how to graph an array data?

1. Member
Join Date
Aug 2012
Posts
4
Rep Power
0

## how to graph an array data?

I am an beginner in IntellijIDEA and I have a problem to graphing my data. can you help me?

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Scanner;

public class GraphingData
{
{
Scanner cs = new Scanner(System.in);
System.out.println("");
System.out.println(" << Computation Program For Graphing Water Surface Profile >> ");
System.out.println(" __________________________________________________ ___________________________ ");
System.out.println("");
System.out.println("2. Spatially Varried Flow With Increasing Discharge");
System.out.println("3. Spatially Varried Flow With Decreasing Discharge");

...

for (int i = 0; i < Y.length; i++)
{
for (int j = 0; j < i; j++)
{
Xy[i] = Xy[i] + deltaXy [j];
}
}
// I Want to graph "Xy[] and Y[]" using this metod
}

int[] data =
{
21, 14, 18, 30, 86, 88, 74, 87, 54, 77,
61, 55, 48, 60, 49, 36, 38, 27, 20, 18
};

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw ordinate.
// Draw abcissa.
// Draw labels.
Font font = g2.getFont();
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics("0", frc);
float sh = lm.getAscent() + lm.getDescent();
// Ordinate label.
String s = "Y";
float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
for(int i = 0; i < s.length(); i++)
{
String letter = String.valueOf(s.charAt(i));
float sw = (float)font.getStringBounds(letter, frc).getWidth();
float sx = (PAD - sw)/2;
g2.drawString(letter, sx, sy);
sy += sh;
}
// Abcissa label.
s = "X";
float sw = (float)font.getStringBounds(s, frc).getWidth();
float sx = (w - sw)/2;
g2.drawString(s, sx, sy);
// Draw lines.
double xInc = (double)(w - 2*PAD)/(data.length-1);
double scale = (double)(h - 2*PAD)/getMax();
g2.setPaint(Color.green.darker());
for(int i = 0; i < data.length-1; i++)
{
double x1 = PAD + i*xInc;
double y1 = h - PAD - scale*data[i];
double x2 = PAD + (i+1)*xInc;
double y2 = h - PAD - scale*data[i+1];
g2.draw(new Line2D.Double(x1, y1, x2, y2));
}
// Mark data points.
g2.setPaint(Color.red);
for(int i = 0; i < data.length; i++)
{
double x = PAD + i*xInc;
double y = h - PAD - scale*data[i];
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}
}

private int getMax()
{
int max = -Integer.MAX_VALUE;
{
}
return max;
}

public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,400);
f.setLocation(300,200);
f.setVisible(true);
}
}

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
13

## Re: how to graph an array data?

Please wrap all code in the code tags.

You might wish to look into libraries such as JFreeChart , which handle the graphing for you and display in Swing Components

3. Member
Join Date
Aug 2012
Posts
4
Rep Power
0

## Re: how to graph an array data?

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Scanner;

public class GraphingData
{
{
Scanner cs = new Scanner(System.in);
System.out.println("");
System.out.println(" << Computation Program For Graphing Water Surface Profile >> ");
System.out.println(" __________________________________________________ ___________________________ ");
System.out.println("");

double Q = 28.32;
double n = 0.025;
double So = 0.001;
double m = 1.5;
double B = 6.25;
double yn = Q/(m*B);
double A = yn*(B+(m*yn));
double WP = B + 2*yn*Math.sqrt(Math.pow(m,2.0)+1);
double R = A / WP;
double Qn = (1/n)*A*Math.pow(R,(2.0/3.0))*Math.pow(So,0.5);
while (Math.abs(Q - Qn) > 0.00000001)
{
yn = yn*(Q/Qn);
Qn = (1/n)*yn*(B+(m*yn))*Math.pow(((yn*(B+(m*yn)))/(B+(2*yn*Math.sqrt(Math.pow(m,2)+1)))),(2.0/3.0))
*Math.pow(So,0.5);
}
double q = Q/B;
double yc = Math.pow((Math.pow(q,2)/9.81),(1.0/3.0));
double Qc = Math.sqrt(9.81*(Math.pow((yc*(B+(m*yc))),3.0)/(B+(2*m*yc))));
while (Math.abs(Q - Qc) > 0.00000001)
{
yc = yc*(Q/Qc);
Qc = Math.sqrt(9.81*(Math.pow((yc*(B+(m*yc))),3.0)/(B+(2*m*yc))));
}
double distance = Math.abs(yn - yc);
int intdistance = (int) (distance * 14);
double[] Y = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
Y [i] = yc +(i*(distance/Y.length));
}
double [] Ay = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
Ay [i] = Y[i] * (B + (m*Y[i]));
}
double [] WPy = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
WPy [i] = B + (2*Y[i]*Math.sqrt((Math.pow(m,2))+1));
}
double [] Ry = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
Ry [i] = Ay[i] / WPy[i];
}
double [] C2 = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
C2 [i] = Math.pow(((1/n) * Math.pow(Ry[i],(1.0/6.0))) , 2.0);
}
double [] Vy = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
Vy [i] = Qn / Ay[i];
}
double [] Ey = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
Ey [i] = Y[i] + (Math.pow(Vy[i],2.0)/(2*9.81));
}
double [] V2C2Ry = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
V2C2Ry [i] = (Math.pow(Vy[i],2.0)) / (C2[i] * Ry [i]);
}
double [] S0V2C2Ry = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
S0V2C2Ry [i] = So - V2C2Ry [i];
}
double [] deltaEy = new double [intdistance];
for (int i = 0; i < Y.length-1; i++)
{
deltaEy [i] = Ey[i] - Ey[i+1];
}
double [] deltaXy = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
deltaXy [i] = -1*(deltaEy[i] / S0V2C2Ry [i]);
}
double [] Xy = new double [intdistance];
for (int i = 0; i < Y.length; i++)
{
for (int j = 0; j < i; j++)
{
Xy[i] = Xy[i] + deltaXy [j];
}
}
for (int i = 0; i < Xy.length; i++)
{
System.out.println("x"+i+"="+Xy[i]);
System.out.println("y"+i+"="+Y[i]);
}
}

}
// I want to plot the Y[i] and Xy[i] instead of numbers below? but it doesn't work.
int[] data =
{
21, 14, 18, 30, 86, 88, 74, 87, 54, 77,
61, 55, 48, 60, 49, 36, 38, 27, 20, 18

};

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw ordinate.
// Draw abcissa.
// Draw labels.
Font font = g2.getFont();
FontRenderContext frc = g2.getFontRenderContext();
LineMetrics lm = font.getLineMetrics("0", frc);
float sh = lm.getAscent() + lm.getDescent();
// Ordinate label.
String s = "Y";
float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
for(int i = 0; i < s.length(); i++)
{
String letter = String.valueOf(s.charAt(i));
float sw = (float)font.getStringBounds(letter, frc).getWidth();
float sx = (PAD - sw)/2;
g2.drawString(letter, sx, sy);
sy += sh;
}
// Abcissa label.
s = "X";
float sw = (float)font.getStringBounds(s, frc).getWidth();
float sx = (w - sw)/2;
g2.drawString(s, sx, sy);
// Draw lines.
double xInc = (double)(w - 2*PAD)/(data.length-1);
double scale = (double)(h - 2*PAD)/getMax();
g2.setPaint(Color.green.darker());
for(int i = 0; i < data.length-1; i++)
{
double x1 = PAD + i*xInc;
double y1 = h - PAD - scale*data[i];
double x2 = PAD + (i+1)*xInc;
double y2 = h - PAD - scale*data[i+1];
g2.draw(new Line2D.Double(x1, y1, x2, y2));
}
// Mark data points.
g2.setPaint(Color.red);
for(int i = 0; i < data.length; i++)
{
double x = PAD + i*xInc;
double y = h - PAD - scale*data[i];
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}
}

private int getMax()
{
int max = -Integer.MAX_VALUE;
{
}
return max;
}

public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,400);
f.setLocation(300,200);
f.setVisible(true);
}
}

4. Member
Join Date
Aug 2012
Posts
4
Rep Power
0

## Re: how to graph an array data?

I also use jFreeChart, but could not add it to IntellijIDEA library.

5. ## Re: how to graph an array data?

but could not add it to IntellijIDEA library.
Why not?

7. Member
Join Date
Aug 2012
Posts
4
Rep Power
0

8. ## Re: how to graph an array data?

Well no, how would it know to do that? You have to add the jar[s] to your project settings as a library, or make jfreechart a maven dependency and let IntelliJ install it for you. Once IntelliJ knows of jfreecharts existence, then you can import and use the chart classes directly without issue.

#### Posting Permissions

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