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

    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
    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.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") ;
    	GridLayout layout = new GridLayout(2, 1);
    	JPanel jptop = new JPanel();
    	JPanel jpbottom = new JPanel(new BorderLayout());
    	JTextArea text = new JTextArea();
    	mainframe.getContentPane().add(text, BorderLayout.CENTER);
    	JPanel graph = new JPanel();
        // Create application menu
    	JMenuBar menubar = new JMenuBar();
    	//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");
    	JMenuItem exit = new JMenuItem("Exit");
    	JMenu help = new JMenu("Help");
    	JMenuItem about = new JMenuItem("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.setSize(600, 500);
    			JPanel helppanel = new JPanel();
    			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) {
    	        load.addActionListener(new openaction()); // Apply ActionListener to Load button

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    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.

    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
    Rep Power

    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
    MA, USA
    Rep Power

    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