Results 1 to 8 of 8
  1. #1
    ehsan65mns is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Question 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("Please Select the Number of Your Flowrate Type:");
    System.out.println("1. Steady gradually-Varried Flow (Free overfall)");
    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
    };
    final int PAD = 20;

    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.
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
    // Draw abcissa.
    g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
    // 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";
    sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
    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;
    for (int aData : data)
    {
    if (aData > max)
    max = aData;
    }
    return max;
    }

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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. #3
    ehsan65mns is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Question 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

    };
    final int PAD = 20;

    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.
    g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
    // Draw abcissa.
    g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
    // 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";
    sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
    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;
    for (int aData : data)
    {
    if (aData > max)
    max = aData;
    }
    return max;
    }

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

  4. #4
    ehsan65mns is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: how to graph an array data?

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

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to graph an array data?

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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: how to graph an array data?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    ehsan65mns is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: how to graph an array data?

    when I Download the jfreechart-1.0.14 from site and open IntellijIdea, it don't ask for library.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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.

Similar Threads

  1. Data Graph Java
    By antonello12 in forum JDBC
    Replies: 3
    Last Post: 12-08-2011, 11:01 PM
  2. How to plot the graph,where the data from text file?
    By Haryanti in forum New To Java
    Replies: 2
    Last Post: 12-01-2011, 07:11 PM
  3. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 07:49 PM
  4. Replies: 1
    Last Post: 03-21-2008, 04:14 PM
  5. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 05:45 AM

Posting Permissions

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