package br;

import java.awt.Dimension;  
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import org.jfree.chart.*;  
import org.jfree.chart.plot.PlotOrientation;  
import org.jfree.data.xy.*;  
import org.jfree.ui.ApplicationFrame;   
import org.jfree.ui.RefineryUtilities;

public class AutoVetores extends ApplicationFrame{
	
	   private static final long serialVersionUID = 1L;
	   XYSeries xyseries;
	   
	   public AutoVetores(String s){
	       super(s);
	       
	   }
	   
	   public void windowClosing(WindowEvent e){ 
	      dispose();
	   } 
	   
	   public void mostrarTela(){
		   ChartPanel chartpanel = null;
		   JFreeChart jfreechart = null;
		   XYDataset xydataset = null;
		   if (chartpanel != null){
			   xydataset = createDataset();  
			   jfreechart = ChartFactory.createXYLineChart("Amostras: "+String.valueOf(xyseries.getItemCount()), "milisegundos", "milivots", xydataset, PlotOrientation.VERTICAL, true, true, false);
			   chartpanel.setChart(jfreechart);
			   setContentPane(chartpanel);
			   this.pack();
			   System.out.println("chartpanel null");
			   return;
		   }
		   
		   xydataset = createDataset();  
	       jfreechart = createChart(xydataset);  
	       chartpanel = new ChartPanel(jfreechart);  
	       chartpanel.setPreferredSize(new Dimension(1000, 500));  
	       setContentPane(chartpanel);
	       
		   this.pack();
	       //RefineryUtilities.centerFrameOnScreen(this); 
		   RefineryUtilities.positionFrameOnScreen(this, 0.5, 0.7);
	       this.setVisible(true);
	   }
	
	   public void addValueXYseries(float x){
		   if (xyseries == null){
			   xyseries = new XYSeries("valores analgicos");
		   }else{
			   if (xyseries.getItemCount() > 0)
			   xyseries.add(x, xyseries.getX(xyseries.getItemCount()-1));
		   }
	   }
	   
	   private JFreeChart createChart(XYDataset xydataset){  
	       JFreeChart jfreechart = ChartFactory.createXYLineChart("Amostras: "+String.valueOf(xyseries.getItemCount()), "milisegundos", "milivots", xydataset, PlotOrientation.VERTICAL, true, true, false);
	       return jfreechart;  
	   }  
	 
	
	   private XYDataset createDataset(){
	 
		   XYSeriesCollection xy = new XYSeriesCollection();  
		   xy.addSeries(xyseries);  
	    
		   return xy;  
	   }  
}
