Plot Chart from a Txt file

Reply

Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Plot Chart from a Txt file

 
0
  #1
Nov 21st, 2008
Hello my friends

I have a txt file with the follow layout
error1 255
error2 34
error5 111
error3 23
and what i'm trying to achieve is, from this text file plot a trend chart with the errors and respective quantity!

this is the code that i'm using....
import java.io.File;   
import java.io.FileInputStream;   
import java.io.InputStreamReader;   
import java.io.BufferedReader;   
import java.io.*;   
import java.util.*;   
import java.lang.NumberFormatException;   
import java.text.NumberFormat;   
import java.text.MessageFormat;   

import java.awt.Font;   
import javax.swing.JFrame;   
import javax.swing.JPanel;   
import org.jfree.chart.ChartFactory;   
import org.jfree.chart.ChartPanel;   
import org.jfree.chart.JFreeChart;   
import org.jfree.chart.plot.PiePlot;   
import org.jfree.data.general.DefaultPieDataset;   

import org.jfree.chart.ChartFactory;   
import org.jfree.chart.ChartPanel;   
import org.jfree.chart.JFreeChart;   
import org.jfree.chart.axis.NumberAxis;   
import org.jfree.chart.plot.PlotOrientation;   
import org.jfree.chart.plot.XYPlot;   
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;   
import org.jfree.data.*;   
import org.jfree.data.xy.XYDataset;   
import org.jfree.data.xy.XYSeries;   
import org.jfree.data.xy.XYSeriesCollection;   
import org.jfree.data.DomainOrder;   
import org.jfree.data.general.SeriesDataset;   
import org.jfree.data.category.DefaultCategoryDataset;   


public class graph extends JFrame{   

	/**
	 * 
	 */
	private static final long serialVersionUID = 0L;

	public graph(String title) throws Exception{   

		super(title);   

		XYDataset dataset = createDataset();   
		JFreeChart chart = createChart(dataset);   
		ChartPanel chartPanel = new ChartPanel(chart);   
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));   
		setContentPane(chartPanel);   
		chartPanel.setMouseZoomable(true, false);   
	}   


	// Well, here I read a file from a table in txt each line with 4 columns of numbers), save it in a string and then on a string 
	// Array of 4 elements. Then convert each element of the string to double (not know if I'm being very stupid, I am more beginner in java ...) 
	// What I'm trying to do is get two of those column (now in double) and put points on the method series.add below 
	// I think I have to do a loop of some way, then do what you suggested q ... But the loop that I did overwrite the variables 
	// My attempt was a normal access to the txt file

	/* public extrator extract(String) throws Exception {  
        File file = new File("F:\\Ekralog.txt");      
        FileInputStream in = new FileInputStream(file);  
        InputStreamReader convert = new InputStreamReader(in);  
        BufferedReader bf = new BufferedReader(convert);  
        String line;  
        String[] element;  
        line = bf.readLine();  
        element = line.split("[\\s]+");  
        bf.close();  
        in.close();return  

	 */   


	private XYDataset createDataset() throws Exception {   
		//CONVERT STRING TO DOUBLE   
		//double element0;     
		//element0 = Double.parseDouble(element[0]); /* element[0] its one string*/   

		XYSeries series1 = new XYSeries("TRZ");   
		series1.add(1.0, 1.0); // some point that i've inserted to test and it is
		series1.add(2.0, 4.0);// should i do a loop to test it.. there are more than 100 points
		series1.add(3.0, 3.0);   
		series1.add(4.0, 5.0);   
		series1.add(5.0, 5.0);   
		series1.add(6.0, 7.0);   
		series1.add(7.0, 7.0);   
		series1.add(8.0, 8.0);   



		XYSeriesCollection dataset = new XYSeriesCollection();   
		dataset.addSeries(series1);   

		return dataset;   


	}   
	// ListDouble colunas = new ArrayListDouble();   

	//public ListDouble getColunas(){   

	//    return colunas;   

	// }   


	private JFreeChart createChart(XYDataset dataset) {   
		// create the chart...   
		JFreeChart chart = ChartFactory.createXYLineChart(   
				"Medida", // chart title   
				"X", // x axis label   
				"Y", // y axis label   
				dataset, // data   
				PlotOrientation.VERTICAL,   
				true, // include legend   
				true, // tooltips   
				false // urls   

		);   

		return chart;   
	}   

	public static void main(String[] args) throws Exception {   
		graph teste = new graph("test");   
		teste.pack();   
		teste.setVisible(true);   
	}   

}
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Plot Chart from a Txt file

 
0
  #2
