Solved

drawing back to origin...

Posted on 2009-04-08
6
464 Views
Last Modified: 2012-05-06
I wrote a code to draw jfreechart... but there is one problem with it...

I am able to draw the graph for all the input values in the file...but after drawing all the values it is drawing back to origin..

what i mean is after plotting the last point it is taking (0,0) as next point and coming back...

so plz correct my code...
import java.io.*;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;
 
public class DisplayGraphics 
{	
  class ReadFile
  { 	
  	int kMaxLines = 60000; 	 	
  	double[][] valuePairs = new double[2][kMaxLines];
  	public void fileRead()
  	{
   		try  		
  		{  	
  	 		int kMaxLines = 60000;  
	   		FileInputStream fstream = new FileInputStream("c:/perl/output.txt");
	   		DataInputStream datastream = new DataInputStream(fstream);
	   		BufferedReader br = new BufferedReader(new InputStreamReader(datastream));
	   		String strLine;
	   		int line = 0;
	   		while ((strLine = br.readLine()) != null)
		 		{
	   			String temp_strs[] = strLine.split("\t");
	  			for (int j = 0; j < 2; j++)
	  				valuePairs[j][line] = Double.parseDouble(temp_strs[j]);
	  			//System.out.println(temp_strs[0] + " " + temp_strs[1]);
	  			line++;
	   		}
	 	 		datastream.close();
  		}
  		catch (Exception e)
  		{
  	    //file not found exception...
  			e.printStackTrace();
  		}
  	}
  }
  
  public void drawGraph()
  {
  	ReadFile rFile= new ReadFile();
  	rFile.fileRead();
  	DefaultXYDataset set = new DefaultXYDataset();
  	//addseries requires double array
    set.addSeries("Values" , rFile.valuePairs);
    //wrapper class that converts a standard XYDataset, 
    //into an IntervalXYDataset suitable for use in creating XY bar charts
    XYBarDataset barset = new XYBarDataset(set , 0.8);
    JFreeChart chart = ChartFactory.createXYBarChart(
    		"coverage depth" , "x" , false , "y" , 
        barset , PlotOrientation.VERTICAL , true , true , false);
    JFrame frame = new JFrame("Graph Showing Coverage values for chromosome-9");
    frame.setContentPane(new ChartPanel(chart));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);   
  }
 
  public static void main(String[] args)
  {
  	DisplayGraphics XY = new DisplayGraphics();
  	XY.drawGraph();
  }
}

Open in new window

0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24104153
you need to adjust the size of your array to match the number od data points
I'd suggest reading the points into a List and creating your aray from that list

http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line-into-a-list
0
 

Author Comment

by:shragi
ID: 24104363
can u tell how to do ..that.. the link u had provided is understandable...but I am unable to make it to my code....so can u make those changes to my code ...
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 500 total points
ID: 24107823
Try,
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;
 
public class DisplayGraphics 
{       
  class ReadFile
  {     
        double[][] valuePairs;
        public void fileRead()
        {
		ArrayList<Double> temp1=new ArrayList<Double>(60000);
		ArrayList<Double> temp2=new ArrayList<Double>(60000);
                try             
                {         
                        FileInputStream fstream = new FileInputStream("c:/perl/output.txt");
                        DataInputStream datastream = new DataInputStream(fstream);
                        BufferedReader br = new BufferedReader(new InputStreamReader(datastream));
                        String strLine;
                        while ((strLine = br.readLine()) != null)
                                {
                                String temp_strs[] = strLine.split("\t");
				temp1.add(Double.parseDouble(temp_strs[0]);
				temp2.add(Double.parseDouble(temp_strs[1]);
                        }
                        datastream.close();
                }
                catch (Exception e)
                {
            //file not found exception...
                        e.printStackTrace();
                }
		valuePairs=new double[2][temp1.size()];
		//now populate the array
		for(int i=0; i<temp1.size(); i++)
		{
			valuePairs[0][i]=temp1.get(i);
			valuePairs[1][i]=temp2.get(i);
		}
		temp1.clear();//cleanup
		temp2.clear();//
        }
  }
  
  public void drawGraph()
  {
        ReadFile rFile= new ReadFile();
        rFile.fileRead();
        DefaultXYDataset set = new DefaultXYDataset();
        //addseries requires double array
    set.addSeries("Values" , rFile.valuePairs);
    //wrapper class that converts a standard XYDataset, 
    //into an IntervalXYDataset suitable for use in creating XY bar charts
    XYBarDataset barset = new XYBarDataset(set , 0.8);
    JFreeChart chart = ChartFactory.createXYBarChart(
                "coverage depth" , "x" , false , "y" , 
        barset , PlotOrientation.VERTICAL , true , true , false);
    JFrame frame = new JFrame("Graph Showing Coverage values for chromosome-9");
    frame.setContentPane(new ChartPanel(chart));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);   
  }
 
  public static void main(String[] args)
  {
        DisplayGraphics XY = new DisplayGraphics();
        XY.drawGraph();
  }
}

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:shragi
ID: 24108708
I got a numbe format exception...at

temp1.add(Double.parseDouble(temp_strs[0]);
temp2.add(Double.parseDouble(temp_strs[1]);

but unable to fix it...can u..
0
 

Author Comment

by:shragi
ID: 24109644
yup i got..it... thnks dude...
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24109872
Replace those lines with

try
{
    temp1.add(Double.parseDouble(temp_strs[0]);
    temp2.add(Double.parseDouble(temp_strs[1]);
}catch(Exception e2){System.out.println(temp_strs[0]+","+temp_strs[1]);}

are they definitely numbers?

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question