Results 1 to 2 of 2
  1. #1
    priti is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default how to plot a 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
    x y
    1 20
    2 30
    4 50

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

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

    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 04-02-2012, 02:56 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. java graph plot taking file data
    By niloR19 in forum AWT / Swing
    Replies: 3
    Last Post: 11-28-2011, 07:56 PM
  4. Replies: 3
    Last Post: 06-26-2010, 09:11 AM
  5. Reading part of a line in a text file
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-04-2009, 08:57 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
  •