import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** 
 * Download jfreechart-1.0.17.zip from
 * http://sourceforge.net/projects/jfreechart/files/1.%20JFreeChart/1.0.17/jfreechart-1.0.17.zip/download?use_mirror=cznic&r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjfreechart%2Ffiles%2F&use_mirror=cznic
 * 
 * Unpack the jfreechart-1.0.17.zip in an arbitrary folder.
 * 
 * Eclipse Project Setup:
 * In the Project Properties dialog (press right mouse button on project in
 * Package Explorer) select the tab "Libraries" in "Java Build Path".
 * Then click button "Add External JARs..." and select both
 * jfreechart-1.0.17.jar and jcommon-1.0.21.jar by pressing Ctrl key while
 * selecting.  
 */
public class JFreeChartDemo extends JPanel {

	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFreeChart chart;
	 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame top = new JFrame("JFreeChart Test");
		top.setBounds(100, 100, 800, 600);
		top.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JFreeChartDemo jfc = new JFreeChartDemo();
		top.getContentPane().add(jfc);
		top.setVisible(true);
		jfc.run();
	}
	
	public void run() {
		double[] x = new double[100];
		double[] y = new double[100];
		for (int i = 0; i < x.length; i++) {
			x[i] = i * 2 * Math.PI / x.length;
			y[i] = Math.sin(x[i]);
		}


		XYSeries series = new XYSeries("sin(x)");
		for (int i = 0; i < Math.min(x.length, y.length); i++) {
			series.add(x[i], y[i]);
		}
		XYDataset dataset = new XYSeriesCollection(series);
		chart = ChartFactory.createXYLineChart("Plot", "x", "y", dataset);
	
		repaint();
	}
	
	public void paint(Graphics g) {
		if (chart != null) {
			Rectangle b = g.getClipBounds();
			chart.draw((Graphics2D)g, new Rectangle2D.Double(b.x, b.y, b.width, b.height));
		}
	}
	

}
