Results 1 to 4 of 4
  1. #1
    luke271066 is offline Member
    Join Date
    Jul 2016
    Posts
    2
    Rep Power
    0

    Default Plotting Graph Using Data From Text File

    Trying to plot a graph using the data read from a text file. Currently my program opens a text file and displays the data in a TextArea. I am now trying to take the data from the text file and use it to plot a line graph. This is the format of my text file:

    Title: Effect of Age on Ability
    Xlabel:Age
    Ylabel:Ability
    start:0
    interval:15
    0 3 4.2 7 5.1 10 3.2

    The title of the graph should be the title defined in the file, axis labelled with those labels defined in the file and the data in the final line should be plotted on the graph to create the line graph.

    Please help!

    Current code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;
    public class DisplayData {
    public static void main (String[] args) {
    	
    	// Create main application frame
    	JFrame mainframe = new JFrame("Main") ;
    	mainframe.setSize(800,700);
    	mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	GridLayout layout = new GridLayout(2, 1);
    	mainframe.getContentPane().setLayout(layout);
    	
    	JPanel jptop = new JPanel();
    	JPanel jpbottom = new JPanel(new BorderLayout());
    	
    	JTextArea text = new JTextArea();
    	text.setSize(400,350);
    	mainframe.getContentPane().add(text, BorderLayout.CENTER);
    	
    	JPanel graph = new JPanel();
    	mainframe.getContentPane().add(graph);
    	
    	mainframe.setVisible(true);
    	
        // Create application menu
    	JMenuBar menubar = new JMenuBar();
    	mainframe.setJMenuBar(menubar);
    
    	//Creating and adding options to menu
    	JMenu file = new JMenu("File"); // // Option is created
    	menubar.add(file);  // Option is added to menu bar
    	JMenuItem load = new JMenuItem("Load"); // Creates item for File list
    	file.add(load); // Adds item to File in menu bar
    	JMenuItem save = new JMenuItem("Save");
    	file.add(save); 
    	JMenuItem exit = new JMenuItem("Exit");
    	file.add(exit);
    	JMenu help = new JMenu("Help");
    	menubar.add(help);
    	JMenuItem about = new JMenuItem("About");
    	help.add(about);
    	
    	// Create ActionListener for Exit Button
    	class exitaction implements ActionListener{ 
            public void actionPerformed (ActionEvent e) {
    		System.exit(0); // Close application
    	}
    	}
    		exit.addActionListener(new exitaction()); // Apply ActionListener to Exit button
    	
    	// Create ActionListener for Help Button	
    	class helpaction implements ActionListener{
    		public void actionPerformed (ActionEvent e) {
    			JFrame helpframe = new JFrame("About"); // Creates Help Frame
    			helpframe.setVisible(true);
    			helpframe.setSize(600, 500);
    			JPanel helppanel = new JPanel();
    			helpframe.getContentPane().add(helppanel);
    			helpframe.getContentPane().setLayout(new BorderLayout());
    			helpframe.getContentPane().add (new JLabel ("<html>This is a data visualisation tool.<br>It can be used to view data from a .txt file.<br>Current Version: 1.0</html>"), BorderLayout.NORTH);
    			
    	}
    	}
    	    about.addActionListener(new helpaction()); // Apply ActionListener to About button
    	    
    	 // Create ActionListener for Load Button
    		class openaction implements ActionListener{ 
    			public void actionPerformed (ActionEvent e) {
    		        JFileChooser chooser = new JFileChooser(); // Create new File Chooser
    		        chooser.setDialogTitle("Open a Text File");
    		        int result = chooser.showOpenDialog(null);
    		           if (result == JFileChooser.APPROVE_OPTION) {
    		            File file = chooser.getSelectedFile();
    		            // Read in lines.
    		            try(BufferedReader br = new BufferedReader(new FileReader(file))) {
    		                // Opens text file in TextField, adds break at the end of lines
    		                br.lines().forEach(line -> text.append(line + System.lineSeparator()));
    		            } catch (Exception ex) {
    		                ex.printStackTrace();
    		            }
    		        }
    		    }
    		}
    	    
    	        	
    	        load.addActionListener(new openaction()); // Apply ActionListener to Load button
    		
    }
    
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Plotting Graph Using Data From Text File

    Assuming you really want to do graphics (and not a simple ASCII line plot), you need to override paintComponent() in your JPanel. The best
    way to do that is extend JPanel your in current class or create a new class.

    I recommend you read the tutorials in my signature about how to paint, then try some stuff and then ask questions. I also recommend
    you try some simple graphics by drawing lines or shapes before you try to do it for real in your program.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    luke271066 is offline Member
    Join Date
    Jul 2016
    Posts
    2
    Rep Power
    0

    Default Re: Plotting Graph Using Data From Text File

    I will use the paintComponent method that you suggested, that is best suited to the requirements. I'm not going to waste your time I'll cut to the chase, I really have no idea where to begin doing what you have suggested however I do appreciate the support. Could you give an example based on my code?

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Plotting Graph Using Data From Text File

    Jim suggested that you override the paintComponent() method. For examples check this.

Similar Threads

  1. To import CSV file into java for Plotting graph.
    By Niisha in forum Advanced Java
    Replies: 0
    Last Post: 03-25-2012, 05:31 PM
  2. Plotting a graph
    By cet_1990 in forum Java 2D
    Replies: 2
    Last Post: 12-20-2011, 11:11 PM
  3. 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, 06:11 PM
  4. plotting graph
    By dhruvaharsha in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2010, 07:04 PM
  5. Help regarding 3D graph plotting...
    By Megatron in forum New To Java
    Replies: 1
    Last Post: 05-21-2009, 04:26 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
  •