Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to plot the line graph using jfreechart reading from text file

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to plot the line graph using jfreechart reading from text file

    hello,
    In the below code we are able to read y-axis but not able to read x-axis on the graph.. can tel me how to read x-axis on the graph.
    thanks in advance


    import java.io.*;
    import java.util.*;//Scanner is included
    import org.jfree.chart.*;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.data.xy.*;
    import org.jfree.data.*;
    public class Plot_1
    {
    static Scanner scanner;
    static XYSeries series;
    //static File file;
    public static void main(String[] args)
    {
    plotResult();



    }//end of main





    //**********



    //Method that reads in an external file with numbers (doubles)

    //and plots the content in a line chart using JFreeChart





    public static void plotResult() {



    //Read in the file "calculation.txt" using Scanner and call hasNextDouble on it



    try {

    int index = 0;



    File file = new File("data.txt");

    series = new XYSeries("Pk");

    scanner = new Scanner(file);

    // Scanner scanner = new Scanner(file);

    while (scanner.hasNextDouble()) {

    series.add(index++,scanner.nextDouble());

    }

    scanner.close();

    }

    catch (FileNotFoundException e) {
    e.printStackTrace();

    }





    //Round off the doubles to two decimals - not done yet







    //Plot the line chart







    /*

    XYSeries series = new XYSeries("Pk");

    series.add(1, 0.8201791789916324 );

    series.add(2, 0.9727097916233601 );

    series.add(3, 0.9949972829175494);

    */



    XYDataset xyDataset = new XYSeriesCollection(series);





    JFreeChart chart = ChartFactory.createXYLineChart

    ("Temp = 85", "k", "Pk",xyDataset, PlotOrientation.VERTICAL, true, true, false);

    ChartFrame frame1=new ChartFrame("XYLine Chart",chart);

    frame1.setVisible(true);

    frame1.setSize(600,400);







    }//End of method





    //***************





    }//end of class

    in data.txt has following values
    1 20
    2 40
    4 50


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to plot the line graph using jfreechart reading from text file

    Cross posted at how to plot a line graph using jfreechart reading from text file?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to plot line graph using jfreechart reading from text file
    By priti in forum Java Theory & Questions
    Replies: 9
    Last Post: March 31st, 2012, 01:39 PM
  2. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  3. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  4. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 09:11 PM
  5. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM