Results 1 to 12 of 12
  1. #1
    theWall is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default How to plot graph in java with multiple lines for given samples (stored in arrays)?

    I need to plot multiple lines on one graph axis in java, the data for each line is stored in an Integer Array. I found a useful example in a thread from a couple of years ago, code posted by 'hardwire'. This was the code-

    Code:

    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class GraphingData extends JPanel {
        int[] data = {
            21, 14, 18, 03, 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_ANTIALIASING,
                                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 = "data";
            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 axis";
            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 i = 0; i < data.length; i++) {
                if(data[i] > max)
                    max = data[i];
            }
            return max;
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new GraphingData());
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    This plots one line on the graph from the array 'data'. I have been trying to modify it so that you can add more lines from more arrays. I am new to java 2D and have been have some problems and was not sure how to do this. Could anyone help? Or give some hints/tips as to how it can be done?

    Thanks.
    Last edited by Fubarable; 05-08-2011 at 05:06 PM. Reason: code tags added

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Homework?

    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    db

  3. #3
    theWall is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Ok thanks, I was wondering how to do that.
    It is not a homework question, its a small feature I need to be able to do, as part of larger project I am working on.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Ever heard of JFreeChart?

    db

  5. #5
    theWall is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    I have heard a little about it, but I would prefer not to use a 3rd party plugin if possible.

  6. #6
    theWall is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    I have heard a little about it, but I would prefer not to use a 3rd party plugin if possible.

  7. #7
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    use a double array (eg.int[][] data)

    In this part..

    <Code>
    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));
    }


    </Code>
    Just contain that within another four loop, and iterate through you first data array. Set the graphic to a different colour and you should be ticketyboo.
    read my blog : www.blue-walrus.com

  8. #8
    theWall is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the reply Oliver, am I right in thinking that that would only allow for 2 lines to be plotted though (using a double array)? I ideally need to have about 24 lines on one graph you see.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Shoot, Hardwire already showed how to plot lines:

    Java Code:
            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));
            }
    You could use a 2-d array of data as suggested above and then enclose this for loop in another for loop that loops through each row of data.

    Also, I added code tags added to your first post above to help make it readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code[i][/i]]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code[i][/i]]
    Best of luck
    Last edited by Fubarable; 05-08-2011 at 05:10 PM.

  10. #10
    theWall is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for your help Fubarable, I have done what I needed to do now. :)

  11. #11
    dns
    dns is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Re: How to plot graph in java with multiple lines for given samples (stored in arrays

    HI there , the code is really help full. I want to plot the values of the error at each run of the for loop and the number of runs are not predecided. I am saving the data in the vector data type. Help me to plot the data saved in the Vector.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to plot graph in java with multiple lines for given samples (stored in arrays

    Quote Originally Posted by dns View Post
    HI there , the code is really help full. I want to plot the values of the error at each run of the for loop and the number of runs are not predecided. I am saving the data in the vector data type. Help me to plot the data saved in the Vector.
    You'll want to ask this question in its own thread, not hijack an old question (which I'm going to lock). In your question, you'll want to provide more details on just where you're stuck, since your post above doesn't ask a truly answerable question. Please see the links in my signature below including the one on how to ask smart questions as it will tell you what information we need to make your question answerable.

Similar Threads

  1. plot graph
    By alaa in forum New To Java
    Replies: 9
    Last Post: 04-15-2011, 09:13 PM
  2. How to plot graph in java for given samples
    By annesteve31 in forum New To Java
    Replies: 27
    Last Post: 11-29-2009, 09:27 PM
  3. i want plot realtime graph in java
    By santhosh_el in forum New To Java
    Replies: 3
    Last Post: 02-26-2009, 08:32 AM
  4. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 06:49 PM
  5. How to plot a dot graph
    By Manfizy in forum Java 2D
    Replies: 3
    Last Post: 01-28-2009, 02:57 PM

Posting Permissions

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