Nov 21st, 2008
and your point is???
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Plot Chart from a Txt file

 
0
  #3
Nov 21st, 2008
Originally Posted by stultuske View Post
and your point is???
Hi there...

The graphic is not plotting can anyone help me to find where is the problem...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,355
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 500
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Plot Chart from a Txt file

 
0
  #4
Nov 21st, 2008
The graph shows just fine for me.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Plot Chart from a Txt file

 
0
  #5
Nov 21st, 2008
Originally Posted by Ezzaral View Post
The graph shows just fine for me.
Really... it getting the data from the txt file ?

Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,355
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 500
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Plot Chart from a Txt file

 
0
  #6
Nov 21st, 2008
No of course not. I don't have that file and you have it commented out. You said the graphic was not plotting.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Plot Chart from a Txt file

 
0
  #7
Nov 21st, 2008
Originally Posted by Ezzaral View Post
No of course not. I don't have that file and you have it commented out. You said the graphic was not plotting.
It was not plotting the data imported from the file,,,
the file contents...
error1 255
error2 34
error5 111
error3 23
it looks like this.... 1st column is the error description and the second the quantity...

sorry about my fail
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,355
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 500
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Plot Chart from a Txt file

 
0
  #8
Nov 21st, 2008
I don't think you want an XYSeries for that. If you want it to be a line chart (bar chart seems more appropriate to me, but you seem to want the line chart), try this instead
  1. private CategoryDataset createDataset() throws Exception {
  2.  
  3. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  4. dataset.addValue(255, "Errors", "error1");
  5. dataset.addValue(34, "Errors", "error2");
  6. dataset.addValue(111, "Errors", "error5");
  7. dataset.addValue(23, "Errors", "error3");
  8.  
  9. return dataset;
  10. }
  11.  
  12. private JFreeChart createChart(CategoryDataset dataset) {
  13. // create the chart...
  14. JFreeChart chart = ChartFactory.createLineChart(
  15. "Medida", // chart title
  16. "Error Code", // x axis label
  17. "Count", // y axis label
  18. dataset, // data
  19. PlotOrientation.VERTICAL,
  20. true, // include legend
  21. true, // tooltips
  22. false // urls
  23. );
  24.  
  25. return chart;
  26. }
All you have to do is build that category dataset with the first and second elements of your spilt() on the text file line.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Plot Chart from a Txt file

 
0
  #9
Nov 21st, 2008
You are right... i want a bar chart... but as a newby i'm still trying to discover to potential of the javafreechart... reading trying reading trying... i'm too slowwww...

thank you for your lights...~

to change for a bar type what should i dooo??

Originally Posted by Ezzaral View Post
I don't think you want an XYSeries for that. If you want it to be a line chart (bar chart seems more appropriate to me, but you seem to want the line chart), try this instead
  1. private CategoryDataset createDataset() throws Exception {
  2.  
  3. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  4. dataset.addValue(255, "Errors", "error1");
  5. dataset.addValue(34, "Errors", "error2");
  6. dataset.addValue(111, "Errors", "error5");
  7. dataset.addValue(23, "Errors", "error3");
  8.  
  9. return dataset;
  10. }
  11.  
  12. private JFreeChart createChart(CategoryDataset dataset) {
  13. // create the chart...
  14. JFreeChart chart = ChartFactory.createLineChart(
  15. "Medida", // chart title
  16. "Error Code", // x axis label
  17. "Count", // y axis label
  18. dataset, // data
  19. PlotOrientation.VERTICAL,
  20. true, // include legend
  21. true, // tooltips
  22. false // urls
  23. );
  24.  
  25. return chart;
  26. }
All you have to do is build that category dataset with the first and second elements of your spilt() on the text file line.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: jorgeflorencio is an unknown quantity at this point 
Solved Threads: 0
jorgeflorencio jorgeflorencio is offline Offline
Light Poster

Re: Plot Chart from a Txt file

 
0
  #10
Nov 21st, 2008
something like this^??????
  1. JFreeChart chart = ChartFactory.createBarChart(
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC